blob: abb877acfcee87a02b0b4e81e4bf50fde0a170d8 [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):
Jakub Kuderskibe24f022023-06-21 14:44:18 -040014 # 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 Laurenzo36781bb2021-02-27 19:28:25 -080020
Jakub Kuderskibe24f022023-06-21 14:44:18 -040021 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 Tan8712d402021-03-28 01:18:22 +080024 else:
Jakub Kuderskibe24f022023-06-21 14:44:18 -040025 # 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 Tan8712d402021-03-28 01:18:22 +080043
Jakub Kuderskibe24f022023-06-21 14:44:18 -040044 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 Laurenzo36781bb2021-02-27 19:28:25 -080055
56
Stella Laurenzod5b91272020-05-28 17:42:06 -070057def write_platform(bazelrc):
Jakub Kuderskibe24f022023-06-21 14:44:18 -040058 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 Laurenzod5b91272020-05-28 17:42:06 -070064
Stella Laurenzoa3e97f12020-12-05 23:29:13 -080065if len(sys.argv) > 1:
Jakub Kuderskibe24f022023-06-21 14:44:18 -040066 local_bazelrc = sys.argv[1]
Stella Laurenzoa3e97f12020-12-05 23:29:13 -080067else:
Jakub Kuderskibe24f022023-06-21 14:44:18 -040068 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:
Jakub Kuderskibe24f022023-06-21 14:44:18 -040070 write_platform(bazelrc)
Stella Laurenzod5b91272020-05-28 17:42:06 -070071
72print("Wrote", local_bazelrc)