From 92564f33fdaa8f0e9968712863e7e00f7073cdf7 Mon Sep 17 00:00:00 2001 From: Christian Falch Date: Tue, 15 Sep 2026 07:40:13 -0700 Subject: [PATCH] Derive the generated manifests' iOS platform floor from the app (#58379) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: The SwiftPM manifests React Native generates — the `Autolinked` aggregate, the per-dependency synth packages, and the scaffolded community packages — hardcoded `platforms: [.iOS(.v15)]`. SwiftPM refuses to link a product whose minimum platform is above the depending target's, so any self-managed package with a higher floor could not be autolinked. Every Expo package declares iOS 16.4, so a stock Expo app on 0.87.1 fails at build time with: ``` error: The package product 'Expo' requires minimum platform version 16.4 for the iOS platform, but this target supports 15.0 (in target 'AutolinkedAggregate' from project 'Autolinked') ``` The app itself is correct (`IPHONEOS_DEPLOYMENT_TARGET = 16.4`); the only disagreement was the generated manifests. The floor is now the app's own `IPHONEOS_DEPLOYMENT_TARGET`, read from the injected `.xcodeproj`: - Target selection: the injection marker's `targetUuid`, else the application target named by `--product-name`, else every application target. - Per configuration: the target's own value, else the project-level configuration of the same name. The lowest value wins, since one manifest floor has to hold for every configuration. - Clamped to React Native's minimum (15.1, `min_ios_version_supported`) and emitted in string form (`.iOS("16.4")`), because the `.vNN` enum form cannot express a minor version. `setup-apple-spm.js` resolves the value once per `add`/`update`/`sync`/`scaffold` run, logs it (`iOS deployment target: 16.4 (from MyApp.xcodeproj)`), and passes it to the generator, the sync script, and the scaffolder via a new `--ios-deployment-target` flag. The flag value is sanitized at the generator boundary as well. `SCAFFOLDER_VERSION` is bumped to 20 so existing scaffolds regenerate with the new floor. A floor set in an `.xcconfig` the configuration is based on is honored, including `#include` chains (target literal → target xcconfig → project literal → project xcconfig). A floor supplied through a build-setting variable (`$(MY_FLOOR)`) is not resolved and falls back to the minimum; the log line says so. `spm add` and `spm update` also refresh the `platforms:` element of existing scaffolded manifests (those carrying the scaffolder marker, including the pre-v20 `.iOS(.v15)` form, over the same expanded dependency set the scaffolder uses, transitive SwiftPM dependencies included) without creating new scaffolds or touching anything else in them. Known limitation, documented in `scripts/spm/__docs__/spm-scripts.md`: the project file is not one of the auto-sync build phase's staleness inputs, so a deployment target changed after `spm add` takes effect on the next `spm update`. The default floor moves from 15.0 to 15.1 for apps without a readable project setting; React Native already requires 15.1 for the app target, so this is not observable. ## Changelog: [IOS] [FIXED] - SwiftPM: generated manifests derive their iOS platform floor from the app's `IPHONEOS_DEPLOYMENT_TARGET` instead of hardcoding iOS 15, so dependencies with a higher minimum (e.g. Expo, 16.4) can be autolinked Pull Request resolved: https://github.com/react/react-native/pull/58379 Test Plan: Unit tests were written first and seen failing, then made green: ``` yarn jest --no-cache -i packages/react-native/scripts/spm Test Suites: 37 passed, 37 total Tests: 1733 passed, 1733 total ``` New/updated coverage: `ios-deployment-target-test.js` (pbxproj reading: target-level, project-level fallback, xcconfig chain with `#include` and `` anchoring, minimum across configurations, marker/`--product-name`/all-targets selection, invalid values, clamping, unreadable project), `scaffold-package-swift-test.js` (emitted floor, refresh of existing scaffolded manifests), `generate-spm-autolinking-test.js` (aggregate and synth templates, `--ios-deployment-target` through `main()`), `scaffold-package-swift-test.js`, `sync-spm-autolinking-test.js` (flag forwarding), `setup-apple-spm-test.js` (resolution and log line). ESLint (`--max-warnings 0`), Prettier, and Flow are clean on the touched files. Not run here: an end-to-end Xcode build of an Expo app on this branch. The failing scenario and the fix were verified by the Expo team against 0.87.1 with the same manifest change patched in locally (build proceeds past SwiftPM resolution into compiling `Expo`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: cortinico Differential Revision: D119653699 Pulled By: cipolleschi fbshipit-source-id: 7c69dd4d97bed719f7bbd28fb0832cdf2f151f55 --- .../react-native/scripts/setup-apple-spm.js | 70 +++- .../scripts/spm/__docs__/spm-scripts.md | 17 + .../generate-spm-autolinking-test.js | 93 +++++ .../__tests__/ios-deployment-target-test.js | 268 +++++++++++++ .../scripts/spm/__tests__/pbxproj-variants.js | 146 +++++++ .../__tests__/scaffold-package-swift-test.js | 206 ++++++++++ .../spm/__tests__/setup-apple-spm-test.js | 50 +++ .../__tests__/sync-spm-autolinking-test.js | 20 +- .../scripts/spm/generate-spm-autolinking.js | 21 +- .../scripts/spm/generate-spm-xcodeproj.js | 4 +- .../scripts/spm/ios-deployment-target.js | 378 ++++++++++++++++++ .../scripts/spm/scaffold-package-swift.js | 235 ++++++++--- .../react-native/scripts/spm/spm-pbxproj.js | 63 +-- .../react-native/scripts/spm/spm-types.js | 4 + .../scripts/spm/sync-spm-autolinking.js | 13 +- 15 files changed, 1493 insertions(+), 95 deletions(-) create mode 100644 packages/react-native/scripts/spm/__tests__/ios-deployment-target-test.js create mode 100644 packages/react-native/scripts/spm/__tests__/pbxproj-variants.js create mode 100644 packages/react-native/scripts/spm/ios-deployment-target.js diff --git a/packages/react-native/scripts/setup-apple-spm.js b/packages/react-native/scripts/setup-apple-spm.js index a89f9f138dc..0ace9683a03 100644 --- a/packages/react-native/scripts/setup-apple-spm.js +++ b/packages/react-native/scripts/setup-apple-spm.js @@ -99,10 +99,18 @@ const { findInjectedXcodeproj, injectSpmIntoExistingXcodeproj, readArtifactsVersionOverride, + readMarker, readPinnedConfigCommand, removeSpmInjection, } = require('./spm/generate-spm-xcodeproj'); -const {scaffoldAll} = require('./spm/scaffold-package-swift'); +const { + MIN_IOS_VERSION_SUPPORTED, + resolveIosDeploymentTarget, +} = require('./spm/ios-deployment-target'); +const { + refreshScaffoldedPlatformFloors, + scaffoldAll, +} = require('./spm/scaffold-package-swift'); const { RemoteVersionError, buildPerAppHeaderTree, @@ -452,6 +460,7 @@ async function runScaffold( appRoot /*: string */, projectRoot /*: string */, reactNativeRoot /*: string */, + iosDeploymentTarget /*: string */, ) /*: Promise */ { // Resolve the cache slot identifier so the scaffolded files carry it as // a comment — that's how SPM's manifest hash bumps on slot transitions. @@ -472,6 +481,7 @@ async function runScaffold( projectRoot, reactNativeRoot, cacheSlotLabel, + iosDeploymentTarget, // Always force a re-render so re-running after editing a podspec picks // up the new content. force: true, @@ -788,6 +798,29 @@ function resolveInjectionTarget( return {path: path.join(appRoot, names[0])}; } +/** The app's own floor for every manifest this run generates, logged once. */ +function resolveAppIosDeploymentTarget( + args /*: SetupArgs */, + appRoot /*: string */, +) /*: string */ { + const target = resolveInjectionTarget(args, appRoot); + const xcodeprojPath = target.path ?? null; + const read = resolveIosDeploymentTarget({ + xcodeprojPath, + targetUuid: + xcodeprojPath != null ? readMarker(xcodeprojPath)?.targetUuid : null, + targetName: args.productName, + }); + const value = read ?? MIN_IOS_VERSION_SUPPORTED; + const origin = + read != null && xcodeprojPath != null + ? `from ${path.basename(xcodeprojPath)}` + : // `sync` and `scaffold` don't otherwise surface why no project was picked. + `react-native default${target.error != null ? `; ${target.error}` : ''}`; + log(`iOS deployment target: ${value} (${origin})`); + return value; +} + /** * Inject SPM packages into the user's existing .xcodeproj, in place — the only * xcodeproj strategy (`add` and `update` both run this; there is no @@ -1117,6 +1150,9 @@ async function main(argv /*:: ?: Array */) /*: Promise */ { } log(`Wrote ${path.relative(appRoot, autolinkingConfigResult.outputPath)}`); } + const iosDeploymentTarget /*: string */ = needsCliConfig + ? resolveAppIosDeploymentTarget(args, appRoot) + : MIN_IOS_VERSION_SUPPORTED; const reactNativeRoot = resolveReactNativeRoot( autolinkingConfigResult, projectRoot, @@ -1160,6 +1196,8 @@ async function main(argv /*:: ?: Array */) /*: Promise */ { appRoot, '--react-native-root', reactNativeRoot, + '--ios-deployment-target', + iosDeploymentTarget, ]); } catch (e) { if (e instanceof MissingManifestError) { @@ -1193,10 +1231,35 @@ async function main(argv /*:: ?: Array */) /*: Promise */ { // visible and fixed deliberately (scaffold + patch-package, or upstream). // Auto-scaffolding would silently hide that real error. if (action === 'scaffold') { - await runScaffold(args, appRoot, projectRoot, reactNativeRoot); + await runScaffold( + args, + appRoot, + projectRoot, + reactNativeRoot, + iosDeploymentTarget, + ); } runCodegenStep(projectRoot, appRoot, reactNativeRoot, args.skipCodegen); + + // A manifest scaffolded before the app's deployment target changed (or by a + // pre-v20 scaffolder) would pin a floor SwiftPM then refuses to link against. + if ( + (action === 'add' || action === 'update') && + autolinkingConfigResult != null + ) { + for (const refreshed of refreshScaffoldedPlatformFloors({ + appRoot, + autolinkingJsonPath: autolinkingConfigResult.outputPath, + iosDeploymentTarget, + })) { + log( + `Refreshed platform floor in ${path.relative(appRoot, refreshed.path)}: ` + + `${refreshed.from} → ${refreshed.to}`, + ); + } + } + log('Generating build/generated/autolinking/Package.swift...'); try { generateAutolinking([ @@ -1204,6 +1267,8 @@ async function main(argv /*:: ?: Array */) /*: Promise */ { appRoot, '--react-native-root', reactNativeRoot, + '--ios-deployment-target', + iosDeploymentTarget, ]); } catch (e) { if (e instanceof MissingManifestError) { @@ -1288,6 +1353,7 @@ module.exports = { generateAutolinkingConfigOrFailClosed, parseArgs, resolveAction, + resolveAppIosDeploymentTarget, resolveConfigCommandToPin, resolveExplicitConfigCommand, shouldAutoDeintegrate, diff --git a/packages/react-native/scripts/spm/__docs__/spm-scripts.md b/packages/react-native/scripts/spm/__docs__/spm-scripts.md index 8a38007b81c..47059cb79da 100644 --- a/packages/react-native/scripts/spm/__docs__/spm-scripts.md +++ b/packages/react-native/scripts/spm/__docs__/spm-scripts.md @@ -216,6 +216,23 @@ load from Metro instead of a bundled `main.jsbundle`. CocoaPods injects it at `pod install` time, so this keeps SwiftPM apps at parity. An existing value is left alone. +### iOS deployment target + +Every manifest React Native generates — the `Autolinked` aggregate, the synth +package per dependency, and each scaffolded community package — declares the +same platform floor: your app's `IPHONEOS_DEPLOYMENT_TARGET`, never below React +Native's own minimum (15.1). SwiftPM refuses to link a product whose minimum is +higher than the target depending on it, so a dependency that needs more (Expo's +packages need iOS 16.4) only resolves once the app asks for at least as much: +raise the deployment target in Xcode and re-run `react-native spm update`. + +A floor set in an `.xcconfig` your configuration is based on is honored, +`#include` chains included; one set through a build-setting variable +(`$(MY_FLOOR)`) is not, and falls back to React Native's minimum. `spm add` and +`spm update` also refresh the platform-floor line of existing scaffolded +manifests (they never create new ones) — if you persisted a scaffold with +`patch-package`, re-run `npx patch-package ` afterwards. + ## Files the tool touches Paths are relative to the Xcode project directory (`ios/`) unless noted. diff --git a/packages/react-native/scripts/spm/__tests__/generate-spm-autolinking-test.js b/packages/react-native/scripts/spm/__tests__/generate-spm-autolinking-test.js index baa0a8fa20b..27fb060fee0 100644 --- a/packages/react-native/scripts/spm/__tests__/generate-spm-autolinking-test.js +++ b/packages/react-native/scripts/spm/__tests__/generate-spm-autolinking-test.js @@ -466,6 +466,99 @@ describe('generateSynthPackageSwift', () => { }); }); +// --------------------------------------------------------------------------- +// Platform floor — a manifest must not sit below the app's own iOS deployment +// target: SwiftPM refuses a product whose minimum is higher than the depending +// target's, so a self-managed dep at iOS 16.4 (Expo) would fail to link. +// --------------------------------------------------------------------------- + +describe('iOS platform floor', () => { + it('defaults the aggregator to the React Native minimum, and raises it on request', () => { + expect(generateAutolinkedPackageSwift({})).toContain( + 'platforms: [.iOS("15.1")]', + ); + expect( + generateAutolinkedPackageSwift({iosDeploymentTarget: '16.4'}), + ).toContain('platforms: [.iOS("16.4")]'); + }); + + it('defaults each synth package to the React Native minimum, and raises it on request', () => { + const spec = {swiftName: 'MyDep'}; + expect(generateSynthPackageSwift(spec)).toContain( + 'platforms: [.iOS("15.1")]', + ); + expect( + generateSynthPackageSwift({...spec, iosDeploymentTarget: '16.4'}), + ).toContain('platforms: [.iOS("16.4")]'); + }); +}); + +describe('main() — --ios-deployment-target', () => { + let created = []; + let spies = []; + + beforeEach(() => { + for (const m of ['log', 'warn', 'error']) { + spies.push(jest.spyOn(console, m).mockImplementation(() => {})); + } + }); + afterEach(() => { + for (const s of spies) s.mockRestore(); + spies = []; + for (const d of created) fs.rmSync(d, {recursive: true, force: true}); + created = []; + }); + + // An app-local spm.module is the only route that produces BOTH the + // aggregator and a synth manifest without a community dep on disk. + function buildApp() { + const appRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-platform-')); + created.push(appRoot); + const rnRoot = path.join(appRoot, 'rn'); + fs.mkdirSync(rnRoot, {recursive: true}); + fs.writeFileSync( + path.join(appRoot, 'package.json'), + JSON.stringify({name: 'app'}), + ); + const modDir = path.join(appRoot, 'ios', 'MyNativeModule'); + fs.mkdirSync(modDir, {recursive: true}); + fs.writeFileSync(path.join(modDir, 'Module.mm'), '// native source\n'); + fs.writeFileSync( + path.join(appRoot, 'react-native.config.js'), + `module.exports = ${JSON.stringify({ + spm: {modules: [{name: 'MyNativeModule', path: 'ios/MyNativeModule'}]}, + })};\n`, + ); + const autolinkDir = path.join(appRoot, 'build', 'generated', 'autolinking'); + fs.mkdirSync(autolinkDir, {recursive: true}); + fs.writeFileSync( + path.join(autolinkDir, 'autolinking.json'), + JSON.stringify({dependencies: {}}), + ); + return {appRoot, rnRoot, autolinkDir}; + } + + it('sanitizes the flag into both the aggregator and the synth manifest', () => { + const {appRoot, rnRoot, autolinkDir} = buildApp(); + main([ + '--app-root', + appRoot, + '--react-native-root', + rnRoot, + '--ios-deployment-target', + '16', + ]); + for (const manifest of [ + path.join(autolinkDir, 'Package.swift'), + path.join(autolinkDir, 'packages', 'MyNativeModule', 'Package.swift'), + ]) { + expect(fs.readFileSync(manifest, 'utf8')).toContain( + 'platforms: [.iOS("16.0")]', + ); + } + }); +}); + // --------------------------------------------------------------------------- // linkHeaderTree // diff --git a/packages/react-native/scripts/spm/__tests__/ios-deployment-target-test.js b/packages/react-native/scripts/spm/__tests__/ios-deployment-target-test.js new file mode 100644 index 00000000000..dab233c0510 --- /dev/null +++ b/packages/react-native/scripts/spm/__tests__/ios-deployment-target-test.js @@ -0,0 +1,268 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @noflow + */ + +'use strict'; + +const { + MIN_IOS_VERSION_SUPPORTED, + readIosDeploymentTargetFromPbxproj, + resolveIosDeploymentTarget, + sanitizeIosDeploymentTarget, +} = require('../ios-deployment-target'); +const { + PLAIN_APP, + SECOND_TARGET, + TARGET_DEBUG, + TARGET_RELEASE, + raisedTarget, + twoAppTargets, + withoutProjectSetting, + withProjectSetting, + withSetting, + withXcconfigRef, +} = require('./pbxproj-variants'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); + +const RAISED = raisedTarget('16.4'); +const TWO = twoAppTargets('16.4'); +const MIXED_CONFIGS = withSetting( + withSetting(PLAIN_APP, TARGET_DEBUG, '17.0'), + TARGET_RELEASE, + '16.4', +); +const VARIABLE_VALUE = withProjectSetting(PLAIN_APP, '"$(SOME_VAR)"'); +const NOTHING_DECLARED = withoutProjectSetting(PLAIN_APP); +const GONE_UUID = 'CC0000000000000000000000'; + +describe('sanitizeIosDeploymentTarget', () => { + it.each([ + ['16', '16.0'], + ['16.4', '16.4'], + ['14.0', MIN_IOS_VERSION_SUPPORTED], + ['$(FOO)', MIN_IOS_VERSION_SUPPORTED], + [null, MIN_IOS_VERSION_SUPPORTED], + ])('normalizes and clamps %p to %p', (raw, expected) => { + expect(sanitizeIosDeploymentTarget(raw)).toBe(expected); + }); +}); + +describe('readIosDeploymentTargetFromPbxproj', () => { + it.each([ + ['project-level fallback', PLAIN_APP, {}, '15.1'], + ['target-level wins', RAISED, {}, '16.4'], + ['minimum across configurations', MIXED_CONFIGS, {}, '16.4'], + ['a variable reference is ignored', VARIABLE_VALUE, {}, null], + ['nothing declared', NOTHING_DECLARED, {}, null], + ['minimum over every app target', TWO, {}, '15.1'], + ['named target', TWO, {targetName: 'MyApp'}, '16.4'], + ['unmatched targetName', TWO, {targetName: 'Nope'}, '15.1'], + [ + 'uuid beats name', + TWO, + {targetUuid: SECOND_TARGET, targetName: 'MyApp'}, + '15.1', + ], + ['uuid that is gone', TWO, {targetUuid: GONE_UUID}, '15.1'], + ])('%s', (_name, text, opts, expected) => { + expect(readIosDeploymentTargetFromPbxproj(text, opts)).toBe(expected); + }); +}); + +describe('readIosDeploymentTargetFromPbxproj — xcconfig chain', () => { + const SRC_ROOT = '/app'; + const APP_XCCONFIG = `${SRC_ROOT}/Config/App.xcconfig`; + const BASE_XCCONFIG = `${SRC_ROOT}/Config/Base.xcconfig`; + // Both target configurations point at the xcconfig, and the project level + // declares nothing — so the xcconfig alone decides the floor. + const XCCONFIG_ONLY = withXcconfigRef(withoutProjectSetting(PLAIN_APP), [ + TARGET_DEBUG, + TARGET_RELEASE, + ]); + + function read(text, files, opts) { + const readFile = jest.fn(absPath => files[absPath] ?? null); + const value = readIosDeploymentTargetFromPbxproj(text, { + srcRoot: SRC_ROOT, + readFile, + ...opts, + }); + return {value, readFile}; + } + + it('falls back to the xcconfig the configuration references', () => { + expect( + read(XCCONFIG_ONLY, { + [APP_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n', + }).value, + ).toBe('16.4'); + }); + + it('prefers a literal in the configuration over its xcconfig', () => { + const text = withSetting( + withSetting(XCCONFIG_ONLY, TARGET_DEBUG, '17.0'), + TARGET_RELEASE, + '17.0', + ); + expect( + read(text, {[APP_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n'}).value, + ).toBe('17.0'); + }); + + it('follows an #include chain', () => { + expect( + read(XCCONFIG_ONLY, { + [APP_XCCONFIG]: '#include "Base.xcconfig"\n', + [BASE_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n', + }).value, + ).toBe('16.4'); + }); + + it('lets an assignment after the #include win', () => { + expect( + read(XCCONFIG_ONLY, { + [APP_XCCONFIG]: + '#include? "Base.xcconfig"\nIPHONEOS_DEPLOYMENT_TARGET = 17.0;\n', + [BASE_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n', + }).value, + ).toBe('17.0'); + }); + + it('ignores comments and conditional assignments', () => { + expect( + read(XCCONFIG_ONLY, { + [APP_XCCONFIG]: + '// IPHONEOS_DEPLOYMENT_TARGET = 18.0\n' + + 'IPHONEOS_DEPLOYMENT_TARGET[sdk=iphonesimulator*] = 16.4\n', + }).value, + ).toBeNull(); + }); + + it('ignores a value that is not a plain version', () => { + expect( + read(XCCONFIG_ONLY, { + [APP_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = $(inherited)\n', + }).value, + ).toBeNull(); + }); + + it('resolves a "" reference through the group path', () => { + const text = withXcconfigRef( + withoutProjectSetting(PLAIN_APP), + [TARGET_DEBUG, TARGET_RELEASE], + {filePath: 'App.xcconfig', groupPath: 'Config'}, + ); + const {value, readFile} = read(text, { + [APP_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n', + }); + expect(value).toBe('16.4'); + expect(readFile).toHaveBeenCalledWith(APP_XCCONFIG); + }); + + it('reports nothing when the xcconfig is missing', () => { + expect(read(XCCONFIG_ONLY, {}).value).toBeNull(); + }); + + it('terminates on an #include cycle', () => { + expect( + read(XCCONFIG_ONLY, { + [APP_XCCONFIG]: + 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n#include "Base.xcconfig"\n', + [BASE_XCCONFIG]: '#include "App.xcconfig"\n', + }).value, + ).toBe('16.4'); + }); + + it('skips the xcconfig step without a srcRoot', () => { + const {value, readFile} = read( + XCCONFIG_ONLY, + {[APP_XCCONFIG]: 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n'}, + {srcRoot: null}, + ); + expect(value).toBeNull(); + expect(readFile).not.toHaveBeenCalled(); + }); +}); + +describe('resolveIosDeploymentTarget', () => { + const dirs = []; + + afterEach(() => { + for (const dir of dirs) { + fs.rmSync(dir, {recursive: true, force: true}); + } + dirs.length = 0; + }); + + // A throwaway .xcodeproj, so the fs path runs for real. + function write(text) { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-iosdt-')); + dirs.push(dir); + const xcodeprojPath = path.join(dir, 'MyApp.xcodeproj'); + fs.mkdirSync(xcodeprojPath); + fs.writeFileSync(path.join(xcodeprojPath, 'project.pbxproj'), text, 'utf8'); + return xcodeprojPath; + } + + it('reads the app value, clamped to the React Native minimum', () => { + expect(resolveIosDeploymentTarget({xcodeprojPath: write(RAISED)})).toBe( + '16.4', + ); + expect( + resolveIosDeploymentTarget({ + xcodeprojPath: write(withProjectSetting(PLAIN_APP, '14.0')), + }), + ).toBe(MIN_IOS_VERSION_SUPPORTED); + }); + + it('returns null when there is nothing usable to read', () => { + expect( + resolveIosDeploymentTarget({xcodeprojPath: write(NOTHING_DECLARED)}), + ).toBeNull(); + expect(resolveIosDeploymentTarget({xcodeprojPath: null})).toBeNull(); + expect( + resolveIosDeploymentTarget({xcodeprojPath: '/no/such/App.xcodeproj'}), + ).toBeNull(); + }); + + it('reads a floor that only an xcconfig on disk declares', () => { + const xcodeprojPath = write( + withXcconfigRef(withoutProjectSetting(PLAIN_APP), [ + TARGET_DEBUG, + TARGET_RELEASE, + ]), + ); + const configDir = path.join(path.dirname(xcodeprojPath), 'Config'); + fs.mkdirSync(configDir); + fs.writeFileSync( + path.join(configDir, 'App.xcconfig'), + 'IPHONEOS_DEPLOYMENT_TARGET = 16.4\n', + 'utf8', + ); + expect(resolveIosDeploymentTarget({xcodeprojPath})).toBe('16.4'); + }); + + it('matches min_ios_version_supported in helpers.rb', () => { + const helpers = fs.readFileSync( + path.join(__dirname, '..', '..', 'cocoapods', 'helpers.rb'), + 'utf8', + ); + const match = helpers.match( + /def self\.min_ios_version_supported\s+return\s+'([\d.]+)'/, + ); + if (match == null) { + throw new Error( + 'could not read min_ios_version_supported from scripts/cocoapods/helpers.rb', + ); + } + expect(MIN_IOS_VERSION_SUPPORTED).toBe(match[1]); + }); +}); diff --git a/packages/react-native/scripts/spm/__tests__/pbxproj-variants.js b/packages/react-native/scripts/spm/__tests__/pbxproj-variants.js new file mode 100644 index 00000000000..25b226285b5 --- /dev/null +++ b/packages/react-native/scripts/spm/__tests__/pbxproj-variants.js @@ -0,0 +1,146 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @noflow + */ + +'use strict'; + +// Variants of the plain-app pbxproj fixture, shared by the tests that exercise +// iOS-deployment-target reading. The base fixture has one app target whose own +// Debug/Release configs carry no IPHONEOS_DEPLOYMENT_TARGET, and two +// project-level configs that set 15.1. + +const fs = require('node:fs'); +const path = require('node:path'); + +const PLAIN_APP = fs.readFileSync( + path.join(__dirname, '__fixtures__', 'plain-app.pbxproj'), + 'utf8', +); + +const TARGET_DEBUG = 'AA0000000000000000000901'; +const TARGET_RELEASE = 'AA00000000000000000000A2'; +const SECOND_TARGET = 'BB0000000000000000000101'; + +/** Insert IPHONEOS_DEPLOYMENT_TARGET into one XCBuildConfiguration. */ +function withSetting(text, configUuid, value) { + const at = text.indexOf(configUuid + ' /*'); + const lineEnd = text.indexOf('\n', text.indexOf('buildSettings = {', at)) + 1; + return ( + text.slice(0, lineEnd) + + `\t\t\t\tIPHONEOS_DEPLOYMENT_TARGET = ${value};\n` + + text.slice(lineEnd) + ); +} + +function withProjectSetting(text, value) { + return text.replaceAll( + 'IPHONEOS_DEPLOYMENT_TARGET = 15.1;', + `IPHONEOS_DEPLOYMENT_TARGET = ${value};`, + ); +} + +function withoutProjectSetting(text) { + return text.replaceAll('\t\t\t\tIPHONEOS_DEPLOYMENT_TARGET = 15.1;\n', ''); +} + +/** The single app target declaring `version` in both its configurations. */ +function raisedTarget(version) { + return withSetting( + withSetting(PLAIN_APP, TARGET_DEBUG, version), + TARGET_RELEASE, + version, + ); +} + +/** + * Two app targets: MyApp declares `version`, while Second has no configurations + * of its own and inherits the project's 15.1 — so target selection and the + * minimum-over-targets rule are both observable. + */ +function twoAppTargets(version) { + return raisedTarget(version).replace( + '/* End PBXNativeTarget section */', + `\t\t${SECOND_TARGET} /* Second */ = { + isa = PBXNativeTarget; + buildConfigurationList = AA0000000000000000000601 /* project configs */; + name = Second; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */`, + ); +} + +const XCCONFIG_REF = 'DD0000000000000000000101'; +const XCCONFIG_GROUP = 'DD0000000000000000000201'; + +function insertIntoObject(text, uuid, line) { + const lineEnd = + text.indexOf('\n', text.indexOf('= {', text.indexOf(uuid + ' /*'))) + 1; + return text.slice(0, lineEnd) + `\t\t\t${line}\n` + text.slice(lineEnd); +} + +/** + * Point the given configurations at one `.xcconfig` file reference. + * `sourceTree` defaults to SOURCE_ROOT; pass `groupPath` to place the + * reference in a `""` PBXGroup carrying that path instead. + */ +function withXcconfigRef(text, configUuids, opts = {}) { + const filePath = opts.filePath ?? 'Config/App.xcconfig'; + const groupPath = opts.groupPath ?? null; + const sourceTree = + groupPath != null ? '""' : (opts.sourceTree ?? 'SOURCE_ROOT'); + const name = path.basename(filePath); + + let out = text; + for (const configUuid of configUuids) { + out = insertIntoObject( + out, + configUuid, + `baseConfigurationReference = ${XCCONFIG_REF} /* ${name} */;`, + ); + } + out = out.replace( + '/* End PBXFileReference section */', + `\t\t${XCCONFIG_REF} /* ${name} */ = { + isa = PBXFileReference; + lastKnownFileType = text.xcconfig; + path = ${filePath}; + sourceTree = ${sourceTree}; + }; +/* End PBXFileReference section */`, + ); + if (groupPath != null) { + out = out.replace( + '/* End PBXGroup section */', + `\t\t${XCCONFIG_GROUP} /* ${groupPath} */ = { + isa = PBXGroup; + children = ( + ${XCCONFIG_REF} /* ${name} */, + ); + path = ${groupPath}; + sourceTree = ""; + }; +/* End PBXGroup section */`, + ); + } + return out; +} + +module.exports = { + PLAIN_APP, + SECOND_TARGET, + TARGET_DEBUG, + TARGET_RELEASE, + raisedTarget, + twoAppTargets, + withProjectSetting, + withSetting, + withXcconfigRef, + withoutProjectSetting, +}; diff --git a/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js b/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js index 83b34597c7f..e05e07e011d 100644 --- a/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js +++ b/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js @@ -14,6 +14,7 @@ const { SCAFFOLDER_MARKER, SCAFFOLDER_VERSION, emitScaffoldedPackageSwift, + refreshScaffoldedPlatformFloors, scaffoldAll, scaffoldPackageSwiftForDep, translatePodspecToSpmTarget, @@ -562,6 +563,19 @@ describe('emitScaffoldedPackageSwift', () => { expect(out).toContain('// Cache slot: 0.87.0-nightly-20260513-abc/debug'); }); + it('floors the platform at the React Native minimum by default, in string form', () => { + const out = emitScaffoldedPackageSwift(baseSpec()); + expect(out).toContain('platforms: [.iOS("15.1")]'); + }); + + it('raises the platform floor to the app deployment target', () => { + const out = emitScaffoldedPackageSwift(baseSpec(), { + cacheSlotLabel: null, + iosDeploymentTarget: '16.4', + }); + expect(out).toContain('platforms: [.iOS("16.4")]'); + }); + it('emits DEBUG/NDEBUG config-gated cxxSettings so Fabric C++ matches the prebuilt React.framework ABI', () => { const out = emitScaffoldedPackageSwift(baseSpec()); expect(out).toContain('.define("DEBUG", .when(configuration: .debug))'); @@ -1527,6 +1541,10 @@ describe('SCAFFOLDER_VERSION', () => { expect(SCAFFOLDER_VERSION).toBeGreaterThanOrEqual(1); }); + it('is at the version the current emitter output requires', () => { + expect(SCAFFOLDER_VERSION).toBe(20); + }); + it('emitter writes the current version to the file', () => { const out = emitScaffoldedPackageSwift({ swiftName: 'foo', @@ -1629,3 +1647,191 @@ describe('scaffoldPackageSwiftForDep — version-based regen', () => { expect(result.status).toBe('skipped-scaffolder-marker'); }); }); + +// --------------------------------------------------------------------------- +// refreshScaffoldedPlatformFloors — `spm add`/`update` bring the floor of +// manifests scaffolded earlier (possibly by an older generator) up to the +// app's current deployment target, without regenerating them. +// --------------------------------------------------------------------------- + +describe('refreshScaffoldedPlatformFloors', () => { + let appRoot; + + beforeEach(() => { + appRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-refresh-floor-')); + }); + + afterEach(() => { + fs.rmSync(appRoot, {recursive: true, force: true}); + }); + + function manifest(floorLine) { + return ( + `// swift-tools-version: 6.0\n${SCAFFOLDER_MARKER}\n` + + '// AUTO-SCAFFOLDED-VERSION: 19\n\nlet package = Package(\n' + + ' name: "foo",\n' + + ` ${floorLine}\n` + + ' products: [],\n)\n' + ); + } + + // One dep per entry: `content` is written to /Package.swift unless null. + function writeApp(deps) { + const autolinkingDir = path.join(appRoot, 'build/generated/autolinking'); + fs.mkdirSync(autolinkingDir, {recursive: true}); + const dependencies = {}; + for (const [name, content] of Object.entries(deps)) { + const root = path.join(appRoot, 'node_modules', name); + fs.mkdirSync(root, {recursive: true}); + if (content != null) { + fs.writeFileSync(path.join(root, 'Package.swift'), content, 'utf8'); + } + dependencies[name] = {root, platforms: {ios: {}}}; + } + fs.writeFileSync( + path.join(autolinkingDir, 'autolinking.json'), + JSON.stringify({dependencies}), + ); + } + + function refresh() { + return refreshScaffoldedPlatformFloors({ + appRoot, + iosDeploymentTarget: '16.4', + }); + } + + function read(depName) { + return fs.readFileSync( + path.join(appRoot, 'node_modules', depName, 'Package.swift'), + 'utf8', + ); + } + + it('rewrites only the platform line of a scaffolded manifest', () => { + const before = manifest('platforms: [.iOS("15.1")],'); + writeApp({'react-native-foo': before}); + + expect(refresh()).toEqual([ + { + depName: 'react-native-foo', + path: path.join(appRoot, 'node_modules/react-native-foo/Package.swift'), + from: '15.1', + to: '16.4', + }, + ]); + expect(read('react-native-foo')).toBe( + before.replace('.iOS("15.1")', '.iOS("16.4")'), + ); + }); + + it('rewrites the enum form an older scaffolder emitted', () => { + writeApp({'react-native-foo': manifest('platforms: [.iOS(.v15)],')}); + + expect(refresh()).toEqual([ + expect.objectContaining({from: '.v15', to: '16.4'}), + ]); + expect(read('react-native-foo')).toContain('platforms: [.iOS("16.4")]'); + }); + + it('rewrites only the .iOS element of an extended platforms array', () => { + writeApp({ + 'react-native-foo': manifest( + 'platforms: [.iOS("15.1"), .macOS(.v13), .tvOS("16.0")],', + ), + }); + + expect(refresh()).toEqual([expect.objectContaining({from: '15.1'})]); + expect(read('react-native-foo')).toContain( + 'platforms: [.iOS("16.4"), .macOS(.v13), .tvOS("16.0")],', + ); + }); + + it('leaves a manifest already on the floor untouched', () => { + writeApp({'react-native-foo': manifest('platforms: [.iOS("16.4")],')}); + const manifestPath = path.join( + appRoot, + 'node_modules/react-native-foo/Package.swift', + ); + const before = fs.statSync(manifestPath).mtimeMs; + + expect(refresh()).toEqual([]); + expect(fs.statSync(manifestPath).mtimeMs).toBe(before); + }); + + it('ignores manifests it does not own, and deps with none', () => { + const upstream = '// hand-authored\nplatforms: [.iOS("15.1")],\n'; + const autogen = `// AUTO-GENERATED by scripts/generate-spm-autolinking.js\n${SCAFFOLDER_MARKER}\nplatforms: [.iOS("15.1")],\n`; + writeApp({ + 'react-native-upstream': upstream, + 'react-native-autogen': autogen, + 'react-native-none': null, + }); + + expect(refresh()).toEqual([]); + expect(read('react-native-upstream')).toBe(upstream); + expect(read('react-native-autogen')).toBe(autogen); + }); + + it('refreshes a transitive spm.dependency that autolinking.json never lists', () => { + writeApp({'react-native-a': manifest('platforms: [.iOS("15.1")],')}); + fs.writeFileSync( + path.join(appRoot, 'node_modules/react-native-a/package.json'), + JSON.stringify({ + name: 'react-native-a', + swiftpmConfig: {dependencies: ['react-native-transitive']}, + }), + ); + const transitiveRoot = path.join( + appRoot, + 'node_modules', + 'react-native-transitive', + ); + fs.mkdirSync(transitiveRoot, {recursive: true}); + fs.writeFileSync( + path.join(transitiveRoot, 'package.json'), + JSON.stringify({name: 'react-native-transitive', version: '1.0.0'}), + ); + fs.writeFileSync( + path.join(transitiveRoot, 'react-native.config.js'), + 'module.exports = {dependency: {platforms: {ios: {}}}};\n', + ); + fs.writeFileSync( + path.join(transitiveRoot, 'Package.swift'), + manifest('platforms: [.iOS("15.1")],'), + 'utf8', + ); + + expect( + refresh() + .map(entry => entry.depName) + .sort(), + ).toEqual(['react-native-a', 'react-native-transitive']); + expect(read('react-native-transitive')).toContain( + 'platforms: [.iOS("16.4")]', + ); + }); + + it('returns nothing when there is no autolinking.json', () => { + expect(refresh()).toEqual([]); + }); + + it('honors an autolinking.json written outside the default location', () => { + writeApp({'react-native-foo': manifest('platforms: [.iOS("15.1")],')}); + const moved = path.join(appRoot, 'elsewhere', 'autolinking.json'); + fs.mkdirSync(path.dirname(moved)); + fs.renameSync( + path.join(appRoot, 'build/generated/autolinking/autolinking.json'), + moved, + ); + + expect(refresh()).toEqual([]); + expect( + refreshScaffoldedPlatformFloors({ + appRoot, + autolinkingJsonPath: moved, + iosDeploymentTarget: '16.4', + }), + ).toEqual([expect.objectContaining({depName: 'react-native-foo'})]); + }); +}); diff --git a/packages/react-native/scripts/spm/__tests__/setup-apple-spm-test.js b/packages/react-native/scripts/spm/__tests__/setup-apple-spm-test.js index 362538af284..6e6278e4c1a 100644 --- a/packages/react-native/scripts/spm/__tests__/setup-apple-spm-test.js +++ b/packages/react-native/scripts/spm/__tests__/setup-apple-spm-test.js @@ -18,12 +18,14 @@ const { generateAutolinkingConfigOrFailClosed, parseArgs, resolveAction, + resolveAppIosDeploymentTarget, resolveConfigCommandToPin, resolveExplicitConfigCommand, shouldAutoDeintegrate, } = require('../../setup-apple-spm'); const {REQUIRED_ARTIFACTS} = require('../download-spm-artifacts'); const {SPM_INJECTED_MARKER} = require('../generate-spm-xcodeproj'); +const {twoAppTargets} = require('./pbxproj-variants'); const {execFileSync} = require('node:child_process'); const fs = require('node:fs'); const os = require('node:os'); @@ -650,3 +652,51 @@ describe('determineVersion', () => { ); }); }); + +describe('resolveAppIosDeploymentTarget', () => { + let appRoot; + let logSpy; + + beforeEach(() => { + appRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-setup-iosdt-')); + logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + }); + + afterEach(() => { + logSpy.mockRestore(); + fs.rmSync(appRoot, {recursive: true, force: true}); + }); + + function logged() { + return logSpy.mock.calls.map(call => call.join(' ')).join('\n'); + } + + // No marker — the first `spm add`, where only --product-name identifies the + // target among the fixture's two app targets. + function mkTwoTargetProject() { + const dir = path.join(appRoot, 'MyApp.xcodeproj'); + fs.mkdirSync(dir, {recursive: true}); + fs.writeFileSync( + path.join(dir, 'project.pbxproj'), + twoAppTargets('16.4'), + 'utf8', + ); + } + + it('reads the app target --product-name selects, not the lowest one', () => { + mkTwoTargetProject(); + expect(resolveAppIosDeploymentTarget({productName: 'MyApp'}, appRoot)).toBe( + '16.4', + ); + expect(logged()).toContain( + 'iOS deployment target: 16.4 (from MyApp.xcodeproj)', + ); + }); + + it('names the reason alongside the default when no project could be picked', () => { + expect(resolveAppIosDeploymentTarget({}, appRoot)).toBe('15.1'); + expect(logged()).toContain( + 'iOS deployment target: 15.1 (react-native default; no .xcodeproj found', + ); + }); +}); diff --git a/packages/react-native/scripts/spm/__tests__/sync-spm-autolinking-test.js b/packages/react-native/scripts/spm/__tests__/sync-spm-autolinking-test.js index c917f1acf02..b2e81d23e5d 100644 --- a/packages/react-native/scripts/spm/__tests__/sync-spm-autolinking-test.js +++ b/packages/react-native/scripts/spm/__tests__/sync-spm-autolinking-test.js @@ -47,8 +47,12 @@ describe('sync-spm-autolinking main', () => { }; } + function baseArgv() { + return ['--app-root', appRoot, '--react-native-root', rnRoot]; + } + function run(deps) { - return main(['--app-root', appRoot, '--react-native-root', rnRoot], deps); + return main(baseArgv(), deps); } function stampPath() { @@ -92,6 +96,20 @@ describe('sync-spm-autolinking main', () => { expect(fs.existsSync(stampPath())).toBe(true); }); + it('forwards --ios-deployment-target to the autolinker only when given', async () => { + const withFlag = makeDeps(); + await main([...baseArgv(), '--ios-deployment-target', '16.4'], withFlag); + expect(withFlag.generateAutolinking).toHaveBeenCalledWith([ + ...baseArgv(), + '--ios-deployment-target', + '16.4', + ]); + + const withoutFlag = makeDeps(); + await run(withoutFlag); + expect(withoutFlag.generateAutolinking).toHaveBeenCalledWith(baseArgv()); + }); + it('continues with existing output when codegen fails', async () => { const deps = makeDeps({ runCodegenAndInstallTemplate: jest.fn(() => { diff --git a/packages/react-native/scripts/spm/generate-spm-autolinking.js b/packages/react-native/scripts/spm/generate-spm-autolinking.js index ecb91f8d64a..3b41a6b56ad 100644 --- a/packages/react-native/scripts/spm/generate-spm-autolinking.js +++ b/packages/react-native/scripts/spm/generate-spm-autolinking.js @@ -67,6 +67,10 @@ const { defaultResolveDep, expandSpmDependencies, } = require('./expand-spm-dependencies'); +const { + MIN_IOS_VERSION_SUPPORTED, + sanitizeIosDeploymentTarget, +} = require('./ios-deployment-target'); const {findPodspecs, readPodspecCached} = require('./read-podspec'); const { AUTOLINKED_PACKAGE_NAME, @@ -210,6 +214,10 @@ function parseArgs(argv /*: Array */) /*: AutolinkingArgs */ { describe: 'Path to the xcframeworks sub-package (absolute or relative to appRoot)', }) + .option('ios-deployment-target', { + type: 'string', + describe: `Platform floor of the generated manifests (default: ${MIN_IOS_VERSION_SUPPORTED})`, + }) .usage( 'Usage: $0 [options]\n\nGenerates autolinked/Package.swift for SPM autolinking.', ) @@ -222,6 +230,9 @@ function parseArgs(argv /*: Array */) /*: AutolinkingArgs */ { autolinkingJson: parsed['autolinking-json'] ?? null, output: parsed.output ?? null, xcframeworksPath: parsed['xcframeworks-path'] ?? null, + iosDeploymentTarget: sanitizeIosDeploymentTarget( + parsed['ios-deployment-target'], + ), }; } @@ -941,6 +952,8 @@ function generateAutolinkedPackageSwift( input.pluginPackageDeps ?? []; const pluginProductDeps /*: ReadonlyArray */ = input.pluginProductDeps ?? []; + const iosDeploymentTarget /*: string */ = + input.iosDeploymentTarget ?? MIN_IOS_VERSION_SUPPORTED; // Package-level dependencies: one .package(path:) per autolinked dep, // plus ReactNative if any inline target needs to import React headers. @@ -1074,7 +1087,7 @@ import Foundation ${guardBlock}let package = Package( name: "${AUTOLINKED_PACKAGE_NAME}", - platforms: [.iOS(.v15)], + platforms: [.iOS("${iosDeploymentTarget}")], products: [ .library(name: "${AUTOLINKED_PACKAGE_NAME}", targets: ["AutolinkedAggregate"]), ], @@ -1124,6 +1137,8 @@ function generateSynthPackageSwift(spec /*: SynthPackageSpec */) /*: string */ { const targetPath /*: string */ = spec.targetPath ?? `Sources/${swiftName}`; const siblingSynthAbsolutePaths /*: {[string]: string} */ = spec.siblingSynthAbsolutePaths ?? {}; + const iosDeploymentTarget /*: string */ = + spec.iosDeploymentTarget ?? MIN_IOS_VERSION_SUPPORTED; // Package dependencies — ReactNative + each spm sibling synth package. // The React + codegen package paths are plain relative strings computed by @@ -1222,7 +1237,7 @@ import PackageDescription let package = Package( name: "${swiftName}", - platforms: [.iOS(.v15)], + platforms: [.iOS("${iosDeploymentTarget}")], products: [ .library(name: "${swiftName}"${isDynamic ? ', type: .dynamic' : ''}, targets: ["${swiftName}"]), ], @@ -1697,6 +1712,7 @@ function main(argv /*:: ?: Array */) /*: void */ { const synthContent = generateSynthPackageSwift({ swiftName: target.name, + iosDeploymentTarget: args.iosDeploymentTarget, exclude: prefixedExclude, sources: prefixedSources, // Stub include/ subdir lives in the wrapper dir; satisfies SPM's @@ -1871,6 +1887,7 @@ function main(argv /*:: ?: Array */) /*: void */ { // autolinked dep is a real SPM package in its own source dir. const aggregatorContent = generateAutolinkedPackageSwift({ npmDeps: aggregatorPackageDeps, + iosDeploymentTarget: args.iosDeploymentTarget, hasReactDep, xcframeworksRelPath, pluginPackageDeps, diff --git a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js index e0a43692fc8..acb3c5192b0 100644 --- a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js +++ b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js @@ -2224,7 +2224,7 @@ function readScriptPhasesManifest( */ function readMarker( xcodeprojPath /*: string */, -) /*: ?{generatedSources?: {[string]: Array}, scriptPhases?: {[string]: string}, artifactsVersionOverride?: ?string, configCommand?: ?Array, buildSettingChanges?: Array, createdArrayFields?: Array, scheme?: {file?: ?string, created?: ?boolean}, ...} */ { +) /*: ?{targetUuid?: ?string, generatedSources?: {[string]: Array}, scriptPhases?: {[string]: string}, artifactsVersionOverride?: ?string, configCommand?: ?Array, buildSettingChanges?: Array, createdArrayFields?: Array, scheme?: {file?: ?string, created?: ?boolean}, ...} */ { const markerPath = path.join(xcodeprojPath, SPM_INJECTED_MARKER); try { // $FlowFixMe[incompatible-return] JSON.parse returns any @@ -2761,6 +2761,7 @@ module.exports = { buildSchemePreActionScript, buildEmbedFrameworksScript, flavorForBuildConfiguration, + targetBuildConfigUuids, frameworkConditionalSettings, ensureStubPackages, buildSpmDependencyGraph, @@ -2775,6 +2776,7 @@ module.exports = { addPreActionToScheme, removePreActionFromScheme, findInjectedXcodeproj, + readMarker, readArtifactsVersionOverride, readPinnedConfigCommand, readScriptPhasesManifest, diff --git a/packages/react-native/scripts/spm/ios-deployment-target.js b/packages/react-native/scripts/spm/ios-deployment-target.js new file mode 100644 index 00000000000..b6239b01925 --- /dev/null +++ b/packages/react-native/scripts/spm/ios-deployment-target.js @@ -0,0 +1,378 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +'use strict'; + +/** + * The iOS platform floor of the SwiftPM manifests React Native generates. + * SwiftPM refuses to link a product whose minimum is above the depending + * target's, so the floor tracks the app's own deployment target rather than a + * hardcoded value a dependency (every Expo package: iOS 16.4) can exceed. + */ + +const {targetBuildConfigUuids} = require('./generate-spm-xcodeproj'); +const { + findApplicationTargets, + findField, + findObjectByUuid, + findProjectObject, + forEachObjectInSection, + uuidsInArray, +} = require('./spm-pbxproj'); +const fs = require('node:fs'); +const path = require('node:path'); + +// Keep in sync with `min_ios_version_supported` in scripts/cocoapods/helpers.rb. +const MIN_IOS_VERSION_SUPPORTED /*: string */ = '15.1'; + +const IOS_VERSION_RE = /^\d+(\.\d+){0,2}$/; + +const DEPLOYMENT_TARGET_KEY = 'IPHONEOS_DEPLOYMENT_TARGET'; + +// An xcconfig may `#include` another; cap the chain instead of trusting it. +const MAX_XCCONFIG_DEPTH = 16; + +/*:: +// Everything the reader needs to follow a configuration's xcconfig. Without a +// srcRoot the xcconfig step is skipped (a caller with only pbxproj text). +type XcconfigContext = {srcRoot: ?string, readFile: (absPath: string) => ?string}; +*/ + +function normalizeIosVersion(version /*: string */) /*: string */ { + return version.includes('.') ? version : `${version}.0`; +} + +function segment( + parts /*: Array */, + index /*: number */, +) /*: number */ { + return index < parts.length ? Number(parts[index]) : 0; +} + +/** Numeric, not lexicographic: `9.0` < `10.0` and `16.4` < `16.10`. */ +function compareIosVersions(a /*: string */, b /*: string */) /*: number */ { + const left = a.split('.'); + const right = b.split('.'); + for (let i = 0; i < Math.max(left.length, right.length); i++) { + const delta = segment(left, i) - segment(right, i); + if (delta !== 0) { + return delta < 0 ? -1 : 1; + } + } + return 0; +} + +function unquote(value /*: string */) /*: string */ { + return value.replace(/^"|"$/g, ''); +} + +/** + * A version reduced to something safe to interpolate into a manifest: + * normalized, and never below React Native's own minimum. + */ +function sanitizeIosDeploymentTarget(raw /*: ?string */) /*: string */ { + if (raw == null || !IOS_VERSION_RE.test(raw)) { + return MIN_IOS_VERSION_SUPPORTED; + } + const version = normalizeIosVersion(raw); + return compareIosVersions(version, MIN_IOS_VERSION_SUPPORTED) > 0 + ? version + : MIN_IOS_VERSION_SUPPORTED; +} + +function defaultReadFile(absPath /*: string */) /*: ?string */ { + try { + return fs.readFileSync(absPath, 'utf8'); + } catch { + return null; + } +} + +/** + * `IPHONEOS_DEPLOYMENT_TARGET` as an xcconfig defines it, following `#include` + * lines in place so a later assignment in the includer wins. Null when the file + * is unreadable, sets no unconditional value, or sets one that is not a plain + * version (`$(inherited)`). + */ +function parseXcconfigSetting( + absPath /*: string */, + content /*: string */, + readFile /*: (absPath: string) => ?string */, + visited /*: Set */ = new Set(), + depth /*: number */ = 0, +) /*: ?string */ { + if (visited.has(absPath)) { + return null; + } + visited.add(absPath); + const assignment = new RegExp(`^${DEPLOYMENT_TARGET_KEY}\\s*=\\s*([^;]*);?$`); + let value /*: ?string */ = null; + for (const rawLine of content.split('\n')) { + const line = rawLine.replace(/\/\/.*$/, '').trim(); + const include = line.match(/^#include\??\s+"([^"]+)"/); + if (include != null) { + const included = readXcconfigSetting( + path.resolve(path.dirname(absPath), include[1]), + readFile, + visited, + depth + 1, + ); + if (included != null) { + value = included; + } + continue; + } + const match = line.match(assignment); + if (match != null) { + const raw = unquote(match[1].trim()); + value = IOS_VERSION_RE.test(raw) ? raw : null; + } + } + return value; +} + +/** parseXcconfigSetting for a path not read yet — the `#include` entry point. */ +function readXcconfigSetting( + absPath /*: string */, + readFile /*: (absPath: string) => ?string */, + visited /*: Set */, + depth /*: number */, +) /*: ?string */ { + if (depth > MAX_XCCONFIG_DEPTH) { + return null; + } + const content = readFile(absPath); + return content != null + ? parseXcconfigSetting(absPath, content, readFile, visited, depth) + : null; +} + +/** + * Directory components of the PBXGroup chain holding `uuid`, outermost first — + * how a `""` file reference's path is anchored to the project dir. + */ +function groupPathPrefix( + text /*: string */, + uuid /*: string */, +) /*: Array */ { + const groups /*: Array<{uuid: string, path: ?string, children: Set}> */ = + []; + forEachObjectInSection(text, 'PBXGroup', ({uuid: groupUuid, ...body}) => { + const children = findField(text, body, 'children'); + const groupPath = findField(text, body, 'path'); + groups.push({ + uuid: groupUuid, + path: groupPath != null ? unquote(groupPath.value) : null, + children: children != null ? uuidsInArray(children.value) : new Set(), + }); + }); + + const parts /*: Array */ = []; + let current = uuid; + for (let i = 0; i < groups.length; i++) { + const parent = groups.find(group => group.children.has(current)); + if (parent == null) { + break; + } + if (parent.path != null) { + parts.unshift(parent.path); + } + current = parent.uuid; + } + return parts; +} + +/** + * Absolute paths to try for the xcconfig a configuration is based on, in order: + * the reference's own anchoring first, then plain `/`. + */ +function xcconfigCandidates( + text /*: string */, + configObj /*: {bodyOpen: number, bodyClose: number, ...} */, + srcRoot /*: string */, +) /*: Array */ { + const base = findField(text, configObj, 'baseConfigurationReference'); + const refUuid = base?.value.match(/[0-9A-Fa-f]{24}/)?.[0]; + if (refUuid == null) { + return []; + } + const ref = findObjectByUuid(text, refUuid); + if (ref == null) { + return []; + } + const pathField = findField(text, ref, 'path'); + if (pathField == null) { + return []; + } + const refPath = unquote(pathField.value); + if (path.isAbsolute(refPath)) { + return [refPath]; + } + const sourceTreeField = findField(text, ref, 'sourceTree'); + const sourceTree = + sourceTreeField != null ? unquote(sourceTreeField.value) : ''; + const fallback = path.join(srcRoot, refPath); + if (sourceTree !== '') { + return [fallback]; + } + const anchored = path.join( + srcRoot, + ...groupPathPrefix(text, refUuid), + refPath, + ); + return anchored === fallback ? [fallback] : [anchored, fallback]; +} + +function configName( + text /*: string */, + configObj /*: {bodyOpen: number, bodyClose: number, ...} */, +) /*: ?string */ { + const field = findField(text, configObj, 'name'); + return field != null ? unquote(field.value) : null; +} + +/** A plain-version `IPHONEOS_DEPLOYMENT_TARGET` literal in a configuration. */ +function configLiteral( + text /*: string */, + configObj /*: {bodyOpen: number, bodyClose: number, ...} */, +) /*: ?string */ { + const settings = findField(text, configObj, 'buildSettings'); + if (settings == null) { + return null; + } + const field = findField( + text, + {bodyOpen: settings.valueStart, bodyClose: settings.tokenEnd - 1}, + DEPLOYMENT_TARGET_KEY, + ); + if (field == null) { + return null; + } + const raw = unquote(field.value); + return IOS_VERSION_RE.test(raw) ? raw : null; +} + +/** + * What one configuration declares: its own literal, else the xcconfig it is + * based on (Xcode's own precedence). + */ +function configDeploymentTarget( + text /*: string */, + configObj /*: {bodyOpen: number, bodyClose: number, ...} */, + ctx /*: XcconfigContext */, +) /*: ?string */ { + const literal = configLiteral(text, configObj); + if (literal != null || ctx.srcRoot == null) { + return literal; + } + for (const candidate of xcconfigCandidates(text, configObj, ctx.srcRoot)) { + const content = ctx.readFile(candidate); + if (content != null) { + return parseXcconfigSetting(candidate, content, ctx.readFile); + } + } + return null; +} + +/** + * The app's declared iOS deployment target, or null when nothing declares one. + * Target selection: the marker's `targetUuid`, else the app target named + * `targetName` (`--product-name`), else every app target. Each configuration + * falls back to the project-level one of the same name (each level accepting a + * literal or the xcconfig it is based on), and the lowest wins — a single + * manifest floor has to hold for every configuration. + */ +function readIosDeploymentTargetFromPbxproj( + text /*: string */, + opts /*:: ?: { + targetUuid?: ?string, + targetName?: ?string, + srcRoot?: ?string, + readFile?: (absPath: string) => ?string, + } */, +) /*: ?string */ { + const ctx /*: XcconfigContext */ = { + srcRoot: opts?.srcRoot, + readFile: opts?.readFile ?? defaultReadFile, + }; + const targetUuid = opts?.targetUuid; + const marked = targetUuid != null ? findObjectByUuid(text, targetUuid) : null; + const apps = findApplicationTargets(text); + const named = + opts?.targetName != null + ? apps.find(app => app.name === opts.targetName) + : null; + const targets = marked != null ? [marked] : named != null ? [named] : apps; + + const projectDefaults /*: Map */ = new Map(); + const project = findProjectObject(text); + if (project != null) { + for (const uuid of targetBuildConfigUuids(text, project)) { + const config = findObjectByUuid(text, uuid); + const name = config != null ? configName(text, config) : null; + const value = + config != null ? configDeploymentTarget(text, config, ctx) : null; + if (name != null && value != null) { + projectDefaults.set(name, value); + } + } + } + + let floor /*: ?string */ = null; + for (const target of targets) { + for (const uuid of targetBuildConfigUuids(text, target)) { + const config = findObjectByUuid(text, uuid); + if (config == null) { + continue; + } + const name = configName(text, config); + const value = + configDeploymentTarget(text, config, ctx) ?? + (name != null ? projectDefaults.get(name) : null) ?? + null; + if ( + value != null && + (floor == null || compareIosVersions(value, floor) < 0) + ) { + floor = value; + } + } + } + return floor; +} + +/** + * The sanitized floor read from `/project.pbxproj`, or null when + * there is no project, it cannot be read, or it declares nothing usable. + */ +function resolveIosDeploymentTarget( + opts /*: {xcodeprojPath: ?string, targetUuid?: ?string, targetName?: ?string} */, +) /*: ?string */ { + const {xcodeprojPath, targetUuid, targetName} = opts; + if (xcodeprojPath == null) { + return null; + } + try { + const found = readIosDeploymentTargetFromPbxproj( + fs.readFileSync(path.join(xcodeprojPath, 'project.pbxproj'), 'utf8'), + {targetUuid, targetName, srcRoot: path.dirname(xcodeprojPath)}, + ); + return found != null ? sanitizeIosDeploymentTarget(found) : null; + } catch { + return null; + } +} + +module.exports = { + MIN_IOS_VERSION_SUPPORTED, + readIosDeploymentTargetFromPbxproj, + resolveIosDeploymentTarget, + sanitizeIosDeploymentTarget, +}; diff --git a/packages/react-native/scripts/spm/scaffold-package-swift.js b/packages/react-native/scripts/spm/scaffold-package-swift.js index 5c0e3f8325a..cf7e5879ce2 100644 --- a/packages/react-native/scripts/spm/scaffold-package-swift.js +++ b/packages/react-native/scripts/spm/scaffold-package-swift.js @@ -44,6 +44,7 @@ const { resolveSwiftName, } = require('./expand-spm-dependencies'); const {expandSpmSourceGlobs} = require('./generate-spm-autolinking'); +const {MIN_IOS_VERSION_SUPPORTED} = require('./ios-deployment-target'); const {findPodspecs, readPodspecCached} = require('./read-podspec'); const { REACT_CODEGEN_PACKAGE_NAME, @@ -107,7 +108,9 @@ const {log, warn} = makeLogger('scaffold-package-swift'); // v19: scaffolded C++ targets carry DEBUG/NDEBUG config defines so their Fabric // ABI matches the prebuilt React.framework (Release strips DebugStringConvertible // under NDEBUG). Bumped so existing scaffolds regenerate with the defines. -const SCAFFOLDER_VERSION = 19; +// v20: the platform floor is the app's iOS deployment target in string form +// (the `.v15` enum cannot express a dependency minimum like 16.4). +const SCAFFOLDER_VERSION = 20; const SCAFFOLDER_VERSION_LINE_RE = /^\/\/ AUTO-SCAFFOLDED-VERSION: (\d+)$/m; const AUTOGEN_MARKER = @@ -562,6 +565,7 @@ type EmitContext = { // Relative path to the app's local xcframeworks package // (/build/xcframeworks). Only referenced when remote == null. localXcfwPackageDir?: ?string, + iosDeploymentTarget?: ?string, }; */ @@ -578,6 +582,8 @@ function emitScaffoldedPackageSwift( ) /*: string */ { const slotComment = ctx.cacheSlotLabel != null ? `\n// Cache slot: ${ctx.cacheSlotLabel}` : ''; + const iosDeploymentTarget /*: string */ = + ctx.iosDeploymentTarget ?? MIN_IOS_VERSION_SUPPORTED; // React headers need NO search paths — they come from the React / // ReactNativeHeaders binaryTargets and the ReactAppHeaders product (see @@ -765,7 +771,7 @@ import PackageDescription let package = Package( name: "${spec.swiftName}", - platforms: [.iOS(.v15)], + platforms: [.iOS("${iosDeploymentTarget}")], products: [ .library(name: "${spec.swiftName}", targets: ["${spec.swiftName}"]), ], @@ -863,6 +869,7 @@ type ScaffoldContext = { // references honor each sibling's declared name. swiftNameByNpm?: Map, remote: ?{url: string, version: string, identity: string}, + iosDeploymentTarget?: ?string, }; */ @@ -1062,6 +1069,7 @@ function scaffoldPackageSwiftForDep( const content = emitScaffoldedPackageSwift(spec, { cacheSlotLabel: ctx.cacheSlotLabel, remote: ctx.remote, + iosDeploymentTarget: ctx.iosDeploymentTarget, codegenPackageDir: relFromManifest('build', 'generated', 'ios'), localXcfwPackageDir: relFromManifest('build', 'xcframeworks'), }); @@ -1123,6 +1131,151 @@ function scaffoldPackageSwiftForDep( }; } +/** + * The dependency set the autolinker considers: the iOS entries of + * autolinking.json plus their transitive SwiftPM dependencies. Null when the + * file declares none; throws when it cannot be read or parsed. `onSkipped` + * reports the entries dropped for having no iOS platform. + */ +function collectAutolinkedDeps( + opts /*: { + autolinkingJsonPath: string, + remote: ?{url: string, version: string, identity: string}, + onSkipped?: (name: string) => void, + } */, +) /*: ?Array */ { + const {autolinkingJsonPath, remote, onSkipped} = opts; + /*:: type AutolinkingJson = {dependencies?: ?{[string]: {root?: string, platforms?: {ios?: ?{...}, ...}, ...}}, ...}; */ + // $FlowFixMe[incompatible-type] JSON.parse returns any + const data /*: AutolinkingJson */ = JSON.parse( + fs.readFileSync(autolinkingJsonPath, 'utf8'), + ); + const deps = data.dependencies; + if (deps == null) { + return null; + } + + const directDeps /*: Array */ = []; + for (const name of Object.keys(deps)) { + const raw = deps[name]; + if (raw == null) continue; + const root = raw.root; + const ios = raw.platforms?.ios; + if (typeof root !== 'string' || ios == null) { + onSkipped?.(name); + continue; + } + // $FlowFixMe[incompatible-type] `ios` shape is runtime-validated above + const iosPlatform /*: AutolinkingIosPlatform */ = ios; + directDeps.push({name, root, platforms: {ios: iosPlatform}}); + } + + try { + return expandSpmDependencies(directDeps, { + readConfig: defaultReadConfig, + resolveDep: defaultResolveDep, + readPodspec: defaultReadPodspec, + extraReservedNames: remote != null ? [remote.identity] : undefined, + }); + } catch (e) { + if (e instanceof SpmNameCollisionError) { + throw e; + } + // A transitive-resolution failure shouldn't abort the whole pass; fall back + // to the direct deps so at least those are covered. They are named the way + // the autolinker names them — a manifest written under any other name + // outlives this error on disk and then fails to resolve. + log(`Transitive dependency expansion failed: ${e.message}`); + return directDeps.map(dep => { + const resolved = resolveSwiftName( + dep.name, + readSwiftpmConfig(dep.root, defaultReadConfig(dep.root)), + defaultReadPodspec(dep.root, dep.platforms.ios.podspecPath), + ); + return { + ...dep, + swiftName: resolved.name, + swiftNameSource: resolved.source, + swiftNamePodspecKey: resolved.podspecKey, + }; + }); + } +} + +// The `.iOS(...)` element of the emitted platforms array — the `.v15` enum form +// pre-v20 scaffolds carry included, and without the array's closing bracket so +// a user-extended array (`[.iOS(…), .macOS(…)]`) is still matched. +const PLATFORM_FLOOR_RE = /platforms: \[\.iOS\((\.v\d+|"[^"]*")\)/; + +/** + * Bring the platform floor of already-scaffolded manifests up to the app's + * deployment target — `spm add`/`update` must not leave a dep pinned below the + * app (SwiftPM would refuse to link it), and re-scaffolding is the user's call. + * Only the platform line of a file carrying our own marker is rewritten; no + * file is created. `from` is the previous token, verbatim (`15.1` or `.v15`). + */ +function refreshScaffoldedPlatformFloors( + opts /*: {appRoot: string, autolinkingJsonPath?: string, iosDeploymentTarget: string} */, +) /*: Array<{depName: string, path: string, from: string, to: string}> */ { + const {appRoot, iosDeploymentTarget} = opts; + const autolinkingJsonPath = + opts.autolinkingJsonPath ?? + path.join(appRoot, 'build', 'generated', 'autolinking', 'autolinking.json'); + const refreshed = []; + + // Best-effort: an unreadable autolinking.json, a name collision or a + // malformed remote config is the autolinker's error to report a moment + // later, not this pass's. + let deps /*: ?Array */ = null; + try { + deps = collectAutolinkedDeps({ + autolinkingJsonPath, + remote: remotePackageConfig(appRoot), + }); + } catch { + return refreshed; + } + if (deps == null) { + return refreshed; + } + + for (const {name: depName, root} of deps) { + const manifestPath = path.join(root, 'Package.swift'); + let content; + try { + content = fs.readFileSync(manifestPath, 'utf8'); + } catch { + continue; + } + if ( + !content.includes(SCAFFOLDER_MARKER) || + content.includes(AUTOGEN_MARKER) + ) { + continue; + } + const match = content.match(PLATFORM_FLOOR_RE); + if (match == null) { + continue; + } + const from = match[1].replace(/"/g, ''); + if (from === iosDeploymentTarget) { + continue; + } + fs.writeFileSync( + manifestPath, + content.replace(match[0], `platforms: [.iOS("${iosDeploymentTarget}")`), + 'utf8', + ); + refreshed.push({ + depName, + path: manifestPath, + from, + to: iosDeploymentTarget, + }); + } + return refreshed; +} + // --------------------------------------------------------------------------- // Multi-dep orchestrator // --------------------------------------------------------------------------- @@ -1136,6 +1289,7 @@ type ScaffoldAllOptions = { dryRun?: boolean, cacheSlotLabel?: ?string, autolinkingJsonPath?: string, + iosDeploymentTarget?: ?string, // npm dep names to skip entirely — used when the user declined the // confirmation prompt for first-time scaffolds. Skipped deps still // appear in the returned results array with status='skipped-opt-out'. @@ -1163,76 +1317,29 @@ function scaffoldAll( return []; } - /*:: type AutolinkingJson = {dependencies?: ?{[string]: {root?: string, platforms?: {ios?: ?{...}, ...}, ...}}, ...}; */ - // $FlowFixMe[incompatible-type] JSON.parse returns any - const data /*: AutolinkingJson */ = JSON.parse( - fs.readFileSync(autolinkingJsonPath, 'utf8'), - ); - const deps = data.dependencies; - if (deps == null) { - return []; - } - - // Narrow the direct autolinking.json entries with an iOS platform, then - // expand transitive `spm.dependencies` so the scaffolder covers EXACTLY the - // set the autolinker considers. Without this, a transitive native dep that + // The scaffolder covers EXACTLY the set the autolinker considers, transitive + // `spm.dependencies` included. Without them, a transitive native dep that // ships no Package.swift would be flagged by the autolinker but never // scaffolded here — leaving `react-native spm scaffold` unable to clear the // autolinker's missing-manifest error. const results /*: Array */ = []; - const directDeps /*: Array */ = []; - for (const name of Object.keys(deps)) { - const raw = deps[name]; - if (raw == null) continue; - const root = raw.root; - const ios = raw.platforms?.ios; - if (typeof root !== 'string' || ios == null) { + // Outside collectAutolinkedDeps' own try: a malformed remote config + // (RemoteVersionError) is a misconfiguration to surface, not an expansion + // failure to degrade past. + const remote = remotePackageConfig(appRoot); + const allDeps = collectAutolinkedDeps({ + autolinkingJsonPath, + remote, + onSkipped: name => { results.push({ depName: name, status: 'skipped-no-ios', reason: 'no iOS platform in autolinking.json', }); - continue; - } - // $FlowFixMe[incompatible-type] `ios` shape is runtime-validated above - const iosPlatform /*: AutolinkingIosPlatform */ = ios; - directDeps.push({name, root, platforms: {ios: iosPlatform}}); - } - - // Outside the try: a malformed remote config (RemoteVersionError) is a - // misconfiguration to surface, not an expansion failure to degrade past. - const remote = remotePackageConfig(appRoot); - - let allDeps /*: Array */ = []; - try { - allDeps = expandSpmDependencies(directDeps, { - readConfig: defaultReadConfig, - resolveDep: defaultResolveDep, - readPodspec: defaultReadPodspec, - extraReservedNames: remote != null ? [remote.identity] : undefined, - }); - } catch (e) { - if (e instanceof SpmNameCollisionError) { - throw e; - } - // A transitive-resolution failure shouldn't abort the whole scaffold pass; - // fall back to the direct deps so at least those get manifests. They are - // named the same way the autolinker names them — a manifest written under - // any other name outlives this error on disk and then fails to resolve. - log(`Transitive dependency expansion failed: ${e.message}`); - allDeps = directDeps.map(dep => { - const resolved = resolveSwiftName( - dep.name, - readSwiftpmConfig(dep.root, defaultReadConfig(dep.root)), - defaultReadPodspec(dep.root, dep.platforms.ios.podspecPath), - ); - return { - ...dep, - swiftName: resolved.name, - swiftNameSource: resolved.source, - swiftNamePodspecKey: resolved.podspecKey, - }; - }); + }, + }); + if (allDeps == null) { + return results; } // Index every autolinked dep's podspec name → its npm name, so a dep that @@ -1271,6 +1378,7 @@ function scaffoldAll( force: opts.force === true, dryRun: opts.dryRun === true, cacheSlotLabel: opts.cacheSlotLabel ?? null, + iosDeploymentTarget: opts.iosDeploymentTarget ?? null, podToNpm, swiftNameByNpm, remote, @@ -1296,6 +1404,7 @@ function scaffoldAll( } module.exports = { + refreshScaffoldedPlatformFloors, scaffoldAll, scaffoldPackageSwiftForDep, translatePodspecToSpmTarget, diff --git a/packages/react-native/scripts/spm/spm-pbxproj.js b/packages/react-native/scripts/spm/spm-pbxproj.js index c116ff38e13..9cd40df867a 100644 --- a/packages/react-native/scripts/spm/spm-pbxproj.js +++ b/packages/react-native/scripts/spm/spm-pbxproj.js @@ -267,6 +267,30 @@ function findProjectObject(text /*: string */) /*: ObjectRange | null */ { return findObjectByUuid(text, m[1]); } +/** Visit every top-level object of a section, in file order. */ +function forEachObjectInSection( + text /*: string */, + name /*: string */, + fn /*: (obj: {uuid: string, comment: ?string, bodyOpen: number, bodyClose: number}) => void */, +) /*: void */ { + const section = findSection(text, name); + if (section == null) { + return; + } + const re = /\n\t\t([0-9A-Fa-f]{24})(?: \/\* (.*?) \*\/)? = \{/g; + re.lastIndex = section.contentStart; + for (;;) { + const m = re.exec(text); + if (m == null || m.index >= section.end) { + break; + } + const bodyOpen = text.indexOf('{', m.index); + const bodyClose = scanToClose(text, bodyOpen); + fn({uuid: m[1], comment: m[2], bodyOpen, bodyClose}); + re.lastIndex = bodyClose; + } +} + /** * Every PBXNativeTarget whose productType is an application. Returns uuid + * name + body range for each. Used to pick the app target to inject into @@ -275,37 +299,27 @@ function findProjectObject(text /*: string */) /*: ObjectRange | null */ { function findApplicationTargets( text /*: string */, ) /*: Array<{uuid: string, name: string, bodyOpen: number, bodyClose: number}> */ { - const section = findSection(text, 'PBXNativeTarget'); - if (section == null) { - return []; - } const out = []; - const re = /\n\t\t([0-9A-Fa-f]{24})(?: \/\* (.*?) \*\/)? = \{/g; - re.lastIndex = section.contentStart; - for (;;) { - const m = re.exec(text); - if (m == null || m.index >= section.end) { - break; - } - const uuid = m[1]; - const comment = m[2]; - const bodyOpen = text.indexOf('{', m.index); - const bodyClose = scanToClose(text, bodyOpen); - const obj = {uuid, bodyOpen, bodyClose}; - const productType = findField(text, obj, 'productType'); - if ( - productType != null && - /com\.apple\.product-type\.application/.test(productType.value) - ) { + forEachObjectInSection( + text, + 'PBXNativeTarget', + ({uuid, comment, bodyOpen, bodyClose}) => { + const obj = {uuid, bodyOpen, bodyClose}; + const productType = findField(text, obj, 'productType'); + if ( + productType == null || + !/com\.apple\.product-type\.application/.test(productType.value) + ) { + return; + } const nameField = findField(text, obj, 'name'); const name = nameField != null ? nameField.value.replace(/^"|"$/g, '') : (comment ?? uuid); out.push({uuid, name, bodyOpen, bodyClose}); - } - re.lastIndex = bodyClose; - } + }, + ); return out; } @@ -766,6 +780,7 @@ module.exports = { findSection, findProjectObject, findApplicationTargets, + forEachObjectInSection, uuidsInArray, detectFieldIndent, insertObjectsIntoSection, diff --git a/packages/react-native/scripts/spm/spm-types.js b/packages/react-native/scripts/spm/spm-types.js index dcda8d3addd..a89142f6ada 100644 --- a/packages/react-native/scripts/spm/spm-types.js +++ b/packages/react-native/scripts/spm/spm-types.js @@ -67,6 +67,8 @@ export type AutolinkingArgs = { autolinkingJson: string | null, output: string | null, xcframeworksPath: string | null, + // Platform floor of every manifest this run writes; sanitized by parseArgs. + iosDeploymentTarget: string, }; export type SpmTarget = { @@ -206,6 +208,7 @@ export type AggregatorInput = { // the aggregator's package deps + the AutolinkedAggregate target deps. pluginPackageDeps?: ReadonlyArray, pluginProductDeps?: ReadonlyArray, + iosDeploymentTarget?: ?string, }; // --- Autolinking plugins (PREVIEW / unstable contract) --- @@ -377,6 +380,7 @@ export type SynthPackageSpec = { // `` resolve through the // dep's own `common/cpp/` subtree. headerSearchPaths?: ?Array, + iosDeploymentTarget?: ?string, }; diff --git a/packages/react-native/scripts/spm/sync-spm-autolinking.js b/packages/react-native/scripts/spm/sync-spm-autolinking.js index b37a7e89da7..78e1fcbd43a 100644 --- a/packages/react-native/scripts/spm/sync-spm-autolinking.js +++ b/packages/react-native/scripts/spm/sync-spm-autolinking.js @@ -82,6 +82,10 @@ async function main( demandOption: true, describe: 'Path to react-native package root', }) + .option('ios-deployment-target', { + type: 'string', + describe: 'Platform floor forwarded to the autolinker', + }) .help() .parseSync(); @@ -115,12 +119,17 @@ async function main( deps.installSpmCodegenTemplate(appRoot, reactNativeRoot, {log}); log('Re-generating build/generated/autolinking/Package.swift...'); - deps.generateAutolinking([ + const autolinkingArgv = [ '--app-root', appRoot, '--react-native-root', reactNativeRoot, - ]); + ]; + const iosDeploymentTarget = parsed['ios-deployment-target']; + if (iosDeploymentTarget != null) { + autolinkingArgv.push('--ios-deployment-target', iosDeploymentTarget); + } + deps.generateAutolinking(autolinkingArgv); // Rebuild the per-app generated-headers farm (vended as the ReactAppHeaders // SPM target inside the codegen package). React core headers need no trees