2525import sys
2626import errno
2727
28+ g_macos_deployment_target = '10.10'
29+
2830def note (message ):
2931 print ("--- %s: note: %s" % (os .path .basename (sys .argv [0 ]), message ))
3032 sys .stdout .flush ()
@@ -71,6 +73,11 @@ def main():
7173 """ )
7274 subparsers = parser .add_subparsers (dest = 'command' )
7375
76+ # clean
77+ parser_clean = subparsers .add_parser ("clean" , help = "cleans build artifacts" )
78+ parser_clean .set_defaults (func = clean )
79+ add_global_args (parser_clean )
80+
7481 # build
7582 parser_build = subparsers .add_parser ("build" , help = "builds TSC using CMake" )
7683 parser_build .set_defaults (func = build )
@@ -120,6 +127,7 @@ def parse_global_args(args):
120127 """Parses and cleans arguments necessary for all actions."""
121128 args .build_dir = os .path .abspath (args .build_dir )
122129 args .project_root = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
130+ args .swift_collections_source_dir = os .path .join (args .project_root , ".." , "swift-collections" )
123131
124132 if platform .system () == 'Darwin' :
125133 args .sysroot = call_output (["xcrun" , "--sdk" , "macosx" , "--show-sdk-path" ], verbose = args .verbose )
@@ -133,6 +141,7 @@ def parse_build_args(args):
133141 args .swiftc_path = get_swiftc_path (args )
134142 args .cmake_path = get_cmake_path (args )
135143 args .ninja_path = get_ninja_path (args )
144+ args .target_dir = os .path .join (args .build_dir , get_build_target (args ))
136145
137146def get_swiftc_path (args ):
138147 """Returns the path to the Swift compiler."""
@@ -180,14 +189,38 @@ def get_ninja_path(args):
180189 else :
181190 return call_output (["which" , "ninja" ], verbose = args .verbose )
182191
192+ def get_build_target (args , cross_compile = False ):
193+ """Returns the target-triple of the current machine or for cross-compilation."""
194+ try :
195+ command = [args .swiftc_path , '-print-target-info' ]
196+ if cross_compile :
197+ cross_compile_json = json .load (open (args .cross_compile_config ))
198+ command += ['-target' , cross_compile_json ["target" ]]
199+ target_info_json = subprocess .check_output (command ,
200+ stderr = subprocess .PIPE , universal_newlines = True ).strip ()
201+ args .target_info = json .loads (target_info_json )
202+ return args .target_info ["target" ]["unversionedTriple" ]
203+ except Exception as e :
204+ # Temporary fallback for Darwin.
205+ if platform .system () == 'Darwin' :
206+ return 'x86_64-apple-macosx'
207+ else :
208+ error (str (e ))
209+
183210# -----------------------------------------------------------
184211# Actions
185212# -----------------------------------------------------------
186213
187214def build (args ):
188215 parse_build_args (args )
216+ build_swift_collections (args )
189217 build_tsc (args )
190218
219+ def clean (args ):
220+ parse_global_args (args )
221+
222+ call (["rm" , "-rf" , args .build_dir ], verbose = args .verbose )
223+
191224# -----------------------------------------------------------
192225# Build functions
193226# -----------------------------------------------------------
@@ -222,11 +255,27 @@ def build_with_cmake(args, cmake_args, source_path, build_dir):
222255
223256 call (ninja_cmd , cwd = build_dir , verbose = args .verbose )
224257
225- def build_tsc (args ):
258+ def build_swift_collections (args ):
259+ note ("Building swift-collections" )
260+ args .swift_collections_build_dir = os .path .join (args .target_dir , "swift-collections" )
261+
226262 cmake_flags = []
227263 if platform .system () == 'Darwin' :
228- cmake_flags .append ("-DCMAKE_C_FLAGS=-target x86_64-apple-macosx10.10" )
229- cmake_flags .append ("-DCMAKE_OSX_DEPLOYMENT_TARGET=10.10" )
264+ cmake_flags .append ("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target (args ), g_macos_deployment_target ))
265+ cmake_flags .append ("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target )
266+
267+ build_with_cmake (args , cmake_flags , args .swift_collections_source_dir , args .swift_collections_build_dir )
268+
269+
270+ def build_tsc (args ):
271+ note ("Building TSC" )
272+
273+ cmake_flags = [
274+ "-DSwiftCollections_DIR=" + os .path .join (args .swift_collections_build_dir , "cmake/modules" ),
275+ ]
276+ if platform .system () == 'Darwin' :
277+ cmake_flags .append ("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target (args ), g_macos_deployment_target ))
278+ cmake_flags .append ("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target )
230279
231280 build_with_cmake (args , cmake_flags , args .project_root , args .build_dir )
232281
0 commit comments