Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 1 | # Copyright 2020 The IREE Authors |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 2 | # |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 3 | # 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 Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 6 | |
| 7 | import platform |
| 8 | import os |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | |
Stella Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 13 | def 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 Tan | 8712d40 | 2021-03-28 01:18:22 +0800 | [diff] [blame] | 22 | print(f"build --config=macos_clang", file=bazelrc) |
| 23 | print(f"build:release --config=macos_clang_release", file=bazelrc) |
Stella Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 24 | else: |
Stella Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 25 | |
freedom" Koan-Sin Tan | 8712d40 | 2021-03-28 01:18:22 +0800 | [diff] [blame] | 26 | # 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 Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 53 | |
| 54 | |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 55 | def write_platform(bazelrc): |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 56 | if platform.system() == "Windows": |
Stella Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 57 | 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 Tan | 4c49a0c | 2020-12-30 03:46:29 +0800 | [diff] [blame] | 61 | if not (platform.system() == "Darwin"): |
Geoffrey Martin-Noble | f03c3c1 | 2021-02-26 10:44:03 -0800 | [diff] [blame] | 62 | print("common --config=non_darwin", file=bazelrc) |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 63 | |
| 64 | |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 65 | if len(sys.argv) > 1: |
| 66 | local_bazelrc = sys.argv[1] |
| 67 | else: |
| 68 | local_bazelrc = os.path.join(os.path.dirname(__file__), "configured.bazelrc") |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 69 | with open(local_bazelrc, "wt") as bazelrc: |
| 70 | write_platform(bazelrc) |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 71 | |
| 72 | print("Wrote", local_bazelrc) |