From 3463b323e151c5de6eaea19581ed780603cb2670 Mon Sep 17 00:00:00 2001 From: Channing Conger Date: Sat, 14 Mar 2026 06:10:42 +0000 Subject: [PATCH] Working v8 musl build --- MODULE.bazel | 23 ++++++++++++++++ third_party/v8/BUILD.bazel | 53 +++++++++++++++++++++++++++++++++++- third_party/v8/smoke_test.cc | 38 ++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 third_party/v8/smoke_test.cc diff --git a/MODULE.bazel b/MODULE.bazel index e24006f789..d3e1e34ee2 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -295,6 +295,10 @@ module.write_text(text) defs = Path("bazel/defs.bzl") text = defs.read_text() text = text.replace(' deps = [":define_flags", "@libcxx//:libc++"],\\n', ' deps = [":define_flags"],\\n') +text = text.replace( + ' "//conditions:default": ["-Wl,--no-as-needed -ldl -latomic -pthread"],\\n', + ' "//conditions:default": ["-Wl,--no-as-needed -ldl -pthread"],\\n', +) defs.write_text(text) build = Path("BUILD.bazel") @@ -351,6 +355,25 @@ text = text.replace( ) stack_trace.write_text(text) +platform_posix = Path("src/base/platform/platform-posix.cc") +text = platform_posix.read_text() +text = text.replace( + '#if defined(V8_LIBC_GLIBC)\\nextern "C" void* __libc_stack_end;\\n#endif\\n', + '', + 1, +) +text = text.replace( + '#if defined(V8_LIBC_GLIBC)\\n // pthread_getattr_np can fail for the main thread.\\n // For the main thread we prefer using __libc_stack_end (if it exists) since\\n // it generally provides a tighter limit for CSS.\\n return __libc_stack_end;\\n#else\\n return nullptr;\\n#endif // !defined(V8_LIBC_GLIBC)\\n', + ' return nullptr;\\n', + 1, +) +text = text.replace( + '#if defined(V8_LIBC_GLIBC)\\n // __libc_stack_end is process global and thus is only valid for\\n // the main thread. Check whether this is the main thread by checking\\n // __libc_stack_end is within the thread\\'s stack.\\n if ((base <= __libc_stack_end) && (__libc_stack_end <= stack_start)) {\\n DCHECK(MainThreadIsCurrentThread());\\n return __libc_stack_end;\\n }\\n#endif // !defined(V8_LIBC_GLIBC)\\n', + '', + 1, +) +platform_posix.write_text(text) + thread_isolated_allocator = Path("src/libplatform/default-thread-isolated-allocator.cc") text = thread_isolated_allocator.read_text() text = text.replace( diff --git a/third_party/v8/BUILD.bazel b/third_party/v8/BUILD.bazel index 20861b471f..550e950d5d 100644 --- a/third_party/v8/BUILD.bazel +++ b/third_party/v8/BUILD.bazel @@ -1,5 +1,5 @@ load("@rules_cc//cc:cc_static_library.bzl", "cc_static_library") -load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_import", "cc_library") package(default_visibility = ["//visibility:public"]) @@ -42,6 +42,57 @@ cc_library( ], ) +cc_library( + name = "v8_146_4_0_public_headers", + hdrs = ["@v8//:public_header_files"], + includes = ["../../external/v8+/include"], + deps = ["@v8//:rusty_v8_internal_headers"], +) + +cc_binary( + name = "v8_146_4_0_smoke_test", + srcs = ["smoke_test.cc"], + copts = select({ + "@v8//bazel/config:is_clang": ["-std=c++20"], + "@v8//bazel/config:is_gcc": ["-std=gnu++2a"], + "@v8//bazel/config:is_windows": ["/std:c++20"], + "//conditions:default": [], + }), + tags = ["manual"], + deps = [":v8_146_4_0_binding"], +) + +cc_import( + name = "v8_146_4_0_x86_64_unknown_linux_musl_archive", + static_library = ":v8_146_4_0_x86_64_unknown_linux_musl", +) + +cc_library( + name = "v8_146_4_0_x86_64_unknown_linux_musl_imported", + linkopts = [ + "-Wl,--no-as-needed", + "-ldl", + "-pthread", + ], + deps = [ + ":v8_146_4_0_x86_64_unknown_linux_musl_archive", + ":v8_146_4_0_public_headers", + ], +) + +cc_binary( + name = "v8_146_4_0_x86_64_unknown_linux_musl_imported_smoke_test", + srcs = ["smoke_test.cc"], + copts = select({ + "@v8//bazel/config:is_clang": ["-std=c++20"], + "@v8//bazel/config:is_gcc": ["-std=gnu++2a"], + "@v8//bazel/config:is_windows": ["/std:c++20"], + "//conditions:default": [], + }), + tags = ["manual"], + deps = [":v8_146_4_0_x86_64_unknown_linux_musl_imported"], +) + cc_static_library( name = "v8_146_4_0_aarch64_apple_darwin", deps = [":v8_146_4_0_binding"], diff --git a/third_party/v8/smoke_test.cc b/third_party/v8/smoke_test.cc new file mode 100644 index 0000000000..12b7990f77 --- /dev/null +++ b/third_party/v8/smoke_test.cc @@ -0,0 +1,38 @@ +#include + +#include "libplatform/libplatform.h" +#include "v8.h" + +int main(int argc, char* argv[]) { + v8::V8::InitializeICUDefaultLocation(argv[0]); + v8::V8::InitializeExternalStartupData(argv[0]); + std::unique_ptr platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); + v8::V8::Initialize(); + + v8::Isolate::CreateParams create_params; + create_params.array_buffer_allocator = + v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + v8::Isolate* isolate = v8::Isolate::New(create_params); + + int exit_code = 1; + { + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + v8::Local context = v8::Context::New(isolate); + v8::Context::Scope context_scope(context); + + v8::Local source = + v8::String::NewFromUtf8Literal(isolate, "1 + 2"); + v8::Local script = + v8::Script::Compile(context, source).ToLocalChecked(); + v8::Local result = script->Run(context).ToLocalChecked(); + exit_code = result->Int32Value(context).FromMaybe(-1) == 3 ? 0 : 1; + } + + isolate->Dispose(); + v8::V8::Dispose(); + v8::V8::DisposePlatform(); + delete create_params.array_buffer_allocator; + return exit_code; +}