Carmelo Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 1 | """ |
| 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 | """ |
| 18 | Extension to genrule that has the JDK bin directory in the PATH, thus allowing |
| 19 | to invoke commands like jar directly in the genrule "cmd" attribute. |
| 20 | This allows using JDK-related tools on a host system that does not have the JDK |
| 21 | installed, instead using the current JDK used by Bazel, e.g. the embedded or |
| 22 | remote one. |
| 23 | """ |
| 24 | |
| 25 | def 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 Cascone | 3dcb7fd | 2019-06-26 14:08:36 -0700 | [diff] [blame] | 32 | |
| 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 Cascone | d33d3b4 | 2019-06-18 12:12:36 -0700 | [diff] [blame] | 40 | "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 | ) |