From b186046dfc142a1dee91a2a2762f65a163431251 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <hramos@fb.com>
Date: Fri, 20 May 2022 12:01:20 -0700
Subject: [PATCH 1/3] Hermes: Use pre-built hermes-engine if
 hermes-artifact-url is set

Differential Revision: D36532561

fbshipit-source-id: dea7bdfa3022f68bd52155d603da82bc92c93c0e
---
 sdks/hermes-engine/hermes-engine.podspec | 68 ++++++++++++++++--------
 1 file changed, 46 insertions(+), 22 deletions(-)

diff --git a/sdks/hermes-engine/hermes-engine.podspec b/sdks/hermes-engine/hermes-engine.podspec
index 96f2bd5249a6..1bd4cdc55120 100644
--- a/sdks/hermes-engine/hermes-engine.podspec
+++ b/sdks/hermes-engine/hermes-engine.podspec
@@ -3,26 +3,48 @@
 # This source code is licensed under the MIT license found in the
 # LICENSE file in the root directory of this source tree.
 
-hermes_tag = 'main'
-hermes_tag_file = File.join(__dir__, ".hermesversion")
-if (File.exist?(hermes_tag_file))
-  hermes_tag = File.read(hermes_tag_file)
+require "json"
+
+# sdks/.hermesversion
+hermes_tag_file = File.join(__dir__, "..", ".hermesversion")
+
+# sdks/hermesc/osx-bin/ImportHermesc.cmake
+import_hermesc_file=File.join(__dir__, "..", "hermesc", "osx-bin", "ImportHermesc.cmake")
+
+# package.json
+package_file = File.join(__dir__, "..", "..", "package.json")
+package = JSON.parse(File.read(package_file))
+version = package['version']
+
+source = {}
+git = "https://github.com/facebook/hermes.git"
+building_from_source = false
+
+if ENV['hermes-artifact-url'] then
+  source[:http] = ENV['hermes-artifact-url']
+elsif File.exist?(hermes_tag_file) then
+  building_from_source = true
+  source[:git] = git
+  source[:tag] = File.read(hermes_tag_file)
+else
+  building_from_source = true
+  source[:git] = git
+  hermes_commit_sha = `git ls-remote https://github.com/facebook/hermes main | cut -f 1`.strip
+  source[:commit] = hermes_commit_sha
 end
-hermes_tag_sha = `git ls-remote https://github.com/facebook/hermes #{hermes_tag} | cut -f 1`.strip
-source = { :git => 'https://github.com/facebook/hermes.git', :commit => hermes_tag_sha }
 
 module HermesHelper
   # BUILD_TYPE = :debug
   BUILD_TYPE = :release
 end
 
-Pod::UI.puts '[Hermes] Hermes needs to be compiled, installing hermes-engine may take a while...'.yellow if Object.const_defined?("Pod::UI")
-
-import_hermesc_path=File.join(__dir__, "../hermesc/osx-bin/ImportHermesc.cmake")
+if building_from_source
+  Pod::UI.puts '[Hermes] Hermes needs to be compiled, installing hermes-engine may take a while...'.yellow if Object.const_defined?("Pod::UI")
+end
 
 Pod::Spec.new do |spec|
   spec.name        = "hermes-engine"
-  spec.version     = "1000.0.0-#{hermes_tag_sha.slice(0,6)}"
+  spec.version     = version
   spec.summary     = "Hermes is a small and lightweight JavaScript engine optimized for running React Native."
   spec.description = "Hermes is a JavaScript engine optimized for fast start-up of React Native apps. It features ahead-of-time static optimization and compact bytecode."
   spec.homepage    = "https://hermesengine.dev"
@@ -40,19 +62,21 @@ Pod::Spec.new do |spec|
 
   spec.xcconfig            = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++17", "CLANG_CXX_LIBRARY" => "compiler-default", "GCC_PREPROCESSOR_DEFINITIONS" => "HERMES_ENABLE_DEBUGGER=1" }
 
