Skip to content

Commit 9a54f95

Browse files
committed
Add compatibility shims and enhance CMake configuration for Proton toolchain
- Introduced compatibility shims for simdutf and std::atomic_ref to handle API drift and provide necessary atomic wrappers. - Added CMake checks for library existence and updated include paths to use Proton's Abseil headers. - Created minimal shims for Linux systems lacking kernel UAPI headers. - Updated CMakeLists.txt to ensure proper linking and include directory management.
1 parent fee604d commit 9a54f95

File tree

7 files changed

+201
-39
lines changed

7 files changed

+201
-39
lines changed

CMakeLists.txt

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ include(GNUInstallDirs)
4949

5050
include(CheckPythonModuleExists)
5151
include(GenerateBuiltinsList)
52+
include(CheckLibraryExists)
5253

5354
check_python_module_exists(PYTHON_HAVE_MARKUPSAFE markupsafe)
5455

@@ -58,7 +59,7 @@ set(CMAKE_CXX_STANDARD 20)
5859
# endif()
5960

6061
if (NOT CMAKE_C_STANDARD)
61-
set(CMAKE_C_STANDARD 90)
62+
set(CMAKE_C_STANDARD 90)
6263
endif()
6364

6465
set(is-freebsd $<PLATFORM_ID:FreeBSD>)
@@ -100,6 +101,13 @@ option(V8_ENABLE_SPARKPLUG "Enable Sparkplug baseline JIT" ON)
100101
option(V8_ENABLE_TEMPORAL "Enable Temporal API support" OFF)
101102
option(V8_ENABLE_WEBASSEMBLY "Enable WebAssembly support" OFF)
102103

