Hermes: Use pre-built artifacts in hermes-engine

Summary:
Update `hermes-engine.podspec` to use pre-built Hermes artifacts from the corresponding React Native GitHub Release when targeting a specific React Native release.
Otherwise, fallback to building Hermes from source.

Changelog: [Internal]

Reviewed By: cortinico, cipolleschi

Differential Revision: D36609257

fbshipit-source-id: 6179c9e255558c7eaf1417ff46a2e7db120295f0
This commit is contained in:
Héctor Ramos
2022-05-30 15:07:56 +02:00
committed by fortmarek
parent ea6706e166
commit 6b1e193f15
5 changed files with 127 additions and 88 deletions
+29 -31
View File
@@ -11,6 +11,7 @@ import * as path from 'path';
const {
copyBuildScripts,
copyPodSpec,
downloadHermesTarball,
expandHermesTarball,
getHermesTagSHA,
@@ -193,45 +194,42 @@ describe('hermes-utils', () => {
it('should fail if Hermes tarball does not exist', () => {
expect(() => {
expandHermesTarball();
}).toThrow('[Hermes] Failed to expand Hermes tarball.');
}).toThrow('[Hermes] Could not locate Hermes tarball.');
expect(execCalls.tar).toBeUndefined();
});
});
describe('copyBuildScripts', () => {
it('should copy React Native Hermes build scripts to Hermes source directory', () => {
fs.mkdirSync(path.join(SDKS_DIR, 'hermes', 'utils'), {
recursive: true,
});
copyBuildScripts();
expect(
fs.readFileSync(
path.join(
ROOT_DIR,
'sdks',
'hermes',
'utils',
'build-mac-framework.sh',
[
'build-apple-framework.sh',
'build-ios-framework.sh',
'build-mac-framework.sh',
].forEach(buildScript => {
expect(
fs.readFileSync(
path.join(ROOT_DIR, 'sdks', 'hermes', 'utils', buildScript),
{
encoding: 'utf8',
flag: 'r',
},
),
{
encoding: 'utf8',
flag: 'r',
},
),
).toEqual(
fs.readFileSync(
path.join(
ROOT_DIR,
'sdks',
'hermes-engine',
'utils',
'build-mac-framework.sh',
).toEqual(
fs.readFileSync(
path.join(ROOT_DIR, 'sdks', 'hermes-engine', 'utils', buildScript),
{
encoding: 'utf8',
flag: 'r',
},
),
{
encoding: 'utf8',
flag: 'r',
},
),
);
);
});
});
});
describe('copyPodSpec', () => {
it('should copy React Native Hermes Podspec to Hermes source directory', () => {
copyPodSpec();
expect(
fs.readFileSync(
path.join(SDKS_DIR, 'hermes', 'hermes-engine.podspec'),
+41 -27
View File
@@ -15,6 +15,7 @@ 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');
@@ -25,20 +26,6 @@ const MACOS_IMPORT_HERMESC_PATH = path.join(
'ImportHermesc.cmake',
);
function prepareFileSystem() {
if (!fs.existsSync(SDKS_DIR)) {
fs.mkdirSync(SDKS_DIR, {recursive: true});
}
if (!fs.existsSync(HERMES_DIR)) {
fs.mkdirSync(HERMES_DIR, {recursive: true});
}
if (!fs.existsSync(HERMES_TARBALL_DOWNLOAD_DIR)) {
fs.mkdirSync(HERMES_TARBALL_DOWNLOAD_DIR, {recursive: true});
}
}
function readHermesTag() {
if (fs.existsSync(HERMES_TAG_FILE_PATH)) {
const data = fs
@@ -64,8 +51,9 @@ function setHermesTag(hermesTag) {
return;
}
prepareFileSystem();
if (!fs.existsSync(SDKS_DIR)) {
fs.mkdirSync(SDKS_DIR, {recursive: true});
}
fs.writeFileSync(HERMES_TAG_FILE_PATH, hermesTag.trim());
console.log('Hermes tag has been updated. Please commit your changes.');
}
@@ -89,12 +77,14 @@ function downloadHermesTarball() {
const hermesTarballDownloadPath = getHermesTarballDownloadPath(hermesTag);
let hermesTarballUrl = HERMES_TARBALL_BASE_URL + hermesTag;
prepareFileSystem();
if (fs.existsSync(hermesTarballDownloadPath)) {
return;
}
if (!fs.existsSync(HERMES_TARBALL_DOWNLOAD_DIR)) {
fs.mkdirSync(HERMES_TARBALL_DOWNLOAD_DIR, {recursive: true});
}
console.info(
`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`,
);
@@ -110,12 +100,13 @@ function expandHermesTarball() {
const hermesTagSHA = getHermesTagSHA(hermesTag);
const hermesTarballDownloadPath = getHermesTarballDownloadPath(hermesTag);
prepareFileSystem();
if (!fs.existsSync(hermesTarballDownloadPath)) {
throw new Error(`[Hermes] Failed to expand Hermes tarball.`);
throw new Error('[Hermes] Could not locate Hermes tarball.');
}
if (!fs.existsSync(HERMES_DIR)) {
fs.mkdirSync(HERMES_DIR, {recursive: true});
}
console.info(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`);
try {
execSync(
@@ -127,16 +118,16 @@ function expandHermesTarball() {
}
function copyBuildScripts() {
if (!fs.existsSync(HERMES_DIR)) {
if (!fs.existsSync(SDKS_DIR)) {
throw new Error(
'[Hermes] Failed to copy Hermes build scripts, no Hermes source directory found.',
'[Hermes] Failed to copy Hermes build scripts, no SDKs directory found.',
);
}
fs.copyFileSync(
`${SDKS_DIR}/hermes-engine/hermes-engine.podspec`,
`${HERMES_DIR}/hermes-engine.podspec`,
);
if (!fs.existsSync(HERMES_DIR)) {
fs.mkdirSync(path.join(HERMES_DIR, 'utils'), {recursive: true});
}
fs.copyFileSync(
`${SDKS_DIR}/hermes-engine/utils/build-apple-framework.sh`,
`${HERMES_DIR}/utils/build-apple-framework.sh`,
@@ -151,6 +142,27 @@ function copyBuildScripts() {
);
}
function copyPodSpec() {
if (!fs.existsSync(SDKS_DIR)) {
throw new Error(
'[Hermes] Failed to copy Hermes Podspec, no SDKs directory found.',
);
}
if (!fs.existsSync(HERMES_DIR)) {
fs.mkdirSync(HERMES_DIR, {recursive: true});
}
fs.copyFileSync(
path.join(SDKS_DIR, 'hermes-engine', 'hermes-engine.podspec'),
path.join(HERMES_DIR, 'hermes-engine.podspec'),
);
}
function shouldBuildHermesFromSource() {
const hermesTag = readHermesTag();
return hermesTag === DEFAULT_HERMES_TAG;
}
function shouldUsePrebuiltHermesC(os) {
if (os === 'macos') {
return fs.existsSync(MACOS_HERMESC_PATH);
@@ -178,10 +190,12 @@ set_target_properties(native-hermesc PROPERTIES
module.exports = {
configureMakeForPrebuiltHermesC,
copyBuildScripts,
copyPodSpec,
downloadHermesTarball,
expandHermesTarball,
getHermesTagSHA,
readHermesTag,
setHermesTag,
shouldBuildHermesFromSource,
shouldUsePrebuiltHermesC,
};
+20 -6
View File
@@ -16,16 +16,30 @@
const {
configureMakeForPrebuiltHermesC,
copyBuildScripts,
copyPodSpec,
downloadHermesTarball,
expandHermesTarball,
shouldUsePrebuiltHermesC,
shouldBuildHermesFromSource,
} = require('./hermes-utils');
downloadHermesTarball();
expandHermesTarball();
copyBuildScripts();
async function main() {
if (!shouldBuildHermesFromSource()) {
copyPodSpec();
return;
}
if (shouldUsePrebuiltHermesC('macos')) {
console.log('[Hermes] Using pre-built HermesC');
configureMakeForPrebuiltHermesC();
downloadHermesTarball();
expandHermesTarball();
copyPodSpec();
copyBuildScripts();
if (shouldUsePrebuiltHermesC('macos')) {
console.log('[Hermes] Using pre-built HermesC');
configureMakeForPrebuiltHermesC();
}
}
main().then(() => {
process.exit(0);
});
+2
View File
@@ -113,6 +113,8 @@ def use_react_native! (options={})
pod 'React-hermes', :path => "#{prefix}/ReactCommon/hermes"
pod 'hermes-engine', :path => "#{prefix}/sdks/hermes/hermes-engine.podspec"
pod 'libevent', '~> 2.1.12'
system("(cd #{prefix} && node scripts/hermes/prepare-hermes-for-build)")
pod 'hermes-engine', :path => "#{prefix}/sdks/hermes/hermes-engine.podspec"
end
pods_to_update = LocalPodspecPatch.pods_to_update(options)