• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 and OpenBSD."""
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)  # buildifier: disable=deprecated-function
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    is_bsd = cpu == "freebsd" or cpu == "openbsd"
60    compiler = "compiler"
61    toolchain_identifier = "local_{}".format(cpu) if is_bsd else "stub_armeabi-v7a"
62    host_system_name = "local" if is_bsd else "armeabi-v7a"
63    target_system_name = "local" if is_bsd else "armeabi-v7a"
64    target_libc = "local" if is_bsd else "armeabi-v7a"
65    abi_version = "local" if is_bsd else "armeabi-v7a"
66    abi_libc_version = "local" if is_bsd else "armeabi-v7a"
67
68    objcopy_embed_data_action = action_config(
69        action_name = "objcopy_embed_data",
70        enabled = True,
71        tools = [tool(path = "/usr/bin/objcopy")],
72    )
73
74    action_configs = [objcopy_embed_data_action] if is_bsd else []
75
76    default_link_flags_feature = feature(
77        name = "default_link_flags",
78        enabled = True,
79        flag_sets = [
80            flag_set(
81                actions = all_link_actions,
82                flag_groups = [
83                    flag_group(
84                        flags = [
85                            "-lstdc++",
86                            "-Wl,-z,relro,-z,now",
87                            "-no-canonical-prefixes",
88                        ],
89                    ),
90                ],
91            ),
92            flag_set(
93                actions = all_link_actions,
94                flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])],
95                with_features = [with_feature_set(features = ["opt"])],
96            ),
97        ],
98    )
99
100    unfiltered_compile_flags_feature = feature(
101        name = "unfiltered_compile_flags",
102        enabled = True,
103        flag_sets = [
104            flag_set(
105                actions = all_compile_actions,
106                flag_groups = [
107                    flag_group(
108                        flags = [
109                            "-no-canonical-prefixes",
110                            "-Wno-builtin-macro-redefined",
111                            "-D__DATE__=\"redacted\"",
112                            "-D__TIMESTAMP__=\"redacted\"",
113                            "-D__TIME__=\"redacted\"",
114                        ],
115                    ),
116                ],
117            ),
118        ],
119    )
120
121    supports_pic_feature = feature(name = "supports_pic", enabled = True)
122
123    default_compile_flags_feature = feature(
124        name = "default_compile_flags",
125        enabled = True,
126        flag_sets = [
127            flag_set(
128                actions = all_compile_actions,
129                flag_groups = [
130                    flag_group(
131                        flags = [
132                            "-U_FORTIFY_SOURCE",
133                            "-D_FORTIFY_SOURCE=1",
134                            "-fstack-protector",
135                            "-Wall",
136                            "-fno-omit-frame-pointer",
137                        ],
138                    ),
139                ],
140            ),
141            flag_set(
142                actions = all_compile_actions,
143                flag_groups = [flag_group(flags = ["-g"])],
144                with_features = [with_feature_set(features = ["dbg"])],
145            ),
146            flag_set(
147                actions = all_compile_actions,
148                flag_groups = [
149                    flag_group(
150                        flags = [
151                            "-g0",
152                            "-O2",
153                            "-DNDEBUG",
154                            "-ffunction-sections",
155                            "-fdata-sections",
156                        ],
157                    ),
158                ],
159                with_features = [with_feature_set(features = ["opt"])],
160            ),
161            flag_set(
162                actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
163                flag_groups = [flag_group(flags = ["-std=c++17"])],
164            ),
165        ],
166    )
167
168    opt_feature = feature(name = "opt")
169
170    supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True)
171
172    objcopy_embed_flags_feature = feature(
173        name = "objcopy_embed_flags",
174        enabled = True,
175        flag_sets = [
176            flag_set(
177                actions = ["objcopy_embed_data"],
178                flag_groups = [flag_group(flags = ["-I", "binary"])],
179            ),
180        ],
181    )
182
183    dbg_feature = feature(name = "dbg")
184
185    user_compile_flags_feature = feature(
186        name = "user_compile_flags",
187        enabled = True,
188        flag_sets = [
189            flag_set(
190                actions = all_compile_actions,
191                flag_groups = [
192                    flag_group(
193                        flags = ["%{user_compile_flags}"],
194                        iterate_over = "user_compile_flags",
195                        expand_if_available = "user_compile_flags",
196                    ),
197                ],
198            ),
199        ],
200    )
201
202    sysroot_feature = feature(
203        name = "sysroot",
204        enabled = True,
205        flag_sets = [
206            flag_set(
207                actions = [
208                    ACTION_NAMES.c_compile,
209                    ACTION_NAMES.cpp_compile,
210                    ACTION_NAMES.linkstamp_compile,
211                    ACTION_NAMES.preprocess_assemble,
212                    ACTION_NAMES.cpp_header_parsing,
213                    ACTION_NAMES.cpp_module_compile,
214                    ACTION_NAMES.cpp_module_codegen,
215                    ACTION_NAMES.clif_match,
216                    ACTION_NAMES.lto_backend,
217                ] + all_link_actions,
218                flag_groups = [
219                    flag_group(
220                        flags = ["--sysroot=%{sysroot}"],
221                        expand_if_available = "sysroot",
222                    ),
223                ],
224            ),
225        ],
226    )
227
228    if is_bsd:
229        features = [
230            default_compile_flags_feature,
231            default_link_flags_feature,
232            supports_dynamic_linker_feature,
233            supports_pic_feature,
234            objcopy_embed_flags_feature,
235            opt_feature,
236            dbg_feature,
237            user_compile_flags_feature,
238            sysroot_feature,
239            unfiltered_compile_flags_feature,
240        ]
241    else:
242        features = [supports_dynamic_linker_feature, supports_pic_feature]
243
244    if (is_bsd):
245        cxx_builtin_include_directories = ["/usr/lib/clang", "/usr/local/include", "/usr/include"]
246    else:
247        cxx_builtin_include_directories = []
248
249    if is_bsd:
250        tool_paths = [
251            tool_path(name = "ar", path = "/usr/bin/ar"),
252            tool_path(name = "compat-ld", path = "/usr/bin/ld"),
253            tool_path(name = "cpp", path = "/usr/bin/cpp"),
254            tool_path(name = "dwp", path = "/usr/bin/dwp"),
255            tool_path(name = "gcc", path = "/usr/bin/clang"),
256            tool_path(name = "gcov", path = "/usr/bin/gcov"),
257            tool_path(name = "ld", path = "/usr/bin/ld"),
258            tool_path(name = "nm", path = "/usr/bin/nm"),
259            tool_path(name = "objcopy", path = "/usr/bin/objcopy"),
260            tool_path(name = "objdump", path = "/usr/bin/objdump"),
261            tool_path(name = "strip", path = "/usr/bin/strip"),
262        ]
263    else:
264        tool_paths = [
265            tool_path(name = "ar", path = "/bin/false"),
266            tool_path(name = "compat-ld", path = "/bin/false"),
267            tool_path(name = "cpp", path = "/bin/false"),
268            tool_path(name = "dwp", path = "/bin/false"),
269            tool_path(name = "gcc", path = "/bin/false"),
270            tool_path(name = "gcov", path = "/bin/false"),
271            tool_path(name = "ld", path = "/bin/false"),
272            tool_path(name = "nm", path = "/bin/false"),
273            tool_path(name = "objcopy", path = "/bin/false"),
274            tool_path(name = "objdump", path = "/bin/false"),
275            tool_path(name = "strip", path = "/bin/false"),
276        ]
277
278    out = ctx.actions.declare_file(ctx.label.name)
279    ctx.actions.write(out, "Fake executable")
280    return [
281        cc_common.create_cc_toolchain_config_info(
282            ctx = ctx,
283            features = features,
284            action_configs = action_configs,
285            cxx_builtin_include_directories = cxx_builtin_include_directories,
286            toolchain_identifier = toolchain_identifier,
287            host_system_name = host_system_name,
288            target_system_name = target_system_name,
289            target_cpu = cpu,
290            target_libc = target_libc,
291            compiler = compiler,
292            abi_version = abi_version,
293            abi_libc_version = abi_libc_version,
294            tool_paths = tool_paths,
295        ),
296        DefaultInfo(
297            executable = out,
298        ),
299    ]
300
301cc_toolchain_config = rule(
302    implementation = _impl,
303    attrs = {
304        "cpu": attr.string(mandatory = True),
305    },
306    provides = [CcToolchainConfigInfo],
307    executable = True,
308)
309