Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions API/hermes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ target_link_options(libhermes PRIVATE ${HERMES_EXTRA_LINKER_FLAGS})
target_include_directories(libhermes PUBLIC .. ../../public ${HERMES_COMMON_DIR}/public ${HERMES_JSI_DIR})

# Avoid becoming liblibhermes (and there's already a target called 'hermes')
# TODO: (vmoroz) Make conditional
#set_target_properties(libhermes PROPERTIES OUTPUT_NAME hermes)
set_target_properties(libhermes PROPERTIES OUTPUT_NAME hermes)

# Create a lean version of libhermes in the same way.
add_library(libhermes_lean ${api_sources})
Expand Down Expand Up @@ -165,24 +164,6 @@ if(APPLE AND HERMES_BUILD_APPLE_FRAMEWORK)
endif()
endif()

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
# Same as above, but for windows. Note that there is no equivalent of -fvisibility=default.
set(compile_flags "")

# Enable exeption
set(compile_flags "${compile_flags} /EHsc")

# Enable RTTI
set(compile_flags "${compile_flags} /GR")

# Generate PDBs
set(compile_flags "${compile_flags} /Zi")
endif()

set_target_properties(libhermes PROPERTIES
COMPILE_FLAGS "${compile_flags}"
)

install(TARGETS libhermes
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
Expand Down
39 changes: 18 additions & 21 deletions API/hermes_shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

set(HERMES_ENABLE_EH_RTTI ON)

# Windows-specific configuration for libhermes targets (defined in ../hermes)
if(MSVC)
# Enable exceptions and RTTI for libhermes and libhermes_lean
target_compile_options(libhermes PRIVATE /EHsc /GR)
target_compile_options(libhermes_lean PRIVATE /EHsc /GR)

# Keep libhermes.lib name on Windows (don't rename to hermes.lib)
# to avoid conflict with hermes.dll from libshared
set_target_properties(libhermes PROPERTIES OUTPUT_NAME libhermes)
endif()

add_subdirectory(inspector)

add_library(hermesRuntimeApi INTERFACE
Expand Down Expand Up @@ -53,31 +64,17 @@ target_include_directories(libshared PUBLIC
${REACT_NATIVE_SOURCE}/ReactCommon/runtimeexecutor
${REACT_NATIVE_SOURCE_72}/ReactCommon)

if(MSVC)
# Same as above, but for windows. Note that there is no equivalent of -fvisibility=default.
set(compile_flags "")

# Enable exception
set(compile_flags "${compile_flags} /EHsc")

# Enable RTTI
set(compile_flags "${compile_flags} /GR")
target_link_options(libshared PRIVATE ${HERMES_EXTRA_LINKER_FLAGS})

# Generate PDBs
set(compile_flags "${compile_flags} /Zi")
if(MSVC)
# Enable exceptions and RTTI (required for shared library)
target_compile_options(libshared PRIVATE /EHsc /GR)

# Temporary avoid the auto-vectorization optimization since VS 17.14.0 produces incorrect code.
target_compile_options(libshared PRIVATE $<$<CONFIG:Release>:/O1>)
endif()

target_link_options(libshared PRIVATE ${HERMES_EXTRA_LINKER_FLAGS})

set_target_properties(libshared PROPERTIES
COMPILE_FLAGS "${compile_flags}"

# To make sure that the resulting DLL name is hermes.dll
OUTPUT_NAME hermes
)

if(MSVC)
# Temporary avoid the auto-vectorization optimization since VS 17.14.0 produces incorrect code.
# Set /O1 only for Release configs using modern CMake target_compile_options.
target_compile_options(libshared PRIVATE $<$<CONFIG:Release>:/O1>)
endif()
21 changes: 0 additions & 21 deletions API/hermes_shared/inspector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ add_hermes_library(hermesinspector
${inspector_public_headers}
${CMAKE_CURRENT_BINARY_DIR}/version.rc)

#target_compile_features(hermesinspector PUBLIC cxx_std_20)

target_link_libraries(hermesinspector
hermesNodeApi
hermesPublic
Expand All @@ -139,22 +137,3 @@ target_include_directories(hermesinspector PUBLIC
${REACT_NATIVE_SOURCE}/ReactCommon
${REACT_NATIVE_SOURCE}/ReactCommon/runtimeexecutor
${REACT_NATIVE_SOURCE_72}/ReactCommon)

if (MSVC)
# Same as above, but for windows. Note that there is no equivalent of -fvisibility=default.
set(compile_flags " /await ")

# Assuming inspector don't use exceptions
set(compile_flags "${compile_flags} /EHs-c-")

# inspector use rtti
set(compile_flags "${compile_flags} /GR")

# Generate PDBs
set(compile_flags "${compile_flags} /Zi")

set_target_properties(hermesinspector PROPERTIES
COMPILE_FLAGS "${compile_flags}"
OUTPUT_NAME hermesinspector
)
endif()
3 changes: 0 additions & 3 deletions API/jsi/jsi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
# Turn on Error Handling in MSVC, otherwise objects are not destructed
# when they go out of scope due to exceptions.
list(APPEND jsi_compile_flags "/EHsc")
list(APPEND jsi_compile_flags "/Zi")
list(APPEND jsi_compile_flags "/Qspectre")
list(APPEND jsi_compile_flags "/sdl")
endif()
target_compile_options(jsi PRIVATE ${jsi_compile_flags})

Expand Down
16 changes: 6 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ if(POLICY CMP0022)
cmake_policy(SET CMP0022 NEW)
endif()

# Enable CMAKE_MSVC_RUNTIME_LIBRARY variable (CMake 3.15+)
if (POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif()

# Has to be set before `project` as per documentation
# https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_SYSROOT.html
set(CMAKE_OSX_SYSROOT ${HERMES_APPLE_TARGET_PLATFORM})
Expand Down Expand Up @@ -498,8 +503,7 @@ if (GCC_COMPATIBLE)
# Don't export symbols unless we explicitly say so
append("-fvisibility=hidden" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
# We downgrade 4149 to level 3 to keep this as a warning because the default level 2 is bumped to error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w34146")
# Note: Windows-specific flags (security, DLL warnings) are in HermesWindows.cmake
# C4068 unknown pragma
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4068")
# C4200 nonstandard extension used: zero-sized array in struct/union
Expand All @@ -508,14 +512,6 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4201")
# C4530 C++ exception handler used, but unwind semantics are not enabled
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4530")
# C4251 class X needs to have dll-interface to be used by clients of class Y
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4251")
# C4275: non dll-interface class X used as base for dll-interface class Y
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4275")
# C4646: function declared with 'noreturn' has non-void return type
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4646")
# C4312: 'reinterpret_cast': conversion from 'X' to 'hermes::vm::GCCell *' of greater size
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4312")
# Parallelize build
if (HERMES_MSVC_MP)
add_compile_options( /MP )
Expand Down
27 changes: 4 additions & 23 deletions cmake/modules/Hermes.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ function(hermes_update_compile_flags name)

set(flags "")

if (MSVC)
# enable function-level linking
set(flags "${flags} /Gy")

# Ensure debug symbols are generated for all sources.
set(flags "${flags} /Zi")
endif ()

if (NOT HERMES_ENABLE_EH_RTTI)
if (GCC_COMPATIBLE)
set(flags "${flags} -fno-exceptions -fno-rtti")
Expand All @@ -95,8 +87,7 @@ function(hermes_update_compile_flags name)
if (update_src_props)
foreach (fn ${sources})
get_filename_component(suf ${fn} EXT)
# TODO: (vmoroz) It seems that we must remove the ".c" extension from here.
if ("${suf}" STREQUAL ".cpp" OR "${suf}" STREQUAL ".c")
if ("${suf}" STREQUAL ".cpp")
set_property(SOURCE ${fn} APPEND_STRING PROPERTY
COMPILE_FLAGS "${flags}")
endif ()
Expand All @@ -106,12 +97,6 @@ function(hermes_update_compile_flags name)
set_property(TARGET ${name} APPEND_STRING PROPERTY
COMPILE_FLAGS "${flags}")
endif ()

if (MSVC)
# Temporary avoid the auto-vectorization optimization since VS 17.14.0 produces incorrect code.
# Set /O1 only for Release configs using modern CMake target_compile_options.
target_compile_options(${name} PRIVATE $<$<CONFIG:Release>:/O1>)
endif ()
endfunction()

function(add_hermes_library name)
Expand Down Expand Up @@ -273,17 +258,13 @@ if (MSVC)

if (NOT CLANG_CL)
set(msvc_warning_flags
# SDL requires these 3 checks enabled.
# We downgrade 4149 to level 3 to keep this as a warning because the default level 2 is bumped to error
-w34146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
# We don't disable the other two
#-wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
#-wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'

# Disabled warnings.
-wd4141 # Suppress ''modifier' : used more than once' (because of __forceinline combined with inline)
-wd4146 # Suppress 'unary minus operator applied to unsigned type, result still unsigned'
-wd4180 # Suppress 'qualifier applied to function type has no meaning; ignored'
-wd4244 # Suppress ''argument' : conversion from 'type1' to 'type2', possible loss of data'
-wd4258 # Suppress ''var' : definition from the for loop is ignored; the definition from the enclosing scope is used'
-wd4267 # Suppress ''var' : conversion from 'size_t' to 'type', possible loss of data'
-wd4291 # Suppress ''declaration' : no matching operator delete found; memory will not be freed if initialization throws an exception'
-wd4345 # Suppress 'behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized'
-wd4351 # Suppress 'new behavior: elements of array 'array' will be default initialized'
Expand Down
74 changes: 68 additions & 6 deletions cmake/modules/HermesWindows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ if (HERMES_MSVC_USE_PLATFORM_UNICODE_WINGLOB)
add_definitions(-DUSE_PLATFORM_UNICODE_WINGLOB)
endif()

# Configure Hybrid CRT for Windows builds (must be before any targets)
# Uses static C++ runtime with dynamic Universal CRT to avoid MSVCP140.dll dependency
# See for details: https://github.com/microsoft/WindowsAppSDK/blob/main/docs/Coding-Guidelines/HybridCRT.md
# UWP (WindowsStore) targets do not support the hybrid runtime configuration, so let
# them fall back to the toolchain default (dynamic CRT) to keep the ABI contract.
if(NOT CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

# Enable source linking
if ("${CMAKE_C_COMPILER_ID}" MATCHES "MSVC")
# NOTE: Dependencies are not properly setup here.
Expand Down Expand Up @@ -85,12 +94,41 @@ endfunction()

# Configure MSVC compiler flags
function(hermes_windows_configure_msvc_flags)
# Debug information
set(MSVC_CXX_FLAGS "/Zi")
set(MSVC_CXX_FLAGS "")

# Enable function-level linking (enables /OPT:REF linker optimization)
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /Gy")

# Debug information
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /Zi")

# Security flags
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /GS /DYNAMICBASE /guard:cf /Qspectre /sdl /ZH:SHA_256")

# SDL (Security Development Lifecycle) requires these warnings enabled:
# Note: Upstream Hermes.cmake disables these with -wd flags, but we override them here
# for Windows security compliance. Last flag wins in MSVC.

# C4146: Re-enable 'unary minus operator applied to unsigned type, result still unsigned'
# Downgrade from error to warning level 3 (promoted to error by /sdl)
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /w34146")
# C4244: Re-enable 'conversion from type1 to type2, possible loss of data'
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /w44244")
# C4267: Re-enable 'conversion from size_t to type, possible loss of data'
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /w44267")

# Windows-specific warning suppressions for DLL builds

# C4251: class X needs to have dll-interface to be used by clients of class Y
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /wd4251")
# C4275: non dll-interface class X used as base for dll-interface class Y
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /wd4275")
# C4646: function declared with 'noreturn' has non-void return type
set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /wd4646")
# C4312: 'reinterpret_cast': conversion from 'X' to 'hermes::vm::GCCell *' of greater size
Copy link
Member

Choose a reason for hiding this comment

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

seems scary to suppress this one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree, but I had no choice before. I cannot affect the code from Meta. I can try to remove it to see how it behaves today, Or at least change it to a warning instead of error.

set(MSVC_CXX_FLAGS "${MSVC_CXX_FLAGS} /wd4312")

# Apply flags to both C and C++
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MSVC_CXX_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MSVC_CXX_FLAGS}" PARENT_SCOPE)
endfunction()
Expand All @@ -115,18 +153,38 @@ function(hermes_windows_configure_lld_flags)

# Deterministic builds
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:/BREPRO")

# Hybrid CRT: Remove static UCRT, add dynamic UCRT
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:/NODEFAULTLIB:libucrt.lib")
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:/DEFAULTLIB:ucrt.lib")
else()
# Hybrid CRT Debug: Remove static debug UCRT, add dynamic debug UCRT
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:/NODEFAULTLIB:libucrtd.lib")
list(APPEND HERMES_EXTRA_LINKER_FLAGS "LINKER:/DEFAULTLIB:ucrtd.lib")
endif()

set(HERMES_EXTRA_LINKER_FLAGS "${HERMES_EXTRA_LINKER_FLAGS}" PARENT_SCOPE)
endfunction()

# Configure MSVC linker flags
function(hermes_windows_configure_msvc_linker_flags)
# Debug information
list(APPEND MSVC_DEBUG_LINKER_FLAGS "LINKER:/DEBUG:FULL")
# Debug information (common to debug and release)
list(APPEND MSVC_COMMON_LINKER_FLAGS "LINKER:/DEBUG:FULL")

list(APPEND MSVC_RELEASE_LINKER_FLAGS "${MSVC_DEBUG_LINKER_FLAGS}")
# Debug-specific flags
set(MSVC_DEBUG_LINKER_FLAGS "${MSVC_COMMON_LINKER_FLAGS}")

# Hybrid CRT: Remove static UCRT, add dynamic UCRT
list(APPEND MSVC_DEBUG_LINKER_FLAGS "LINKER:/NODEFAULTLIB:libucrtd.lib")
list(APPEND MSVC_DEBUG_LINKER_FLAGS "LINKER:/DEFAULTLIB:ucrtd.lib")

# Release-specific flags
set(MSVC_RELEASE_LINKER_FLAGS "${MSVC_COMMON_LINKER_FLAGS}")

# Hybrid CRT: Remove static UCRT, add dynamic UCRT
list(APPEND MSVC_RELEASE_LINKER_FLAGS "LINKER:/NODEFAULTLIB:libucrt.lib")
list(APPEND MSVC_RELEASE_LINKER_FLAGS "LINKER:/DEFAULTLIB:ucrt.lib")

# Security flags
list(APPEND MSVC_RELEASE_LINKER_FLAGS "LINKER:/ZH:SHA_256")
list(APPEND MSVC_RELEASE_LINKER_FLAGS "LINKER:/guard:cf")
Expand All @@ -142,7 +200,11 @@ function(hermes_windows_configure_msvc_linker_flags)
# Deterministic builds
list(APPEND MSVC_RELEASE_LINKER_FLAGS "LINKER:/BREPRO")

set(HERMES_EXTRA_LINKER_FLAGS "$<IF:$<CONFIG:Release>,${MSVC_RELEASE_LINKER_FLAGS},${MSVC_DEBUG_LINKER_FLAGS}>" PARENT_SCOPE)
set(HERMES_EXTRA_LINKER_FLAGS
"$<$<CONFIG:Debug>:${MSVC_DEBUG_LINKER_FLAGS}>"
"$<$<CONFIG:Release>:${MSVC_RELEASE_LINKER_FLAGS}>"
PARENT_SCOPE
)
endfunction()

# Main configuration function
Expand Down
Loading