Skip to content

Commit

Permalink
Simplify logic to choose if we need to build hermes from source or no…
Browse files Browse the repository at this point in the history
…t. (#34232)

Summary:
Pull Request resolved: #34232

This diff simplify the logic to decide whether we want to build hermes from source or not.

The requirement we have is that we don't want our users to build hermes.
So, we don't want to build hermes when there is a precompiled tarball available, while we want to build hermes in CI.

## Changelog

[General][Changed] - Build hermes when in CI and not when there is a tarball

Reviewed By: cortinico

Differential Revision: D37999748

fbshipit-source-id: 7d9cab51c37fb47db216055c057a22081e10df07
  • Loading branch information
Riccardo Cipolleschi authored and facebook-github-bot committed Jul 22, 2022
1 parent 087f142 commit 4b51207
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 68 deletions.
62 changes: 2 additions & 60 deletions scripts/hermes/hermes-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {execSync} = require('child_process');

const SDKS_DIR = path.normalize(path.join(__dirname, '..', '..', 'sdks'));
const HERMES_DIR = path.join(SDKS_DIR, 'hermes');
const DEFAULT_HERMES_TAG = 'main';
const HERMES_TAG_FILE_PATH = path.join(SDKS_DIR, '.hermesversion');
const HERMES_TARBALL_BASE_URL = 'https://github.com/facebook/hermes/tarball/';
const HERMES_TARBALL_DOWNLOAD_DIR = path.join(SDKS_DIR, 'download');
Expand Down Expand Up @@ -162,65 +161,8 @@ function isTestingAgainstLocalHermesTarball() {
return 'HERMES_ENGINE_TARBALL_PATH' in process.env;
}

function isOnAReactNativeReleaseBranch() {
try {
let currentBranch = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim();
let currentRemote = execSync('git config --get remote.origin.url')
.toString()
.trim();
return (
currentBranch.endsWith('-stable') &&
currentRemote.endsWith('facebook/react-native.git')
);
} catch (error) {
// If not inside a git repo, we're going to fail here and return.
return false;
}
}

function isOnAReactNativeReleaseTag() {
try {
// If on a named tag, this method will return the tag name.
// If not, it will throw as the return code is not 0.
execSync('git describe --exact-match HEAD', {stdio: 'ignore'});
} catch (error) {
return false;
}
let currentRemote = execSync('git config --get remote.origin.url')
.toString()
.trim();
return currentRemote.endsWith('facebook/react-native.git');
}

function isRequestingLatestCommitFromHermesMainBranch() {
const hermesTag = readHermesTag();
return hermesTag === DEFAULT_HERMES_TAG;
}

function isPRAgainstStable(pullRequest) {
if (pullRequest == null) {
return false;
}

const prComponents = pullRequest.split('/');
const prNumber = prComponents[prComponents.length - 1];
const apiURL = `https://api.github.com/repos/facebook/react-native/pulls/${prNumber}`;
const prJson = JSON.parse(execSync(`curl ${apiURL}`).toString());
const baseBranch = prJson.base.label;

return baseBranch.endsWith('-stable');
}

function shouldBuildHermesFromSource(pullRequest) {
return (
!isTestingAgainstLocalHermesTarball() &&
(isOnAReactNativeReleaseBranch() ||
isOnAReactNativeReleaseTag() ||
isPRAgainstStable(pullRequest) ||
isRequestingLatestCommitFromHermesMainBranch())
);
function shouldBuildHermesFromSource(isInCI) {
return !isTestingAgainstLocalHermesTarball() && isInCI;
}

function shouldUsePrebuiltHermesC(os) {
Expand Down
11 changes: 5 additions & 6 deletions scripts/hermes/prepare-hermes-for-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ const {
shouldBuildHermesFromSource,
} = require('./hermes-utils');

async function main(pullRequest) {
if (!shouldBuildHermesFromSource(pullRequest)) {
async function main(isInCI) {
if (!shouldBuildHermesFromSource(isInCI)) {
copyPodSpec();
return;
}

downloadHermesTarball();
expandHermesTarball();
copyPodSpec();
Expand All @@ -40,8 +39,8 @@ async function main(pullRequest) {
}
}

const pullRequest = process.argv.length > 2 ? process.argv[2] : null;
console.log(`Pull request detected: ${pullRequest}`);
main(pullRequest).then(() => {
const isInCI = process.env.CI === 'true';

main(isInCI).then(() => {
process.exit(0);
});
5 changes: 3 additions & 2 deletions sdks/hermes-engine/hermes-engine.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import_hermesc_file=File.join(__dir__, "..", "hermesc", "osx-bin", "ImportHermes
package_file = File.join(__dir__, "..", "..", "package.json")
package = JSON.parse(File.read(package_file))
version = package['version']
isInCI = ENV['CI'] == true
hermestag_file = File.join(__dir__, "..", ".hermesversion")

# We need to check the current git branch/remote to verify if
# we're on a React Native release branch to actually build Hermes.
Expand All @@ -29,9 +31,8 @@ elsif version == '1000.0.0'
Pod::UI.puts '[Hermes] Hermes needs to be compiled, installing hermes-engine may take a while...'.yellow if Object.const_defined?("Pod::UI")
source[:git] = git
source[:commit] = `git ls-remote https://github.com/facebook/hermes main | cut -f 1`.strip
elsif currentremote.strip.end_with?("facebook/react-native.git") and currentbranch.strip.end_with?("-stable")
elsif File.exists?(hermestag_file) && isInCI
Pod::UI.puts '[Hermes] Detected that you are on a React Native release branch, building Hermes from source...'.yellow if Object.const_defined?("Pod::UI")
hermestag_file = File.join(__dir__, "..", ".hermesversion")
hermestag = File.read(hermestag_file).strip
source[:git] = git
source[:tag] = hermestag
Expand Down

0 comments on commit 4b51207

Please sign in to comment.