Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass react build dir to cmake #33736

Closed
wants to merge 1 commit into from

Conversation

janicduplessis
Copy link
Contributor

Summary

The ReactAndroid cmake build assumes that the build directory is ReactAndroid/build, but this is not always the case if gradle is configured with a different build location. Instead of assuming the build location in cmake, pass it from gradle.

Changelog

[Android] [Fixed] - Pass react build dir to cmake

Test Plan

Tested in an app with a custom build dir that RN now builds properly after this change.

In build.gradle add:

allprojects {
    buildDir = "${System.getProperty("user.home")}/.gradle/builds/${rootProject.name}/${project.name}"
}

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. labels Apr 30, 2022
@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels Apr 30, 2022
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 30, 2022
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,775,930 +36
android hermes armeabi-v7a 7,181,362 +16
android hermes x86 8,084,892 -4
android hermes x86_64 8,065,109 +38
android jsc arm64-v8a 9,650,106 +29
android jsc armeabi-v7a 8,424,186 +17
android jsc x86 9,599,553 -3
android jsc x86_64 10,197,181 +43

Base commit: d992ae0
Branch: main

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: d992ae0
Branch: main

@@ -283,6 +283,7 @@ android {
cmake {
arguments "-DREACT_COMMON_DIR=${reactNativeRootDir}/ReactCommon",
"-DREACT_ANDROID_DIR=$projectDir",
"-DREACT_BUILD_DIR=$buildDir",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks fine 👍

I'm curious to know why do you need to change this though.
We need to make some assumptions from time to time (e.g. where the codegen output folder is and so on) so allowing for full customization is not always possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason for me to change the build directory is to avoid having to rebuild everything when node_modules gets changed. Building from source can be quite long so keeping the build cache is nice by moving the build directory to somewhere else.

This used to work with ndk build so I think changing build dir should be supported and is possible to avoid assuming the location.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up here: how do you handle iOS build dir?

The reason why I'm asking is that we have build/ hardcoded in several other places in the codebase:

$ rg "build/"

react.gradle
110:        new File(hermesOverrideDir, "build/bin/hermesc") :
111:        new File(reactRoot, "node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/hermesc")

ReactAndroid/src/main/jni/CMakeLists.txt
20:  add_react_android_subdir(build/third-party-ndk/${relative_path})
87:add_react_android_subdir(build/generated/source/codegen/jni)

scripts/generate-artifacts.js
217:      'build/generated/ios',

scripts/android-e2e-test.js
83:      app: path.resolve('android/app/build/outputs/apk/debug/app-debug.apk'),

scripts/react_native_pods.rb
9:$CODEGEN_OUTPUT_DIR = 'build/generated/ios'

scripts/react_native_pods_utils/__tests__/script_phases.snap.rb
50:    export RCT_SCRIPT_OUTPUT_DIR=$RCT_SCRIPT_POD_INSTALLATION_ROOT/build/generated/ios

scripts/react_native_pods_utils/__tests__/script_phases.test.rb
33:            codegen_output_dir: 'build/generated/ios',

packages/rn-tester/package.json
19:    "clean-ios": "rm -rf build/generated/ios && rm -rf Pods && rm Podfile.lock"

scripts/objc-test.sh
102:    REPORTS_DIR="$THIS_DIR/../build/reports"

packages/rn-tester/Podfile.lock
758:  - React-Codegen (from `build/generated/ios`)
833:    :path: build/generated/ios

packages/rn-tester/android/app/build.gradle
86:    hermesCommand = "$rootDir/ReactAndroid/hermes-engine/build/hermes/bin/hermesc"

packages/react-native-codegen/scripts/buck-oss/combine_js_to_schema.sh
14:node "build/lib/cli/combine/combine-js-to-schema-cli.js" "$@"

packages/react-native-codegen/scripts/buck-oss/generate-all.sh
14:node "build/lib/cli/generators/generate-all.js" "$@"

packages/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/PathUtilsTest.kt
173:    tempFolder.newFolder("node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/")
176:            "node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/hermesc")
199:    tempFolder.newFolder("node_modules/react-native/sdks/hermes/build/bin/")
200:    tempFolder.newFile("node_modules/react-native/sdks/hermes/build/bin/hermesc")
211:    tempFolder.newFolder("node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/")
214:            "node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/hermesc")
226:            "node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/hermesc"),
233:        File("/home/circleci/hermes/build/bin/hermesc"),

packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/PathUtils.kt
47: * 2. The file located in `node_modules/react-native/sdks/hermes/build/bin/hermesc`. This will be
118: * 2. The file located in `node_modules/react-native/sdks/hermes/build/bin/hermesc`. This will be
173:      File(pathOverride, "build/bin/hermesc")
196:    "node_modules/react-native/ReactAndroid/hermes-engine/build/hermes/bin/hermesc"

template/android/app/build.gradle
245:            // https://developer.android.com/studio/build/configure-apk-splits.html

template/_gitignore
7:build/
27:build/

tools/build_defs/oss/rn_defs.bzl
41:GLOG_DEP = "//ReactAndroid/build/third-party-ndk/glog:glog"

bots/report-bundle-size.js
169:    `packages/rn-tester/android/app/build/outputs/apk/${engine}/release/app-${engine}-${arch}-release.apk`,
234:            'packages/rn-tester/build/Build/Products/Release-iphonesimulator/RNTester.app/RNTester',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For iOS the build is already outside node_modules in ~/Library/Developer/Xcode/DerivedData afaik so I don’t do anything special. Codegen output is in /ios/build, but that doesn’t really cause problems.

I think it is fine to assume build path in certain cases when we control output, but in the case of android build directory it is possible to change via gradle so we should use that config value.

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @janicduplessis in 6ab7a99.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. Merged This PR has been merged. Platform: Android Android applications. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants