Make codegen generate a Package.swift file for Codegen'd target (#53619)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53619

With this change, we are making Codegen generate a Package.swift file so that we can integrate the `ReactCodegen` and the `ReactAppDependencyProvider` in apps only using SwiftPM

## Changelog
[iOS][Added] - Make codegen generate PAckage.swift file for the codegen targets

Reviewed By: cortinico

Differential Revision: D81769543

fbshipit-source-id: 1f1a1b9f41126e142931d5eda6e75109a69f828c
This commit is contained in:
Riccardo Cipolleschi
2025-09-10 05:25:54 -07:00
committed by Facebook GitHub Bot
parent 87f8e85b7f
commit a7cd3cc08f
3 changed files with 110 additions and 0 deletions
@@ -0,0 +1,44 @@
/**
* 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';
const {TEMPLATES_FOLDER_PATH} = require('./constants');
const {codegenLog} = require('./utils');
const fs = require('fs');
const path = require('path');
const PACKAGE_SWIFT_TEMPLATE_PATH = path.join(
TEMPLATES_FOLDER_PATH,
'Package.swift.template',
);
function generatePackageSwift(
projectRoot /*: string */,
outputDir /*: string */,
reactNativePath /*: string */,
) {
const fullOutputPath = path.join(projectRoot, outputDir);
fs.mkdirSync(outputDir, {recursive: true});
// Generate PAckage.swift File
codegenLog('Generating Package.swift');
const templateH = fs
.readFileSync(PACKAGE_SWIFT_TEMPLATE_PATH, 'utf8')
.replace(
/{reactNativePath}/,
path.relative(fullOutputPath, reactNativePath),
);
const finalPathH = path.join(outputDir, 'Package.swift');
fs.writeFileSync(finalPathH, templateH);
codegenLog(`Generated artifact: ${finalPathH}`);
}
module.exports = {
generatePackageSwift,
};
@@ -22,6 +22,7 @@ const {
} = require('./generateAppDependencyProvider');
const {generateCustomURLHandlers} = require('./generateCustomURLHandlers');
const {generateNativeCode} = require('./generateNativeCode');
const {generatePackageSwift} = require('./generatePackageSwift');
const {generateRCTModuleProviders} = require('./generateRCTModuleProviders');
const {
generateRCTThirdPartyComponents,
@@ -37,6 +38,7 @@ const {
codegenLog,
findCodegenEnabledLibraries,
findDisabledLibrariesByPlatform,
findReactNativeRootPath,
pkgJsonIncludesGeneratedCode,
readPkgJsonInDirectory,
readReactNativeConfig,
@@ -160,6 +162,11 @@ function execute(
reactCodegenOutputPath,
baseOutputPath,
);
generatePackageSwift(
projectRoot,
outputPath,
findReactNativeRootPath(projectRoot),
);
}
cleanupEmptyFilesAndFolders(outputPath);
@@ -0,0 +1,59 @@
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "React-GeneratedCode",
platforms: [.iOS(.v15), .macCatalyst(SupportedPlatform.MacCatalystVersion.v13)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "ReactCodegen",
targets: ["ReactCodegen"]),
.library(
name: "ReactAppDependencyProvider",
targets: ["ReactAppDependencyProvider"]),
],
dependencies: [
.package(name: "React", path: "{reactNativePath}")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "ReactCodegen",
dependencies: ["React"],
path: "ReactCodegen",
exclude: ["ReactCodegen.podspec"],
publicHeadersPath: ".",
cSettings: [
.headerSearchPath("headers")
],
cxxSettings: [
.headerSearchPath("headers"),
.unsafeFlags(["-std=c++20"]),
],
linkerSettings: [
.linkedFramework("Foundation")
]
),
.target(
name: "ReactAppDependencyProvider",
dependencies: ["ReactCodegen"],
path: "ReactAppDependencyProvider",
exclude: ["ReactAppDependencyProvider.podspec"],
publicHeadersPath: ".",
cSettings: [
.headerSearchPath("headers"),
],
cxxSettings: [
.headerSearchPath("headers"),
.unsafeFlags(["-std=c++20"]),
],
linkerSettings: [
.linkedFramework("Foundation")
]
)
]
)