forked from LLNL/serac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-build.py
executable file
·297 lines (234 loc) · 10.1 KB
/
config-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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/sh
# Copyright (c) 2019-2021, Lawrence Livermore National Security, LLC and
# other Serac Project Developers. See the top-level LICENSE file for
# details.
#
# SPDX-License-Identifier: (BSD-3-Clause)
"exec" "python3" "-u" "-B" "$0" "$@"
# Python wrapper script for generating the correct cmake line with the options specified by the user.
#
# Please keep parser option names as close to possible as the names of the cmake options they are wrapping.
import sys
import os
import subprocess
import argparse
import platform
import shutil
import socket
_host_configs_map = {"rzgenie" : "toss_3_x86_64_ib/clang@4.0.0.cmake",
"rzalastor" : "toss_3_x86_64_ib/clang@4.0.0.cmake",
"rztopaz" : "toss_3_x86_64_ib/clang@4.0.0.cmake",
"quartz" : "toss_3_x86_64_ib/clang@4.0.0.cmake"}
def get_machine_name():
return socket.gethostname().rstrip('1234567890')
def get_default_host_config():
machine_name = get_machine_name()
if machine_name in _host_configs_map.keys():
return _host_configs_map[machine_name]
else:
return ""
def extract_cmake_location(file_path):
# print "Extracting cmake entry from host config file ", file_path
if os.path.exists(file_path):
cmake_line_prefix = "# cmake executable path: "
file_handle = open(file_path, "r")
content = file_handle.readlines()
for line in content:
if line.lower().startswith(cmake_line_prefix):
return line.split(" ")[4].strip()
print("Could not find a cmake entry in host config file.")
return None
def parse_arguments():
parser = argparse.ArgumentParser(description="Configure cmake build.",
epilog="Note: Additional or unrecognized parameters will be passed directly to cmake."
" For example, append '-DENABLE_OPENMP=ON' to enable OpenMP."
)
parser.add_argument("-bp",
"--buildpath",
type=str,
default="",
help="specify path for build directory. If not specified, will create in current directory.")
parser.add_argument("-ip",
"--installpath",
type=str,
default="",
help="specify path for install directory. If not specified, will create in current directory.")
parser.add_argument("-bt",
"--buildtype",
type=str,
choices=["Release", "Debug", "RelWithDebInfo", "MinSizeRel"],
default="Debug",
help="build type.")
parser.add_argument("-e",
"--eclipse",
action='store_true',
help="create an eclipse project file.")
parser.add_argument("-ecc",
"--exportcompilercommands",
action='store_true',
help="generate a compilation database. Can be used by the clang tools such as clang-modernize. Will create a 'compile_commands.json' file in build directory.")
parser.add_argument("-hc",
"--hostconfig",
default="",
type=str,
help="select a specific host-config file to initalize CMake's cache")
parser.add_argument("--print-default-host-config",
action='store_true',
help="print the default host config for this system and exit")
parser.add_argument("--print-machine-name",
action='store_true',
help="print the machine name for this system and exit")
args, unknown_args = parser.parse_known_args()
if unknown_args:
print("[config-build]: Passing the following arguments directly to cmake... %s" % unknown_args)
return args, unknown_args
########################
# Find CMake Cache File
########################
def find_host_config(args, repodir):
if args.hostconfig != "":
hostconfigpath = os.path.abspath(args.hostconfig)
else:
hostconfigpath = get_default_host_config()
if hostconfigpath == "":
print("[config-build]: Error could not find default host-config for this platform.")
print(" Either set one in this script or use the command line argument '-hc'.")
else:
hostconfigpath = os.path.join(repodir, "host-configs", hostconfigpath)
assert os.path.exists( hostconfigpath ), "Could not find CMake host config file '%s'." % hostconfigpath
print("Using host config file: '%s'." % hostconfigpath)
return hostconfigpath
########################
# Get Platform information from host config name
########################
def get_platform_info(hostconfigpath):
platform_info = ""
platform_info = os.path.split(hostconfigpath)[1]
if platform_info.endswith(".cmake"):
platform_info = platform_info[:-6]
return platform_info
#####################
# Setup Build Dir
#####################
def setup_build_dir(args, platform_info):
if args.buildpath != "":
# use explicit build path
buildpath = args.buildpath
else:
# use platform info & build type
buildpath = "-".join(["build", platform_info, args.buildtype.lower()])
buildpath = os.path.abspath(buildpath)
if os.path.exists(buildpath):
print("Build directory '%s' already exists. Deleting..." % buildpath)
shutil.rmtree(buildpath)
print("Creating build directory '%s'..." % buildpath)
os.makedirs(buildpath)
return buildpath
#####################
# Setup Install Dir
#####################
def setup_install_dir(args, platform_info):
# For install directory, we will clean up old ones, but we don't need to create it, cmake will do that.
if args.installpath != "":
installpath = os.path.abspath(args.installpath)
else:
# use platform info & build type
installpath = "-".join(
["install", platform_info, args.buildtype.lower()]
)
installpath = os.path.abspath(installpath)
if os.path.exists(installpath):
print(
"Install directory '%s' already exists, deleting..." % installpath
)
shutil.rmtree(installpath)
print("Creating install path '%s'..." % installpath)
os.makedirs(installpath)
return installpath
############################
# Check if executable exists
############################
def executable_exists(path):
if path == "cmake":
return True
return os.path.isfile(path) and os.access(path, os.X_OK)
############################
# Build CMake command line
############################
def create_cmake_command_line(args, unknown_args, buildpath, installpath, hostconfigpath):
import stat
cmakeline = extract_cmake_location(hostconfigpath)
assert cmakeline, "Host config file doesn't contain valid cmake location, value was %s" % cmakeline
assert executable_exists( cmakeline ), "['%s'] invalid path to cmake executable or file does not have execute permissions" % cmakeline
# create the ccmake command for convenience
cmakedir = os.path.dirname(cmakeline)
ccmake_cmd = cmakedir + "/ccmake"
if executable_exists( ccmake_cmd ):
# write the ccmake command to a file to use for convenience
with open( "%s/ccmake_cmd" % buildpath, "w" ) as ccmakefile:
ccmakefile.write("#!/usr/bin/env bash\n")
ccmakefile.write(ccmake_cmd)
ccmakefile.write(" $@")
ccmakefile.write("\n")
st = os.stat("%s/ccmake_cmd" % buildpath)
os.chmod("%s/ccmake_cmd" % buildpath, st.st_mode | stat.S_IEXEC)
# Add cache file option
cmakeline += " -C %s" % hostconfigpath
# Add build type (opt or debug)
cmakeline += " -DCMAKE_BUILD_TYPE=" + args.buildtype
# Set install dir
cmakeline += " -DCMAKE_INSTALL_PREFIX=%s" % installpath
if args.exportcompilercommands:
cmakeline += " -DCMAKE_EXPORT_COMPILE_COMMANDS=on"
if args.eclipse:
cmakeline += ' -G "Eclipse CDT4 - Unix Makefiles"'
if unknown_args:
cmakeline += " " + " ".join( unknown_args )
rootdir = os.path.dirname( os.path.abspath(sys.argv[0]) )
cmakeline += " %s " % rootdir
# Dump the cmake command to file for convenience
with open("%s/cmake_cmd" % buildpath, "w") as cmdfile:
cmdfile.write(cmakeline)
cmdfile.write("\n")
st = os.stat("%s/cmake_cmd" % buildpath)
os.chmod("%s/cmake_cmd" % buildpath, st.st_mode | stat.S_IEXEC)
return cmakeline
############################
# Run CMake
############################
def run_cmake(buildpath, cmakeline):
print("Changing to build directory...")
os.chdir(buildpath)
print("Executing CMake line: '%s'" % cmakeline)
print()
returncode = subprocess.call(cmakeline, shell=True)
if not returncode == 0:
print("Error: CMake command failed with return code: {0}".format(returncode))
return False
return True
############################
# Main
############################
def main():
repodir = os.path.abspath(os.path.dirname(__file__))
assert os.path.abspath(os.getcwd())==repodir, "config-build must be run from %s" % repodir
args, unknown_args = parse_arguments()
if args.print_machine_name:
machine_name = get_machine_name()
print(machine_name)
return True
if args.print_default_host_config:
default_hc = get_default_host_config()
if default_hc != "":
print(os.path.splitext(default_hc)[0])
return True
else:
return False
basehostconfigpath = find_host_config(args, repodir)
platform_info = get_platform_info(basehostconfigpath)
buildpath = setup_build_dir(args, platform_info)
installpath = setup_install_dir(args, platform_info)
cmakeline = create_cmake_command_line(args, unknown_args, buildpath, installpath, basehostconfigpath)
return run_cmake(buildpath, cmakeline)
if __name__ == '__main__':
exit(0 if main() else 1)