blob: 5d9bae53a051783bb2f82090d073328f1e14ddc6 [file] [log] [blame]
Carmelo Casconed33d3b42019-06-18 12:12:36 -07001"""
2 Copyright 2018-present Open Networking Foundation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15"""
16
17"""
18Extension to genrule that has the JDK bin directory in the PATH, thus allowing
19to invoke commands like jar directly in the genrule "cmd" attribute.
20This allows using JDK-related tools on a host system that does not have the JDK
21installed, instead using the current JDK used by Bazel, e.g. the embedded or
22remote one.
23"""
24
25def jdk_genrule(
26 cmd,
27 tools = [],
28 toolchains = [],
29 **kwargs):
30 new_tools = tools + ["@bazel_tools//tools/jdk:current_java_runtime"]
31 new_toolchains = toolchains + ["@bazel_tools//tools/jdk:current_java_runtime"]
Carmelo Cascone3dcb7fd2019-06-26 14:08:36 -070032
33 # Add JAVABASE/bin to the PATH env.
34 # Prepend PWD (sandbox path) if JAVABASE is not an absolute path.
35 new_cmd = "echo 'if [[ \"$(JAVABASE)\" = /* ]]; then " + \
36 "JHOME=$(JAVABASE); " + \
37 "else JHOME=$$PWD/$(JAVABASE); " + \
38 "fi' > jdk_genrule_setup.sh; " + \
39 "echo 'export PATH=$$JHOME/bin:$$PATH:' >> jdk_genrule_setup.sh; " + \
Carmelo Casconed33d3b42019-06-18 12:12:36 -070040 "source jdk_genrule_setup.sh; " + cmd
41
42 native.genrule(
43 cmd = new_cmd,
44 tools = new_tools,
45 toolchains = new_toolchains,
46 **kwargs
47 )