-
Notifications
You must be signed in to change notification settings - Fork 15
/
rmake.py
257 lines (208 loc) · 8.2 KB
/
rmake.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
#!/usr/bin/python3
"""Copyright (C) 2021-2022 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop-
ies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE-
CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import re
import sys
import os
import platform
import subprocess
import argparse
import pathlib
from fnmatch import fnmatchcase
args = {}
param = {}
OS_info = {}
def parse_args():
"""Parse command-line arguments"""
parser = argparse.ArgumentParser(description="""Checks build arguments""")
# common
parser.add_argument('-g', '--debug', required=False, default = False, action='store_true',
help='Generate Debug build (optional, default: False)')
parser.add_argument( '--build_dir', type=str, required=False, default = "build",
help='Build directory path (optional, default: build)')
# parser.add_argument('-i', '--install', required=False, default = False, dest='install', action='store_true',
# help='Install after build (optional, default: False)')
parser.add_argument( '--cmake_darg', required=False, dest='cmake_dargs', action='append', default=[],
help='List of additional cmake defines for builds (optional, e.g. CMAKE)')
parser.add_argument('-v', '--verbose', required=False, default = False, action='store_true',
help='Verbose build (optional, default: False)')
# rocblas-Examples
parser.add_argument( '--library_path', type=str, required=False, default = "",
help='If non-standard path to the pre-built rocBLAS library (optional, default: C:\hipSDK)')
return parser.parse_args()
def os_detect():
global OS_info
if os.name == "nt":
OS_info["ID"] = platform.system()
else:
inf_file = "/etc/os-release"
if os.path.exists(inf_file):
with open(inf_file) as f:
for line in f:
if "=" in line:
k,v = line.strip().split("=")
OS_info[k] = v.replace('"','')
OS_info["NUM_PROC"] = os.cpu_count()
print(OS_info)
def create_dir(dir_path):
full_path = ""
if os.path.isabs(dir_path):
full_path = dir_path
else:
full_path = os.path.join( os.getcwd(), dir_path )
pathlib.Path(full_path).mkdir(parents=True, exist_ok=True)
return
def delete_dir(dir_path) :
if (not os.path.exists(dir_path)):
return
if os.name == "nt":
run_cmd( "RMDIR" , f"/S /Q {dir_path}")
else:
run_cmd( "rm" , f"-rf {dir_path}")
def cmake_path(os_path):
if os.name == "nt":
return os_path.replace("\\", "/")
else:
return os_path
def config_cmd(src_path, use_hipcc):
global args
global OS_info
cmake_executable = ""
cmake_options = []
cmake_platform_opts = []
if os.name == "nt":
sdk_path = os.getenv( 'ROCM_CMAKE_PATH', "C:/hipSDK")
cmake_executable = "cmake"
#set CPACK_PACKAGING_INSTALL_PREFIX= defined as blank as it is appended to end of path for archive creation
#cmake_platform_opts.append( f"-DCPACK_PACKAGING_INSTALL_PREFIX=" )
#cmake_platform_opts.append( f"-DCMAKE_INSTALL_PREFIX=\"C:/hipSDK\"" )
if use_hipcc:
generator = f"-G Ninja"
cmake_options.append( generator )
else:
sdk_path = os.getenv( 'ROCM_PATH', "/opt/rocm")
if (OS_info["ID"] in ['centos', 'rhel']):
cmake_executable = "cmake" # was cmake3 but now we built cmake
else:
cmake_executable = "cmake"
cmake_platform_opts.append( f"-DROCM_DIR:PATH={sdk_path} -DCPACK_PACKAGING_INSTALL_PREFIX={sdk_path}" )
cmake_platform_opts.append( f"-DCMAKE_INSTALL_PREFIX=\"rocblas-install\"" )
toolchain = "toolchain-linux.cmake"
print( f"Build source path: {src_path}")
if use_hipcc:
cmake_options.append( f"-DCMAKE_CXX_COMPILER=hipcc.bat" )
cmake_options.extend( cmake_platform_opts )
if args.library_path:
prefix_path = cmake_path(args.library_path)
elif os.path.exists(sdk_path):
prefix_path = sdk_path
if prefix_path:
cmake_base_options = f"-DCMAKE_PREFIX_PATH:PATH={prefix_path}"
cmake_options.append( cmake_base_options )
# packaging options
# cmake_pack_options = f"-DCPACK_SET_DESTDIR=OFF"
# cmake_options.append( cmake_pack_options )
if os.getenv('CMAKE_CXX_COMPILER_LAUNCHER'):
cmake_options.append( f"-DCMAKE_CXX_COMPILER_LAUNCHER={os.getenv('CMAKE_CXX_COMPILER_LAUNCHER')}" )
print( cmake_options )
build_dir = os.path.abspath(args.build_dir)
# build type
if use_hipcc:
cmake_config = ""
if not args.debug:
build_path = os.path.join(build_dir, "release")
cmake_config="Release"
else:
build_path = os.path.join(build_dir, "debug")
cmake_config="Debug"
cmake_options.append( f"-DCMAKE_BUILD_TYPE={cmake_config}" )
else:
build_path = os.path.join(build_dir, "msvc")
# clean
delete_dir( build_path )
create_dir( build_path )
os.chdir( build_path )
if args.cmake_dargs:
for i in args.cmake_dargs:
cmake_options.append( f"-D{i}" )
cmake_options.append( f"{src_path}")
cmd_opts = " ".join(cmake_options)
return cmake_executable, cmd_opts
def make_cmd():
global args
global OS_info
make_options = []
nproc = OS_info["NUM_PROC"]
if os.name == "nt":
make_executable = f"cmake.exe --build . " # ninja
if args.verbose:
make_options.append( "--verbose" )
make_options.append( "--target all" )
# if args.install:
# make_options.append( "--target package --target install" )
else:
make_executable = f"make -j{nproc}"
if args.verbose:
make_options.append( "VERBOSE=1" )
if True: # args.install:
make_options.append( "install" )
cmd_opts = " ".join(make_options)
return make_executable, cmd_opts
def msvc_cmd():
global args
global OS_info
make_options = []
nproc = OS_info["NUM_PROC"]
if os.name == "nt":
make_executable = f"msbuild rocblas-examples.sln -property:Configuration=Release"
if args.verbose:
make_options.append( "--verbose" )
else:
make_executable = f"make -j{nproc}"
if args.verbose:
make_options.append( "VERBOSE=1" )
if True: # args.install:
make_options.append( "install" )
cmd_opts = " ".join(make_options)
return make_executable, cmd_opts
def run_cmd(exe, opts):
program = f"{exe} {opts}"
print(program)
proc = subprocess.run(program, check=True, stderr=subprocess.STDOUT, shell=True)
return proc.returncode
def main():
global args
os_detect()
args = parse_args()
cwd = os.getcwd()
src_path = cmake_path(cwd)
# configure for non hipcc
exe, opts = config_cmd(src_path, False)
run_cmd(exe, opts)
# make
exe, opts = msvc_cmd()
run_cmd(exe, opts)
# second pass with hipcc and ninja on windows
os.chdir(cwd)
# configure for hipcc
exe, opts = config_cmd(src_path, True)
run_cmd(exe, opts)
# make
exe, opts = make_cmd()
run_cmd(exe, opts)
if __name__ == '__main__':
main()