-  spec.prepare_command = <<-EOS
-    # When true, debug build will be used.
-    # See `build-apple-framework.sh` for details
-    DEBUG=#{HermesHelper::BUILD_TYPE == :debug}
+  unless ENV['hermes-artifact-url']
+    spec.prepare_command = <<-EOS
+      # When true, debug build will be used.
+      # See `build-apple-framework.sh` for details
+      DEBUG=#{HermesHelper::BUILD_TYPE == :debug}
 
-    # Set HERMES_OVERRIDE_HERMESC_PATH if pre-built HermesC is available
-    #{File.exist?(import_hermesc_path) ? "export HERMES_OVERRIDE_HERMESC_PATH=#{import_hermesc_path}" : ""}
-    #{File.exist?(import_hermesc_path) ? "echo \"Overriding HermesC path...\"" : ""}
+      # Set HERMES_OVERRIDE_HERMESC_PATH if pre-built HermesC is available
+      #{File.exist?(import_hermesc_file) ? "export HERMES_OVERRIDE_HERMESC_PATH=#{import_hermesc_file}" : ""}
+      #{File.exist?(import_hermesc_file) ? "echo \"Overriding HermesC path...\"" : ""}
 
-    # Build iOS framework
-    ./utils/build-ios-framework.sh
+      # Build iOS framework
+      ./utils/build-ios-framework.sh
 
-    # Build Mac framework
-    ./utils/build-mac-framework.sh
-  EOS
+      # Build Mac framework
+      ./utils/build-mac-framework.sh
+    EOS
+  end
 end

From 284979fb888e6f4cdcb00cc3f5ba1f5afa4f3902 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <hramos@fb.com>
Date: Fri, 20 May 2022 12:01:20 -0700
Subject: [PATCH 2/3] Hermes: Use published hermes-engine for tagged RN
 releases

Differential Revision: D36533683

fbshipit-source-id: f01bae367f8aed078525f297873cb4d68760ff72
---
 scripts/react_native_pods.rb | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb
index 52a18c6a3e2a..a0e16d4895b1 100644
--- a/scripts/react_native_pods.rb
+++ b/scripts/react_native_pods.rb
@@ -3,6 +3,7 @@
 # This source code is licensed under the MIT license found in the
 # LICENSE file in the root directory of this source tree.
 
+require 'json'
 require 'pathname'
 require_relative './react_native_pods_utils/script_phases.rb'
 require_relative './cocoapods/flipper.rb'
@@ -17,6 +18,8 @@
 DEFAULT_OTHER_CPLUSPLUSFLAGS = '$(inherited)'
 NEW_ARCH_OTHER_CPLUSPLUSFLAGS = '$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1'
 
+$START_TIME = Time.now.to_i
+
 def use_react_native! (options={})
   # The prefix to react-native
   prefix = options[:path] ||= "../node_modules/react-native"
@@ -102,10 +105,27 @@ def use_react_native! (options={})
   end
 
   if hermes_enabled
-    system("(cd #{prefix} && node scripts/hermes/prepare-hermes-for-build)")
     pod 'React-hermes', :path => "#{prefix}/ReactCommon/hermes"
-    pod 'hermes-engine', :path => "#{prefix}/sdks/hermes/hermes-engine.podspec"
     pod 'libevent', '~> 2.1.12'
+
+    sdks_dir = Pod::Config.instance.installation_root.join(prefix, "sdks")
+    hermes_tag_file = sdks_dir.join(".hermesversion")
+
+    if (File.exist?(hermes_tag_file))
+      # Use published pod with pre-builts.
+      Pod::UI.puts "[Hermes] Tag file exists at path: #{hermes_tag_file}"
+      package = JSON.parse(File.read(File.join(__dir__, "..", "package.json")))
+      hermes_version = package['version']
+      Pod::UI.puts "[Hermes] Loading version: #{hermes_version}"
+      pod 'hermes-engine', hermes_version
+    else
+      # Use local podspec and build from source.
+      path_to_hermes = "#{prefix}/sdks/hermes/hermes-engine.podspec"
+      Pod::UI.puts "[Hermes] Use local version from #{path_to_hermes}"
+      system("(cd #{prefix} && node scripts/hermes/prepare-hermes-for-build)")
+      pod 'hermes-engine', :path => path_to_hermes
+    end
+
   end
 
   pods_to_update = LocalPodspecPatch.pods_to_update(options)
