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): |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 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! |
Stella Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 20 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 21 | if platform.system() == "Darwin": |
| 22 | print(f"build --config=macos_clang", file=bazelrc) |
| 23 | print(f"build:release --config=macos_clang_release", file=bazelrc) |
freedom" Koan-Sin Tan | 8712d40 | 2021-03-28 01:18:22 +0800 | [diff] [blame] | 24 | else: |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 25 | # If the user specified a CXX environment var, bazel will later respect that, |
| 26 | # so we just see if it says "clang". |
| 27 | cxx = os.environ.get("CXX") |
| 28 | cc = os.environ.get("CC") |
| 29 | if (cxx is not None and cc is None) or (cxx is None and cc is not None): |
| 30 | print( |
| 31 | "WARNING: Only one of CXX or CC is set, which can confuse bazel. " |
| 32 | "Recommend: set both appropriately (or none)" |
| 33 | ) |
| 34 | if cc is not None and cxx is not None: |
| 35 | # Persist the variables. |
| 36 | print(f'build --action_env CC="{cc}"', file=bazelrc) |
| 37 | print(f'build --action_env CXX="{cxx}"', file=bazelrc) |
| 38 | else: |
| 39 | print( |
| 40 | "WARNING: CC and CXX are not set, which can cause mismatches between " |
| 41 | "flag configurations and compiler. Recommend setting them explicitly." |
| 42 | ) |
freedom" Koan-Sin Tan | 8712d40 | 2021-03-28 01:18:22 +0800 | [diff] [blame] | 43 | |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 44 | if cxx is not None and "clang" in cxx: |
| 45 | print(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( |
| 50 | f"Choosing generic_gcc config by default because no CXX set or " |
| 51 | f"not recognized as clang ({cxx})" |
| 52 | ) |
| 53 | print(f"build --config=generic_gcc", file=bazelrc) |
| 54 | print(f"build:release --config=generic_gcc_release", file=bazelrc) |
Stella Laurenzo | 36781bb | 2021-02-27 19:28:25 -0800 | [diff] [blame] | 55 | |
| 56 | |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 57 | def write_platform(bazelrc): |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 58 | if platform.system() == "Windows": |
| 59 | print(f"build --config=msvc", file=bazelrc) |
| 60 | print(f"build:release --config=msvc_release", file=bazelrc) |
| 61 | else: |
| 62 | detect_unix_platform_config(bazelrc) |
| 63 | |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 64 | |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 65 | if len(sys.argv) > 1: |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 66 | local_bazelrc = sys.argv[1] |
Stella Laurenzo | a3e97f1 | 2020-12-05 23:29:13 -0800 | [diff] [blame] | 67 | else: |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 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: |
Jakub Kuderski | be24f02 | 2023-06-21 14:44:18 -0400 | [diff] [blame] | 70 | write_platform(bazelrc) |
Stella Laurenzo | d5b9127 | 2020-05-28 17:42:06 -0700 | [diff] [blame] | 71 | |
| 72 | print("Wrote", local_bazelrc) |