Skip to content

Commit 57b6fa1

Browse files
chrfalchgabrieldonadel
authored andcommitted
SwiftPM scaffold: define RCT_NEW_ARCH_ENABLED for install_modules_dependencies podspecs (#59632)
* SwiftPM scaffold: define RCT_NEW_ARCH_ENABLED for install_modules_dependencies podspecs CocoaPods' install_modules_dependencies(s) adds -DRCT_NEW_ARCH_ENABLED=1 to a library pod. The SwiftPM scaffolder strips that helper before it reads the podspec, so scaffolded libraries compiled their old-architecture code. Emit .define("RCT_NEW_ARCH_ENABLED", to: "1") in cSettings and cxxSettings when the podspec calls the helper, and bump SCAFFOLDER_VERSION to 22. * Annotate preprocessorDefines for Flow (cherry picked from commit 35c929e)
1 parent bccb1df commit 57b6fa1

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,57 @@ describe('scaffoldAll', () => {
15521552
});
15531553
});
15541554

1555+
// ---------------------------------------------------------------------------
1556+
// RCT_NEW_ARCH_ENABLED — CocoaPods' install_modules_dependencies(s) adds
1557+
// -DRCT_NEW_ARCH_ENABLED=1 to the pod; the scaffolder strips that helper, so it
1558+
// must emit the define itself or `#ifdef RCT_NEW_ARCH_ENABLED` code is dropped.
1559+
// ---------------------------------------------------------------------------
1560+
1561+
describe('RCT_NEW_ARCH_ENABLED define', () => {
1562+
const NEW_ARCH_DEFINE = '.define("RCT_NEW_ARCH_ENABLED", to: "1")';
1563+
1564+
function translate(overrides) {
1565+
return translatePodspecToSpmTarget(podspec(overrides), autolinkedDep());
1566+
}
1567+
1568+
function emit(spec) {
1569+
return emitScaffoldedPackageSwift(spec, {
1570+
cacheSlotLabel: null,
1571+
remote: null,
1572+
codegenPackageDir: '../../ios/build/generated/ios',
1573+
localXcfwPackageDir: '../../ios/build/xcframeworks',
1574+
});
1575+
}
1576+
1577+
function newArchDefines(spec) {
1578+
return spec.preprocessorDefines.filter(
1579+
d => d.name === 'RCT_NEW_ARCH_ENABLED',
1580+
);
1581+
}
1582+
1583+
it('adds RCT_NEW_ARCH_ENABLED=1 when the podspec calls install_modules_dependencies', () => {
1584+
const spec = translate({usesInstallModulesDependencies: true});
1585+
expect(newArchDefines(spec)).toEqual([
1586+
{name: 'RCT_NEW_ARCH_ENABLED', value: '1', config: null},
1587+
]);
1588+
});
1589+
1590+
it('does not add the define when the podspec does not call install_modules_dependencies', () => {
1591+
const spec = translate({usesInstallModulesDependencies: false});
1592+
expect(newArchDefines(spec)).toEqual([]);
1593+
});
1594+
1595+
it('emits the define in both cSettings and cxxSettings, not as an unsafe flag', () => {
1596+
const out = emit(translate({usesInstallModulesDependencies: true}));
1597+
const cSettings = out.split('\n').find(l => l.includes('cSettings:'));
1598+
const cxxSettings = out.split('\n').find(l => l.includes('cxxSettings:'));
1599+
expect(cSettings).toContain(NEW_ARCH_DEFINE);
1600+
expect(cxxSettings).toContain(NEW_ARCH_DEFINE);
1601+
expect(out).not.toContain('-DRCT_NEW_ARCH_ENABLED');
1602+
expect(out).not.toContain('FOLLY_CFG_NO_COROUTINES');
1603+
});
1604+
});
1605+
15551606
// ---------------------------------------------------------------------------
15561607
// SCAFFOLDER_VERSION — auto-regen when the emitter's output format changes
15571608
//
@@ -1569,7 +1620,7 @@ describe('SCAFFOLDER_VERSION', () => {
15691620
});
15701621

15711622
it('is at the version the current emitter output requires', () => {
1572-
expect(SCAFFOLDER_VERSION).toBe(21);
1623+
expect(SCAFFOLDER_VERSION).toBe(22);
15731624
});
15741625

15751626
it('emitter writes the current version to the file', () => {

‎packages/react-native/scripts/spm/scaffold-package-swift.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
AutolinkedDep,
1616
AutolinkingIosPlatform,
1717
PodspecModel,
18+
PreprocessorDefine,
1819
ScaffoldResult,
1920
SpmScaffoldSpec,
2021
} from './spm-types';
@@ -112,7 +113,9 @@ const {log, warn} = makeLogger('scaffold-package-swift');
112113
// (the `.v15` enum cannot express a dependency minimum like 16.4).
113114
// v21: `resources:` is emitted before `publicHeadersPath:`, the argument order
114115
// `.target(...)` requires (the reverse order fails to resolve).
115-
const SCAFFOLDER_VERSION = 21;
116+
// v22: podspecs that call `install_modules_dependencies(s)` get
117+
// `.define("RCT_NEW_ARCH_ENABLED", to: "1")`, as CocoaPods adds it.
118+
const SCAFFOLDER_VERSION = 22;
116119
const SCAFFOLDER_VERSION_LINE_RE = /^\/\/ AUTO-SCAFFOLDED-VERSION: (\d+)$/m;
117120

118121
const AUTOGEN_MARKER =
@@ -528,11 +531,23 @@ function translatePodspecToSpmTarget(
528531
}
529532
}
530533

534+
// CocoaPods' `install_modules_dependencies(s)` adds -DRCT_NEW_ARCH_ENABLED=1
535+
// to the pod (scripts/cocoapods/new_architecture.rb), and libraries gate
536+
// their TurboModule/Fabric code on it. The helper is stripped before the
537+
// podspec is read, so restore the define here.
538+
const preprocessorDefines /*: Array<PreprocessorDefine> */ =
539+
model.usesInstallModulesDependencies === true
540+
? [
541+
...model.preprocessorDefines,
542+
{name: 'RCT_NEW_ARCH_ENABLED', value: '1', config: null},
543+
]
544+
: model.preprocessorDefines;
545+
531546
return {
532547
swiftName,
533548
sources: expandedSources,
534549
headerSearchPaths,
535-
preprocessorDefines: model.preprocessorDefines,
550+
preprocessorDefines,
536551
needsObjCPrefix,
537552
coreReactNative,
538553
siblingNames,

0 commit comments

Comments
 (0)