xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/private/toolchain/freebsd_cc_toolchain_config.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1# Copyright 2019 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A Starlark cc_toolchain configuration rule for freebsd."""
16
17load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
18load(
19    "@rules_cc//cc:cc_toolchain_config_lib.bzl",
20    "action_config",
21    "feature",
22    "flag_group",
23    "flag_set",
24    "tool",
25    "tool_path",
26    "with_feature_set",
27)
28
29all_compile_actions = [
30    ACTION_NAMES.c_compile,
31    ACTION_NAMES.cpp_compile,
32    ACTION_NAMES.linkstamp_compile,
33    ACTION_NAMES.assemble,
34    ACTION_NAMES.preprocess_assemble,
35    ACTION_NAMES.cpp_header_parsing,
36    ACTION_NAMES.cpp_module_compile,
37    ACTION_NAMES.cpp_module_codegen,
38    ACTION_NAMES.clif_match,
39    ACTION_NAMES.lto_backend,
40]
41
42all_cpp_compile_actions = [
43    ACTION_NAMES.cpp_compile,
44    ACTION_NAMES.linkstamp_compile,
45    ACTION_NAMES.cpp_header_parsing,
46    ACTION_NAMES.cpp_module_compile,
47    ACTION_NAMES.cpp_module_codegen,
48    ACTION_NAMES.clif_match,
49]
50
51all_link_actions = [
52    ACTION_NAMES.cpp_link_executable,
53    ACTION_NAMES.cpp_link_dynamic_library,
54    ACTION_NAMES.cpp_link_nodeps_dynamic_library,
55]
56
57def _impl(ctx):
58    cpu = ctx.attr.cpu
59    compiler = "compiler"
60    toolchain_identifier = "local_freebsd" if cpu == "freebsd" else "stub_armeabi-v7a"
61    host_system_name = "local" if cpu == "freebsd" else "armeabi-v7a"
62    target_system_name = "local" if cpu == "freebsd" else "armeabi-v7a"
63    target_libc = "local" if cpu == "freebsd" else "armeabi-v7a"
64    abi_version = "local" if cpu == "freebsd" else "armeabi-v7a"
65    abi_libc_version = "local" if cpu == "freebsd" else "armeabi-v7a"
66
67    objcopy_embed_data_action = action_config(
68        action_name = "objcopy_embed_data",
69        enabled = True,
70        tools = [tool(path = "/usr/bin/objcopy")],
71    )
72
73    action_configs = [objcopy_embed_data_action] if cpu == "freebsd" else []
74
75    default_link_flags_feature = feature(
76        name = "default_link_flags",
77        enabled = True,
78        flag_sets = [
79            flag_set(
80                actions = all_link_actions,
81                flag_groups = [
82                    flag_group(
83                        flags = [
84                            "-lstdc++",
85                            "-Wl,-z,relro,-z,now",
86                            "-no-canonical-prefixes",
87                        ],
88                    ),
89                ],
90            ),
91            flag_set(
92                actions = all_link_actions,
93                flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])],
94                with_features = [with_feature_set(features = ["opt"])],
95            ),
96        ],
97    )
98
99    unfiltered_compile_flags_feature = feature(
100        name = "unfiltered_compile_flags",
101        enabled = True,
102        flag_sets = [
103            flag_set(
104                actions = all_compile_actions,
105                flag_groups = [
106                    flag_group(
107                        flags = [
108                            "-no-canonical-prefixes",
109                            "-Wno-builtin-macro-redefined",
110                            "-D__DATE__=\"redacted\"",
111                            "-D__TIMESTAMP__=\"redacted\"",
112                            "-D__TIME__=\"redacted\"",
113                        ],
114                    ),
115                ],
116            ),
117        ],
118    )
119
120    supports_pic_feature = feature(name = "supports_pic", enabled = True)
121
122    default_compile_flags_feature = feature(
123        name = "default_compile_flags",
124        enabled = True,
125        flag_sets = [
126            flag_set(
127                actions = all_compile_actions,
128                flag_groups = [
129                    flag_group(
130                        flags = [
131                            "-U_FORTIFY_SOURCE",
132                            "-D_FORTIFY_SOURCE=1",
133                            "-fstack-protector",
134                            "-Wall",
135                            "-fno-omit-frame-pointer",
136                        ],
137                    ),
138                ],
139            ),
140            flag_set(
141                actions = all_compile_actions,
142                flag_groups = [flag_group(flags = ["-g"])],
143                with_features = [with_feature_set(features = ["dbg"])],
144            ),
145            flag_set(
146                actions = all_compile_actions,
147                flag_groups = [
148                    flag_group(
149                        flags = [
150                            "-g0",
151                            "-O2",
152                            "-DNDEBUG",
153                            "-ffunction-sections",
154                            "-fdata-sections",
155                        ],
156                    ),
157                ],
158                with_features = [with_feature_set(features = ["opt"])],
159            ),
160            flag_set(
161                actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
162                flag_groups = [flag_group(flags = ["-std=c++0x"])],
163            ),
164        ],
165    )
166
167    opt_feature = feature(name = "opt")
168
169    supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True)
170
171    objcopy_embed_flags_feature = feature(
172        name = "objcopy_embed_flags",
173        enabled = True,
174        flag_sets = [
175            flag_set(
176                actions = ["objcopy_embed_data"],
177                flag_groups = [flag_group(flags = ["-I", "binary"])],
178            ),
179        ],
180    )
181
182    dbg_feature = feature(name = "dbg")
183
184    user_compile_flags_feature = feature(
185        name = "user_compile_flags",
186        enabled = True,
187        flag_sets = [
188            flag_set(
189                actions = all_compile_actions,
190                flag_groups = [
191                    flag_group(
192                        flags = ["%{user_compile_flags}"],
193                        iterate_over = "user_compile_flags",
194                        expand_if_available = "user_compile_flags",
195                    ),
196                ],
197            ),
198        ],
199    )
200
201    sysroot_feature = feature(
202        name = "sysroot",
203        enabled = True,
204        flag_sets = [
205            flag_set(
206                actions = [
207                    ACTION_NAMES.c_compile,
208                    ACTION_NAMES.cpp_compile,
209                    ACTION_NAMES.linkstamp_compile,
210                    ACTION_NAMES.preprocess_assemble,
211                    ACTION_NAMES.cpp_header_parsing,
212                    ACTION_NAMES.cpp_module_compile,
213                    ACTION_NAMES.cpp_module_codegen,
214                    ACTION_NAMES.clif_match,
215                    ACTION_NAMES.lto_backend,
216                ] + all_link_actions,
217                flag_groups = [
218                    flag_group(
219                        flags = ["--sysroot=%{sysroot}"],
220                        expand_if_available = "sysroot",
221                    ),
222                ],
223            ),
224        ],
225    )
226
227    if cpu == "freebsd":
228        features = [
229            default_compile_flags_feature,
230            default_link_flags_feature,
231            supports_dynamic_linker_feature,
232            supports_pic_feature,
233            objcopy_embed_flags_feature,
234            opt_feature,
235            dbg_feature,
236            user_compile_flags_feature,
237            sysroot_feature,
238            unfiltered_compile_flags_feature,
239        ]
240    else:
241        features = [supports_dynamic_linker_feature, supports_pic_feature]
242
243    if (cpu == "freebsd"):
244        cxx_builtin_include_directories = ["/usr/lib/clang", "/usr/local/include", "/usr/include"]
245    else:
246        cxx_builtin_include_directories = []
247
248    if cpu == "freebsd":
249        tool_paths = [
250            tool_path(name = "ar", path = "/usr/bin/ar"),
251            tool_path(name = "compat-ld", path = "/usr/bin/ld"),
252            tool_path(name = "cpp", path = "/usr/bin/cpp"),
253            tool_path(name = "dwp", path = "/usr/bin/dwp"),
254            tool_path(name = "gcc", path = "/usr/bin/clang"),
255            tool_path(name = "gcov", path = "/usr/bin/gcov"),
256            tool_path(name = "ld", path = "/usr/bin/ld"),
257            tool_path(name = "nm", path = "/usr/bin/nm"),
258            tool_path(name = "objcopy", path = "/usr/bin/objcopy"),
259            tool_path(name = "objdump", path = "/usr/bin/objdump"),
260            tool_path(name = "strip", path = "/usr/bin/strip"),
261        ]
262    else:
263        tool_paths = [
264            tool_path(name = "ar", path = "/bin/false"),
265            tool_path(name = "compat-ld", path = "/bin/false"),
266            tool_path(name = "cpp", path = "/bin/false"),
267            tool_path(name = "dwp", path = "/bin/false"),
268            tool_path(name = "gcc", path = "/bin/false"),
269            tool_path(name = "gcov", path = "/bin/false"),
270            tool_path(name = "ld", path = "/bin/false"),
271            tool_path(name = "nm", path = "/bin/false"),
272            tool_path(name = "objcopy", path = "/bin/false"),
273            tool_path(name = "objdump", path = "/bin/false"),
274            tool_path(name = "strip", path = "/bin/false"),
275        ]
276
277    out = ctx.actions.declare_file(ctx.label.name)
278    ctx.actions.write(out, "Fake executable")
279    return [
280        cc_common.create_cc_toolchain_config_info(
281            ctx = ctx,
282            features = features,
283            action_configs = action_configs,
284            cxx_builtin_include_directories = cxx_builtin_include_directories,
285            toolchain_identifier = toolchain_identifier,
286            host_system_name = host_system_name,
287            target_system_name = target_system_name,
288            target_cpu = cpu,
289            target_libc = target_libc,
290            compiler = compiler,
291            abi_version = abi_version,
292            abi_libc_version = abi_libc_version,
293            tool_paths = tool_paths,
294        ),
295        DefaultInfo(
296            executable = out,
297        ),
298    ]
299
300cc_toolchain_config = rule(
301    implementation = _impl,
302    attrs = {
303        "cpu": attr.string(mandatory = True),
304    },
305    provides = [CcToolchainConfigInfo],
306    executable = True,
307)
308