Skip to content

Commit

Permalink
Static Hermes for React Native (#1566)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#48327

This PR introduces necessary changes to let React Native uses latest version of static Hermes.

## Explanation

### Part 1
```cmake
append("/d2UndefIntOverflow-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
```
It seems like this flag doesn’t exist anymore in the MSVC 16 compiler.
CI logs - https://github.com/piaskowyk/react-native/actions/runs/11815096269/job/32915591004
```
fatal error C1007: unrecognized flag '-UndefIntOverflow-' in 'p2'
```

### Part 2
```cmake
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
  # MSVC needs C++20
  set(CMAKE_CXX_STANDARD 20)
else()
  set(CMAKE_CXX_STANDARD 17)
endif()
```

Some of the new syntax in static Hermes requires the newer C++ standard on MSVC.

### Part 3
```cmake
# Changes in lib/CMakeLists.txt
```

These updates are necessary to successfully build the Hermes Framework for iOS.

### Part 4
```diff
namespace hermes {
namespace hbc {
namespace {

class BytecodeSerializer {
-  friend void visitBytecodeSegmentsInOrder<BytecodeSerializer>(
+  friend void hermes::hbc::visitBytecodeSegmentsInOrder<BytecodeSerializer>(
```

Due to additional additional anonymous namespace, the MSVC wasn't able to recognise proper symbol without explicite definition.

Pull Request resolved: #1566

Test Plan: Build RNTester app from here - https://github.com/piaskowyk/react-native/tree/%40piaskowyk/build-static-hermes

Reviewed By: tmikov, cipolleschi

Differential Revision: D67316013

Pulled By: neildhar

fbshipit-source-id: cf03850f94a75acd827b68794700a8f143a90e09
  • Loading branch information
piaskowyk authored and facebook-github-bot committed Dec 20, 2024
1 parent c09f350 commit ecfdb2f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 36 deletions.
3 changes: 0 additions & 3 deletions API/jsi/jsi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
# when they go out of scope due to exceptions.
list(APPEND jsi_compile_flags "/EHsc")
endif()
if (HERMES_ENABLE_BITCODE)
list(APPEND jsi_compile_flags "-fembed-bitcode")
endif ()
target_compile_options(jsi PRIVATE ${jsi_compile_flags})

install(DIRECTORY "${PROJECT_SOURCE_DIR}/API/jsi/" DESTINATION include
Expand Down
28 changes: 12 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ set(CMAKE_OSX_SYSROOT ${HERMES_APPLE_TARGET_PLATFORM})

if(HERMES_APPLE_TARGET_PLATFORM MATCHES "catalyst")
set(CMAKE_OSX_SYSROOT "macosx")
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-target x86_64-arm64-apple-ios14.0-macabi -isystem ${CMAKE_OSX_SYSROOT}/System/iOSSupport/usr/include")
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-target x86_64-arm64-apple-ios14.0-macabi -isystem ${CMAKE_OSX_SYSROOT}/System/iOSSupport/usr/include")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -target x86_64-arm64-apple-ios14.0-macabi -isystem ${CMAKE_OSX_SYSROOT}/System/iOSSupport/usr/include")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -target x86_64-arm64-apple-ios14.0-macabi -isystem ${CMAKE_OSX_SYSROOT}/System/iOSSupport/usr/include")
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
Expand Down Expand Up @@ -202,10 +202,6 @@ set(HERMES_ENABLE_FUZZILLI OFF CACHE BOOL
set(HERMES_ENABLE_TOOLS ON CACHE BOOL
"Enable CLI tools")

# Enable bitcode
set(HERMES_ENABLE_BITCODE OFF CACHE BOOL
"Include bitcode with the framework")

# Set linker flag for building the fuzzer
set(HERMES_FUZZING_FLAG "-fsanitize=fuzzer" CACHE STRING
"Linker argument to link fuzz targets against a given fuzzer.")
Expand Down Expand Up @@ -285,12 +281,9 @@ set(HERMES_ENABLE_UNICODE_REGEXP_PROPERTY_ESCAPES ON CACHE BOOL
set(HERMES_ENABLE_TEST_SUITE ON CACHE BOOL
"Enable the test suite")

set(HERMES_BUILD_APPLE_FRAMEWORK ON CACHE BOOL
set(HERMES_BUILD_APPLE_FRAMEWORK OFF CACHE BOOL
"Whether to build the libhermes target as a framework bundle or dylib on Apple platforms")

set(HERMES_BUILD_APPLE_DSYM OFF CACHE BOOL
"Whether to build a DWARF debugging symbols bundle")

set(HERMES_BUILD_NODE_HERMES OFF CACHE BOOL "Whether to build node-hermes")

set(HERMES_BUILD_LEAN_LIBHERMES OFF CACHE BOOL "Exclude the Hermes compiler from libhermes.")
Expand Down Expand Up @@ -333,10 +326,6 @@ if(HERMES_CHECK_NATIVE_STACK)
add_definitions(-DHERMES_CHECK_NATIVE_STACK)
endif()

if(HERMES_BUILD_APPLE_DSYM)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf")
endif()

if (HERMES_IS_MOBILE_BUILD)
add_definitions(-DHERMES_IS_MOBILE_BUILD)
endif()
Expand Down Expand Up @@ -584,7 +573,8 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
endif()
# MSVC does not have strict aliasing so there is no need to support it.
# Similar to GCC/Clang above, disable strict signed integer overflow.
append("/d2UndefIntOverflow-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
# TODO: Investigate disabling strict signed integer overflow for MSVC
# append("/d2UndefIntOverflow-" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
endif()

# Export a JSON file with the compilation commands that external tools can use
Expand Down Expand Up @@ -730,7 +720,13 @@ if(HERMES_IS_ANDROID)
find_package(fbjni REQUIRED CONFIG)
endif()

set(CMAKE_CXX_STANDARD 17)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
# MSVC requires c++20, because of the use of designated initializers in the codebase.
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Expand Down
4 changes: 2 additions & 2 deletions android/intltest/java/com/facebook/hermes/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if(HERMES_IS_ANDROID)
add_hermes_library(jsijni SHARED
${jni_source_files}
LINK_OBJLIBS
libhermes
hermesvm
hermesapi
compileJS
fbjni::fbjni
Expand All @@ -35,7 +35,7 @@ if(HERMES_IS_ANDROID)
add_hermes_library(jsijniepi SHARED
${epi_source_files}
LINK_OBJLIBS
libhermes
hermesvm
hermesapi
compileJS
fbjni::fbjni
Expand Down
6 changes: 0 additions & 6 deletions cmake/modules/Hermes.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ function(add_hermes_library name)
target_link_libraries(${name} ${ARG_LINK_OBJLIBS} ${ARG_LINK_LIBS})
set_property(TARGET ${name} PROPERTY POSITION_INDEPENDENT_CODE ON)
hermes_update_compile_flags(${name})
if (HERMES_ENABLE_BITCODE)
target_compile_options(${name} PUBLIC "-fembed-bitcode")
endif ()
else()
# When asking to link an OBJECT library (the default), we create an OBJECT
# library with a suffix _obj, which depends on all object libraries in
Expand All @@ -151,9 +148,6 @@ function(add_hermes_library name)
target_link_libraries(${name}_obj ${ARG_LINK_LIBS})
set_property(TARGET ${name}_obj PROPERTY POSITION_INDEPENDENT_CODE ON)
hermes_update_compile_flags(${name}_obj)
if (HERMES_ENABLE_BITCODE)
target_compile_options(${name}_obj PUBLIC "-fembed-bitcode")
endif ()

add_library(${name} STATIC)
target_link_libraries(${name} ${name}_obj ${ARG_LINK_OBJLIBS})
Expand Down
2 changes: 1 addition & 1 deletion lib/BCGen/HBC/BytecodeStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace hbc {
namespace {

class BytecodeSerializer {
friend void visitBytecodeSegmentsInOrder<BytecodeSerializer>(
friend void hermes::hbc::visitBytecodeSegmentsInOrder<BytecodeSerializer>(
BytecodeSerializer &);

/// Output Stream
Expand Down
39 changes: 39 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,45 @@ target_link_libraries(hermesvmlean_a PUBLIC
# CMake requires at least one file for the target, so create an empty file.
add_library(hermesvm SHARED dummy.cpp)
add_library(hermesvmlean SHARED dummy.cpp)

if(APPLE AND HERMES_BUILD_APPLE_FRAMEWORK)
set_target_properties(hermesvm PROPERTIES
FRAMEWORK TRUE
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION}
FRAMEWORK_VERSION ${PROJECT_VERSION_MAJOR}
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${PROJECT_VERSION}
MACOSX_FRAMEWORK_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_FRAMEWORK_IDENTIFIER dev.hermesengine.${HERMES_APPLE_TARGET_PLATFORM}
)
# Define the deployment target in the frameworks metadata
if(HERMES_APPLE_TARGET_PLATFORM MATCHES "iphone")
add_custom_command(TARGET hermesvm POST_BUILD
COMMAND /usr/libexec/PlistBuddy -c "Add :MinimumOSVersion string ${CMAKE_OSX_DEPLOYMENT_TARGET}" $<TARGET_FILE_DIR:hermesvm>/Info.plist
)
elseif(HERMES_APPLE_TARGET_PLATFORM MATCHES "appletv")
add_custom_command(TARGET hermesvm POST_BUILD
COMMAND /usr/libexec/PlistBuddy -c "Add :MinimumOSVersion string ${CMAKE_OSX_DEPLOYMENT_TARGET}" $<TARGET_FILE_DIR:hermesvm>/Info.plist
)
elseif(HERMES_APPLE_TARGET_PLATFORM MATCHES "catalyst")
add_custom_command(TARGET hermesvm POST_BUILD
COMMAND /usr/libexec/PlistBuddy -c "Add :LSMinimumSystemVersion string ${CMAKE_OSX_DEPLOYMENT_TARGET}" $<TARGET_FILE_DIR:hermesvm>/Resources/Info.plist
)
elseif(HERMES_APPLE_TARGET_PLATFORM MATCHES "macos")
add_custom_command(TARGET hermesvm POST_BUILD
COMMAND /usr/libexec/PlistBuddy -c "Add :LSMinimumSystemVersion string ${CMAKE_OSX_DEPLOYMENT_TARGET}" $<TARGET_FILE_DIR:hermesvm>/Resources/Info.plist
)
elseif(HERMES_APPLE_TARGET_PLATFORM MATCHES "xr")
if(CMAKE_VERSION VERSION_LESS 3.28.4)
message("VisionOS Simulator requires CMake version >= 3.28.4")
endif()

add_custom_command(TARGET hermesvm POST_BUILD
COMMAND /usr/libexec/PlistBuddy -c "Add :MinimumOSVersion string ${CMAKE_OSX_DEPLOYMENT_TARGET}" $<TARGET_FILE_DIR:hermesvm>/Info.plist
)
endif()
endif()

# Force the entire hermesvm_a library to be used by hermesvm. There is
# no cross platform way to do this (force_load is for macOS). Once we upgrade
# CMake 3.24, we can use the LINK_LIBRARY generator expression to do this.
Expand Down
11 changes: 3 additions & 8 deletions utils/build-apple-framework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,8 @@ function build_host_hermesc {

# Utility function to configure an Apple framework
function configure_apple_framework {
local build_cli_tools enable_bitcode
local build_cli_tools

if [[ $1 == appletvos || $1 == iphoneos || $1 == catalyst || $1 == visionos ]]; then
enable_bitcode="true"
else
enable_bitcode="false"
fi
if [[ $1 == macosx ]]; then
build_cli_tools="true"
else
Expand All @@ -75,9 +70,9 @@ function configure_apple_framework {
-DHERMES_ENABLE_LIBFUZZER:BOOLEAN=false \
-DHERMES_ENABLE_FUZZILLI:BOOLEAN=false \
-DHERMES_ENABLE_TEST_SUITE:BOOLEAN=false \
-DHERMES_ENABLE_BITCODE:BOOLEAN="$enable_bitcode" \
-DHERMES_BUILD_APPLE_FRAMEWORK:BOOLEAN=true \
-DHERMES_BUILD_APPLE_DSYM:BOOLEAN=true \
-DCMAKE_CXX_FLAGS="-gdwarf" \
-DCMAKE_C_FLAGS="-gdwarf" \
-DHERMES_ENABLE_TOOLS:BOOLEAN="$build_cli_tools" \
-DIMPORT_HERMESC:PATH="$PWD/build_host_hermesc/ImportHermesc.cmake" \
-DCMAKE_INSTALL_PREFIX:PATH=../destroot \
Expand Down

0 comments on commit ecfdb2f

Please sign in to comment.