xref: /aosp_15_r20/external/pigweed/pw_toolchain/clang_tools.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14import("//build_overrides/pigweed.gni")
15import("//build_overrides/pigweed_environment.gni")
16import("$dir_pw_toolchain/rbe.gni")
17
18_default_llvm_prefix = ""
19_default_rust_prefix = ""
20
21# If Pigweed's CIPD environment setup was run, assume a LLVM toolchain and Rust
22# compiler are present there.
23if (defined(pw_env_setup_CIPD_PIGWEED)) {
24  _default_llvm_prefix = pw_env_setup_CIPD_PIGWEED + "/bin/"
25  _default_rust_prefix = pw_env_setup_CIPD_PIGWEED + "/rust/bin/"
26}
27
28declare_args() {
29  # This flag allows you to specify a prefix to use for clang, clang++,
30  # and llvm-ar binaries to use when compiling with a clang-based toolchain.
31  # This is useful for debugging toolchain-related issues by building with an
32  # externally-provided toolchain.
33  #
34  # Pigweed toolchains should NOT override this variable so projects or users
35  # can control it via `.gn` or by setting it as a regular gn argument (e.g.
36  # `gn gen --args='pw_toolchain_CLANG_PREFIX=/path/to/my-llvm-'`).
37  #
38  # Examples:
39  #   pw_toolchain_CLANG_PREFIX = ""
40  #   command: "clang" (from PATH)
41  #
42  #   pw_toolchain_CLANG_PREFIX = "my-"
43  #   command: "my-clang" (from PATH)
44  #
45  #   pw_toolchain_CLANG_PREFIX = "/bin/my-"
46  #   command: "/bin/my-clang" (absolute path)
47  #
48  #   pw_toolchain_CLANG_PREFIX = "//environment/clang_next/"
49  #   command: "../environment/clang_next/clang" (relative path)
50  #
51  # GN templates should use `pw_toolchain_clang_tools.*` to get the intended
52  # command string rather than relying directly on pw_toolchain_CLANG_PREFIX.
53  #
54  # If the prefix begins with "//", it will be rebased to be relative to the
55  # root build directory.
56  pw_toolchain_CLANG_PREFIX = _default_llvm_prefix
57
58  # This flag allows you to specify a prefix for rustc.
59  #
60  # This follows the same rules as pw_toolchain_CLANG_PREFIX, see above for
61  # more information.
62  #
63  # If the prefix begins with "//", it will be rebased to be relative to the
64  # root build directory.
65  pw_toolchain_RUST_PREFIX = _default_rust_prefix
66}
67
68pw_toolchain_clang_tools = {
69  ar = "llvm-ar"
70  cc = "clang"
71  cxx = "clang++"
72  ld = cxx
73  llvm_cov = "llvm-cov"
74  llvm_profdata = "llvm-profdata"
75  rustc = "rustc"
76
77  _toolchain_prefix = pw_toolchain_CLANG_PREFIX
78  if (_toolchain_prefix != "") {
79    # If the prefix is a GN-absolute path, rebase it so it's relative to the
80    # root of the build directory.
81    _split_prefix = string_split(_toolchain_prefix, "//")
82    if (_split_prefix[0] == "") {
83      _toolchain_prefix = rebase_path(_toolchain_prefix, root_build_dir)
84    }
85    if (host_os == "win") {
86      _toolchain_prefix = "./" + _toolchain_prefix
87      _toolchain_prefix = string_replace(_toolchain_prefix, "/", "\\")
88    }
89    ar = _toolchain_prefix + ar
90    cc = _toolchain_prefix + cc
91    cxx = _toolchain_prefix + cxx
92    ld = _toolchain_prefix + ld
93    llvm_cov = _toolchain_prefix + llvm_cov
94    llvm_profdata = _toolchain_prefix + llvm_profdata
95  }
96
97  _rust_prefix = pw_toolchain_RUST_PREFIX
98  if (host_os == "win") {
99    _rust_prefix = string_replace(_rust_prefix, "/", "\\")
100  }
101  if (_rust_prefix != "") {
102    # If the prefix is a GN-absolute path, rebase it so it's relative to the
103    # root of the build directory.
104    _split_rust_prefix = string_split(_rust_prefix, "//")
105    if (_split_rust_prefix[0] == "") {
106      _rust_prefix = rebase_path(_rust_prefix, root_build_dir)
107    }
108    rustc = _rust_prefix + rustc
109  }
110
111  if (pw_toolchain_USE_RBE) {
112    _rbe_debug_flag = ""
113    if (pw_toolchain_RBE_DEBUG) {
114      _rbe_debug_flag = " -v"
115    }
116    _exec_root = rebase_path("//")
117    _rewrapper_binary = "rewrapper"
118    _pw_rbe_config = pw_rbe_clang_config
119    _rbe_toolchain_prefix =
120        _rewrapper_binary +
121        " --labels=type=compile,lang=cpp,compiler=clang --cfg=" +
122        _pw_rbe_config + " --exec_root=" + _exec_root + " -- "
123
124    cc = _rbe_toolchain_prefix + cc + _rbe_debug_flag
125    cxx = _rbe_toolchain_prefix + cxx + _rbe_debug_flag
126  }
127}
128