Skip to content

Commit 72e855c

Browse files
authored
Migrate LLVM build from foreign_cc/CMake to native Bazel (#478)
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 6f72a85 commit 72e855c

File tree

6 files changed

+176
-525
lines changed

6 files changed

+176
-525
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/compile_commands.json
44
/.cache/
55
/_codeql_detected_source_root
6+
bazel/external/__pycache__/

bazel/dependencies.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@bazel_lib//lib:repositories.bzl", "bazel_lib_dependencies", "bazel_lib_re
1717
load("@com_google_googletest//:googletest_deps.bzl", "googletest_deps")
1818
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
1919
load("@envoy_toolshed//sysroot:sysroot.bzl", "setup_sysroots")
20+
load("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure")
2021
load("@proxy_wasm_cpp_host//bazel/cargo/wasmsign/remote:crates.bzl", wasmsign_crate_repositories = "crate_repositories")
2122
load("@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:crates.bzl", wasmtime_crate_repositories = "crate_repositories")
2223
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
@@ -107,6 +108,12 @@ def proxy_wasm_cpp_host_dependencies():
107108

108109
protobuf_deps()
109110

111+
# LLVM with native Bazel build (for WAMR JIT).
112+
# Configure LLVM using its native Bazel overlay from utils/bazel.
113+
# Only build X86 and AArch64 targets to minimize build size.
114+
# Note: LLVM external dependencies (llvm_zlib, llvm_zstd) are defined in repositories.bzl
115+
llvm_configure(name = "llvm-project", targets = ["X86", "AArch64"])
116+
110117
# Wasmtime dependencies.
111118

112119
wasmtime_crate_repositories()

bazel/external/wamr.BUILD

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@rules_cc//cc:defs.bzl", "cc_library")
1516
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
1617

1718
licenses(["notice"]) # Apache 2
@@ -24,7 +25,35 @@ filegroup(
2425
)
2526

2627
cmake(
27-
name = "wamr_lib",
28+
name = "wamr_lib_cmake",
29+
cache_entries = select({
30+
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": {
31+
"BAZEL_BUILD": "ON",
32+
# Set LLVM_INCLUDE_DIR for the patch to use
33+
"LLVM_INCLUDE_DIR": "$$EXT_BUILD_ROOT/external/llvm_toolchain_llvm/include",
34+
},
35+
"//conditions:default": {},
36+
}),
37+
# LLVM dependencies for JIT are provided via Bazel, not CMake
38+
# The patch skips LLVM CMake detection when BAZEL_BUILD is set
39+
# LLVM headers from hermetic toolchain (bzlmod-compatible via data attribute)
40+
# LLVM libraries are linked via cc_library deps (see wamr_lib below)
41+
data = select({
42+
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": [
43+
"@llvm_toolchain_llvm//:all_includes",
44+
],
45+
"//conditions:default": [],
46+
}),
47+
env = select({
48+
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": {
49+
# Reference LLVM headers in sandbox via EXT_BUILD_ROOT
50+
# The data attribute ensures llvm_toolchain_llvm is mounted in sandbox
51+
# This path works with both WORKSPACE and bzlmod
52+
"CFLAGS": "-isystem $$EXT_BUILD_ROOT/external/llvm_toolchain_llvm/include",
53+
"CXXFLAGS": "-isystem $$EXT_BUILD_ROOT/external/llvm_toolchain_llvm/include",
54+
},
55+
"//conditions:default": {},
56+
}),
2857
generate_args = [
2958
# disable WASI
3059
"-DWAMR_BUILD_LIBC_WASI=0",
@@ -47,7 +76,8 @@ cmake(
4776
"-GNinja",
4877
] + select({
4978
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": [
50-
"-DLLVM_DIR=$EXT_BUILD_DEPS/copy_llvm-19_1_0/llvm/lib/cmake/llvm",
79+
# WAMR's CMake will find LLVM via CMAKE_PREFIX_PATH
80+
# No need to set LLVM_DIR explicitly
5181
"-DWAMR_BUILD_AOT=1",
5282
"-DWAMR_BUILD_FAST_INTERP=0",
5383
"-DWAMR_BUILD_INTERP=0",
@@ -65,13 +95,20 @@ cmake(
6595
],
6696
}),
6797
lib_source = ":srcs",
98+
out_static_libs = ["libiwasm.a"],
99+
)
100+
101+
# Wrapper library that adds LLVM dependencies for linking
102+
cc_library(
103+
name = "wamr_lib",
68104
linkopts = select({
69105
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": ["-ldl"],
70106
"//conditions:default": [],
71107
}),
72-
out_static_libs = ["libiwasm.a"],
73-
deps = select({
74-
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": ["@llvm-19_1_0//:llvm_wamr_lib"],
108+
deps = [":wamr_lib_cmake"] + select({
109+
"@proxy_wasm_cpp_host//bazel:engine_wamr_jit": [
110+
"@llvm-raw//:llvm_wamr_lib",
111+
],
75112
"//conditions:default": [],
76113
}),
77114
)

bazel/external/wamr.patch

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,36 @@ index c33b211e..d04d0cc8 100644
1212
EXPORT iwasmTargets
1313
LIBRARY DESTINATION lib
1414
diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake
15-
index cb15b8b0..b71cf077 100644
15+
index cb15b8b0..41c32f79 100644
1616
--- a/build-scripts/config_common.cmake
1717
+++ b/build-scripts/config_common.cmake
18-
@@ -140,7 +140,7 @@ else ()
18+
@@ -103,14 +103,20 @@ if (WAMR_BUILD_JIT EQUAL 1)
19+
if (NOT DEFINED LLVM_DIR)
20+
set (LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm")
21+
set (LLVM_BUILD_ROOT "${LLVM_SRC_ROOT}/build")
22+
- if (NOT EXISTS "${LLVM_BUILD_ROOT}")
23+
- message (FATAL_ERROR "Cannot find LLVM dir: ${LLVM_BUILD_ROOT}")
24+
- endif ()
25+
set (CMAKE_PREFIX_PATH "${LLVM_BUILD_ROOT};${CMAKE_PREFIX_PATH}")
26+
set (LLVM_DIR ${LLVM_BUILD_ROOT}/lib/cmake/llvm)
27+
endif ()
28+
- find_package(LLVM REQUIRED CONFIG)
29+
- include_directories(${LLVM_INCLUDE_DIRS})
30+
+ # Skip LLVM CMake detection when BAZEL_BUILD is set (Bazel provides LLVM)
31+
+ if (NOT DEFINED BAZEL_BUILD)
32+
+ find_package(LLVM REQUIRED CONFIG)
33+
+ include_directories(${LLVM_INCLUDE_DIRS})
34+
+ message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
35+
+ message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
36+
+ else()
37+
+ # Bazel build: LLVM headers from hermetic toolchain, libs via cc_library
38+
+ message(STATUS "Bazel build: Skipping LLVM find_package")
39+
+ include_directories(${LLVM_INCLUDE_DIR})
40+
+ endif()
41+
add_definitions(${LLVM_DEFINITIONS})
42+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
43+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
44+
@@ -140,7 +146,7 @@ else ()
1945
endif ()
2046

2147
# Version

0 commit comments

Comments
 (0)