xref: /aosp_15_r20/external/pytorch/ios/TestApp/benchmark/setup.rb (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1require 'xcodeproj'
2require 'fileutils'
3require 'optparse'
4
5options = {}
6option_parser = OptionParser.new do |opts|
7 opts.banner = 'Script for setting up TestApp.xcodeproj'
8 opts.on('-t', '--team_id ', 'development team ID') { |value|
9    options[:team_id] = value
10 }
11 opts.on('-l', '--lite ', 'use lite interpreter') { |value|
12    options[:lite] = value
13 }
14 opts.on('-b', '--benchmark', 'build app to run benchmark') { |value|
15    options[:benchmark] = value
16 }
17end.parse!
18puts options.inspect
19
20puts "Current directory: #{Dir.pwd}"
21install_path = File.expand_path("../../../build_ios/install")
22if not Dir.exist? (install_path)
23    raise "path doesn't exist:#{install_path}!"
24end
25xcodeproj_path = File.expand_path("../TestApp.xcodeproj")
26if not File.exist? (xcodeproj_path)
27    raise "path doesn't exist:#{xcodeproj_path}!"
28end
29puts "Setting up TestApp.xcodeproj..."
30project = Xcodeproj::Project.open(xcodeproj_path)
31targets = project.targets
32test_target = targets.last
33header_search_path      = ['$(inherited)', "#{install_path}/include"]
34libraries_search_path   = ['$(inherited)', "#{install_path}/lib"]
35other_linker_flags      = ['$(inherited)', "-all_load"]
36# TestApp and TestAppTests
37targets.each do |target|
38    target.build_configurations.each do |config|
39        config.build_settings['HEADER_SEARCH_PATHS']    = header_search_path
40        config.build_settings['LIBRARY_SEARCH_PATHS']   = libraries_search_path
41        config.build_settings['OTHER_LDFLAGS']          = other_linker_flags
42        config.build_settings['ENABLE_BITCODE']         = 'No'
43        if (options[:lite])
44            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = ['$(inherited)', "BUILD_LITE_INTERPRETER"]
45        else
46            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = ['$(inherited)']
47        end
48        if (options[:benchmark])
49            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].append("RUN_BENCHMARK")
50        end
51        dev_team_id = options[:team_id]
52        if dev_team_id
53            config.build_settings['DEVELOPMENT_TEAM'] = dev_team_id
54        end
55    end
56end
57group = project.main_group.find_subpath(File.join('TestApp'),true)
58group.set_source_tree('SOURCE_ROOT')
59group.files.each do |file|
60    if (file.name.to_s.end_with?(".pt") ||
61        file.name.to_s.end_with?(".ptl") ||
62        file.name == "config.json")
63        group.remove_reference(file)
64        targets.each do |target|
65            target.resources_build_phase.remove_file_reference(file)
66        end
67    end
68end
69
70config_path = File.expand_path("./config.json")
71if not File.exist?(config_path)
72    raise "config.json can't be found!"
73end
74config_file_ref = group.new_reference(config_path)
75
76file_refs = []
77# collect models
78puts "Installing models..."
79models_dir = File.expand_path("../models")
80Dir.foreach(models_dir) do |model|
81    if(model.end_with?(".pt") || model.end_with?(".ptl"))
82      model_path = models_dir + "/" + model
83      file_refs.push(group.new_reference(model_path))
84    end
85end
86
87targets.each do |target|
88    target.resources_build_phase.add_file_reference(config_file_ref, true)
89    file_refs.each do |ref|
90        target.resources_build_phase.add_file_reference(ref, true)
91    end
92end
93
94# add test files
95puts "Adding test files..."
96testTarget = targets[1]
97testFilePath = File.expand_path('../TestAppTests/')
98group = project.main_group.find_subpath(File.join('TestAppTests'),true)
99group.files.each do |file|
100    if (file.path.end_with?(".mm"))
101        file.remove_from_project
102    end
103end
104
105if(options[:lite])
106    file = group.new_file("TestLiteInterpreter.mm")
107    testTarget.add_file_references([file])
108else
109    file = group.new_file("TestFullJIT.mm")
110    testTarget.add_file_references([file])
111end
112
113puts "Linking static libraries..."
114libs = ['libc10.a', 'libclog.a', 'libpthreadpool.a', 'libXNNPACK.a', 'libeigen_blas.a', 'libcpuinfo.a', 'libpytorch_qnnpack.a', 'libtorch_cpu.a', 'libtorch.a']
115frameworks = ['CoreML', 'Metal', 'MetalPerformanceShaders', 'Accelerate', 'UIKit']
116targets.each do |target|
117    # NB: All these libraries and frameworks have already been linked by TestApp, adding them
118    # again onto the test target will cause the app to crash on actual devices
119    if (target == test_target)
120        next
121    end
122    target.frameworks_build_phases.clear
123    for lib in libs do
124        path = "#{install_path}/lib/#{lib}"
125        if File.exist?(path)
126            libref = project.frameworks_group.new_file(path)
127            target.frameworks_build_phases.add_file_reference(libref)
128        end
129    end
130     # link system frameworks
131    if frameworks
132        frameworks.each do |framework|
133            path = "System/Library/Frameworks/#{framework}.framework"
134            framework_ref = project.frameworks_group.new_reference(path)
135            framework_ref.name = "#{framework}.framework"
136            framework_ref.source_tree = 'SDKROOT'
137            target.frameworks_build_phases.add_file_reference(framework_ref)
138        end
139    end
140
141end
142
143project.save
144puts "Done."
145