-
Notifications
You must be signed in to change notification settings - Fork 49
/
build.py
215 lines (175 loc) · 6.09 KB
/
build.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import platform
import sys
import multiprocessing
import subprocess
import glob
# find the ninja generator on windows.
def getGenerator(args):
#osStr = (platform.system())
#
#if osStr == "Windows":
#
# for x in args:
# if x.startswith("-G"):
# break
#
# vswhereArgs = ['C:/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe', "-prerelease", "-latest", "-property", "installationPath"]
# rootpath = subprocess.check_output(vswhereArgs).decode("utf-8").strip()
#
# ninja = rootpath + "/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/CMAKE/Ninja/ninja.exe"
# cl = rootpath + "/VC/Tools/MSVC/*/bin/Hostx64/x64/cl.exe"
# cls = glob.glob(cl)
# if len(cls) > 0:
# cl = cls[-1];
#
# # use ninja
# if os.path.exists(ninja) and os.path.exists(cl):
# return "-G \"Ninja\" -DCMAKE_MAKE_PROGRAM=\"{0}\" -DCMAKE_C_COMPILER:FILEPATH=\"{1}\" -DCMAKE_CXX_COMPILER:FILEPATH=\"{1}\" ".format(ninja, cl)
# else:
# print("failed to find ninja at: {0}\n or cl".format(ninja))
#
# use the default
return ""
def parseInstallArgs(args):
prefix = ""
doInstall = False
for x in args:
if x.startswith("--install="):
prefix = x.split("=",1)[1]
prefix = os.path.abspath(os.path.expanduser(prefix))
idx = args.index(x)
args[idx] = "-DCMAKE_INSTALL_PREFIX=" + prefix
doInstall = True
if x == "--install":
idx = args.index(x)
osStr = (platform.system())
if osStr == "Windows":
args[idx] = "-DCMAKE_INSTALL_PREFIX=c:/lib"
else:
args[idx] = "-DCMAKE_INSTALL_PREFIX=/usr/local"
doInstall = True
return (args, doInstall)
def getParallel(args):
par = multiprocessing.cpu_count()
for x in args:
if x.startswith("--par="):
val = x.split("=",1)[1]
par = int(val)
if par < 1:
par = 1
idx = args.index(x)
args[idx] = ""
return (args,par)
def replace(list, find, replace):
if find in list:
idx = list.index(find)
list[idx] = replace;
return list
def Build(projectName, argv):
osStr = (platform.system())
buildDir = ""
config = ""
buildType = ""
# use sudo when installing?
sudo = "--sudo" in argv;
argv = replace(argv, "--sudo", "-DSUDO_FETCH=ON")
if not sudo:
argv.append("-DSUDO_FETCH=OFF")
argv.append("-DENABLE_ALL_PSI=ON")
generator = getGenerator(argv)
# do not automaticly download dependancies
if "--noauto" in argv:
argv = replace(argv, "--noauto", "")
argv.append("-DFETCH_AUTO=OFF")
else:
argv.append("-DFETCH_AUTO=ON")
# get install options
argv, install = parseInstallArgs(argv)
# get parallel build options
argv, par = getParallel(argv)
argv.append("-DPARALLEL_FETCH="+str(par))
# do not run cmake config
noConfig = "--nc" in argv
argv = replace(argv, "--nc", "")
# only run cmake config.
setup = "--setup" in argv;
argv = replace(argv, "--setup", "")
# build type.
if "--debug" in argv:
buildType = "Debug"
else:
buildType = "Release"
argv.append("-DCMAKE_BUILD_TYPE={0}".format(buildType))
argv = replace(argv, "--debug", "")
# build dir
if osStr == "Windows":
buildDir = "out/build/x64-{0}".format(buildType)
config = "--config {0}".format(buildType)
elif osStr == "Darwin":
buildDir = "out/build/osx"
else:
buildDir = "out/build/linux"
# convert args to a string.
argStr = ""
for a in argv:
argStr = argStr + " " + a
# parallel build
parallel = ""
if par != 1:
parallel = " --parallel " + str(par)
# build commands
mkDirCmd = "mkdir -p {0}".format(buildDir);
CMakeCmd = "cmake {0} -S . -B {1} {2} ".format(generator, buildDir, argStr)
BuildCmd = "cmake --build {0} {1} {2} ".format(buildDir, config, parallel)
InstallCmd = ""
if sudo:
sudo = "sudo "
else:
sudo = ""
if install:
InstallCmd = sudo
InstallCmd += "cmake --install {0} {1} ".format(buildDir, config)
# print and execute commands.
print("\n\n====== build.py ("+projectName+") ========")
if not noConfig:
print(mkDirCmd)
print(CMakeCmd)
if not setup:
print(BuildCmd)
if len(InstallCmd):
print(InstallCmd)
print("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n\n")
if not noConfig:
os.system(mkDirCmd)
os.system(CMakeCmd)
if not setup:
os.system(BuildCmd)
if len(sudo) > 0:
print("installing "+projectName+": {0}\n".format(InstallCmd))
os.system(InstallCmd)
def help():
print(" --install \n\tInstructs the script to install whatever is currently being built to the default location.")
print(" --install=prefix \n\tinstall to the provided predix.")
print(" --sudo \n\twhen installing, use sudo. May require password.")
print(" --par=n \n\twhen building do use parallel builds with n threads. default = num cores.")
print(" --noauto \n\twhen building do not automaticly fetch dependancies.")
print(" --par=n \n\twhen building do use parallel builds with n threads. default = num cores.")
print(" --debug \n\tdebug build.")
print("any additioanl arguments are forwared to cmake.\n")
print("-build the library")
print(" python build.py")
print("-build the library with cmake configurations")
print(" python build.py --debug -DLIBPSI_ENABLE_X=ON")
print("-build the library and install with sudo")
print(" python build.py --install --sudo")
print("-build the library and install to prefix")
print(" python build.py --install=~/my/install/dir ")
def main(projectName, argv):
if "--help" in argv:
help()
return
# build the project.
Build(projectName, argv)
if __name__ == "__main__":
main("LIBPSI", sys.argv[1:])