Skip to content
Open
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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions cmake/FindV8.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>::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
"$<BUILD_INTERFACE:${v8_include_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/v8/include>")

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
"$<BUILD_INTERFACE:${v8_lib_SOURCE_DIR}>"
"$<INSTALL_INTERFACE:lib>")

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()
9 changes: 9 additions & 0 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/engine/v8/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

#include <iostream>

#include <libplatform/libplatform.h>
// 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 <v8.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#include <libplatform/libplatform.h>

namespace includejs {

Expand Down
8 changes: 8 additions & 0 deletions test/packaging/find_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)