104+
# Detect presence of libatomic in the current toolchain/sysroot. Some
105+
# hermetic toolchains omit it; in that case, avoid linking -latomic.
106+
set(V8_HAVE_LIBATOMIC 0)
107+
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
108+
check_library_exists(atomic __atomic_fetch_add_8 "" V8_HAVE_LIBATOMIC)
109+
endif()
110+
103111
set(
104112
v8_defines
105113
$<${is-darwin}:V8_HAVE_TARGET_OS>
@@ -179,6 +187,10 @@ target_include_directories(v8-i18n-support
179187
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
180188
)
181189

190+
# Always use Proton's Abseil headers to avoid mismatches with vendored copy.
191+
set(V8_ABSEIL_INCLUDE_DIR "${proton_SOURCE_DIR}/contrib/abseil-cpp")
192+
message(STATUS "Force using Proton Abseil headers: ${V8_ABSEIL_INCLUDE_DIR}")
193+
182194
file(GLOB api-sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS v8/src/api/*.cc)
183195
if(V8_ENABLE_WEBASSEMBLY)
184196
file(GLOB asmjs-sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS v8/src/asmjs/*.cc)
@@ -419,17 +431,24 @@ if(enable-fPIC)
419431
target_compile_options(v8_base_without_compiler PRIVATE ${enable-fpic})
420432
endif()
421433

422-
target_include_directories(v8_base_without_compiler
423-
PRIVATE
424-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/v8/include>
425-
$<BUILD_INTERFACE:$<TARGET_PROPERTY:v8-bytecodes-builtin-list,INTERFACE_INCLUDE_DIRECTORIES>>
426-
${CMAKE_CURRENT_SOURCE_DIR}/v8
427-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp
428-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/highway/src
429-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
430-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/zlib
431-
${CMAKE_CURRENT_BINARY_DIR}
432-
)
434+
target_include_directories(v8_base_without_compiler
435+
PRIVATE
436+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/v8/include>
437+
$<BUILD_INTERFACE:$<TARGET_PROPERTY:v8-bytecodes-builtin-list,INTERFACE_INCLUDE_DIRECTORIES>>
438+
${CMAKE_CURRENT_SOURCE_DIR}/v8
439+
${V8_ABSEIL_INCLUDE_DIR}
440+
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/highway/src
441+
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
442+
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/zlib
443+
${CMAKE_CURRENT_BINARY_DIR}
444+
${CMAKE_CURRENT_SOURCE_DIR}/shims
445+
)
446+
447+
# Ensure compatibility shims are visible in C++ TUs for this lib.
448+
target_compile_options(v8_base_without_compiler PRIVATE
449+
$<$<COMPILE_LANGUAGE:CXX>:-include>
450+
$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/shims/compat_all.h>
451+
)
433452

434453
target_link_libraries(
435454
v8_base_without_compiler
@@ -484,7 +503,7 @@ target_include_directories(v8_compiler
484503
PRIVATE
485504
${CMAKE_CURRENT_BINARY_DIR}
486505
${CMAKE_CURRENT_SOURCE_DIR}/v8
487-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp
506+
${V8_ABSEIL_INCLUDE_DIR}
488507
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/highway/src
489508
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
490509
$<BUILD_INTERFACE:$<TARGET_PROPERTY:v8-bytecodes-builtin-list,INTERFACE_INCLUDE_DIRECTORIES>>
@@ -767,13 +786,13 @@ target_link_libraries(v8_libplatform PRIVATE v8_libbase)
767786

768787
add_library(v8_libsampler STATIC v8/src/libsampler/sampler.cc)
769788

770-
target_include_directories(v8_libsampler
771-
PRIVATE
772-
${CMAKE_CURRENT_SOURCE_DIR}/v8
773-
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
774-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp
775-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
776-
)
789+
target_include_directories(v8_libsampler
790+
PRIVATE
791+
${CMAKE_CURRENT_SOURCE_DIR}/v8
792+
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
793+
${V8_ABSEIL_INCLUDE_DIR}
794+
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
795+
)
777796

778797
target_compile_definitions(v8_libsampler PRIVATE $<${is-msvc}:_HAS_EXCEPTIONS=0>)
779798
target_compile_options(v8_libsampler PRIVATE ${disable-exceptions})
@@ -835,15 +854,16 @@ if(enable-fPIC)
835854
target_compile_options(v8_libbase PRIVATE ${enable-fpic})
836855
endif()
837856
target_include_directories(v8_libbase PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/v8)
838-
target_include_directories(v8_libbase PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp)
857+
target_include_directories(v8_libbase PRIVATE ${V8_ABSEIL_INCLUDE_DIR})
839858
target_include_directories(v8_libbase PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/highway/src)
859+
target_include_directories(v8_libbase PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
840860
target_sources(v8_libbase PRIVATE v8/third_party/highway/src/hwy/abort.cc)
841861
target_link_libraries(v8_libbase
842862
PRIVATE
843863
Threads::Threads
844864
$<${is-linux}:${CMAKE_DL_LIBS}>
845865
$<${is-linux}:rt>
846-
$<${is-linux}:atomic>
866+
$<$<AND:${is-linux},$<BOOL:${V8_HAVE_LIBATOMIC}>>:atomic>
847867
$<${is-win}:winmm>
848868
$<${is-win}:dbghelp>
849869
PUBLIC
@@ -868,7 +888,7 @@ if (NOT CMAKE_CROSSCOMPILING)
868888
PRIVATE
869889
${CMAKE_CURRENT_SOURCE_DIR}/v8
870890
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
871-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp
891+
${V8_ABSEIL_INCLUDE_DIR}
872892
)
873893

874894
target_link_libraries(bytecode_builtins_list_generator v8_libbase)
@@ -1000,15 +1020,15 @@ if(enable-fPIC)
10001020
endif()
10011021
target_link_libraries(v8_torque_generated PRIVATE v8-bytecodes-builtin-list)
10021022

1003-
target_include_directories(v8_torque_generated
1004-
PUBLIC
1005-
${CMAKE_CURRENT_BINARY_DIR}
1006-
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
1007-
${CMAKE_CURRENT_SOURCE_DIR}/v8
1008-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp
1009-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/highway/src
1010-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
1011-
)
1023+
target_include_directories(v8_torque_generated
1024+
PUBLIC
1025+
${CMAKE_CURRENT_BINARY_DIR}
1026+
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
1027+
${CMAKE_CURRENT_SOURCE_DIR}/v8
1028+
${V8_ABSEIL_INCLUDE_DIR}
1029+
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/highway/src
1030+
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/fp16/src/include
1031+
)
10121032

10131033
# we use torque here
10141034
set(_torque_cc_deps ${torque_dirs} ${torque_files_abs})
@@ -1053,7 +1073,7 @@ if (NOT CMAKE_CROSSCOMPILING)
10531073
target_include_directories(torque PRIVATE
10541074
${CMAKE_CURRENT_SOURCE_DIR}/v8
10551075
${CMAKE_CURRENT_SOURCE_DIR}/v8/include
1056-
${CMAKE_CURRENT_SOURCE_DIR}/v8/third_party/abseil-cpp
1076+
${V8_ABSEIL_INCLUDE_DIR}
10571077
)
10581078
target_link_libraries(torque PRIVATE v8_libbase)
10591079
endif()
@@ -1109,7 +1129,12 @@ add_library(ch_contrib::v8_libsampler ALIAS v8_libsampler )
11091129
add_library(ch_contrib::v8_initializers ALIAS v8_initializers )
11101130
add_library(ch_contrib::v8_torque_generated ALIAS v8_torque_generated )
11111131
# Abseil (vendored via v8/third_party/abseil-cpp) is required by V8 >=14.
1112-
add_subdirectory(v8/third_party/abseil-cpp EXCLUDE_FROM_ALL)
1132+
# Proton must provide Abseil; do not build V8's vendored copy to avoid
1133+
# duplicate targets and API drift.
1134+
if (NOT TARGET absl::core_headers)
1135+
message(FATAL_ERROR "Proton Abseil targets not found (absl::core_headers). Ensure contrib/abseil-cpp-cmake is included before v8-cmake.")
1136+
endif()
1137+
message(STATUS "Using Proton Abseil targets; skipping v8/third_party/abseil-cpp")
11131138

11141139
# simdutf (vendored via v8/third_party/simdutf) provides base64 helpers used by V8 builtins.
11151140
add_library(v8_simdutf STATIC v8/third_party/simdutf/simdutf.cpp)

shims/compat_all.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// Aggregated shims for V8 build under Proton toolchain
2+
#pragma once
3+
4+
#include "simdutf_compat.h"
5+
#include "std_atomic_ref_compat.h"
6+

shims/linux/auxvec.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Minimal shim for systems where kernel UAPI headers are not available
2+
// in the configured sysroot. Provides AT_HWCAP and AT_HWCAP2 constants
3+
// used by V8 to query getauxval()/auxv entries.
4+
#ifndef V8_SHIM_LINUX_AUXVEC_H_
5+
#define V8_SHIM_LINUX_AUXVEC_H_
6+
7+
#ifndef AT_HWCAP
8+
#define AT_HWCAP 16
9+
#endif
10+
11+
#ifndef AT_HWCAP2
12+
#define AT_HWCAP2 26
13+
#endif
14+
15+
#endif // V8_SHIM_LINUX_AUXVEC_H_
16+

shims/simdutf_compat.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// proton: starts
2+
/// Compatibility shims for simdutf API drift across versions.
3+
/// - Provide atomic_* wrappers when SIMDUTF_ATOMIC_REF is not available.
4+
/// - Ensure char16_t overloads are routed appropriately.
5+
#pragma once
6+
7+
#include "simdutf.h"
8+
9+
namespace simdutf {
10+
11+
#if !defined(SIMDUTF_ATOMIC_REF) || !(SIMDUTF_ATOMIC_REF)
12+
// atomic_base64_to_binary_safe is unavailable; map to non-atomic variant.
13+
inline result atomic_base64_to_binary_safe(const char *input, size_t length,
14+
char *output, size_t &outlen,
15+
base64_options options = base64_default,
16+
last_chunk_handling_options last_chunk_options = last_chunk_handling_options::loose,
17+
bool decode_up_to_bad_char = false) noexcept {
18+
return base64_to_binary_safe(input, length, output, outlen,
19+
options, last_chunk_options,
20+
decode_up_to_bad_char);
21+
}
22+
23+
inline result atomic_base64_to_binary_safe(const char16_t *input, size_t length,
24+
char *output, size_t &outlen,
25+
base64_options options = base64_default,
26+
last_chunk_handling_options last_chunk_options = last_chunk_handling_options::loose,
27+
bool decode_up_to_bad_char = false) noexcept {
28+
// Delegate to existing char16_t overload if present in this simdutf,
29+
// otherwise fall back to naive narrowing under the assumption that
30+
// base64 strings are ASCII (non-ASCII will be caught as invalid).
31+
#if defined(__cpp_char8_t) || 1
32+
return base64_to_binary_safe(input, length, output, outlen,
33+
options, last_chunk_options,
34+
decode_up_to_bad_char);
35+
#else
36+
// Unreachable for modern toolchains; kept for completeness.
37+
return {error_code::SUCCESS, 0};
38+
#endif
39+
}
40+
41+
inline size_t atomic_binary_to_base64(const char *input, size_t length,
42+
char *output,
43+
base64_options options = base64_default) noexcept {
44+
return binary_to_base64(input, length, output, options);
45+
}
46+
#endif // !SIMDUTF_ATOMIC_REF
47+
48+
} // namespace simdutf
49+
/// proton: ends
50+

shims/std_atomic_ref_compat.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// proton: starts
2+
/// Provide a minimal std::atomic_ref<T> shim when the standard library
3+
/// does not offer it (older libstdc++/libc++ or disabled feature macros).
4+
/// Only load() and store() are implemented as needed by V8 sources.
5+
#pragma once
6+
7+
#include <atomic>
8+
#include <type_traits>
9+
#include <cstdint>
10+
11+
namespace std {
12+
13+
#if !defined(__cpp_lib_atomic_ref) || (__cpp_lib_atomic_ref + 0) < 201806L
14+
template <class T>
15+
class atomic_ref {
16+
static_assert(std::is_trivially_copyable<T>::value,
17+
"atomic_ref requires trivially copyable T");
18+
19+
public:
20+
using value_type = T;
21+
22+
explicit atomic_ref(T& obj) noexcept : ptr_(&obj) {}
23+
atomic_ref(T&&) = delete;
24+
atomic_ref(const atomic_ref&) = default;
25+
atomic_ref& operator=(const atomic_ref&) = default;
26+
27+
void store(T desired,
28+
std::memory_order order = std::memory_order_seq_cst) const
29+
noexcept {
30+
__atomic_store(ptr_, &desired, order_to_gcc(order));
31+
}
32+
33+
T load(std::memory_order order = std::memory_order_seq_cst) const noexcept {
34+
T out;
35+
__atomic_load(ptr_, &out, order_to_gcc(order));
36+
return out;
37+
}
38+
39+
private:
40+
static int order_to_gcc(std::memory_order mo) noexcept {
41+
switch (mo) {
42+
case std::memory_order_relaxed:
43+
return __ATOMIC_RELAXED;
44+
case std::memory_order_consume:
45+
return __ATOMIC_CONSUME;
46+
case std::memory_order_acquire:
47+
return __ATOMIC_ACQUIRE;
48+
case std::memory_order_release:
49+
return __ATOMIC_RELEASE;
50+
case std::memory_order_acq_rel:
51+
return __ATOMIC_ACQ_REL;
52+
case std::memory_order_seq_cst:
53+
default:
54+
return __ATOMIC_SEQ_CST;
55+
}
56+
}
57+
58+
T* ptr_;
59+
};
60+
#endif // !__cpp_lib_atomic_ref
61+
62+
} // namespace std
63+
/// proton: ends
64+

v8/include/v8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "v8-version.h" // NOLINT(build/include_directory)
6262
#include "v8-wasm.h" // NOLINT(build/include_directory)
6363
#include "v8config.h" // NOLINT(build/include_directory)
64+
#include "libplatform/libplatform.h"
6465

6566
// We reserve the V8_* prefix for macros defined in V8 public API and
6667
// assume there are no name conflicts with the embedder's code.

v8/src/regexp/regexp-bytecodes-inl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static constexpr int kBytecodeAlignment = 4;
5151
// offset 1. All other operands are aligned to their own size if
5252
// they are "basic" types.
5353
template <RegExpBytecodeOperandType... operand_types>
54-
consteval auto CalculatePackedOffsets() {
54+
constexpr auto CalculatePackedOffsets() {
5555
constexpr int N = sizeof...(operand_types);
5656
constexpr std::array<uint8_t, N> kOperandSizes = {
5757
RegExpOperandTypeTraits<operand_types>::kSize...};
@@ -118,14 +118,14 @@ class RegExpBytecodeOperandsBase {
118118
using Traits = RegExpBytecodeOperandsTraits<OpTypes...>;
119119
static constexpr int kCount = Traits::kOperandCount;
120120
static constexpr int kTotalSize = Traits::kSize;
121-
static consteval int Index(Operand op) { return static_cast<uint8_t>(op); }
122-
static consteval int Size(Operand op) {
121+
static constexpr int Index(Operand op) { return static_cast<uint8_t>(op); }
122+
static constexpr int Size(Operand op) {
123123
return Traits::kOperandSizes[Index(op)];
124124
}
125-
static consteval int Offset(Operand op) {
125+
static constexpr int Offset(Operand op) {
126126
return Traits::kOperandOffsets[Index(op)];
127127
}
128-
static consteval RegExpBytecodeOperandType Type(Operand op) {
128+
static constexpr RegExpBytecodeOperandType Type(Operand op) {
129129
return Traits::kOperandTypes[Index(op)];
130130
}
131131

0 commit comments

Comments
 (0)