-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
premake
executable file
·160 lines (135 loc) · 3.8 KB
/
premake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# Copyright 2015 Ben Vanik. All Rights Reserved.
"""Premake trampoline script.
"""
__author__ = '[email protected] (Ben Vanik)'
import os
import subprocess
import sys
self_path = os.path.dirname(os.path.abspath(__file__))
premake_path = os.path.join(self_path, 'third_party', 'premake-core')
def main():
# First try the freshly-built premake.
premake5_bin = os.path.join(premake_path, 'bin', 'release', 'premake5')
if not has_bin(premake5_bin):
# No fresh build, so fallback to checked in copy (which we may not have).
premake5_bin = os.path.join('bin', 'premake5')
if not has_bin(premake5_bin):
# Still no valid binary, so build it.
print('premake5 executable not found, attempting build...')
build_premake()
premake5_bin = os.path.join(premake_path, 'bin', 'release', 'premake5')
if not has_bin(premake5_bin):
# Nope, boned.
print('ERROR: cannot build premake5 executable.')
sys.exit(1)
# Ensure the submodule has been checked out.
if not os.path.exists(os.path.join(premake_path, 'scripts', 'package.lua')):
print('third_party/premake-core was not present; fetching...')
git_submodule_update()
return_code = shell_call([
premake5_bin,
'--scripts=%s' % (premake_path),
] + sys.argv[1:],
throw_on_error=False)
sys.exit(return_code)
def build_premake():
"""Builds premake from source.
"""
cwd = os.getcwd()
try:
os.chdir(premake_path)
if sys.platform == 'darwin':
shell_call([
'make',
'-f', 'Bootstrap.mak',
'osx',
])
elif sys.platform == 'win32':
# TODO(benvanik): import VS environment.
shell_call([
'nmake',
'-f', 'Bootstrap.mak',
'windows',
])
else:
shell_call([
'make',
'-f', 'Bootstrap.mak',
'linux',
])
finally:
os.chdir(cwd)
pass
def has_bin(bin):
"""Checks whether the given binary is present.
"""
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, bin)
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
return True
exe_file = exe_file + '.exe'
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
return True
return None
def shell_call(command, throw_on_error=True, stdout_path=None):
"""Executes a shell command.
Args:
command: Command to execute, as a list of parameters.
throw_on_error: Whether to throw an error or return the status code.
stdout_path: File path to write stdout output to.
Returns:
If throw_on_error is False the status code of the call will be returned.
"""
stdout_file = None
if stdout_path:
stdout_file = open(stdout_path, 'w')
result = 0
try:
if throw_on_error:
result = 1
subprocess.check_call(command, shell=False, stdout=stdout_file)
result = 0
else:
result = subprocess.call(command, shell=False, stdout=stdout_file)
finally:
if stdout_file:
stdout_file.close()
return result
def git_submodule_update():
"""Runs a full recursive git submodule init and update.
Older versions of git do not support 'update --init --recursive'. We could
check and run it on versions that do support it and speed things up a bit.
"""
if True:
shell_call([
'git',
'submodule',
'update',
'--init',
'--recursive',
])
else:
shell_call([
'git',
'submodule',
'init',
])
shell_call([
'git',
'submodule',
'foreach',
'--recursive',
'git',
'submodule',
'init',
])
shell_call([
'git',
'submodule',
'update',
'--recursive',
])
if __name__ == '__main__':
main()