Skip to content

Add Swift Package Manager opt-ins for RN_STRICT_API and RN_ALLOW_FRAMEWORKS (#59043) - #59043

Open
coado wants to merge 1 commit into
react:mainfrom
coado:export-D124985657
Open

coado wants to merge 1 commit into
react:mainfrom
coado:export-D124985657

Conversation

@coado

@coado coado commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Let iOS apps that use the Swift Package Manager setup enforce React Native's C++ stable API, and control its "for frameworks" warning, matching the CocoaPods and Android opt-ins. Two env vars are read by npx react-native spm add / update:

  • RN_STRICT_API=1 defines the RN_STRICT_API macro, which arms the react/cxxstableapi guards.
  • RN_ALLOW_FRAMEWORKS=1 defines the RN_ALLOW_FRAMEWORKS macro, which silences their "for frameworks" warning.

Each variable is named after the macro it defines, as in the CocoaPods opt-in.

Each opted-in macro is defined for the app's own code:

  • the app target (GCC_PREPROCESSOR_DEFINITIONS, seeded with $(inherited)),
  • the packages the autolinker generates for the app's local spm.modules,
  • the app codegen package (ReactCodegen, ReactAppDependencyProvider).

Generated code never warns: the codegen package always defines RN_ALLOW_FRAMEWORKS, because nobody can act on a warning in generated code and libraries are held to the API in their own builds.

Under SwiftPM every library builds from its own Package.swift (shipped upstream or created with spm scaffold), so a library declares RN_ALLOW_FRAMEWORKS there, and the app's opt-ins do not reach libraries. React Native itself ships prebuilt, so none of its code is compiled with these macros.

The Xcode build phase runs spm sync without the shell environment, so add/update record the chosen macros in .spm-injected.json and every later sync regenerates the packages from that record. As with pod install, each add/update re-reads the variables: running update without one turns its macro off, and deinit removes the app-target defines along with the rest of the injection.

The SwiftPM guide (scripts/spm/__docs__/spm-scripts.md) documents the opt-ins: what they define, that libraries are only checked when they define the macros in their own Package.swift, and how the choice is remembered for Xcode builds.

The opt-ins are one list (CXX_API_DEFINES in spm-utils.js) passed through as macro names, so adding another opt-in is a one-line change.

Changelog:
[iOS][Added] - Add RN_STRICT_API and RN_ALLOW_FRAMEWORKS opt-ins for the Swift Package Manager setup to enforce React Native's C++ stable API in the app's native code

Differential Revision: D124985657

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 13, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Oct 13, 2026
@meta-codesync

meta-codesync Bot commented Oct 13, 2026

Copy link
Copy Markdown

@coado has exported this pull request. If you are a Meta employee, you can view the originating Diff in D124985657.

@facebook-github-tools facebook-github-tools Bot added the p: Facebook Partner: Facebook label Oct 13, 2026
…EWORKS (react#59043)

Summary:

Let iOS apps that use the Swift Package Manager setup enforce React Native's C++ stable API, and control its "for frameworks" warning, matching the CocoaPods and Android opt-ins. Two env vars are read by `npx react-native spm add` / `update`:

- `RN_STRICT_API=1` defines the `RN_STRICT_API` macro, which arms the `react/cxxstableapi` guards.
- `RN_ALLOW_FRAMEWORKS=1` defines the `RN_ALLOW_FRAMEWORKS` macro, which silences their "for frameworks" warning.

Each variable is named after the macro it defines, as in the CocoaPods opt-in.

Each opted-in macro is defined for the app's own code:
- the app target (`GCC_PREPROCESSOR_DEFINITIONS`, seeded with `$(inherited)`),
- the packages the autolinker generates for the app's local `spm.modules`,
- the app codegen package (`ReactCodegen`, `ReactAppDependencyProvider`).

Generated code never warns: the codegen package always defines `RN_ALLOW_FRAMEWORKS`, because nobody can act on a warning in generated code and libraries are held to the API in their own builds.

Under SwiftPM every library builds from its own `Package.swift` (shipped upstream or created with `spm scaffold`), so a library declares `RN_ALLOW_FRAMEWORKS` there, and the app's opt-ins do not reach libraries. React Native itself ships prebuilt, so none of its code is compiled with these macros.

The Xcode build phase runs `spm sync` without the shell environment, so `add`/`update` record the chosen macros in `.spm-injected.json` and every later sync regenerates the packages from that record. As with `pod install`, each `add`/`update` re-reads the variables: running `update` without one turns its macro off, and `deinit` removes the app-target defines along with the rest of the injection.

The SwiftPM guide (`scripts/spm/__docs__/spm-scripts.md`) documents the opt-ins: what they define, that libraries are only checked when they define the macros in their own `Package.swift`, and how the choice is remembered for Xcode builds.

The opt-ins are one list (`CXX_API_DEFINES` in `spm-utils.js`) passed through as macro names, so adding another opt-in is a one-line change.

Changelog:
[iOS][Added] - Add `RN_STRICT_API` and `RN_ALLOW_FRAMEWORKS` opt-ins for the Swift Package Manager setup to enforce React Native's C++ stable API in the app's native code

Differential Revision: D124985657
@meta-codesync meta-codesync Bot changed the title Add Swift Package Manager opt-in for RN_STRICT_API Add Swift Package Manager opt-ins for RN_STRICT_API and RN_ALLOW_FRAMEWORKS (#59043) Oct 14, 2026
@coado
coado force-pushed the export-D124985657 branch from afdfbd7 to bc5bc92 Compare October 14, 2026 10:22

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported p: Facebook Partner: Facebook p: Software Mansion Partner: Software Mansion Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant