xref: /aosp_15_r20/external/cronet/third_party/protobuf/toolchain/cc_toolchain_config.bzl (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
2load(
3    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
4    "artifact_name_pattern",
5    "feature",
6    "flag_group",
7    "flag_set",
8    "tool_path",
9    "with_feature_set",
10)
11
12all_link_actions = [
13    ACTION_NAMES.cpp_link_executable,
14    ACTION_NAMES.cpp_link_dynamic_library,
15    ACTION_NAMES.cpp_link_nodeps_dynamic_library,
16]
17
18all_compile_actions = [
19    ACTION_NAMES.assemble,
20    ACTION_NAMES.preprocess_assemble,
21    ACTION_NAMES.linkstamp_compile,
22    ACTION_NAMES.c_compile,
23    ACTION_NAMES.cpp_compile,
24    ACTION_NAMES.cpp_header_parsing,
25    ACTION_NAMES.cpp_module_codegen,
26    ACTION_NAMES.cpp_module_compile,
27    ACTION_NAMES.clif_match,
28    ACTION_NAMES.lto_backend,
29]
30
31def _impl(ctx):
32  if "mingw" in ctx.attr.target_full_name:
33      artifact_name_patterns = [
34          artifact_name_pattern(
35              category_name = "executable",
36              prefix = "",
37              extension = ".exe",
38          ),
39      ]
40  else:
41      artifact_name_patterns = []
42
43  tool_paths = [
44      tool_path(
45          name = "gcc",
46          path = "/usr/local/bin/clang",
47      ),
48      tool_path(
49          name = "ld",
50          path = ctx.attr.linker_path,
51      ),
52      tool_path(
53          name = "ar",
54          path = "/usr/local/bin/llvm-ar",
55      ),
56      tool_path(
57          name = "compat-ld",
58          path = ctx.attr.linker_path,
59      ),
60      tool_path(
61          name = "cpp",
62          path = "/bin/false",
63      ),
64      tool_path(
65          name = "dwp",
66          path = "/bin/false",
67      ),
68      tool_path(
69          name = "gcov",
70          path = "/bin/false",
71      ),
72      tool_path(
73          name = "nm",
74          path = "/bin/false",
75      ),
76      tool_path(
77          name = "objcopy",
78          path = "/bin/false",
79      ),
80      tool_path(
81          name = "objdump",
82          path = "/bin/false",
83      ),
84      tool_path(
85          name = "strip",
86          path = "/bin/false",
87      ),
88  ]
89
90  linker_flags = feature(
91      name = "default_linker_flags",
92      enabled = True,
93      flag_sets = [
94          flag_set(
95              actions = all_link_actions,
96              flag_groups = [
97                  flag_group(
98                      flags = [
99                          "-B" + ctx.attr.linker_path,
100                          "-lstdc++",
101                          "--target=" + ctx.attr.target_full_name,
102                      ] + ctx.attr.extra_linker_flags,
103                  ),
104              ],
105          ),
106      ],
107  )
108
109  if "osx" in ctx.attr.target_full_name:
110    sysroot_action_set = all_link_actions
111  else:
112    sysroot_action_set = all_link_actions + all_compile_actions
113
114  sysroot_flags = feature(
115      name = "sysroot_flags",
116      #Only enable this if a sysroot was specified
117      enabled = (ctx.attr.sysroot != ""),
118      flag_sets = [
119          flag_set(
120              actions = sysroot_action_set,
121              flag_groups = [
122                  flag_group(
123                      flags = [
124                          "--sysroot",
125                          ctx.attr.sysroot,
126                      ],
127                  ),
128              ],
129          ),
130      ],
131  )
132
133  if ctx.attr.target_cpu == "x86_32":
134    bit_flag = "-m32"
135  else:
136    bit_flag = "-m64"
137  compiler_flags = feature(
138      name = "default_compile_flags",
139      enabled = True,
140      flag_sets = [
141          flag_set(
142              actions = all_compile_actions,
143              flag_groups = [
144                  flag_group(
145                      flags = [
146                          bit_flag,
147                          "-Wall",
148                          "-no-canonical-prefixes",
149                          "--target=" + ctx.attr.target_full_name,
150                          "-fvisibility=hidden",
151                      ] + ctx.attr.extra_compiler_flags + [
152                          "-isystem",
153                          ctx.attr.sysroot,
154                      ],
155                  ),
156              ],
157          ),
158          flag_set(
159              actions = all_compile_actions,
160              flag_groups = [flag_group(flags = ["-DNDEBUG", "-O3"])],
161              with_features = [with_feature_set(features = ["opt"])],
162          ),
163          flag_set(
164              actions = all_compile_actions,
165              flag_groups = [flag_group(flags = ["-g"])],
166              with_features = [with_feature_set(features = ["dbg"])],
167          ),
168          flag_set(
169              actions = all_compile_actions,
170              flag_groups = [flag_group(flags = ["-O1"])],
171              with_features = [with_feature_set(features = ["fastbuild"])],
172          ),
173      ],
174  )
175
176  features = [
177      linker_flags,
178      compiler_flags,
179      sysroot_flags,
180      feature(name = "dbg"),
181      feature(name = "opt"),
182  ]
183
184  if "mingw" in ctx.attr.target_full_name:
185      features.append(
186          feature(
187              name = "targets_windows",
188              enabled = True,
189          )
190      )
191  else:
192      features.append(
193          feature(
194              name = "supports_pic",
195              enabled = True
196          )
197      )
198
199  return cc_common.create_cc_toolchain_config_info(
200      abi_libc_version = ctx.attr.abi_version,
201      abi_version = ctx.attr.abi_version,
202      artifact_name_patterns = artifact_name_patterns,
203      ctx = ctx,
204      compiler = "clang",
205      cxx_builtin_include_directories = [
206          ctx.attr.sysroot,
207          ctx.attr.extra_include,
208          "/usr/local/include",
209          "/usr/local/lib/clang",
210      ],
211      features = features,
212      host_system_name = "local",
213      target_cpu = ctx.attr.target_cpu,
214      target_libc = ctx.attr.target_cpu,
215      target_system_name = ctx.attr.target_full_name,
216      toolchain_identifier = ctx.attr.target_full_name,
217      tool_paths = tool_paths,
218  )
219
220cc_toolchain_config = rule(
221    implementation = _impl,
222    attrs = {
223        "abi_version": attr.string(default = "local"),
224        "extra_compiler_flags": attr.string_list(),
225        "extra_include": attr.string(mandatory = False),
226        "extra_linker_flags": attr.string_list(),
227        "linker_path": attr.string(mandatory = True),
228        "sysroot": attr.string(mandatory = False),
229        "target_cpu": attr.string(mandatory = True, values = ["aarch64", "ppc64", "systemz", "x86_32", "x86_64"]),
230        "target_full_name": attr.string(mandatory = True),
231    },
232    provides = [CcToolchainConfigInfo],
233)
234