xref: /aosp_15_r20/external/pytorch/scripts/xcode_build.rb (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Workerrequire 'optparse'
2*da0073e9SAndroid Build Coastguard Workerrequire 'xcodeproj'
3*da0073e9SAndroid Build Coastguard Worker
4*da0073e9SAndroid Build Coastguard Workeroptions = {}
5*da0073e9SAndroid Build Coastguard Workeroption_parser = OptionParser.new do |opts|
6*da0073e9SAndroid Build Coastguard Worker opts.banner = 'Tools for building PyTorch iOS framework on MacOS'
7*da0073e9SAndroid Build Coastguard Worker opts.on('-i', '--install_path ', 'path to the cmake install folder') { |value|
8*da0073e9SAndroid Build Coastguard Worker    options[:install] = value
9*da0073e9SAndroid Build Coastguard Worker }
10*da0073e9SAndroid Build Coastguard Worker opts.on('-x', '--xcodeproj_path ', 'path to the XCode project file') { |value|
11*da0073e9SAndroid Build Coastguard Worker    options[:xcodeproj] = value
12*da0073e9SAndroid Build Coastguard Worker }
13*da0073e9SAndroid Build Coastguard Worker opts.on('-p', '--platform ', 'platform for the current build, OS or SIMULATOR') { |value|
14*da0073e9SAndroid Build Coastguard Worker    options[:platform] = value
15*da0073e9SAndroid Build Coastguard Worker }
16*da0073e9SAndroid Build Coastguard Workerend.parse!
17*da0073e9SAndroid Build Coastguard Workerputs options.inspect
18*da0073e9SAndroid Build Coastguard Worker
19*da0073e9SAndroid Build Coastguard Workerinstall_path = File.expand_path(options[:install])
20*da0073e9SAndroid Build Coastguard Workerif not Dir.exist? (install_path)
21*da0073e9SAndroid Build Coastguard Worker    raise "path don't exist:#{install_path}!"
22*da0073e9SAndroid Build Coastguard Workerend
23*da0073e9SAndroid Build Coastguard Workerxcodeproj_path = File.expand_path(options[:xcodeproj])
24*da0073e9SAndroid Build Coastguard Workerif not File.exist? (xcodeproj_path)
25*da0073e9SAndroid Build Coastguard Worker    raise "path don't exist:#{xcodeproj_path}!"
26*da0073e9SAndroid Build Coastguard Workerend
27*da0073e9SAndroid Build Coastguard Worker
28*da0073e9SAndroid Build Coastguard Workerproject = Xcodeproj::Project.open(xcodeproj_path)
29*da0073e9SAndroid Build Coastguard Workertarget = project.targets.first #TestApp
30*da0073e9SAndroid Build Coastguard Workerheader_search_path      = ['$(inherited)', "#{install_path}/include"]
31*da0073e9SAndroid Build Coastguard Workerlibraries_search_path   = ['$(inherited)', "#{install_path}/lib"]
32*da0073e9SAndroid Build Coastguard Workerother_linker_flags      = ['$(inherited)', "-all_load"]
33*da0073e9SAndroid Build Coastguard Worker
34*da0073e9SAndroid Build Coastguard Workertarget.build_configurations.each do |config|
35*da0073e9SAndroid Build Coastguard Worker    config.build_settings['HEADER_SEARCH_PATHS']    = header_search_path
36*da0073e9SAndroid Build Coastguard Worker    config.build_settings['LIBRARY_SEARCH_PATHS']   = libraries_search_path
37*da0073e9SAndroid Build Coastguard Worker    config.build_settings['OTHER_LDFLAGS']          = other_linker_flags
38*da0073e9SAndroid Build Coastguard Worker    config.build_settings['ENABLE_BITCODE']         = 'No'
39*da0073e9SAndroid Build Coastguard Workerend
40*da0073e9SAndroid Build Coastguard Worker
41*da0073e9SAndroid Build Coastguard Worker# link static libraries
42*da0073e9SAndroid Build Coastguard Workertarget.frameworks_build_phases.clear
43*da0073e9SAndroid Build Coastguard Workerlibs = ['libc10.a', 'libclog.a', 'libpthreadpool.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a', 'libkineto.a']
44*da0073e9SAndroid Build Coastguard Workerfor lib in libs do
45*da0073e9SAndroid Build Coastguard Worker    path = "#{install_path}/lib/#{lib}"
46*da0073e9SAndroid Build Coastguard Worker    if File.exist?(path)
47*da0073e9SAndroid Build Coastguard Worker        libref = project.frameworks_group.new_file(path)
48*da0073e9SAndroid Build Coastguard Worker        target.frameworks_build_phases.add_file_reference(libref)
49*da0073e9SAndroid Build Coastguard Worker    end
50*da0073e9SAndroid Build Coastguard Workerend
51*da0073e9SAndroid Build Coastguard Worker# link system frameworks
52*da0073e9SAndroid Build Coastguard Workerframeworks = ['CoreML', 'Metal', 'MetalPerformanceShaders', 'Accelerate', 'UIKit']
53*da0073e9SAndroid Build Coastguard Workerif frameworks
54*da0073e9SAndroid Build Coastguard Worker    frameworks.each do |framework|
55*da0073e9SAndroid Build Coastguard Worker        path = "System/Library/Frameworks/#{framework}.framework"
56*da0073e9SAndroid Build Coastguard Worker        framework_ref = project.frameworks_group.new_reference(path)
57*da0073e9SAndroid Build Coastguard Worker        framework_ref.name = "#{framework}.framework"
58*da0073e9SAndroid Build Coastguard Worker        framework_ref.source_tree = 'SDKROOT'
59*da0073e9SAndroid Build Coastguard Worker        target.frameworks_build_phases.add_file_reference(framework_ref)
60*da0073e9SAndroid Build Coastguard Worker    end
61*da0073e9SAndroid Build Coastguard Workerend
62*da0073e9SAndroid Build Coastguard Workerproject.save
63*da0073e9SAndroid Build Coastguard Worker
64*da0073e9SAndroid Build Coastguard Workersdk = nil
65*da0073e9SAndroid Build Coastguard Workerarch = nil
66*da0073e9SAndroid Build Coastguard Workerif options[:platform] == 'SIMULATOR'
67*da0073e9SAndroid Build Coastguard Worker    sdk = 'iphonesimulator'
68*da0073e9SAndroid Build Coastguard Worker    arch = 'arm64'
69*da0073e9SAndroid Build Coastguard Workerelsif options[:platform] == 'OS'
70*da0073e9SAndroid Build Coastguard Worker    sdk = 'iphoneos'
71*da0073e9SAndroid Build Coastguard Worker    arch = 'arm64'
72*da0073e9SAndroid Build Coastguard Workerelse
73*da0073e9SAndroid Build Coastguard Worker    raise "unsupported platform #{options[:platform]}"
74*da0073e9SAndroid Build Coastguard Workerend
75*da0073e9SAndroid Build Coastguard Worker
76*da0073e9SAndroid Build Coastguard Workerexec "xcodebuild clean build -project #{xcodeproj_path} -alltargets -sdk #{sdk} -configuration Release -arch #{arch}"
77