From 3bc7618c853ba9c29221bb8fadc4ba90d23e763e Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 8 Apr 2024 13:54:50 +0530 Subject: [PATCH] Start building on Windows Signed-off-by: Darshan Sen --- .github/workflows/ci.yml | 5 ++ cmake/FindV8.cmake | 78 ++++++++++++++++++++++ src/engine/CMakeLists.txt | 9 +++ src/engine/v8/engine.cc | 16 ++++- test/packaging/find_package/CMakeLists.txt | 8 +++ 5 files changed, 115 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ad084f6..cdb034c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,11 @@ jobs: cxx: g++ type: shared backend: JavaScriptCore + - os: windows-latest + cc: gcc + cxx: g++ + type: static + backend: V8 # Sanitizers - os: ubuntu-latest diff --git a/cmake/FindV8.cmake b/cmake/FindV8.cmake index eb6cb15f..697553e7 100644 --- a/cmake/FindV8.cmake +++ b/cmake/FindV8.cmake @@ -23,5 +23,83 @@ if(NOT V8_FOUND) INTERFACE V8_ENABLE_SANDBOX) add_library(V8::V8 ALIAS v8) set(V8_FOUND ON) + elseif(WIN32) + include(FetchContent) + + set(V8_VERSION 12.3) + + # This downloads the include.tar.gz file from + # https://github.com/just-js/v8/releases/tag/12.3 + FetchContent_Declare( + v8_include + URL https://github.com/just-js/v8/releases/download/${V8_VERSION}/include.tar.gz + URL_HASH SHA256=cd6b891c30ecd28ccc254a90f33f92ac2d69f822fbfd54043ed74b57bf12c073 + DOWNLOAD_EXTRACT_TIMESTAMP YES # To silence a CMake warning. + ) + + # This downloads the libv8_monolith-win-x64.zip file from + # https://github.com/just-js/v8/releases/tag/12.3 + FetchContent_Declare( + v8_lib + URL https://github.com/just-js/v8/releases/download/${V8_VERSION}/libv8_monolith-win-x64.zip + URL_HASH SHA256=f80c7b3eb4580aa6fec5a8ec616fc9ada07acf7833d1b9c23b2586838e531960 + DOWNLOAD_EXTRACT_TIMESTAMP YES # To silence a CMake warning. + ) + + FetchContent_MakeAvailable(v8_include v8_lib) + + add_library(v8 INTERFACE) + + # These must be defined, otherwise V8 would crash because of assertion errors. + # Refs: https://github.com/just-js/lo/blob/16b5b5daa21f129007177eb4c9aa60b72c74b704/Makefile#L17 + target_compile_definitions(v8 INTERFACE + V8_COMPRESS_POINTERS + V8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=0 + # https://chromium-review.googlesource.com/c/v8/v8/+/5032576 introduced this + # line + # static_assert(I::IsValidSmi(std::numeric_limits::min())); + # which confuses the MSVC compiler and causes + # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4458?view=msvc-170. + # Refs: https://stackoverflow.com/a/1904659 + NOMINMAX) + + # The CMAKE_INSTALL_*DIR variables become undefined during the + # find_package(V8) call while running the packaging test, so we are using + # the hard coded string values instead. + target_include_directories(v8 INTERFACE + "$" + "$") + + install(DIRECTORY "${v8_include_SOURCE_DIR}/include" + DESTINATION "include/v8" + COMPONENT includejs_dev) + + install(FILES "${v8_lib_SOURCE_DIR}/v8_monolith.lib" + DESTINATION "lib" + COMPONENT includejs_dev) + + target_link_directories(v8 INTERFACE + "$" + "$") + + target_link_libraries(v8 INTERFACE + v8_monolith.lib + # V8 depends on the following system libraries, so these must be linked. + # Refs: https://github.com/just-js/lo/blob/16b5b5daa21f129007177eb4c9aa60b72c74b704/Makefile#L86 + winmm.lib + dbghelp.lib + advapi32.lib) + + install(TARGETS v8 + EXPORT v8 + PUBLIC_HEADER DESTINATION "include/v8" + COMPONENT includejs_dev + ARCHIVE DESTINATION "lib" + COMPONENT includejs_dev) + install(EXPORT v8 + DESTINATION "share/cmake/starship" + COMPONENT includejs_dev) + add_library(V8::V8 ALIAS v8) + set(V8_FOUND ON) endif() endif() diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index cfc5c6df..40aa7b85 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -1,3 +1,12 @@ +if(MSVC) + # We link to the v8_monolith.lib static library from just-js that is built in + # release mode with "RuntimeLibrary" set to "MT_StaticRelease", so we are + # adding the /MT compiler option for both the Debug and Release configs on + # Windows. + # Refs: https://stackoverflow.com/a/60844772 + add_compile_options(/MT) +endif() + set(INCLUDEJS_ENGINE_SOURCES common/engine.cc common/engine_context.cc diff --git a/src/engine/v8/engine.cc b/src/engine/v8/engine.cc index d6a2721b..996961fe 100644 --- a/src/engine/v8/engine.cc +++ b/src/engine/v8/engine.cc @@ -2,8 +2,22 @@ #include -#include +// This ignores the compiler warnings that get produced by the MSVC compiler +// while including the v8.h header file. +#if defined(_MSC_VER) +#pragma warning(push) +// warning C4100: '...': unreferenced formal parameter +#pragma warning(disable : 4100) +// warning C4127: conditional expression is constant +#pragma warning(disable : 4127) +// warning C4458: declaration of 'identifier' hides class member +#pragma warning(disable : 4458) +#endif #include +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +#include namespace includejs { diff --git a/test/packaging/find_package/CMakeLists.txt b/test/packaging/find_package/CMakeLists.txt index a80d027e..eeb791fe 100644 --- a/test/packaging/find_package/CMakeLists.txt +++ b/test/packaging/find_package/CMakeLists.txt @@ -3,6 +3,14 @@ project(includejs_hello VERSION 0.0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) +if(MSVC) + # We link to the v8_monolith.lib static library from just-js that is built in + # release mode with "RuntimeLibrary" set to "MT_StaticRelease", so we are + # adding the /MT compiler option for both the Debug and Release configs on + # Windows. + # Refs: https://stackoverflow.com/a/60844772 + add_compile_options(/MT) +endif() find_package(IncludeJS REQUIRED) add_executable(includejs_hello hello.cc) target_link_libraries(includejs_hello PRIVATE includejs::engine)