@@ -20,10 +20,14 @@ See /examples/toolchain for usage example.
20
20
NodeInfo = provider (
21
21
doc = "Information about how to invoke the node executable." ,
22
22
fields = {
23
- "target_tool_path" : "Path to the nodejs executable for the target platform." ,
23
+ "target_tool_path" : "Path to the nodejs executable for this target's platform." ,
24
24
"tool_files" : """Files required in runfiles to make the nodejs executable available.
25
25
26
26
May be empty if the target_tool_path points to a locally installed node binary.""" ,
27
+ "npm_path" : "Path to the npm executable for this target's platform." ,
28
+ "npm_files" : """Files required in runfiles to make the npm executable available.
29
+
30
+ May be empty if the npm_path points to a locally installed npm binary.""" ,
27
31
"run_npm" : """A template for a script that wraps npm.
28
32
On Windows, this is a Batch script, otherwise it uses Bash.""" ,
29
33
},
@@ -41,18 +45,28 @@ def _node_toolchain_impl(ctx):
41
45
fail ("Can only set one of target_tool or target_tool_path but both were set." )
42
46
if not ctx .attr .target_tool and not ctx .attr .target_tool_path :
43
47
fail ("Must set one of target_tool or target_tool_path." )
48
+ if ctx .attr .npm and ctx .attr .npm_path :
49
+ fail ("Can only set one of npm or npm_path but both were set." )
44
50
45
51
tool_files = []
46
52
target_tool_path = ctx .attr .target_tool_path
47
53
48
54
if ctx .attr .target_tool :
49
- tool_files = ctx .attr .target_tool .files .to_list ()
50
- target_tool_path = _to_manifest_path (ctx , tool_files [0 ])
55
+ tool_files = [ctx .file .target_tool ]
56
+ target_tool_path = _to_manifest_path (ctx , ctx .file .target_tool )
57
+
58
+ npm_files = []
59
+ npm_path = ctx .attr .npm_path
60
+
61
+ if ctx .attr .npm :
62
+ npm_files = depset ([ctx .file .npm ] + ctx .files .npm_files ).to_list ()
63
+ npm_path = _to_manifest_path (ctx , ctx .file .npm )
51
64
52
65
# Make the $(NODE_PATH) variable available in places like genrules.
53
66
# See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables
54
67
template_variables = platform_common .TemplateVariableInfo ({
55
68
"NODE_PATH" : target_tool_path ,
69
+ "NPM_PATH" : npm_path ,
56
70
})
57
71
default = DefaultInfo (
58
72
files = depset (tool_files ),
@@ -61,6 +75,8 @@ def _node_toolchain_impl(ctx):
61
75
nodeinfo = NodeInfo (
62
76
target_tool_path = target_tool_path ,
63
77
tool_files = tool_files ,
78
+ npm_path = npm_path ,
79
+ npm_files = npm_files ,
64
80
run_npm = ctx .file .run_npm ,
65
81
)
66
82
@@ -81,20 +97,33 @@ node_toolchain = rule(
81
97
implementation = _node_toolchain_impl ,
82
98
attrs = {
83
99
"target_tool" : attr .label (
84
- doc = "A hermetically downloaded nodejs executable target for the target platform." ,
100
+ doc = "A hermetically downloaded nodejs executable target for this target's platform." ,
85
101
mandatory = False ,
86
102
allow_single_file = True ,
87
103
),
88
104
"target_tool_path" : attr .string (
89
- doc = "Path to an existing nodejs executable for the target platform." ,
105
+ doc = "Path to an existing nodejs executable for this target's platform." ,
106
+ mandatory = False ,
107
+ ),
108
+ "npm" : attr .label (
109
+ doc = "A hermetically downloaded npm executable target for this target's platform." ,
110
+ mandatory = False ,
111
+ allow_single_file = True ,
112
+ ),
113
+ "npm_path" : attr .string (
114
+ doc = "Path to an existing npm executable for this target's platform." ,
115
+ mandatory = False ,
116
+ ),
117
+ "npm_files" : attr .label_list (
118
+ doc = "Files required in runfiles to run npm." ,
90
119
mandatory = False ,
91
120
),
92
121
"run_npm" : attr .label (
93
122
doc = "A template file that allows us to execute npm" ,
94
123
allow_single_file = True ,
95
124
),
96
125
},
97
- doc = """Defines a node toolchain.
126
+ doc = """Defines a node toolchain for a platform .
98
127
99
128
You can use this to refer to a vendored nodejs binary in your repository,
100
129
or even to compile nodejs from sources using rules_foreign_cc or other rules.
@@ -110,7 +139,9 @@ node_toolchain(
110
139
)
111
140
```
112
141
113
- Next, declare which execution platforms or target platforms the toolchain should be selected for:
142
+ Next, declare which execution platforms or target platforms the toolchain should be selected for
143
+ based on constraints.
144
+
114
145
```starlark
115
146
toolchain(
116
147
name = "my_nodejs",
@@ -123,6 +154,9 @@ toolchain(
123
154
)
124
155
```
125
156
157
+ See https://bazel.build/extending/toolchains#toolchain-resolution for more information on toolchain
158
+ resolution.
159
+
126
160
Finally in your `WORKSPACE`, register it with `register_toolchains("//:my_nodejs")`
127
161
128
162
For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains.
0 commit comments