@@ -224,6 +244,8 @@ def react_native_post_install(installer, react_native_path = "../node_modules/re
   modify_flags_for_new_architecture(installer, cpp_flags)
 
   set_node_modules_user_settings(installer, react_native_path)
+
+  puts "Pod install took #{Time.now.to_i - $START_TIME} [s] to run"
 end
 
 def modify_flags_for_new_architecture(installer, cpp_flags)

From 1427df7973ed902dbcf5594f1fcf8664fdcd727d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <hramos@fb.com>
Date: Fri, 20 May 2022 12:01:43 -0700
Subject: [PATCH 3/3] Circle CI: Build Hermes apple runtime artifacts on CI
 (#33876)

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

WIP. Published so we can export and test on CI.

These two jobs can likely be merged onto the existing build_hermesc_macos job.

Changelog: [Internal]

Differential Revision: D36538847

fbshipit-source-id: 15dad8def3e59e04a61cdf1ceb4fb8a0b4a02347
---
 .circleci/config.yml | 67 +++++++++++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 25 deletions(-)

diff --git a/.circleci/config.yml b/.circleci/config.yml
index afeeb6fee35f..03a8bf6d40f1 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -41,7 +41,7 @@ references:
     gems_cache_key: &gems_cache_key v1-gems-{{ checksum "Gemfile.lock" }}
     gradle_cache_key: &gradle_cache_key v1-gradle-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "ReactAndroid/gradle.properties" }}
     hermes_cache_key: &hermes_cache_key v1-hermes-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/hermes/hermesversion" }}
-    hermes_sdk_cache_key: &hermes_sdk_cache_key v1-hermes-{{ .Environment.CIRCLE_JOB }}-{{ checksum "sdks/.hermesversion" }}
+    hermes_sdk_cache_key: &hermes_sdk_cache_key v1-hermes-{{ .Environment.CIRCLE_JOB }}-{{ checksum "sdks/.hermes-cache-key-file" }}
     hermes_windows_cache_key: &hermes_windows_cache_key v1-hermes-{{ .Environment.CIRCLE_JOB }}-{{ checksum "tmp/hermes/hermesversion" }}
     pods_cache_key: &pods_cache_key v6-pods-{{ .Environment.CIRCLE_JOB }}-{{ checksum "packages/rn-tester/Podfile.lock.bak" }}-{{ checksum "packages/rn-tester/Podfile" }}
     windows_yarn_cache_key: &windows_yarn_cache_key v1-win-yarn-cache-{{ arch }}-{{ checksum "yarn.lock" }}
@@ -278,9 +278,9 @@ commands:
       - run:
           name: Setup Hermes cache
           command: |
-            HERMES_VERSION_FILE="sdks/.hermesversion"
-            if [ ! -f "$HERMES_VERSION_FILE" ]; then
-              git ls-remote https://github.com/facebook/hermes main | cut -f 1 > $HERMES_VERSION_FILE
+            HERMES_CACHE_KEY_FILE="sdks/.hermes-cache-key-file"
+            if [ ! -f "$HERMES_CACHE_KEY_FILE" ]; then
+              git ls-remote https://github.com/facebook/hermes main | cut -f 1 > $HERMES_CACHE_KEY_FILE
             fi
       - restore_cache:
           keys:
@@ -946,7 +946,7 @@ jobs:
           paths:
             - linux64-bin
 
-  build_hermesc_macos:
+  build_hermes_macos:
     executor: reactnativeios
     environment:
       - HERMES_WS_DIR: *hermes_workspace_root
@@ -959,28 +959,46 @@ jobs:
           name: Set up workspace
           command: |
             mkdir -p /tmp/hermes/osx-bin
