<s> import subprocess <EOL> import re <EOL> import os <EOL> import errno <EOL> import collections <EOL> import sys <EOL> class Platform ( object ) : <EOL> pass <EOL> sdk_re = re . compile ( r'<STR_LIT>' ) <EOL> def sdkinfo ( sdkname ) : <EOL> ret = { } <EOL> for line in subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , sdkname , '<STR_LIT>' ] , stdout = subprocess . PIPE ) . stdout : <EOL> kv = line . strip ( ) . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if len ( kv ) == <NUM_LIT:2> : <EOL> k , v = kv <EOL> ret [ k ] = v <EOL> return ret <EOL> sim_sdk_info = sdkinfo ( '<STR_LIT>' ) <EOL> device_sdk_info = sdkinfo ( '<STR_LIT>' ) <EOL> def latest_sdks ( ) : <EOL> latest_sim = None <EOL> latest_device = None <EOL> for line in subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , stdout = subprocess . PIPE ) . stdout : <EOL> match = sdk_re . match ( line ) <EOL> if match : <EOL> if '<STR_LIT>' in line : <EOL> latest_sim = match . group ( <NUM_LIT:1> ) <EOL> elif '<STR_LIT>' in line : <EOL> latest_device = match . group ( <NUM_LIT:1> ) <EOL> return latest_sim , latest_device <EOL> sim_sdk , device_sdk = latest_sdks ( ) <EOL> class simulator_platform ( Platform ) : <EOL> sdk = '<STR_LIT>' <EOL> arch = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> triple = '<STR_LIT>' <EOL> sdkroot = sim_sdk_info [ '<STR_LIT>' ] <EOL> prefix = "<STR_LIT>" <EOL> suffix = "<STR_LIT>" <EOL> class device_platform ( Platform ) : <EOL> sdk = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> arch = '<STR_LIT>' <EOL> triple = '<STR_LIT>' <EOL> sdkroot = device_sdk_info [ '<STR_LIT>' ] <EOL> prefix = "<STR_LIT>" <EOL> suffix = "<STR_LIT>" <EOL> def move_file ( src_dir , dst_dir , filename , file_suffix = None , prefix = '<STR_LIT>' , suffix = '<STR_LIT>' ) : <EOL> if not os . path . exists ( dst_dir ) : <EOL> os . makedirs ( dst_dir ) <EOL> out_filename = filename <EOL> if file_suffix : <EOL> split_name = os . path . splitext ( filename ) <EOL> out_filename = "<STR_LIT>" % ( split_name [ <NUM_LIT:0> ] , file_suffix , split_name [ <NUM_LIT:1> ] ) <EOL> with open ( os . path . join ( src_dir , filename ) ) as in_file : <EOL> with open ( os . path . join ( dst_dir , out_filename ) , '<STR_LIT:w>' ) as out_file : <EOL> if prefix : <EOL> out_file . write ( prefix ) <EOL> out_file . write ( in_file . read ( ) ) <EOL> if suffix : <EOL> out_file . write ( suffix ) <EOL> headers_seen = collections . defaultdict ( set ) <EOL> def move_source_tree ( src_dir , dest_dir , dest_include_dir , arch = None , prefix = None , suffix = None ) : <EOL> for root , dirs , files in os . walk ( src_dir , followlinks = True ) : <EOL> relroot = os . path . relpath ( root , src_dir ) <EOL> def move_dir ( arch , prefix = '<STR_LIT>' , suffix = '<STR_LIT>' , files = [ ] ) : <EOL> for file in files : <EOL> file_suffix = None <EOL> if file . endswith ( '<STR_LIT>' ) : <EOL> if dest_include_dir : <EOL> file_suffix = arch <EOL> if arch : <EOL> headers_seen [ file ] . add ( arch ) <EOL> move_file ( root , dest_include_dir , file , arch , prefix = prefix , suffix = suffix ) <EOL> elif dest_dir : <EOL> outroot = os . path . join ( dest_dir , relroot ) <EOL> move_file ( root , outroot , file , prefix = prefix , suffix = suffix ) <EOL> if relroot == '<STR_LIT:.>' : <EOL> move_dir ( arch = arch , <EOL> files = files , <EOL> prefix = prefix , <EOL> suffix = suffix ) <EOL> elif relroot == '<STR_LIT>' : <EOL> move_dir ( arch = '<STR_LIT>' , <EOL> prefix = "<STR_LIT>" , <EOL> suffix = "<STR_LIT>" , <EOL> files = files ) <EOL> elif relroot == '<STR_LIT>' : <EOL> move_dir ( arch = '<STR_LIT>' , <EOL> prefix = "<STR_LIT>" , <EOL> suffix = "<STR_LIT>" , <EOL> files = files ) <EOL> def build_target ( platform ) : <EOL> def xcrun_cmd ( cmd ) : <EOL> return subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' , platform . sdkroot , '<STR_LIT>' , cmd ] ) . strip ( ) <EOL> build_dir = '<STR_LIT>' + platform . name <EOL> if not os . path . exists ( build_dir ) : <EOL> os . makedirs ( build_dir ) <EOL> env = dict ( CC = xcrun_cmd ( '<STR_LIT>' ) , <EOL> LD = xcrun_cmd ( '<STR_LIT>' ) , <EOL> CFLAGS = '<STR_LIT>' % ( platform . arch , platform . sdkroot ) ) <EOL> working_dir = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( build_dir ) <EOL> subprocess . check_call ( [ '<STR_LIT>' , '<STR_LIT>' , platform . triple ] , env = env ) <EOL> move_source_tree ( '<STR_LIT:.>' , None , '<STR_LIT>' , <EOL> arch = platform . arch , <EOL> prefix = platform . prefix , <EOL> suffix = platform . suffix ) <EOL> move_source_tree ( '<STR_LIT>' , None , '<STR_LIT>' , <EOL> arch = platform . arch , <EOL> prefix = platform . prefix , <EOL> suffix = platform . suffix ) <EOL> finally : <EOL> os . chdir ( working_dir ) <EOL> for header_name , archs in headers_seen . iteritems ( ) : <EOL> basename , suffix = os . path . splitext ( header_name ) <EOL> def main ( ) : <EOL> move_source_tree ( '<STR_LIT:src>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> move_source_tree ( '<STR_LIT>' , None , '<STR_LIT>' ) <EOL> build_target ( simulator_platform ) <EOL> build_target ( device_platform ) <EOL> for header_name , archs in headers_seen . iteritems ( ) : <EOL> basename , suffix = os . path . splitext ( header_name ) <EOL> with open ( os . path . join ( '<STR_LIT>' , header_name ) , '<STR_LIT:w>' ) as header : <EOL> for arch in archs : <EOL> header . write ( '<STR_LIT>' % ( basename , arch , suffix ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
