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