From 314743b715472789ea39ce0094aebbe071870560 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Fri, 7 May 2021 17:15:16 +0900 Subject: [PATCH] build: refactor Bazel configuration towards multiple runtime tests. (#160) Signed-off-by: Takeshi Yoneda --- .bazelrc | 14 ++++++ .github/workflows/cpp.yml | 11 +++-- BUILD | 99 ++++++++++++++++++++++++++++++++------- bazel/BUILD | 19 ++++++++ bazel/select.bzl | 37 +++++++++++++++ bazel/variables.bzl | 42 ----------------- test/BUILD | 11 ----- test/common/BUILD | 2 - 8 files changed, 160 insertions(+), 75 deletions(-) create mode 100644 .bazelrc create mode 100644 bazel/select.bzl delete mode 100644 bazel/variables.bzl diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 00000000..d63c7058 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,14 @@ +build --enable_platform_specific_config + +build:linux --cxxopt=-std=c++17 +# See https://bytecodealliance.github.io/wasmtime/c-api/ +build:linux --linkopt=-lm +build:linux --linkopt=-lpthread +build:linux --linkopt=-ldl + +build:macos --cxxopt=-std=c++17 + +# TODO(mathetake): Windows build is not verified yet. +# build:windows --cxxopt="/std:c++17" +# See https://bytecodealliance.github.io/wasmtime/c-api/ +# build:windows --linkopt="ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib ole32.lib" diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml index d509c784..ab27b279 100644 --- a/.github/workflows/cpp.yml +++ b/.github/workflows/cpp.yml @@ -56,7 +56,12 @@ jobs: build: runs-on: ubuntu-latest - + + strategy: + matrix: + # TODO(mathetake): Add other runtimes. + runtime: [ "wasmtime" ] + steps: - uses: actions/checkout@v2 @@ -64,9 +69,9 @@ jobs: uses: actions/cache@v1 with: path: "/home/runner/.cache/bazel" - key: bazel + key: bazel-${{ matrix.runtime }} - name: Test run: | - bazel test //... + bazel test --define runtime=${{ matrix.runtime }} //... diff --git a/BUILD b/BUILD index 74e861df..2f760f84 100644 --- a/BUILD +++ b/BUILD @@ -1,5 +1,11 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("@proxy_wasm_cpp_host//bazel:variables.bzl", "COPTS") +load( + "@proxy_wasm_cpp_host//bazel:select.bzl", + "proxy_wasm_select_runtime_v8", + "proxy_wasm_select_runtime_wamr", + "proxy_wasm_select_runtime_wasmtime", + "proxy_wasm_select_runtime_wavm", +) licenses(["notice"]) # Apache 2 @@ -13,28 +19,87 @@ cc_library( ], ) -# TODO(mathetkae): once other runtimes(WAVM,V8) can be linked in this repos, -# use -define=wasm=v8|wavm|wasmtime and switch cc_library( - name = "lib", - srcs = glob( - [ - "src/**/*.cc", - "src/**/*.h", - ], - exclude = [ - "src/**/v8*", - "src/**/wamr*", - "src/**/wavm*", - ], - ), - hdrs = glob(["src/**/*.h"]), - copts = COPTS, + name = "common_lib", + srcs = glob([ + "src/*.h", + "src/*.cc", + "src/common/*.h", + "src/common/*.cc", + "src/third_party/*.h", + "src/third_party/*.cc", + "src/null/*.cc", + ]), deps = [ ":include", "@boringssl//:crypto", "@com_google_protobuf//:protobuf_lite", "@proxy_wasm_cpp_sdk//:api_lib", + ], +) + +cc_library( + name = "v8_lib", + srcs = glob([ + # TODO(@mathetake): Add V8 lib. + # "src/v8/*.h", + # "src/v8/*.cc", + ]), + deps = [ + ":common_lib", + # TODO(@mathetake): Add V8 lib. + ], +) + +cc_library( + name = "wamr_lib", + srcs = glob([ + # TODO(@mathetake): Add WAMR lib. + # "src/wamr/*.h", + # "src/wamr/*.cc", + ]), + deps = [ + ":common_lib", + # TODO(@mathetake): Add WAMR lib. + ], +) + +cc_library( + name = "wasmtime_lib", + srcs = glob([ + "src/wasmtime/*.h", + "src/wasmtime/*.cc", + ]), + deps = [ + ":common_lib", "@wasm_c_api//:wasmtime_lib", ], ) + +cc_library( + name = "wavm_lib", + srcs = glob([ + # TODO(@mathetake): Add WAVM lib. + # "src/wavm/*.h", + # "src/wavm/*.cc", + ]), + deps = [ + ":common_lib", + # TODO(@mathetake): Add WAVM lib. + ], +) + +cc_library( + name = "lib", + deps = [ + ":common_lib", + ] + proxy_wasm_select_runtime_v8( + [":v8_lib"], + ) + proxy_wasm_select_runtime_wamr( + [":wamr_lib"], + ) + proxy_wasm_select_runtime_wasmtime( + [":wasmtime_lib"], + ) + proxy_wasm_select_runtime_wavm( + [":wavm_lib"], + ), +) diff --git a/bazel/BUILD b/bazel/BUILD index e69de29b..b6eb774c 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -0,0 +1,19 @@ +config_setting( + name = "runtime_v8", + values = {"define": "runtime=v8"}, +) + +config_setting( + name = "runtime_wamr", + values = {"define": "runtime=wamr"}, +) + +config_setting( + name = "runtime_wasmtime", + values = {"define": "runtime=wasmtime"}, +) + +config_setting( + name = "runtime_wavm", + values = {"define": "runtime=wavm"}, +) diff --git a/bazel/select.bzl b/bazel/select.bzl new file mode 100644 index 00000000..7d4a305a --- /dev/null +++ b/bazel/select.bzl @@ -0,0 +1,37 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def proxy_wasm_select_runtime_v8(xs): + return select({ + "@proxy_wasm_cpp_host//bazel:runtime_v8": xs, + "//conditions:default": [], + }) + +def proxy_wasm_select_runtime_wamr(xs): + return select({ + "@proxy_wasm_cpp_host//bazel:runtime_wamr": xs, + "//conditions:default": [], + }) + +def proxy_wasm_select_runtime_wasmtime(xs): + return select({ + "@proxy_wasm_cpp_host//bazel:runtime_wasmtime": xs, + "//conditions:default": [], + }) + +def proxy_wasm_select_runtime_wavm(xs): + return select({ + "@proxy_wasm_cpp_host//bazel:runtime_wavm": xs, + "//conditions:default": [], + }) diff --git a/bazel/variables.bzl b/bazel/variables.bzl deleted file mode 100644 index 1d28c04a..00000000 --- a/bazel/variables.bzl +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -COPTS = select({ - "@bazel_tools//src/conditions:windows": [ - "/std:c++17", - ], - "//conditions:default": [ - "-std=c++17", - ], -}) - -# https://bytecodealliance.github.io/wasmtime/c-api/ -LINKOPTS = select({ - "@bazel_tools//src/conditions:windows": [ - "-", - "ws2_32.lib", - "advapi32.lib", - "userenv.lib", - "ntdll.lib", - "shell32.lib", - "ole32.lib", - ], - "@bazel_tools//src/conditions:darwin": [], - "//conditions:default": [ - # required for linux - "-lpthread", - "-ldl", - "-lm", - ], -}) diff --git a/test/BUILD b/test/BUILD index 60ff362a..f1e179f3 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,12 +1,10 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") -load("@proxy_wasm_cpp_host//bazel:variables.bzl", "COPTS", "LINKOPTS") package(default_visibility = ["//visibility:public"]) cc_test( name = "null_vm_test", srcs = ["null_vm_test.cc"], - copts = COPTS, deps = [ "//:lib", "@com_google_googletest//:gtest", @@ -17,13 +15,11 @@ cc_test( cc_test( name = "runtime_test", srcs = ["runtime_test.cc"], - copts = COPTS, data = [ "//test/test_data:abi_export.wasm", "//test/test_data:callback.wasm", "//test/test_data:trap.wasm", ], - linkopts = LINKOPTS, deps = [ ":utility_lib", "//:lib", @@ -35,12 +31,10 @@ cc_test( cc_test( name = "exports_test", srcs = ["exports_test.cc"], - copts = COPTS, data = [ "//test/test_data:clock.wasm", "//test/test_data:env.wasm", ], - linkopts = LINKOPTS, deps = [ ":utility_lib", "//:lib", @@ -52,7 +46,6 @@ cc_test( cc_test( name = "context_test", srcs = ["context_test.cc"], - copts = COPTS, deps = [ "//:lib", "@com_google_googletest//:gtest", @@ -63,7 +56,6 @@ cc_test( cc_test( name = "shared_data", srcs = ["shared_data_test.cc"], - copts = COPTS, deps = [ "//:lib", "@com_google_googletest//:gtest", @@ -74,7 +66,6 @@ cc_test( cc_test( name = "shared_queue", srcs = ["shared_queue_test.cc"], - copts = COPTS, deps = [ "//:lib", "@com_google_googletest//:gtest", @@ -85,7 +76,6 @@ cc_test( cc_test( name = "vm_id_handle", srcs = ["vm_id_handle_test.cc"], - copts = COPTS, deps = [ "//:lib", "@com_google_googletest//:gtest", @@ -100,7 +90,6 @@ cc_library( "utility.h", ], hdrs = ["utility.h"], - copts = COPTS, deps = [ "//:lib", "@com_google_googletest//:gtest", diff --git a/test/common/BUILD b/test/common/BUILD index c76fc5e4..23e21ba7 100644 --- a/test/common/BUILD +++ b/test/common/BUILD @@ -1,10 +1,8 @@ load("@rules_cc//cc:defs.bzl", "cc_test") -load("@proxy_wasm_cpp_host//bazel:variables.bzl", "COPTS") cc_test( name = "bytecode_util_test", srcs = ["bytecode_util_test.cc"], - copts = COPTS, data = [ "//test/test_data:abi_export.wasm", ],