blob: f0bac9321f95a69a5132c1ea939d99bff1ff7941 [file] [log] [blame]
Stella Laurenzod5b91272020-05-28 17:42:06 -07001# 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
15import platform
16import os
17import subprocess
18import sys
19
20
21def normalize_path(p):
22 if platform.system() == "Windows":
23 # Sure. Good idea, bazel.
24 return p.replace("\\", "/")
Scott Toddf34d6b62020-06-02 09:25:53 -070025 return p
Stella Laurenzod5b91272020-05-28 17:42:06 -070026
27
28def 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
35def 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 Meadowlark9e63d752020-10-16 16:28:33 -070041 print("build --action_env PYTHON_BIN_PATH=\"{}\"".format(python_bin),
42 file=bazelrc)
Stella Laurenzod5b91272020-05-28 17:42:06 -070043
44
45def 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 Meadowlark9e63d752020-10-16 16:28:33 -070055 print("build --action_env PYTHONPATH=\"{}\"".format(
56 normalize_path(user_site)),
57 file=bazelrc)
Stella Laurenzod5b91272020-05-28 17:42:06 -070058
59
60local_bazelrc = os.path.join(os.path.dirname(__file__), "configured.bazelrc")
61with open(local_bazelrc, "wt") as bazelrc:
62 write_platform(bazelrc)
63 write_python_bin(bazelrc)
64 write_python_path(bazelrc)
65
66print("Wrote", local_bazelrc)