diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 000000000..1830fba09 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,7 @@ +build --incompatible_enable_cc_toolchain_resolution + +common --color=yes +common --curses=auto + +build --show_timestamps +build --worker_verbose diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 000000000..dfda3e0b4 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +6.1.0 diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml new file mode 100644 index 000000000..64c5a6e89 --- /dev/null +++ b/.github/workflows/bazel.yml @@ -0,0 +1,35 @@ +name: Bazel build + +on: + pull_request: + paths-ignore: + # ignore markdown files (CHANGELOG.md, README.md, etc.) + - '**/*.md' + push: + paths-ignore: + # ignore markdown files (CHANGELOG.md, README.md, etc.) + - '**/*.md' + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + tests: + name: Tests + strategy: + matrix: + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout source code + uses: actions/checkout@v3 + + - name: Build + run: bazel build :ngx_rust :ngx_wasm_rs_static :ngx_wasm_rs_shared --verbose_failures + + - name: Display artifacts + run: ls -lh bazel-bin/ diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 000000000..ded154e96 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,120 @@ +load("@rules_rust//rust:defs.bzl", "rust_library") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@ngx_rust_crate_index//:defs.bzl", ngx_rust_aliases = "aliases", ngx_rust_all_crate_deps = "all_crate_deps") +load("@ngx_wasm_rs_crate_index//:defs.bzl", ngx_wasm_rs_aliases = "aliases", ngx_wasm_rs_all_crate_deps = "all_crate_deps") +load("//build:deps.bzl", "normalize_name") +load("//build:rust-nightly.bzl", "rust_shared_library_nightly", "rust_static_library_nightly") + +# --//:ngx-wasm-rust-feature-wat=false +bool_flag( + name = "ngx-wasm-rust-feature-wat", + build_setting_default = False, + visibility = ["//visibility:public"], +) + +config_setting( + name = "ngx-wasm-rust-feature-wat_flag", + flag_values = { + ":ngx-wasm-rust-feature-wat": "true", + }, + visibility = ["//visibility:public"], +) + +# ngx-rust + +filegroup( + name = "ngx_rust_srcs", + srcs = glob( + include = ["lib/ngx-rust/**"], + exclude = ["*.bazel"], + ), +) + +rust_library( + name = "ngx_rust", + srcs = [":ngx_rust_srcs"], + aliases = ngx_rust_aliases(), + proc_macro_deps = ngx_rust_all_crate_deps( + proc_macro = True, + ), + visibility = ["//visibility:public"], + deps = ngx_rust_all_crate_deps( + normal = True, + ), +) + +# ngx-wasm-rs deps + +# crate: the dependencies of the crate +ngx_wasm_rs_deps = { + "backtrace": ["c-api"], + "c-api": [], + "wat": ["c-api"], +} + +[filegroup( + name = "ngx_wasm_rs_deps_%s_srcs" % normalize_name(dep), + srcs = glob( + include = ["lib/ngx-wasm-rs/lib/%s/**" % dep], + exclude = ["*.bazel"], + ), +) for dep in ngx_wasm_rs_deps] + +[rust_library( + name = "ngx_wasm_rs_deps_%s" % normalize_name(dep), + srcs = [":ngx_wasm_rs_deps_%s_srcs" % normalize_name(dep)], + aliases = ngx_wasm_rs_aliases() | { + ":ngx_wasm_rs_deps_%s" % normalize_name(d): "ngx_wasm_%s" % normalize_name(d) + for d in ngx_wasm_rs_deps[dep] + }, + proc_macro_deps = ngx_wasm_rs_all_crate_deps( + proc_macro = True, + ), + deps = ngx_wasm_rs_all_crate_deps( + normal = True, + ) + [":ngx_wasm_rs_deps_%s" % normalize_name(d) for d in ngx_wasm_rs_deps[dep]], +) for dep in ngx_wasm_rs_deps] + +filegroup( + name = "ngx_wasm_rs_srcs", + srcs = glob( + include = ["lib/ngx-wasm-rs/src/**"], + exclude = ["*.bazel"], + ), +) + +# ngx-wasm-rs + +rust_library_types = { + "static": rust_static_library_nightly, + "shared": rust_shared_library_nightly, +} + +[rust_library_types[_type]( + name = _type == "static" and "ngx_wasm_rs_" + _type or "ngx_wasm_rs", + srcs = [":ngx_wasm_rs_srcs"], + aliases = ngx_wasm_rs_aliases() | { + ":ngx_wasm_rs_deps_backtrace": "ngx_wasm_backtrace", + } | select({ + ":ngx-wasm-rust-feature-wat_flag": { + ":ngx_wasm_rs_deps_wat": "ngx_wasm_wat", + }, + "//conditions:default": {}, + }), + proc_macro_deps = ngx_wasm_rs_all_crate_deps( + proc_macro = True, + ), + visibility = ["//visibility:public"], + deps = ngx_wasm_rs_all_crate_deps( + build = True, + normal = True, + ) + [":ngx_wasm_rs_deps_backtrace"] + select({ + ":ngx-wasm-rust-feature-wat_flag": [":ngx_wasm_rs_deps_wat"], + "//conditions:default": [], + }), +) for _type in rust_library_types] + +alias( + name = "ngx_wasm_rs_shared", + actual = ":ngx_wasm_rs", +) diff --git a/Cargo.lock b/Cargo.lock index 9017a9b2b..24a73c427 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,9 +41,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" dependencies = [ "memchr", ] @@ -144,9 +144,9 @@ checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" [[package]] name = "cc" -version = "1.0.81" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ "libc", ] @@ -399,9 +399,9 @@ checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "log" -version = "0.4.19" +version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" @@ -594,18 +594,18 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] [[package]] name = "regex" -version = "1.9.1" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" +checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" dependencies = [ "aho-corasick", "memchr", @@ -615,9 +615,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.4" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7b6d6190b7594385f61bd3911cd1be99dfddcfc365a4160cc2ab5bff4aed294" +checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" dependencies = [ "aho-corasick", "memchr", @@ -674,9 +674,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.28" +version = "2.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" +checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" dependencies = [ "proc-macro2", "quote", @@ -808,7 +808,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.28", + "syn 2.0.29", "wasm-bindgen-shared", ] @@ -830,7 +830,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.28", + "syn 2.0.29", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -874,9 +874,9 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.48.1" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", @@ -889,42 +889,42 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_i686_gnu" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_x86_64_gnu" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_msvc" -version = "0.48.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 0aa52f776..b09d2ae21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ -[workspace] + members = [ "lib/ngx-rust", "t/lib/ngx-rust-tests", diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 000000000..51a564ecd --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,13 @@ +workspace(name = "ngx_wasm_module") + +load("//build:repos.bzl", "ngx_wasm_module_repositories") + +ngx_wasm_module_repositories() + +load("//build:deps.bzl", "ngx_wasm_module_dependencies") + +ngx_wasm_module_dependencies(cargo_home_isolated = False) # use system `$CARGO_HOME` to speed up builds + +load("//build:crates.bzl", "ngx_wasm_module_crates") + +ngx_wasm_module_crates() diff --git a/build/BUILD.bazel b/build/BUILD.bazel new file mode 100644 index 000000000..ffd0fb0cd --- /dev/null +++ b/build/BUILD.bazel @@ -0,0 +1 @@ +package(default_visibility = ["//visibility:public"]) diff --git a/build/README.md b/build/README.md new file mode 100644 index 000000000..e9060b1a6 --- /dev/null +++ b/build/README.md @@ -0,0 +1,44 @@ +# Bazel project for atc-router + + +To use in other Bazel projects, add the following to your WORKSPACE file: + +```python + +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + +git_repository( + name = "ngx_wasm_module", + branch = "some-tag", + remote = "https://github.com/Kong/atc-router", +) + +load("@ngx_wasm_module//build:repos.bzl", "ngx_wasm_module_repositories") + +ngx_wasm_module_repositories() + +load("@ngx_wasm_module//build:deps.bzl", "ngx_wasm_module_dependencies") + +ngx_wasm_module_dependencies(cargo_home_isolated = False) # use system `$CARGO_HOME` to speed up builds + +load("@ngx_wasm_module//build:crates.bzl", "ngx_wasm_module_crates") + +ngx_wasm_module_crates() + + +``` + +In your rule, add `ngx_wasm_module` as dependency: + +```python +configure_make( + name = "openresty", + # ... + deps = [ + "@ngx_wasm_module", + ], +) +``` + +When building this library in Bazel, use the `-c opt` flag to ensure optimal performance. The default fastbuild mode produces a less performant binary. diff --git a/build/crates.bzl b/build/crates.bzl new file mode 100644 index 000000000..5eb5b30e7 --- /dev/null +++ b/build/crates.bzl @@ -0,0 +1,8 @@ +"""Setup Crates repostories """ + +load("@ngx_rust_crate_index//:defs.bzl", ngx_rust_crate_repositories = "crate_repositories") +load("@ngx_wasm_rs_crate_index//:defs.bzl", ngx_wasm_rs_crate_repositories = "crate_repositories") + +def ngx_wasm_module_crates(): + ngx_rust_crate_repositories() + ngx_wasm_rs_crate_repositories() diff --git a/build/deps.bzl b/build/deps.bzl new file mode 100644 index 000000000..bc1052b5a --- /dev/null +++ b/build/deps.bzl @@ -0,0 +1,52 @@ +"""Setup dependencies after repostories are downloaded.""" + +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set") +load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") +load("@rules_rust//crate_universe:defs.bzl", "crates_repository") + +def ngx_wasm_module_dependencies(cargo_home_isolated = True): + """ + ngx_wasm_module_dependencies setup rust toolchain and cargo dependency repositories. + + Args: + cargo_home_isolated (bool): cargo_home_isolated to False to reuse system CARGO_HOME + for faster builds. cargo_home_isolated is default False and will use isolated + Cargo home, which takes about 2 minutes to bootstrap. + """ + rules_rust_dependencies() + + rust_register_toolchains( + edition = "2018", + extra_target_triples = ["aarch64-unknown-linux-gnu"], + ) + + rust_repository_set( + name = "rust_linux_arm64_linux_tuple", + edition = "2018", + exec_triple = "x86_64-unknown-linux-gnu", + extra_target_triples = ["aarch64-unknown-linux-gnu"], + versions = ["stable", "nightly"], + ) + + crate_universe_dependencies() + + crates_repository( + name = "ngx_rust_crate_index", + cargo_lockfile = "//:lib/ngx-rust/Cargo.lock", + manifests = [ + "//:lib/ngx-rust/Cargo.toml", + ], + isolated = cargo_home_isolated, + ) + + crates_repository( + name = "ngx_wasm_rs_crate_index", + cargo_lockfile = "//:lib/ngx-wasm-rs/Cargo.lock", + manifests = [ + "//:lib/ngx-wasm-rs/Cargo.toml", + ], + isolated = cargo_home_isolated, + ) + +def normalize_name(n): + return n.replace("-", "_") diff --git a/build/platform/BUILD.bazel b/build/platform/BUILD.bazel new file mode 100644 index 000000000..fe7b7b6b3 --- /dev/null +++ b/build/platform/BUILD.bazel @@ -0,0 +1,8 @@ +platform( + name = "linux-aarch64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:arm64", + ], + visibility = ["//visibility:public"], +) diff --git a/build/repos.bzl b/build/repos.bzl new file mode 100644 index 000000000..1bcb399bb --- /dev/null +++ b/build/repos.bzl @@ -0,0 +1,19 @@ +"""Setup repostories.""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def ngx_wasm_module_repositories(): + http_archive( + name = "bazel_skylib", + sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz", + ], + ) + + http_archive( + name = "rules_rust", + sha256 = "9d04e658878d23f4b00163a72da3db03ddb451273eb347df7d7c50838d698f49", + urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.26.0/rules_rust-v0.26.0.tar.gz"], + ) diff --git a/build/rust-nightly.bzl b/build/rust-nightly.bzl new file mode 100644 index 000000000..fe2752e14 --- /dev/null +++ b/build/rust-nightly.bzl @@ -0,0 +1,62 @@ +load("@rules_rust//rust:defs.bzl", "rust_shared_library", "rust_static_library") + +def _impl(settings, attr): + _ignore = (settings, attr) + return {"@rules_rust//rust/toolchain/channel": "nightly"} + +_platform_transition = transition( + implementation = _impl, + inputs = [], + outputs = ["@rules_rust//rust/toolchain/channel"], +) + +# The implementation of transition_rule: all this does is copy the +# rust_binary's output to its own output and propagate its runfiles. +def _transition_rule_impl(ctx): + actual_binary = ctx.attr.actual_binary[0] + rust_binary_outfile = actual_binary[DefaultInfo].files.to_list()[0] + outfile = ctx.actions.declare_file("lib%s.%s" % (ctx.label.name, rust_binary_outfile.extension)) + ctx.actions.run_shell( + inputs = [rust_binary_outfile], + outputs = [outfile], + command = "cp %s %s" % (rust_binary_outfile.path, outfile.path), + ) + return [DefaultInfo( + files = depset([outfile]), + data_runfiles = actual_binary[DefaultInfo].data_runfiles, + )] + +_transition_rule = rule( + implementation = _transition_rule_impl, + attrs = { + # Outgoing edge transition + "actual_binary": attr.label(cfg = _platform_transition), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), + }, +) + +def rust_shared_library_nightly(name, visibility, **kwargs): + binary_name = name + "_nightly" + _transition_rule( + name = name, + actual_binary = ":%s" % binary_name, + ) + rust_shared_library( + name = binary_name, + visibility = visibility, + **kwargs + ) + +def rust_static_library_nightly(name, visibility, **kwargs): + binary_name = name + "_nightly" + _transition_rule( + name = name, + actual_binary = ":%s" % binary_name, + ) + rust_static_library( + name = binary_name, + visibility = visibility, + **kwargs + ) diff --git a/build/toolchain/BUILD b/build/toolchain/BUILD new file mode 100644 index 000000000..80430c738 --- /dev/null +++ b/build/toolchain/BUILD @@ -0,0 +1,97 @@ +load(":cc_toolchain_config.bzl", "cc_toolchain_config") + +package(default_visibility = ["//visibility:public"]) + +filegroup(name = "empty") + +################### +# aarch64-linux-gnu (installed with system) + +toolchain( + name = "gcc_cross_arm64_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:arm64", + ], + toolchain = ":gcc_cross_arm64_cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +cc_toolchain_config( + name = "gcc_cross_arm64_cc_toolchain_config", + compiler_configuration = {}, + target_cpu = "aarch64", + toolchain_path_prefix = "/usr/aarch64-linux-gnu/", # is this required? + tools_path_prefix = "/usr/bin/aarch64-linux-gnu-", +) + +cc_toolchain( + name = "gcc_cross_arm64_cc_toolchain", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":gcc_cross_arm64_cc_toolchain_config", + toolchain_identifier = "gcc_cross_arm64-toolchain", +) + +################### +# x86_64-linux-musl (downloaded by bazel) + +toolchain( + name = "gcc_cross_musl_x86_64_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + "//:musl", + ], + toolchain = ":gcc_cross_musl_x86_64_cc_toolchain", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +cc_toolchain_config( + name = "gcc_cross_musl_x86_64_cc_toolchain_config", + target_cpu = "x86_64", + target_libc = "musl", + toolchain_path_prefix = "gcc-11-x86_64-linux-musl-wrappers/", # is this required? + tools_path_prefix = "gcc-11-x86_64-linux-musl-wrappers/x86_64-linux-musl-", +) + +filegroup( + name = "wrappers", + srcs = glob([ + "gcc-11-x86_64-linux-musl-wrappers/**", + ]), +) + +filegroup( + name = "musl_toolchain_files", + srcs = [ + ":wrappers", + "@gcc-11-x86_64-linux-musl-cross//:toolchain", + ], +) + +cc_toolchain( + name = "gcc_cross_musl_x86_64_cc_toolchain", + all_files = ":musl_toolchain_files", + compiler_files = ":musl_toolchain_files", + dwp_files = ":empty", + linker_files = ":musl_toolchain_files", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":gcc_cross_musl_x86_64_cc_toolchain_config", + toolchain_identifier = "gcc_cross_musl_x86_64-toolchain", +) diff --git a/build/toolchain/cc_toolchain_config.bzl b/build/toolchain/cc_toolchain_config.bzl new file mode 100644 index 000000000..a1be00a85 --- /dev/null +++ b/build/toolchain/cc_toolchain_config.bzl @@ -0,0 +1,227 @@ +# Copyright 2021 The Bazel Authors. +# +# 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. + +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "tool", + "tool_path", +) +load( + "@bazel_tools//tools/build_defs/cc:action_names.bzl", + "CPP_LINK_EXECUTABLE_ACTION_NAME", +) + +# Bazel 4.* doesn't support nested starlark functions, so we cannot simplify +#_fmt_flags() by defining it as a nested function. +def _fmt_flags(flags, toolchain_path_prefix): + return [f.format(toolchain_path_prefix = toolchain_path_prefix) for f in flags] + +# Macro for calling cc_toolchain_config from @bazel_tools with setting the +# right paths and flags for the tools. +def _impl(ctx): + target_cpu = ctx.attr.target_cpu + toolchain_path_prefix = ctx.attr.toolchain_path_prefix + tools_path_prefix = ctx.attr.tools_path_prefix + compiler_configuration = ctx.attr.compiler_configuration + + # Unfiltered compiler flags; these are placed at the end of the command + # line, so take precendence over any user supplied flags through --copts or + # such. + unfiltered_compile_flags = [ + # Do not resolve our symlinked resource prefixes to real paths. + "-no-canonical-prefixes", + # Reproducibility + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + "-fdebug-prefix-map={}=__bazel_toolchain__/".format(toolchain_path_prefix), + ] + + # Default compiler flags: + compile_flags = [ + # "--target=" + target_system_name, + # Security + "-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247 + "-fstack-protector", + "-fno-omit-frame-pointer", + # Diagnostics + "-fcolor-diagnostics", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + ] + + dbg_compile_flags = ["-g", "-fstandalone-debug"] + + opt_compile_flags = [ + "-g0", + "-O2", + "-D_FORTIFY_SOURCE=1", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ] + + link_flags = [ + # "--target=" + target_system_name, + "-lm", + "-no-canonical-prefixes", + ] + + # Similar to link_flags, but placed later in the command line such that + # unused symbols are not stripped. + link_libs = [] + + # Note that for xcompiling from darwin to linux, the native ld64 is + # not an option because it is not a cross-linker, so lld is the + # only option. + + link_flags.extend([ + "-fuse-ld=lld", + "-Wl,--build-id=md5", + "-Wl,--hash-style=gnu", + "-Wl,-z,relro,-z,now", + ]) + + opt_link_flags = ["-Wl,--gc-sections"] + + # Coverage flags: + coverage_compile_flags = ["-fprofile-instr-generate", "-fcoverage-mapping"] + coverage_link_flags = ["-fprofile-instr-generate"] + + ## NOTE: framework paths is missing here; unix_cc_toolchain_config + ## doesn't seem to have a feature for this. + + # C++ built-in include directories: + cxx_builtin_include_directories = [ + "/usr/" + target_cpu + "-linux-gnu/include", + "/usr/lib/gcc-cross/" + target_cpu + "-linux-gnu/11/include", + ] + + # sysroot_path = compiler_configuration["sysroot_path"] + # sysroot_prefix = "" + # if sysroot_path: + # sysroot_prefix = "%sysroot%" + + # cxx_builtin_include_directories.extend([ + # sysroot_prefix + "/include", + # sysroot_prefix + "/usr/include", + # sysroot_prefix + "/usr/local/include", + # ]) + + if "additional_include_dirs" in compiler_configuration: + cxx_builtin_include_directories.extend(compiler_configuration["additional_include_dirs"]) + + ## NOTE: make variables are missing here; unix_cc_toolchain_config doesn't + ## pass these to `create_cc_toolchain_config_info`. + + # The tool names come from [here](https://github.com/bazelbuild/bazel/blob/c7e58e6ce0a78fdaff2d716b4864a5ace8917626/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java#L76-L90): + # NOTE: Ensure these are listed in toolchain_tools in toolchain/internal/common.bzl. + tool_paths = [ + tool_path( + name = "ar", + path = tools_path_prefix + "ar", + ), + tool_path( + name = "cpp", + path = tools_path_prefix + "g++", + ), + tool_path( + name = "gcc", + path = tools_path_prefix + "gcc", + ), + tool_path( + name = "gcov", + path = tools_path_prefix + "gcov", + ), + tool_path( + name = "ld", + path = tools_path_prefix + "lld", + ), + tool_path( + name = "nm", + path = tools_path_prefix + "nm", + ), + tool_path( + name = "objcopy", + path = tools_path_prefix + "objcopy", + ), + tool_path( + name = "objdump", + path = tools_path_prefix + "objdump", + ), + tool_path( + name = "strip", + path = tools_path_prefix + "strip", + ), + ] + + cxx_flags = [] + + # Replace flags with any user-provided overrides. + if "compile_flags" in compiler_configuration: + compile_flags = _fmt_flags(compiler_configuration["compile_flags"], toolchain_path_prefix) + if "cxx_flags" in compiler_configuration: + cxx_flags = _fmt_flags(compiler_configuration["cxx_flags"], toolchain_path_prefix) + if "link_flags" in compiler_configuration: + link_flags = _fmt_flags(compiler_configuration["link_flags"], toolchain_path_prefix) + if "link_libs" in compiler_configuration: + link_libs = _fmt_flags(compiler_configuration["link_libs"], toolchain_path_prefix) + if "opt_compile_flags" in compiler_configuration: + opt_compile_flags = _fmt_flags(compiler_configuration["opt_compile_flags"], toolchain_path_prefix) + if "opt_link_flags" in compiler_configuration: + opt_link_flags = _fmt_flags(compiler_configuration["opt_link_flags"], toolchain_path_prefix) + if "dbg_compile_flags" in compiler_configuration: + dbg_compile_flags = _fmt_flags(compiler_configuration["dbg_compile_flags"], toolchain_path_prefix) + if "coverage_compile_flags" in compiler_configuration: + coverage_compile_flags = _fmt_flags(compiler_configuration["coverage_compile_flags"], toolchain_path_prefix) + if "coverage_link_flags" in compiler_configuration: + coverage_link_flags = _fmt_flags(compiler_configuration["coverage_link_flags"], toolchain_path_prefix) + if "unfiltered_compile_flags" in compiler_configuration: + unfiltered_compile_flags = _fmt_flags(compiler_configuration["unfiltered_compile_flags"], toolchain_path_prefix) + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + compiler = "gcc", + toolchain_identifier = target_cpu + "-linux-gnu", + host_system_name = "local", + target_cpu = target_cpu, + target_system_name = target_cpu + "-linux-gnu", + target_libc = ctx.attr.target_libc, + # abi_version = "unknown", + # abi_libc_version = "unknown", + cxx_builtin_include_directories = cxx_builtin_include_directories, + tool_paths = tool_paths, + action_configs = [ + action_config( + action_name = CPP_LINK_EXECUTABLE_ACTION_NAME, + enabled = True, + tools = [tool(path = tools_path_prefix + "lld")], + ), + ], + ) + +cc_toolchain_config = rule( + implementation = _impl, + attrs = { + "target_cpu": attr.string(), + "toolchain_path_prefix": attr.string(), + "tools_path_prefix": attr.string(), + "compiler_configuration": attr.string_list_dict(allow_empty = True, default = {}), + "target_libc": attr.string(default = "gnu"), + }, + provides = [CcToolchainConfigInfo], +) diff --git a/lib/ngx-rust/Cargo.lock b/lib/ngx-rust/Cargo.lock index 95aaa3e05..5b85dda52 100644 --- a/lib/ngx-rust/Cargo.lock +++ b/lib/ngx-rust/Cargo.lock @@ -1,5 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. + [[package]] name = "ngx" version = "0.1.0" diff --git a/lib/ngx-wasm-rs/Cargo.lock b/lib/ngx-wasm-rs/Cargo.lock index 1ea1b3db4..5c507114a 100644 --- a/lib/ngx-wasm-rs/Cargo.lock +++ b/lib/ngx-wasm-rs/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "aho-corasick" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" dependencies = [ "memchr", ] @@ -19,9 +19,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "cc" -version = "1.0.81" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ "libc", ] @@ -106,6 +106,8 @@ version = "0.1.0" dependencies = [ "ngx-wasm-backtrace", "ngx-wasm-wat", + "rustc-demangle", + "wasmparser", ] [[package]] @@ -150,18 +152,18 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] [[package]] name = "regex" -version = "1.9.1" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" +checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" dependencies = [ "aho-corasick", "memchr", @@ -171,9 +173,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.4" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7b6d6190b7594385f61bd3911cd1be99dfddcfc365a4160cc2ab5bff4aed294" +checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" dependencies = [ "aho-corasick", "memchr", @@ -200,15 +202,15 @@ checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "serde" -version = "1.0.180" +version = "1.0.185" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea67f183f058fe88a4e3ec6e2788e003840893b91bac4559cabedd00863b3ed" +checksum = "be9b6f69f1dfd54c3b568ffa45c310d6973a5e5148fd40cf515acaf38cf5bc31" [[package]] name = "serde_derive" -version = "1.0.180" +version = "1.0.185" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24e744d7782b686ab3b73267ef05697159cc0e5abbed3f47f9933165e5219036" +checksum = "dc59dfdcbad1437773485e0367fea4b090a2e0a16d9ffc46af47764536a298ec" dependencies = [ "proc-macro2", "quote", @@ -217,9 +219,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.104" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" +checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" dependencies = [ "itoa", "ryu", @@ -228,9 +230,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.28" +version = "2.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" +checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" dependencies = [ "proc-macro2", "quote", diff --git a/lib/ngx-wasm-rs/Cargo.toml b/lib/ngx-wasm-rs/Cargo.toml index 70a027956..051c4d2f6 100644 --- a/lib/ngx-wasm-rs/Cargo.toml +++ b/lib/ngx-wasm-rs/Cargo.toml @@ -8,6 +8,10 @@ edition = "2018" ngx-wasm-wat = { path = "lib/wat", optional = true } ngx-wasm-backtrace = { path = "lib/backtrace" } +# from backtrace +wasmparser = "0.102" +rustc-demangle = "0.1" + [features] wat = ["dep:ngx-wasm-wat"] @@ -16,5 +20,4 @@ name = "ngx_wasm_rs" crate-type = ["staticlib", "cdylib"] [workspace] -# not in a workspace - +resolver = "2"