diff --git a/docs/Core.md b/docs/Core.md
index 0b602fac6a..47aae1f4be 100644
--- a/docs/Core.md
+++ b/docs/Core.md
@@ -115,7 +115,7 @@ Defaults to `["https://nodejs.org/dist/v{version}/{filename}"]`
(*String*): the specific version of NodeJS to install
-Defaults to `"18.17.1"`
+Defaults to `"18.18.0"`
@@ -142,7 +142,7 @@ Defaults to `None`
**USAGE**
-node_toolchain(name, npm, npm_files, npm_path, run_npm, target_tool, target_tool_path)
+node_toolchain(name, headers, npm, npm_files, npm_path, run_npm, target_tool, target_tool_path)
Defines a node toolchain for a platform.
@@ -193,6 +193,12 @@ You can use the `--toolchain_resolution_debug` flag to `bazel` to help diagnose
(*Name, mandatory*): A unique name for this target.
+
+
+(*Label*): A cc_library that contains the Node/v8 header files for this target platform.
+
+Defaults to `None`
+
(*Label*): A hermetically downloaded npm executable target for this target's platform.
diff --git a/docs/install.md b/docs/install.md
index bdf3bf324f..8c635ad337 100644
--- a/docs/install.md
+++ b/docs/install.md
@@ -27,7 +27,7 @@ If you'd like to use a version of NodeJS that is not currently supported here,
for example one that you host within your org, you can manually specify those in your `WORKSPACE`:
```python
-load("@rules_nodejs//:index.bzl", "node_repositories")
+load("@rules_nodejs//nodejs:repositories.bzl", "node_repositories")
node_repositories(
node_version = "8.10.0",
@@ -45,4 +45,4 @@ Specifying `node_urls` is optional. If omitted, the default values will be used.
You can use your own Node.js binary rather than fetching from the internet.
You could check in a binary file, or build Node.js from sources.
-To use See [`node_toolchain`](./Core.md#node_toolchain) for docs.
\ No newline at end of file
+To use See [`node_toolchain`](./Core.md#node_toolchain) for docs.
diff --git a/e2e/smoke/BUILD.bazel b/e2e/smoke/BUILD.bazel
index 8001660dae..b2ab2ec56a 100644
--- a/e2e/smoke/BUILD.bazel
+++ b/e2e/smoke/BUILD.bazel
@@ -276,3 +276,18 @@ diff_test(
file1 = "write_node_version_16",
file2 = "thing_toolchain_16",
)
+
+cc_binary(
+ name = "using_headers_test",
+ srcs = ["using_headers.cc"],
+ copts = select({
+ "@platforms//os:windows": ["/std:c++14"],
+ "//conditions:default": ["-std=c++14"],
+ }),
+ target_compatible_with = select({
+ # Windows does not ship headers in the release artifact so this won't work yet.
+ "@platforms//os:windows": ["@platforms//:incompatible"],
+ "//conditions:default": [],
+ }),
+ deps = ["@rules_nodejs//nodejs:current_node_cc_headers"],
+)
diff --git a/e2e/smoke/using_headers.cc b/e2e/smoke/using_headers.cc
new file mode 100644
index 0000000000..13e0298da5
--- /dev/null
+++ b/e2e/smoke/using_headers.cc
@@ -0,0 +1,3 @@
+#include "node.h"
+
+int main() { return 0; }
diff --git a/nodejs/BUILD.bazel b/nodejs/BUILD.bazel
index 2d125de047..46ed921154 100644
--- a/nodejs/BUILD.bazel
+++ b/nodejs/BUILD.bazel
@@ -1,6 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//nodejs/private:toolchains_repo.bzl", "PLATFORMS")
load("//nodejs/private:user_build_settings.bzl", "user_args")
+load("//nodejs/private:current_node_cc_headers.bzl", "current_node_cc_headers")
exports_files([
"index.for_docs.bzl",
@@ -43,3 +44,11 @@ user_args(
build_setting_default = "--preserve-symlinks",
visibility = ["//visibility:public"],
)
+
+# This target provides the C headers for whatever the current toolchain is
+# for the consuming rule. It basically acts like a cc_library by forwarding
+# on the providers for the underlying cc_library that the toolchain is using.
+current_node_cc_headers(
+ name = "current_node_cc_headers",
+ visibility = ["//visibility:public"],
+)
diff --git a/nodejs/private/current_node_cc_headers.bzl b/nodejs/private/current_node_cc_headers.bzl
new file mode 100644
index 0000000000..53a8c17b23
--- /dev/null
+++ b/nodejs/private/current_node_cc_headers.bzl
@@ -0,0 +1,50 @@
+# Copyright 2023 The Bazel Authors. All rights reserved.
+#
+# 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.
+
+"""Implementation of current_node_cc_headers rule."""
+
+def _current_node_cc_headers_impl(ctx):
+ return ctx.toolchains["//nodejs:toolchain_type"].nodeinfo.headers.providers_map.values()
+
+current_node_cc_headers = rule(
+ implementation = _current_node_cc_headers_impl,
+ toolchains = ["//nodejs:toolchain_type"],
+ provides = [CcInfo],
+ doc = """\
+Provides the currently active Node toolchain's C++ headers.
+
+This is a wrapper around the underlying `cc_library()` for the
+C headers for the consuming target's currently active Node toolchain.
+
+Note, "node.h" is only usable from C++, and you'll need to
+ensure you are compiling with c++14 or later.
+
+Also, on Windows, NodeJS releases do not ship headers, so this rule is currently
+not usable with the built-in toolchains. If you define your own toolchain on Windows,
+you can include the headers and then this rule will work.
+
+To use, simply depend on this target where you would have wanted the
+toolchain's underlying `:headers` target:
+
+```starlark
+cc_library(
+ name = "foo",
+ srcs = ["foo.cc"],
+ # If toolchain sets this already, you can omit.
+ copts = ["-std=c++14"],
+ deps = ["@rules_nodejs//:current_node_cc_headers"]
+)
+```
+""",
+)
diff --git a/nodejs/private/node_versions.bzl b/nodejs/private/node_versions.bzl
index 12f84c3f4a..47d4ddf8ef 100644
--- a/nodejs/private/node_versions.bzl
+++ b/nodejs/private/node_versions.bzl
@@ -2327,6 +2327,14 @@ NODE_VERSIONS = {
"18.17.1-linux_s390x": ("node-v18.17.1-linux-s390x.tar.xz", "node-v18.17.1-linux-s390x", "6f30e71d5f6d58f9bef88adf527232cbd82305472f20cc3d1fe8a984f549be0d"),
"18.17.1-linux_amd64": ("node-v18.17.1-linux-x64.tar.xz", "node-v18.17.1-linux-x64", "07e76408ddb0300a6f46fcc9abc61f841acde49b45020ec4e86bb9b25df4dced"),
"18.17.1-windows_amd64": ("node-v18.17.1-win-x64.zip", "node-v18.17.1-win-x64", "afc83f5cf6e8b45a4d3fb842904f604dcd271fefada31ad6654f8302f8da28c9"),
+ # 18.18.0
+ "18.18.0-darwin_arm64": ("node-v18.18.0-darwin-arm64.tar.gz", "node-v18.18.0-darwin-arm64", "b21a2e8a3e75f81eb6124ea463d67cf4648a04dce9062e8d5a1bc44c99606a15"),
+ "18.18.0-darwin_amd64": ("node-v18.18.0-darwin-x64.tar.gz", "node-v18.18.0-darwin-x64", "993e43ac4750609437a8523937a42663e5a7aa7973acac3f26b8d478aca1285d"),
+ "18.18.0-linux_arm64": ("node-v18.18.0-linux-arm64.tar.xz", "node-v18.18.0-linux-arm64", "e2931643cc3ee37375ae5c6dc2028ff526948a227d9fd5d481316240de6e58a5"),
+ "18.18.0-linux_ppc64le": ("node-v18.18.0-linux-ppc64le.tar.xz", "node-v18.18.0-linux-ppc64le", "121204a93ac931146b3325a1748ae06d8b4d75db37566a792032e1c859bc8911"),
+ "18.18.0-linux_s390x": ("node-v18.18.0-linux-s390x.tar.xz", "node-v18.18.0-linux-s390x", "8d3f21b77618d3c3d3ede230a65a89c9b1d5b942b79a6cea184d027b938de2a2"),
+ "18.18.0-linux_amd64": ("node-v18.18.0-linux-x64.tar.xz", "node-v18.18.0-linux-x64", "3008408e9098f2462f7b1a0f6a48b8a46079beb1c92b6ec43b04713265c96978"),
+ "18.18.0-windows_amd64": ("node-v18.18.0-win-x64.zip", "node-v18.18.0-win-x64", "ae45bc05f4fcc02a17c724670534dc928a2ff4287a14b40f17afa8172601e790"),
# 19.0.0
"19.0.0-darwin_arm64": ("node-v19.0.0-darwin-arm64.tar.gz", "node-v19.0.0-darwin-arm64", "e30054d93857d3b2f55d22a4305e379ba9544adea885428900ff57bae465435e"),
"19.0.0-darwin_amd64": ("node-v19.0.0-darwin-x64.tar.gz", "node-v19.0.0-darwin-x64", "a1b46d199bbc307f6ef8621b118e71356c626a279eb421c6b3ce7a7741573041"),
@@ -2495,4 +2503,36 @@ NODE_VERSIONS = {
"20.5.1-linux_s390x": ("node-v20.5.1-linux-s390x.tar.xz", "node-v20.5.1-linux-s390x", "e49c735f347656c1beb9d02640d945e4c71793b46aa98ada015166f8a9d4f73c"),
"20.5.1-linux_amd64": ("node-v20.5.1-linux-x64.tar.xz", "node-v20.5.1-linux-x64", "a4a700bbca51ac26538eda2250e449955a9cc49638a45b38d5501e97f5b020b4"),
"20.5.1-windows_amd64": ("node-v20.5.1-win-x64.zip", "node-v20.5.1-win-x64", "5d2596a00699fadf0ffa8e651f47ff5d719991014b920544d59c80d78569d42f"),
+ # 20.6.0
+ "20.6.0-darwin_arm64": ("node-v20.6.0-darwin-arm64.tar.gz", "node-v20.6.0-darwin-arm64", "1ed71ea4ef5985f11b65c3961875ad91223c201f2042c00568a82fe7020a5cea"),
+ "20.6.0-darwin_amd64": ("node-v20.6.0-darwin-x64.tar.gz", "node-v20.6.0-darwin-x64", "16657f07cbe8c3d5589d70104ef51955c04abf249ea83b1109f924444ed828aa"),
+ "20.6.0-linux_arm64": ("node-v20.6.0-linux-arm64.tar.xz", "node-v20.6.0-linux-arm64", "675c54b1ad4947836ec146f2be37202b5ac2c6e511ba5d9066e52e3da6a2a419"),
+ "20.6.0-linux_ppc64le": ("node-v20.6.0-linux-ppc64le.tar.xz", "node-v20.6.0-linux-ppc64le", "1787d08ab4b2564de759657211191c8582c63fbbddfcb6085b682059b666f055"),
+ "20.6.0-linux_s390x": ("node-v20.6.0-linux-s390x.tar.xz", "node-v20.6.0-linux-s390x", "d2857f92cda010df123b4aae6bb5b73221c6ab75e1cf7cdd3d13baa1dbb66ee7"),
+ "20.6.0-linux_amd64": ("node-v20.6.0-linux-x64.tar.xz", "node-v20.6.0-linux-x64", "365a7b4eed12bf88011d048ddd668573daef981e048ec860b28d5ebb3587be6c"),
+ "20.6.0-windows_amd64": ("node-v20.6.0-win-x64.zip", "node-v20.6.0-win-x64", "afaffd4e90528f8344f1c908a6945efac995a63e3fe22d4338f34579851272f8"),
+ # 20.6.1
+ "20.6.1-darwin_arm64": ("node-v20.6.1-darwin-arm64.tar.gz", "node-v20.6.1-darwin-arm64", "d8ba8018d45b294429b1a7646ccbeaeb2af3cdf45b5c91dabbd93e2a2035cb46"),
+ "20.6.1-darwin_amd64": ("node-v20.6.1-darwin-x64.tar.gz", "node-v20.6.1-darwin-x64", "365ec544c6596f194afff9a613554abfc68d4a2274181b7651386d9a11cf5862"),
+ "20.6.1-linux_arm64": ("node-v20.6.1-linux-arm64.tar.xz", "node-v20.6.1-linux-arm64", "6823720796b287465bb4aa8e7611143322ffd6cbdb9c6e3b149576f6d87953bf"),
+ "20.6.1-linux_ppc64le": ("node-v20.6.1-linux-ppc64le.tar.xz", "node-v20.6.1-linux-ppc64le", "27884935b025b6676e4b8737f334673ee825947d0baef61aa0326374597aeb05"),
+ "20.6.1-linux_s390x": ("node-v20.6.1-linux-s390x.tar.xz", "node-v20.6.1-linux-s390x", "3968d629989b6de16b8872b6d7ee6e6cdf1204def99c43412a6ee28203ed0022"),
+ "20.6.1-linux_amd64": ("node-v20.6.1-linux-x64.tar.xz", "node-v20.6.1-linux-x64", "591f9f274104f266a8cf085d2c7d5d2848ba73b98ae323d501db2d4c4b7026e5"),
+ "20.6.1-windows_amd64": ("node-v20.6.1-win-x64.zip", "node-v20.6.1-win-x64", "88371914f1f75d594bb367570e163cf5ecebeb514fd54cc765093819ebb0ed48"),
+ # 20.7.0
+ "20.7.0-darwin_arm64": ("node-v20.7.0-darwin-arm64.tar.gz", "node-v20.7.0-darwin-arm64", "08aa09792f30a86e8904e334ba6d348ad73e926b5e441ed9abcdcbea651dc926"),
+ "20.7.0-darwin_amd64": ("node-v20.7.0-darwin-x64.tar.gz", "node-v20.7.0-darwin-x64", "ceeba829f44e7573949f2ce2ad5def27f1d6daa55f2860bea82964851fae01bc"),
+ "20.7.0-linux_arm64": ("node-v20.7.0-linux-arm64.tar.xz", "node-v20.7.0-linux-arm64", "c97b51decb0f4a3e8e5bd8cbc6ff43ae4782f2b8b6e3c2b513b77b8f97fffcc5"),
+ "20.7.0-linux_ppc64le": ("node-v20.7.0-linux-ppc64le.tar.xz", "node-v20.7.0-linux-ppc64le", "21c5b774795bd368b9e67b8f1834ca573252a10e500da57226cfef35063b0f47"),
+ "20.7.0-linux_s390x": ("node-v20.7.0-linux-s390x.tar.xz", "node-v20.7.0-linux-s390x", "c2c015b941dddb56c5943057729497db2cf323f4cc4ef363bd4e0150fb5c8b37"),
+ "20.7.0-linux_amd64": ("node-v20.7.0-linux-x64.tar.xz", "node-v20.7.0-linux-x64", "a4251c24c6bf6d3bdee4521ca294bc0897a6c466137e02caa2521af5d456f55e"),
+ "20.7.0-windows_amd64": ("node-v20.7.0-win-x64.zip", "node-v20.7.0-win-x64", "2b1a117e63f0602bad1e9e31679932b64e9b130a96dc2feb0c367ca816c5a5cb"),
+ # 20.8.0
+ "20.8.0-darwin_arm64": ("node-v20.8.0-darwin-arm64.tar.gz", "node-v20.8.0-darwin-arm64", "cbcb7fdbcd9341662256df5e4488a0045242f87382879242093e0f0699511abc"),
+ "20.8.0-darwin_amd64": ("node-v20.8.0-darwin-x64.tar.gz", "node-v20.8.0-darwin-x64", "a6f6b573ea656c149956f69f35e04ebb242b945d59972bea2e96a944bbf50ad1"),
+ "20.8.0-linux_arm64": ("node-v20.8.0-linux-arm64.tar.xz", "node-v20.8.0-linux-arm64", "ec2d98894d58d07260e61e6a70b88cabea98292f0b2801cbeebd864d242e1087"),
+ "20.8.0-linux_ppc64le": ("node-v20.8.0-linux-ppc64le.tar.xz", "node-v20.8.0-linux-ppc64le", "ae8130354dbf2526ddffa92c406864d97c08044ddb66b8aaaccb54be03085a27"),
+ "20.8.0-linux_s390x": ("node-v20.8.0-linux-s390x.tar.xz", "node-v20.8.0-linux-s390x", "a529f569b6783bd3cb948b7cb5cfee2270a720db1b347e1e168f46ad9123394d"),
+ "20.8.0-linux_amd64": ("node-v20.8.0-linux-x64.tar.xz", "node-v20.8.0-linux-x64", "66056a2acc368db142b8a9258d0539e18538ae832b3ccb316671b0d35cb7c72c"),
+ "20.8.0-windows_amd64": ("node-v20.8.0-win-x64.zip", "node-v20.8.0-win-x64", "6afd5a7aa126f4e255f041de66c4a608f594190d34dcaba72f7b348d2410ca66"),
}
diff --git a/nodejs/repositories.bzl b/nodejs/repositories.bzl
index 08ded10f47..ecb23960df 100644
--- a/nodejs/repositories.bzl
+++ b/nodejs/repositories.bzl
@@ -376,6 +376,7 @@ node_toolchain(
npm = ":npm",
npm_files = [":npm_files"],
run_npm = ":run_npm.template",
+ headers = ":headers",
)
"""
repository_ctx.file("BUILD.bazel", content = build_content)
diff --git a/nodejs/toolchain.bzl b/nodejs/toolchain.bzl
index 9a2691f73c..7980ed26ef 100644
--- a/nodejs/toolchain.bzl
+++ b/nodejs/toolchain.bzl
@@ -28,6 +28,27 @@ May be empty if the target_tool_path points to a locally installed node binary."
May be empty if the npm_path points to a locally installed npm binary.""",
"run_npm": """A template for a script that wraps npm.
On Windows, this is a Batch script, otherwise it uses Bash.""",
+ "headers": """\
+(struct) Information about the header files, with fields:
+ * providers_map: a dict of string to provider instances. The key should be
+ a fully qualified name (e.g. `@rules_foo//bar:baz.bzl#MyInfo`) of the
+ provider to uniquely identify its type.
+
+ The following keys are always present:
+ * CcInfo: the CcInfo provider instance for the headers.
+ * DefaultInfo: the DefaultInfo provider instance for the headers.
+
+ A map is used to allow additional providers from the originating headers
+ target (typically a `cc_library`) to be propagated to consumers (directly
+ exposing a Target object can cause memory issues and is an anti-pattern).
+
+ When consuming this map, it's suggested to use `providers_map.values()` to
+ return all providers; or copy the map and filter out or replace keys as
+ appropriate. Note that any keys begining with `_` (underscore) are
+ considered private and should be forward along as-is (this better allows
+ e.g. `:current_node_cc_headers` to act as the underlying headers target it
+ represents).
+""",
},
)
@@ -76,6 +97,12 @@ def _node_toolchain_impl(ctx):
npm_path = npm_path,
npm_files = npm_files,
run_npm = ctx.file.run_npm,
+ headers = struct(
+ providers_map = {
+ "CcInfo": ctx.attr.headers[CcInfo],
+ "DefaultInfo": ctx.attr.headers[DefaultInfo],
+ },
+ ),
)
# Export all the providers inside our ToolchainInfo
@@ -120,6 +147,9 @@ node_toolchain = rule(
doc = "A template file that allows us to execute npm",
allow_single_file = True,
),
+ "headers": attr.label(
+ doc = "A cc_library that contains the Node/v8 header files for this target platform.",
+ ),
},
doc = """Defines a node toolchain for a platform.