|
| 1 | +require 'open3' |
| 2 | + |
| 3 | +module RunLoop |
| 4 | + |
| 5 | + # An error class for signaling an incompatible architecture. |
| 6 | + class IncompatibleArchitecture < StandardError |
| 7 | + end |
| 8 | + |
| 9 | + # A class for interacting with the lipo command-line tool to verify that an |
| 10 | + # executable is valid for the test target (device or simulator). |
| 11 | + # |
| 12 | + # @note All lipo commands are run in the context of `xcrun`. |
| 13 | + class Lipo |
| 14 | + |
| 15 | + # The path to the application bundle we are inspecting. |
| 16 | + # @!attribute [wr] bundle_path |
| 17 | + # @return [String] The path to the application bundle (.app). |
| 18 | + attr_accessor :bundle_path |
| 19 | + |
| 20 | + def initialize(bundle_path) |
| 21 | + @bundle_path = bundle_path |
| 22 | + @plist_buddy = RunLoop::PlistBuddy.new |
| 23 | + end |
| 24 | + |
| 25 | + # Inspect the `CFBundleExecutable` in the app bundle path with `lipo` and |
| 26 | + # compare the result with the target device's instruction set. |
| 27 | + # |
| 28 | + # **Simulators** |
| 29 | + # |
| 30 | + # If the target is a simulator and the binary contains an i386 slice, the |
| 31 | + # app will launch on the 64-bit simulators. |
| 32 | + # |
| 33 | + # If the target is a simulator and the binary contains _only_ an x86_64 |
| 34 | + # slice, the app will not launch on these simulators: |
| 35 | + # |
| 36 | + # ``` |
| 37 | + # iPhone 4S, iPad 2, iPhone 5, and iPad Retina. |
| 38 | + # ``` |
| 39 | + # |
| 40 | + # All other simulators are 64-bit. |
| 41 | + # |
| 42 | + # **Devices** |
| 43 | + # |
| 44 | + # @see {https://www.innerfence.com/howto/apple-ios-devices-dates-versions-instruction-sets} |
| 45 | + # |
| 46 | + # ``` |
| 47 | + # armv7 <== 3gs, 4s, iPad 2, iPad mini, iPad 3, iPod 3, iPod 4, iPod 5 |
| 48 | + # armv7s <== 5, 5c, iPad 4 |
| 49 | + # arm64 <== 5s, 6, 6 Plus, Air, Air 2, iPad Mini Retina, iPad Mini 3 |
| 50 | + # ``` |
| 51 | + # |
| 52 | + # @note At the moment, we are focusing on simulator compatibility. Since we |
| 53 | + # don't have an automated way of installing an .ipa on local device, we |
| 54 | + # don't require an .ipa path. Without an .ipa path, we cannot verify the |
| 55 | + # architectures. Further, we would need to adopt a third-party tool like |
| 56 | + # ideviceinfo to find the target device's instruction set. |
| 57 | + # @param [RunLoop::Device] device The test target. |
| 58 | + # @raise [RuntimeError] Raises an error if the device is a physical device. |
| 59 | + # @raise [RunLoop::IncompatibleArchitecture] Raises an error if the instruction set of the target |
| 60 | + # device is not compatible with the executable in the application. |
| 61 | + def expect_compatible_arch(device) |
| 62 | + if device.physical_device? |
| 63 | + raise 'Ensuring compatible arches for physical devices is NYI' |
| 64 | + else |
| 65 | + arches = self.info |
| 66 | + # An i386 binary will run on any simulator. |
| 67 | + return true if arches.include?('i386') |
| 68 | + |
| 69 | + instruction_set = device.instruction_set |
| 70 | + unless arches.include?(instruction_set) |
| 71 | + raise RunLoop::IncompatibleArchitecture, |
| 72 | + ['Binary at:', |
| 73 | + binary_path, |
| 74 | + "does not contain a compatible architecture for target device.", |
| 75 | + "Expected '#{instruction_set}' but found #{arches}."].join("\n") |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # Returns a list of architecture in the binary. |
| 81 | + # @return [Array<String>] A list of architecture. |
| 82 | + def info |
| 83 | + execute_lipo("-info #{binary_path}") do |stdout, _, _| |
| 84 | + output = stdout.read.strip |
| 85 | + output.split(':')[-1].strip.split |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + private |
| 90 | + |
| 91 | + def execute_lipo(argument) |
| 92 | + command = "xcrun lipo #{argument}" |
| 93 | + Open3.popen3(command) do |_, stdout, stderr, wait_thr| |
| 94 | + yield stdout, stderr, wait_thr |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + def plist_path |
| 99 | + File.join(@bundle_path, 'Info.plist'); |
| 100 | + end |
| 101 | + |
| 102 | + def binary_path |
| 103 | + binary_relative_path = @plist_buddy.plist_read('CFBundleExecutable', plist_path) |
| 104 | + File.join(@bundle_path, binary_relative_path) |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments