blob: dba46ecdb9d175e111aa20d90d3529534e389156 [file] [log] [blame]
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07001# Copyright 2020 The IREE Authors
Stella Laurenzod5b91272020-05-28 17:42:06 -07002#
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -07003# Licensed under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Stella Laurenzod5b91272020-05-28 17:42:06 -07006
7import platform
8import os
9import subprocess
10import sys
11
12
Stella Laurenzo36781bb2021-02-27 19:28:25 -080013def detect_unix_platform_config(bazelrc):
14 # This is hoaky. Ideally, bazel had any kind of rational way of selecting
15 # options from within its environment (key word: "rational"), but sadly, it
16 # is unintelligible to mere mortals. Why should a build system have a way for
17 # people to condition their build options on what compiler they are using
18 # (without descending down the hole of deciphering what a Bazel toolchain is)?
19 # All I want to do is set a couple of project specific warning options!
20
21 if platform.system() == "Darwin":
freedom" Koan-Sin Tan8712d402021-03-28 01:18:22 +080022 print(f"build --config=macos_clang", file=bazelrc)
23 print(f"build:release --config=macos_clang_release", file=bazelrc)
Stella Laurenzo36781bb2021-02-27 19:28:25 -080024 else:
Stella Laurenzo36781bb2021-02-27 19:28:25 -080025
freedom" Koan-Sin Tan8712d402021-03-28 01:18:22 +080026 # If the user specified a CXX environment var, bazel will later respect that,
27 # so we just see if it says "clang".
28 cxx = os.environ.get("CXX")
29 cc = os.environ.get("CC")
30 if (cxx is not None and cc is None) or (cxx is None and cc is not None):
31 print("WARNING: Only one of CXX or CC is set, which can confuse bazel. "
32 "Recommend: set both appropriately (or none)")
33 if cc is not None and cxx is not None:
34 # Persist the variables.
35 print(f"build --action_env CC=\"{cc}\"", file=bazelrc)
36 print(f"build --action_env CXX=\"{cxx}\"", file=bazelrc)
37 else:
38 print(
39 "WARNING: CC and CXX are not set, which can cause mismatches between "
40 "flag configurations and compiler. Recommend setting them explicitly."
41 )
42
43 if cxx is not None and "clang" in cxx:
44 print(
45 f"Choosing generic_clang config because CXX is set to clang ({cxx})")
46 print(f"build --config=generic_clang", file=bazelrc)
47 print(f"build:release --config=generic_clang_release", file=bazelrc)
48 else:
49 print(f"Choosing generic_gcc config by default because no CXX set or "
50 f"not recognized as clang ({cxx})")
51 print(f"build --config=generic_gcc", file=bazelrc)
52 print(f"build:release --config=generic_gcc_release", file=bazelrc)
Stella Laurenzo36781bb2021-02-27 19:28:25 -080053
54
Stella Laurenzod5b91272020-05-28 17:42:06 -070055def write_platform(bazelrc):
Stella Laurenzod5b91272020-05-28 17:42:06 -070056 if platform.system() == "Windows":
Stella Laurenzo36781bb2021-02-27 19:28:25 -080057 print(f"build --config=msvc", file=bazelrc)
58 print(f"build:release --config=msvc_release", file=bazelrc)
59 else:
60 detect_unix_platform_config(bazelrc)
freedom" Koan-Sin Tan4c49a0c2020-12-30 03:46:29 +080061 if not (platform.system() == "Darwin"):
Geoffrey Martin-Noblef03c3c12021-02-26 10:44:03 -080062 print("common --config=non_darwin", file=bazelrc)
Stella Laurenzod5b91272020-05-28 17:42:06 -070063
64
Stella Laurenzoa3e97f12020-12-05 23:29:13 -080065if len(sys.argv) > 1:
66 local_bazelrc = sys.argv[1]
67else:
68 local_bazelrc = os.path.join(os.path.dirname(__file__), "configured.bazelrc")
Stella Laurenzod5b91272020-05-28 17:42:06 -070069with open(local_bazelrc, "wt") as bazelrc:
70 write_platform(bazelrc)
Stella Laurenzod5b91272020-05-28 17:42:06 -070071
72print("Wrote", local_bazelrc)