1212#
1313# SPDX-License-Identifier: Apache-2.0
1414
15- import json
1615import os
1716import subprocess
1817from argparse import ArgumentParser
19- from pathlib import Path
20- from typing import List
2118
19+ from shell_source import source
2220from velocitas_lib import get_valid_arch , get_workspace_dir
2321
2422CMAKE_EXECUTABLE = "cmake"
@@ -33,35 +31,36 @@ def safe_get_workspace_dir() -> str:
3331 return os .path .abspath ("." )
3432
3533
36- def get_build_folder (build_arch : str , host_arch : str ) :
37- if host_arch == build_arch :
38- return os . path . join ( safe_get_workspace_dir (), "build" )
39- return os . path . join ( safe_get_workspace_dir (), f"build_linux_ { host_arch } " )
34+ def get_build_folder (op_sys : str , arch : str , build_type : str ) -> str :
35+ return os . path . join (
36+ safe_get_workspace_dir (), f "build- { op_sys } - { arch } " , f" { build_type } "
37+ )
4038
4139
42- def get_build_tools_path ( build_folder_path : str ) -> str :
43- paths : List [ str ] = []
44- with open (
45- os .path . join ( build_folder_path , "conanbuildinfo.txt" ), encoding = "utf-8"
46- ) as file :
47- for line in file :
48- if line . startswith ( "PATH=" ) :
49- path_list = json . loads ( line [ len ( "PATH=" ) :] )
50- paths . extend ( path_list )
51- return ";" . join ( paths )
40+ def safe_create_symlink_to_folder ( target_folder : str , link_name : str ) :
41+ try :
42+ if os . readlink ( link_name ):
43+ os .remove ( link_name )
44+ except FileNotFoundError :
45+ pass
46+ except OSError :
47+ print ( f"File ' { link_name } ' exists, but is not a symlink. Will not overwrite!" )
48+ return
49+ os . symlink ( target_folder , link_name , target_is_directory = True )
5250
5351
5452def print_build_info (
55- build_variant : str ,
53+ build_type : str ,
5654 build_arch : str ,
5755 host_arch : str ,
5856 build_target : str ,
5957 is_static_build : bool ,
58+ coverage : bool ,
6059) -> None :
6160 """Print information about the build.
6261
6362 Args:
64- build_variant (str): The variant of the build: "release" or "debug"
63+ build_type (str): The type of the build: "release" or "debug"
6564 build_arch (str): The architecture the app is built for.
6665 build_target (str): Which artefact is being built.
6766 is_static_build (bool): Enable static building.
@@ -77,70 +76,72 @@ def print_build_info(
7776 print (f"Conan version { conan_version } " )
7877 print (f"Build arch { build_arch } " )
7978 print (f"Host arch { host_arch } " )
80- print (f"Build variant { build_variant } " )
79+ print (f"Build type { build_type } " )
8180 print (f"Build target { build_target } " )
8281 print (f"Static build { 'yes' if is_static_build else 'no' } " )
82+ print (f"Coverage enabled { 'yes' if coverage else 'no' } " )
8383
8484
8585def build (
86- build_variant : str ,
86+ build_type : str ,
8787 build_arch : str ,
8888 host_arch : str ,
8989 build_target : str ,
9090 static_build : bool ,
9191 coverage : bool = False ,
92+ num_jobs : int = 2 ,
9293) -> None :
9394 cxx_flags = ["-g" ]
9495 if coverage :
9596 cxx_flags .append ("--coverage" )
9697
97- if build_variant == "release " :
98+ if build_type == "Release " :
9899 cxx_flags .append ("-s" )
99100 cxx_flags .append ("-O3" )
100101 else :
101102 cxx_flags .append ("-O0" )
102103
103- build_folder = get_build_folder (build_arch , host_arch )
104+ build_folder = get_build_folder ("linux" , host_arch , build_type )
104105 os .makedirs (build_folder , exist_ok = True )
105-
106- xcompile_toolchain_file = ""
107- if build_arch != host_arch :
108- profile_build_path = (
109- Path (__file__ )
110- .absolute ()
111- .parent .joinpath ("cmake" , f"{ build_arch } _to_{ host_arch } .cmake" )
106+ if build_arch == host_arch :
107+ safe_create_symlink_to_folder (
108+ build_folder , os .path .join (safe_get_workspace_dir (), "build" )
112109 )
113- xcompile_toolchain_file = f"-DCMAKE_TOOLCHAIN_FILE={ profile_build_path } "
110+
111+ build_env = source (
112+ os .path .join (build_folder , "generators/conanbuild.sh" ),
113+ "bash" ,
114+ ignore_locals = True ,
115+ )
114116
115117 subprocess .run (
116118 [
117119 CMAKE_EXECUTABLE ,
118120 "--no-warn-unused-cli" ,
119121 "-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE" ,
120- f"-DCMAKE_BUILD_TYPE:STRING={ build_variant } " ,
121- f'-DBUILD_TOOLS_PATH:STRING="{ get_build_tools_path (build_folder )} "' ,
122- f"-DSTATIC_BUILD:BOOL={ 'TRUE' if static_build else 'FALSE' } " ,
123- xcompile_toolchain_file ,
122+ f"-DCMAKE_BUILD_TYPE:STRING={ build_type } " ,
123+ f"-DSTATIC_BUILD:BOOL={ 'ON' if static_build else 'OFF' } " ,
124+ f"-DCMAKE_CXX_FLAGS={ ' ' .join (cxx_flags )} " ,
125+ "-DCMAKE_TOOLCHAIN_FILE=generators/conan_toolchain.cmake" ,
126+ "-G" ,
127+ "Ninja" ,
124128 "-S" ,
125129 safe_get_workspace_dir (),
126130 "-B." ,
127- "-G" ,
128- "Ninja" ,
129- f"-DCMAKE_CXX_FLAGS={ ' ' .join (cxx_flags )} " ,
130131 ],
131132 cwd = build_folder ,
133+ env = build_env ,
132134 )
133135 subprocess .run (
134136 [
135137 CMAKE_EXECUTABLE ,
136138 "--build" ,
137139 "." ,
138- "--config" ,
139- build_variant ,
140140 "--target" ,
141141 build_target ,
142142 ],
143143 cwd = build_folder ,
144+ env = build_env ,
144145 )
145146
146147
@@ -154,21 +155,24 @@ def cli() -> None:
154155 "-d" ,
155156 "--debug" ,
156157 action = "store_const" ,
157- const = "debug " ,
158- dest = "variant " ,
158+ const = "Debug " ,
159+ dest = "build_type " ,
159160 help = "Builds the target(s) in debug mode." ,
160161 )
161162 parser .add_argument (
162163 "-r" ,
163164 "--release" ,
164165 action = "store_const" ,
165- const = "release " ,
166- dest = "variant " ,
166+ const = "Release " ,
167+ dest = "build_type " ,
167168 help = "Builds the target(s) in release mode." ,
168169 )
169170 parser .add_argument (
170171 "-t" , "--target" , help = "Builds only the target <name> instead of all targets."
171172 )
173+ parser .add_argument (
174+ "-j" , "--jobs" , help = "Number of parallel jobs to use for building."
175+ )
172176 parser .add_argument (
173177 "-s" , "--static" , action = "store_true" , help = "Links all dependencies statically."
174178 )
@@ -179,22 +183,32 @@ def cli() -> None:
179183 help = "Enables cross-compilation to the defined target architecture." ,
180184 )
181185 parser .add_argument ("--coverage" , action = "store_true" , help = "Enable gtest coverage" )
186+
182187 args = parser .parse_args ()
183- if not args .variant :
184- args .variant = "debug "
188+ if not args .build_type :
189+ args .build_type = "Debug "
185190 if not args .target :
186191 args .target = "all"
187192 build_arch = subprocess .check_output (["arch" ], encoding = "utf-8" ).strip ()
188193
189194 host_arch = args .cross
190-
191195 if host_arch is None :
192196 host_arch = build_arch
193197 else :
194198 host_arch = get_valid_arch (host_arch )
195199
196- print_build_info (args .variant , build_arch , host_arch , args .target , args .static )
197- build (args .variant , build_arch , host_arch , args .target , args .static , args .coverage )
200+ print_build_info (
201+ args .build_type , build_arch , host_arch , args .target , args .static , args .coverage
202+ )
203+ build (
204+ args .build_type ,
205+ build_arch ,
206+ host_arch ,
207+ args .target ,
208+ args .static ,
209+ args .coverage ,
210+ args .jobs ,
211+ )
198212
199213
200214if __name__ == "__main__" :
0 commit comments