Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 1 | # Copyright 2020 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import platform |
| 16 | import os |
| 17 | import subprocess |
| 18 | import sys |
| 19 | |
| 20 | |
| 21 | def normalize_path(p): |
| 22 | if platform.system() == "Windows": |
| 23 | # Sure. Good idea, bazel. |
| 24 | return p.replace("\\", "/") |
Scott Todd | f34d6b6 | 2020-06-02 09:25:53 -0700 | [diff] [blame] | 25 | return p |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def write_platform(bazelrc): |
| 29 | platform_config = "generic_clang" |
| 30 | if platform.system() == "Windows": |
| 31 | platform_config = "windows" |
| 32 | print("build --config={}".format(platform_config), file=bazelrc) |
| 33 | |
| 34 | |
| 35 | def write_python_bin(bazelrc): |
| 36 | python_bin = normalize_path(sys.executable) |
| 37 | print("build --python_path=\"{}\"".format(python_bin), file=bazelrc) |
| 38 | # IREE extension compilation requires PYTHON_BIN |
| 39 | print("build --action_env PYTHON_BIN=\"{}\"".format(python_bin), file=bazelrc) |
| 40 | # TensorFlow defines this one. No idea why. |
Phoenix Meadowlark | 9e63d75 | 2020-10-16 16:28:33 -0700 | [diff] [blame] | 41 | print("build --action_env PYTHON_BIN_PATH=\"{}\"".format(python_bin), |
| 42 | file=bazelrc) |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def write_python_path(bazelrc): |
| 46 | # For some reason, bazel doesn't always find the user site path, which |
| 47 | # is typically where "pip install --user" libraries end up. Inject it. |
| 48 | try: |
| 49 | user_site = subprocess.check_output( |
| 50 | [sys.executable, "-m", "site", "--user-site"]).decode("utf-8").strip() |
| 51 | print("Found user site directory:", user_site) |
| 52 | except OSError: |
| 53 | print("Could not resolve user site directory") |
| 54 | return |
Phoenix Meadowlark | 9e63d75 | 2020-10-16 16:28:33 -0700 | [diff] [blame] | 55 | print("build --action_env PYTHONPATH=\"{}\"".format( |
| 56 | normalize_path(user_site)), |
| 57 | file=bazelrc) |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | local_bazelrc = os.path.join(os.path.dirname(__file__), "configured.bazelrc") |
| 61 | with open(local_bazelrc, "wt") as bazelrc: |
| 62 | write_platform(bazelrc) |
| 63 | write_python_bin(bazelrc) |
| 64 | write_python_path(bazelrc) |
| 65 | |
| 66 | print("Wrote", local_bazelrc) |