Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: build prebuilt xcframeworks in an isolated clone

Ammar Ahmedbe466852443d
diff --git a/apps/mobile/.gitignore b/apps/mobile/.gitignore
index 7ea1a7b45..c496cbeaf 100644
--- a/apps/mobile/.gitignore
+++ b/apps/mobile/.gitignore
@@ -21,6 +21,7 @@ build_cache/
android/.prebuilt-aars/*
!android/.prebuilt-aars/*.aar
ios/.prebuilt-xcframeworks/.cache/
+.ios-prebuilt-build/
# Xcode
#
diff --git a/apps/mobile/ios/prebuilt-libraries.json b/apps/mobile/ios/prebuilt-libraries.json
index d8a6c88f4..dce1eb5f9 100644
--- a/apps/mobile/ios/prebuilt-libraries.json
+++ b/apps/mobile/ios/prebuilt-libraries.json
@@ -18,10 +18,7 @@
},
"react-native-quick-sqlite": {
"pod": "react-native-quick-sqlite",
- "swift": false,
- "buildSettings": {
- "GCC_PREPROCESSOR_DEFINITIONS": "SQLITE_CORE=1 SQLITE_ENABLE_FTS5=1 SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT=1 HAVE_CIPHER_AES_128_CBC=0 HAVE_CIPHER_AES_256_CBC=0 HAVE_CIPHER_SQLCIPHER=0 HAVE_CIPHER_RC4=0 HAVE_CIPHER_CHACHA20=1 SQLITE_OMIT_PROGRESS_CALLBACK=1 SQLITE_MAX_EXPR_DEPTH=0 SQLITE_OMIT_DEPRECATED=1 SQLITE_LIKE_DOESNT_MATCH_BLOBS=1 SQLITE_DQS=0 SQLITE_DEFAULT_MEMSTATUS=0 SQLITE_USE_ALLOCA=1 SQLITE_ENABLE_REGEXP=1"
- }
+ "swift": false
},
"react-native-mmkv-storage": {
"pod": "react-native-mmkv-storage",
diff --git a/apps/mobile/ios/scripts/prebuilt_pods.rb b/apps/mobile/ios/scripts/prebuilt_pods.rb
index ae423c102..efe0c5f5b 100644
--- a/apps/mobile/ios/scripts/prebuilt_pods.rb
+++ b/apps/mobile/ios/scripts/prebuilt_pods.rb
@@ -16,6 +16,10 @@ module PrebuiltPods
ENV['NN_USE_PREBUILT_IOS'] == '0'
end
+ def self.third_party_disabled?
+ ENV['NN_PREBUILT_THIRD_PARTY'] == '0'
+ end
+
def self.node_executable
return @node_executable if defined?(@node_executable) && @node_executable
@@ -32,7 +36,6 @@ module PrebuiltPods
@libraries ||= File.exist?(CONFIG_PATH) ? JSON.parse(File.read(CONFIG_PATH))['libraries'] || {} : {}
end
- # Asks the Node build script which pods have a valid, current artifact.
def self.available_artifacts
return @available_artifacts if defined?(@available_artifacts)
@@ -46,10 +49,6 @@ module PrebuiltPods
@available_artifacts
end
- # Both configurations must be present: a pod may only be swapped for a prebuilt
- # binary when the Debug and the Release slice both exist, because React Native
- # links a different React core per configuration and mixing the two crashes at
- # runtime.
def self.reason_available(pod, artifacts)
configs = artifacts[pod]
if configs.nil?
@@ -59,15 +58,10 @@ module PrebuiltPods
end
end
- # The artifact filename carries the hash, so a marker keyed on that name is
- # enough to skip re-extracting an unchanged zip while still re-extracting when
- # a new artifact replaces an old one.
def self.unzip_artifact(zip, destination)
marker = File.join(destination, ".extracted-#{File.basename(zip)}")
return if File.exist?(marker)
- # A partial extraction would leave the previous artifacts' slices behind
- # because an archive only overwrites what it contains.
FileUtils.rm_rf(destination)
FileUtils.mkdir_p(destination)
system('unzip', '-oq', zip, '-d', destination) || raise(
@@ -80,10 +74,6 @@ module PrebuiltPods
File.join(CACHE_ROOT, pod)
end
- # CocoaPods records `:path` verbatim in Podfile.lock, and the Podfile directory
- # is what it resolves those paths against. Handing it an absolute path would
- # bake this machine's checkout location into the lockfile and churn it on every
- # other machine and on CI.
def self.relative_pod_dir(pod)
Pathname.new(pod_dir(pod)).relative_path_from(
Pod::Config.instance.installation_root
@@ -96,13 +86,6 @@ module PrebuiltPods
package && package[:podspec_path]
end
- # Swaps the configuration being built into the vendored xcframework. CocoaPods
- # can only vendor one configuration, so the copy it installs into the build
- # directory would otherwise always be that one. Replacing the vendored
- # framework before it runs leaves CocoaPods to select the slice for the current
- # platform and arch, and keeps Debug and Release from mixing React core ABIs.
- # The copy is refreshed so that CocoaPods' own copy step is not skipped as up
- # to date.
def self.select_configuration_script(pod)
<<~SH
set -e
@@ -123,8 +106,6 @@ module PrebuiltPods
SH
end
- # Rewrites the resolved podspec so that the prebuilt binary replaces the pod's
- # sources, keeping dependencies and build settings intact.
def self.prepare_prebuilt_pod(pod, podspec_path, artifacts)
return nil unless podspec_path && File.exist?(podspec_path)
@@ -132,10 +113,6 @@ module PrebuiltPods
target_dir = pod_dir(pod)
FileUtils.mkdir_p(target_dir)
- # A pod may already vendor binaries of its own (for example
- # react-native-fast-openpgp vendors OpenPGPBridge.xcframework alongside its
- # sources). Those must be preserved, but podspec file patterns have to stay
- # relative to the pod root, so bring them into the prebuilt pod directory.
pod_root = File.dirname(podspec_path)
inherited_frameworks = Array(spec.attributes_hash['vendored_frameworks']).map do |path|
source = File.expand_path(path, pod_root)
@@ -153,9 +130,6 @@ module PrebuiltPods
unzip_artifact(artifacts[pod][config], File.join(target_dir, config))
end
- # CocoaPods resolves and validates the vendored xcframework at install time,
- # so one configuration has to be in place before the first build. The script
- # phase replaces it with whichever configuration Xcode is building.
vendored_dir = File.join(target_dir, "#{pod}.xcframework")
FileUtils.rm_rf(vendored_dir)
FileUtils.cp_r(File.join(target_dir, 'debug', "#{pod}.xcframework"), vendored_dir)
@@ -180,14 +154,6 @@ module PrebuiltPods
spec.subspecs.each { |subspec| strip_sources(subspec) }
end
- # Reading a podspec evaluates it, and third-party podspecs branch on the
- # New Architecture environment at evaluation time. `install_modules_dependencies`
- # also decides whether a pod depends on the source third-party pods or on the
- # prebuilt ReactNativeDependencies pod at evaluation time. React Native only sets
- # all of that inside `use_react_native!`, which runs after this file, so establish
- # the same state first or podspecs are read as if the New Architecture were off.
- # These assignments are idempotent, so the later ones from `use_react_native!`
- # are no-ops.
def self.ensure_react_native_prebuilt_state(native_modules)
ENV['RCT_NEW_ARCH_ENABLED'] = '1'
ENV['RCT_FABRIC_ENABLED'] = '1'
@@ -207,6 +173,7 @@ module PrebuiltPods
return if disabled?
ensure_react_native_prebuilt_state(native_modules)
+ return if third_party_disabled?
artifacts = available_artifacts
libraries.each do |npm_name, lib|
@@ -230,8 +197,6 @@ module PrebuiltPods
end
end
- # Keeps third-party podspecs that hardcode `s.dependency "RCT-Folly"` resolvable
- # when React Native's prebuilt dependencies omit the source pods.
def self.rn_dependencies_prebuilt?
return false if disabled?
return false unless defined?(ReactNativeDependenciesUtils)
diff --git a/apps/mobile/scripts/build-prebuilt-ios.js b/apps/mobile/scripts/build-prebuilt-ios.js
index a4184d230..f1b392e27 100644
--- a/apps/mobile/scripts/build-prebuilt-ios.js
+++ b/apps/mobile/scripts/build-prebuilt-ios.js
@@ -24,9 +24,11 @@ const { execFileSync } = require("child_process");
const mobileDir = path.resolve(__dirname, "..");
const iosDir = path.join(mobileDir, "ios");
+const podsDir = path.join(iosDir, "Pods");
const prebuiltDir = path.join(iosDir, ".prebuilt-xcframeworks");
const cacheDir = path.join(prebuiltDir, ".cache");
const configPath = path.join(iosDir, "prebuilt-libraries.json");
+const buildDir = path.join(mobileDir, ".ios-prebuilt-build");
const CONFIG = JSON.parse(fs.readFileSync(configPath, "utf8"));
const LIBRARIES = CONFIG.libraries || {};
@@ -38,16 +40,24 @@ const CONFIGURATIONS = {
const DEFAULT_CONFIGURATIONS = ["debug", "release"];
-// The simulator architectures are compiled together so that create-xcframework
-// emits one ios-arm64_x86_64-simulator slice: two separate builds cannot both
-// claim the same platform identifier. The artifacts are shared by every checkout
-// and by CI, so they carry arm64 simulator even though this Podfile currently
-// excludes it.
const SLICES = [
{ sdk: "iphoneos", archs: ["arm64"] },
{ sdk: "iphonesimulator", archs: ["x86_64", "arm64"] }
];
+const USAGE = `Notesnook iOS prebuilt xcframework builder
+
+Usage:
+ node scripts/build-prebuilt-ios.js --list
+ node scripts/build-prebuilt-ios.js --help
+ node scripts/build-prebuilt-ios.js [<npm-name>] [debug|release]
+
+Artifacts are built in .ios-prebuilt-build/, a disposable clone of ios/, so the
+working ios/ is never built against or modified.
+
+Commit ios/Podfile.lock only in the all-prebuilt state, built with the team Xcode version.
+`;
+
function readJson(file) {
return JSON.parse(fs.readFileSync(file, "utf8"));
}
@@ -71,8 +81,6 @@ function xcodeVersion() {
return match ? match[1] : out.trim();
}
-// Resolved the same way the Podfile resolves them, so that an artifact built by
-// hand and an artifact looked up from `pod install` hash identically.
function rnPrebuiltState() {
if (process.env.NN_USE_PREBUILT_IOS === "0") {
return { rncore: "0", rndep: "0" };
@@ -108,9 +116,6 @@ function podVersions(pods) {
.join(",");
}
-// The npm package's own podspec is what identifies the sources being replaced.
-// The lockfile checksum cannot be used: it changes as soon as the pod is swapped
-// for the prebuilt one, which would invalidate the artifact that caused the swap.
function podspecContent(npmName) {
const dir = path.join(mobileDir, "node_modules", npmName);
if (!fs.existsSync(dir)) return "";
@@ -155,19 +160,12 @@ function isValidArtifact(file) {
return fs.existsSync(file) && !isLfsPointer(file);
}
-function buildSettingsFor(lib, config, slice) {
- const settings = Object.assign({}, lib.buildSettings || {});
+function buildSettingsFor(lib, slice) {
+ const settings = {};
if (lib.swift) {
settings.BUILD_LIBRARY_FOR_DISTRIBUTION = "YES";
- // NitroModules re-exports its own Objective-C++ module from Swift. Verifying
- // the emitted module interface in isolation cannot see that module and fails
- // with "underlying Objective-C module 'NitroModules' not found", even though
- // the interface itself is correct and is what create-xcframework needs.
settings.OTHER_SWIFT_FLAGS = "$(inherited) -no-verify-emitted-module-interface";
}
- // The Podfile excludes arm64 for the simulator, which would otherwise drop the
- // slice this script exists to produce. Overriding the unconditional setting is
- // enough to clear the conditional one as well.
if (slice.sdk === "iphonesimulator") {
settings.EXCLUDED_ARCHS = "";
settings.ONLY_ACTIVE_ARCH = "NO";
@@ -175,6 +173,48 @@ function buildSettingsFor(lib, config, slice) {
return settings;
}
+function snapshotRealState() {
+ const state = { markers: {}, react: {} };
+ const markers = [
+ path.join(podsDir, ".last_build_configuration"),
+ path.join(podsDir, "React-Core-prebuilt", ".last_build_configuration"),
+ path.join(podsDir, "ReactNativeDependencies", ".last_build_configuration")
+ ];
+ for (const marker of markers) {
+ if (fs.existsSync(marker)) state.markers[marker] = fs.readFileSync(marker, "utf8");
+ }
+ const frameworkDir = path.join(podsDir, "React-Core-prebuilt", "React.xcframework");
+ if (fs.existsSync(frameworkDir)) {
+ for (const slice of fs.readdirSync(frameworkDir)) {
+ const binary = path.join(frameworkDir, slice, "React.framework", "React");
+ if (fs.existsSync(binary)) state.react[binary] = fs.statSync(binary).size;
+ }
+ }
+ return JSON.stringify(state);
+}
+
+function cloneIos() {
+ fs.rmSync(buildDir, { recursive: true, force: true });
+ try {
+ execFileSync("cp", ["-c", "-R", iosDir, buildDir], { stdio: "inherit" });
+ } catch (e) {
+ execFileSync("cp", ["-R", iosDir, buildDir], { stdio: "inherit" });
+ }
+}
+
+function installClone() {
+ const env = Object.assign({}, process.env, {
+ NN_PREBUILT_THIRD_PARTY: "0",
+ RCT_USE_PREBUILT_RNCORE: "1",
+ RCT_USE_RN_DEP: "1"
+ });
+ execFileSync("pod", ["install"], { cwd: buildDir, stdio: "inherit", env });
+}
+
+function removeClone() {
+ fs.rmSync(buildDir, { recursive: true, force: true });
+}
+
function build(npmName, config) {
const lib = LIBRARIES[npmName];
const pod = lib.pod;
@@ -206,14 +246,13 @@ function build(npmName, config) {
"-derivedDataPath",
derivedData
];
- const settings = buildSettingsFor(lib, config, slice);
- for (const [key, value] of Object.entries(settings)) {
+ for (const [key, value] of Object.entries(buildSettingsFor(lib, slice))) {
args.push(`${key}=${value}`);
}
args.push("build");
console.log(`[BUILD] ${pod} ${config} ${slice.sdk}/${slice.archs.join("+")}`);
- execFileSync("xcodebuild", args, { cwd: iosDir, stdio: "inherit" });
+ execFileSync("xcodebuild", args, { cwd: buildDir, stdio: "inherit" });
const productsDir = path.join(derivedData, "Build", "Products");
const productName = fs
@@ -227,7 +266,7 @@ function build(npmName, config) {
built.push({ slice, staticLib, podProducts });
}
- const publicHeaders = path.join(iosDir, "Pods", "Headers", "Public", pod);
+ const publicHeaders = path.join(buildDir, "Pods", "Headers", "Public", pod);
const outDir = path.join(workDir, "out");
fs.mkdirSync(outDir, { recursive: true });
@@ -274,8 +313,14 @@ function listArtifacts() {
return result;
}
-if (require.main === module) {
+function main() {
const args = process.argv.slice(2);
+
+ if (args[0] === "--help" || args[0] === "-h") {
+ process.stdout.write(USAGE);
+ process.exit(0);
+ }
+
if (args[0] === "--list") {
process.stdout.write(JSON.stringify(listArtifacts()) + "\n");
process.exit(0);
@@ -284,14 +329,51 @@ if (require.main === module) {
const targetLib = args[0];
const targetConfig = args[1];
const libs = targetLib ? [targetLib] : Object.keys(LIBRARIES);
+ const jobs = [];
+
for (const npmName of libs) {
if (!LIBRARIES[npmName]) throw new Error(`Unknown library: ${npmName}`);
const configs = targetConfig ? [targetConfig] : DEFAULT_CONFIGURATIONS;
+ const lib = LIBRARIES[npmName];
for (const config of configs) {
if (!CONFIGURATIONS[config]) throw new Error(`Unknown configuration: ${config}`);
- build(npmName, config);
+ const target = path.join(prebuiltDir, artifactName(lib, config, computeHash(npmName)));
+ if (isValidArtifact(target)) {
+ console.log(`[SKIP] ${path.basename(target)} already exists`);
+ continue;
+ }
+ jobs.push([npmName, config]);
}
}
+
+ if (jobs.length === 0) return;
+
+ const before = snapshotRealState();
+ let failure = null;
+
+ try {
+ cloneIos();
+ installClone();
+ for (const [npmName, config] of jobs) build(npmName, config);
+ } catch (e) {
+ failure = e;
+ } finally {
+ removeClone();
+ }
+
+ const after = snapshotRealState();
+ if (before !== after) {
+ throw new Error(
+ `The artifact build modified the working ios/ state, which must never be built against.\n` +
+ ` before: ${before}\n after: ${after}`
+ );
+ }
+
+ if (failure) throw failure;
+}
+
+if (require.main === module) {
+ main();
}
module.exports = { computeHash, listArtifacts, LIBRARIES, artifactName, prebuiltDir, cacheDir };