• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                            "-lm",
102                            "--target=" + ctx.attr.target_full_name,
103                        ] + ctx.attr.extra_linker_flags,
104                    ),
105                ],
106            ),
107        ],
108    )
109
110    if "osx" in ctx.attr.target_full_name:
111        sysroot_action_set = all_link_actions
112    else:
113        sysroot_action_set = all_link_actions + all_compile_actions
114
115    sysroot_flags = feature(
116        name = "sysroot_flags",
117        #Only enable this if a sysroot was specified
118        enabled = (ctx.attr.sysroot != ""),
119        flag_sets = [
120            flag_set(
121                actions = sysroot_action_set,
122                flag_groups = [
123                    flag_group(
124                        flags = [
125                            "--sysroot",
126                            ctx.attr.sysroot,
127                        ],
128                    ),
129                ],
130            ),
131        ],
132    )
133
134    if ctx.attr.target_cpu == "x86_32":
135        bit_flag = "-m32"
136    else:
137        bit_flag = "-m64"
138    compiler_flags = feature(
139        name = "default_compile_flags",
140        enabled = True,
141        flag_sets = [
142            flag_set(
143                actions = all_compile_actions,
144                flag_groups = [
145                    flag_group(
146                        flags = [
147                            bit_flag,
148                            "-Wall",
149                            "-no-canonical-prefixes",
150                            "--target=" + ctx.attr.target_full_name,
151                            "-fvisibility=hidden",
152                        ] + ctx.attr.extra_compiler_flags + [
153                            "-isystem",
154                            ctx.attr.sysroot,
155                        ],
156                    ),
157                ],
158            ),
159            flag_set(
160                actions = all_compile_actions,
161                flag_groups = [flag_group(flags = ["-DNDEBUG", "-O3"])],
162                with_features = [with_feature_set(features = ["opt"])],
163            ),
164            flag_set(
165                actions = all_compile_actions,
166                flag_groups = [flag_group(flags = ["-g"])],
167                with_features = [with_feature_set(features = ["dbg"])],
168            ),
169            flag_set(
170                actions = all_compile_actions,
171                flag_groups = [flag_group(flags = ["-O1"])],
172                with_features = [with_feature_set(features = ["fastbuild"])],
173            ),
174        ],
175    )
176
177    features = [
178        linker_flags,
179        compiler_flags,
180        sysroot_flags,
181        feature(name = "dbg"),
182        feature(name = "opt"),
183    ]
184
185    if "mingw" in ctx.attr.target_full_name:
186        features.append(
187            feature(
188                name = "targets_windows",
189                enabled = True,
190            ),
191        )
192    else:
193        features.append(
194            feature(
195                name = "supports_pic",
196                enabled = True,
197            ),
198        )
199
200    return cc_common.create_cc_toolchain_config_info(
201        abi_libc_version = ctx.attr.abi_version,
202        abi_version = ctx.attr.abi_version,
203        artifact_name_patterns = artifact_name_patterns,
204        ctx = ctx,
205        compiler = "clang",
206        cxx_builtin_include_directories = [
207            ctx.attr.sysroot,
208            ctx.attr.extra_include,
209            "/usr/local/include",
210            "/usr/local/lib/clang",
211        ],
212        features = features,
213        host_system_name = "local",
214        target_cpu = ctx.attr.target_cpu,
215        target_libc = ctx.attr.target_cpu,
216        target_system_name = ctx.attr.target_full_name,
217        toolchain_identifier = ctx.attr.target_full_name,
218        tool_paths = tool_paths,
219    )
220
221cc_toolchain_config = rule(
222    implementation = _impl,
223    attrs = {
224        "abi_version": attr.string(default = "local"),
225        "extra_compiler_flags": attr.string_list(),
226        "extra_include": attr.string(mandatory = False),
227        "extra_linker_flags": attr.string_list(),
228        "linker_path": attr.string(mandatory = True),
229        "sysroot": attr.string(mandatory = False),
230        "target_cpu": attr.string(mandatory = True, values = ["aarch64", "ppc64", "systemz", "x86_32", "x86_64"]),
231        "target_full_name": attr.string(mandatory = True),
232    },
233    provides = [CcToolchainConfigInfo],
234)
235