-            cp ~/react-native/sdks/hermes-engine/utils/* "$HERMES_WS_DIR/hermes/utils/."
+            mkdir -p ~/react-native/sdks/hermes
+            cp -r $HERMES_WS_DIR/hermes/* ~/react-native/sdks/hermes/.
       - run:
           name: Install dependencies
           command: |
             brew install cmake
       - run:
-          name: Build HermesC for macOS
+          name: Build the Hermes iOS frameworks
           command: |
-            if [ -f /tmp/hermes/osx-bin/hermesc ]; then
-              echo 'Skipping; Clean "/tmp/hermes/osx-bin" to rebuild.'
-            else
-              cd "$HERMES_WS_DIR/hermes"
-              ./utils/build-mac-framework.sh
-              cp build_macosx/bin/hermesc /tmp/hermes/osx-bin/.
-            fi
+            cd ~/react-native/sdks/hermes
+            ./utils/build-ios-framework.sh
+      - run:
+          name: Build the Hermes Mac frameworks
+          command: |
+            cd ~/react-native/sdks/hermes
+            ./utils/build-mac-framework.sh
+            cp build_macosx/bin/hermesc /tmp/hermes/osx-bin/.
+      - run:
+          name: Package the Hermes Apple frameworks
+          command: |
+            cd ~/react-native/sdks/hermes
+            . ./utils/build-apple-framework.sh
+
+            mkdir -p /tmp/cocoapods-package-root/destroot
+            mkdir -p /tmp/hermes/output
+            cp -R ./destroot /tmp/cocoapods-package-root
+            cp hermes-engine.podspec LICENSE /tmp/cocoapods-package-root
+
+            tar -C /tmp/cocoapods-package-root/ -czvf /tmp/hermes/output/hermes-runtime-darwin-v$(get_release_version).tar.gz .
       - save_cache:
           key: *hermes_cache_key
           paths:
-            - /tmp/hermes/osx-bin/
-            - /tmp/hermes/hermes/destroot/
-            - /tmp/hermes/hermes/build_host_hermesc/
-            - /tmp/hermes/hermes/build_macosx/
+            - ~/react-native/hermes/build_host_hermesc
+            - ~/react-native/hermes/build_iphoneos
+            - ~/react-native/hermes/build_catalyst
+            - ~/react-native/hermes/build_iphonesimulator
+            - ~/react-native/hermes/build_macosx
+            - ~/react-native/hermes/destroot
+      - store_artifacts:
+          path: /tmp/hermes/output/
       - store_artifacts:
           path: /tmp/hermes/osx-bin/
       - persist_to_workspace:
@@ -1232,7 +1250,7 @@ workflows:
       - build_hermesc_linux:
           requires:
             - prepare_hermes_workspace
-      - build_hermesc_macos:
+      - build_hermes_macos:
           requires:
             - prepare_hermes_workspace
       - build_hermesc_windows:
@@ -1243,7 +1261,7 @@ workflows:
           publish_npm_args: --dry-run
           requires:
             - build_hermesc_linux
-            - build_hermesc_macos
+            - build_hermes_macos
             - build_hermesc_windows
       - test_js:
           run_disabled_tests: false
@@ -1290,7 +1308,7 @@ workflows:
           filters: *only_release_tags
           requires:
             - prepare_hermes_workspace
-      - build_hermesc_macos:
+      - build_hermes_macos:
           filters: *only_release_tags
           requires:
             - prepare_hermes_workspace
@@ -1306,7 +1324,7 @@ workflows:
           filters: *only_release_tags
           requires:
             - build_hermesc_linux
-            - build_hermesc_macos
+            - build_hermes_macos
             - build_hermesc_windows
 
   analysis:
@@ -1340,16 +1358,15 @@ workflows:
       - build_hermesc_linux:
           requires:
             - prepare_hermes_workspace
-      - build_hermesc_macos:
+      - build_hermes_macos:
           requires:
             - prepare_hermes_workspace
       - build_hermesc_windows:
           requires:
             - prepare_hermes_workspace
-
       - build_npm_package:
           publish_npm_args: --nightly
           requires:
             - build_hermesc_linux
-            - build_hermesc_macos
+            - build_hermes_macos
             - build_hermesc_windows
