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