# Define a global platform for your project
#
# Match the canonical Apple deployment floor in runanywhere-commons/VERSIONS
# and every RunAnywhere Flutter podspec.
platform :ios, '17.5'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  # CocoaPods links the RACommons and backend archives into the app so their
  # exported C ABI remains available to Dart FFI. MLX also vendors the tiny
  # dynamic RunAnywhereMLXMetal framework so Xcode embeds the active platform
  # slice and its default.metallib; it is intentionally not a static archive.
  # The iOS loader resolves through DynamicLibrary.process() first and falls
  # back to DynamicLibrary.executable() for layouts that use the main binary.
  use_frameworks! :linkage => :static

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  target 'RunnerTests' do
    inherit! :search_paths
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.5'

      # Locally built RACommons / RABackend* xcframeworks ship an
      # `ios-arm64-simulator` slice (arm64 only, no x86_64). Xcode's default
      # simulator architecture list includes x86_64 on Intel hosts, so the
      # linker tries to pull a slice that doesn't exist. Excluding x86_64
      # across every pod and on the Runner app target keeps builds green
      # on both Apple Silicon and Intel hosts.
      config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'x86_64'

      # Enable permissions for permission_handler
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
        'PERMISSION_MICROPHONE=1',
        'PERMISSION_SPEECH_RECOGNIZER=1',
        'PERMISSION_CAMERA=1',
      ]
    end
  end

  # Mirror the x86_64 exclusion onto the Runner Xcode project itself so the
  # app target and any sibling targets (RunnerTests) pick it up — pods alone
  # aren't enough because the app's build configuration drives the final
  # linker invocation.
  installer.aggregate_targets.map(&:user_project).uniq.each do |project|
    project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'x86_64'
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '17.5'
      end
    end
    project.build_configurations.each do |config|
      config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'x86_64'
    end
    project.save
  end

  # NOTE: runanywhere.podspec retains and exports the static C ABI archives.
  # Backend-specific podspecs may use narrower symbol roots; MLX retains only
  # `_ra_mlx_runtime_is_available`. DynamicLibrary.process() can resolve the
  # exported symbols from every loaded image, with executable() as fallback.
  #
  # DO NOT use -exported_symbols_list here as it breaks Flutter's debug mode
  # by hiding the debug dylib entry point.
end
