File: cc_toolchain_config.bzl

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (234 lines) | stat: -rw-r--r-- 6,865 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
    "artifact_name_pattern",
    "feature",
    "flag_group",
    "flag_set",
    "tool_path",
    "with_feature_set",
)

all_link_actions = [
    ACTION_NAMES.cpp_link_executable,
    ACTION_NAMES.cpp_link_dynamic_library,
    ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]

all_compile_actions = [
    ACTION_NAMES.assemble,
    ACTION_NAMES.preprocess_assemble,
    ACTION_NAMES.linkstamp_compile,
    ACTION_NAMES.c_compile,
    ACTION_NAMES.cpp_compile,
    ACTION_NAMES.cpp_header_parsing,
    ACTION_NAMES.cpp_module_codegen,
    ACTION_NAMES.cpp_module_compile,
    ACTION_NAMES.clif_match,
    ACTION_NAMES.lto_backend,
]

def _impl(ctx):
    if "mingw" in ctx.attr.target_full_name:
        artifact_name_patterns = [
            artifact_name_pattern(
                category_name = "executable",
                prefix = "",
                extension = ".exe",
            ),
        ]
    else:
        artifact_name_patterns = []

    tool_paths = [
        tool_path(
            name = "gcc",
            path = "/usr/local/bin/clang",
        ),
        tool_path(
            name = "ld",
            path = ctx.attr.linker_path,
        ),
        tool_path(
            name = "ar",
            path = "/usr/local/bin/llvm-ar",
        ),
        tool_path(
            name = "compat-ld",
            path = ctx.attr.linker_path,
        ),
        tool_path(
            name = "cpp",
            path = "/bin/false",
        ),
        tool_path(
            name = "dwp",
            path = "/bin/false",
        ),
        tool_path(
            name = "gcov",
            path = "/bin/false",
        ),
        tool_path(
            name = "nm",
            path = "/bin/false",
        ),
        tool_path(
            name = "objcopy",
            path = "/bin/false",
        ),
        tool_path(
            name = "objdump",
            path = "/bin/false",
        ),
        tool_path(
            name = "strip",
            path = "/bin/false",
        ),
    ]

    linker_flags = feature(
        name = "default_linker_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = all_link_actions,
                flag_groups = [
                    flag_group(
                        flags = [
                            "-B" + ctx.attr.linker_path,
                            "-lstdc++",
                            "-lm",
                            "--target=" + ctx.attr.target_full_name,
                        ] + ctx.attr.extra_linker_flags,
                    ),
                ],
            ),
        ],
    )

    if "osx" in ctx.attr.target_full_name:
        sysroot_action_set = all_link_actions
    else:
        sysroot_action_set = all_link_actions + all_compile_actions

    sysroot_flags = feature(
        name = "sysroot_flags",
        #Only enable this if a sysroot was specified
        enabled = (ctx.attr.sysroot != ""),
        flag_sets = [
            flag_set(
                actions = sysroot_action_set,
                flag_groups = [
                    flag_group(
                        flags = [
                            "--sysroot",
                            ctx.attr.sysroot,
                        ],
                    ),
                ],
            ),
        ],
    )

    if ctx.attr.target_cpu == "x86_32":
        bit_flag = "-m32"
    else:
        bit_flag = "-m64"
    compiler_flags = feature(
        name = "default_compile_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = all_compile_actions,
                flag_groups = [
                    flag_group(
                        flags = [
                            bit_flag,
                            "-Wall",
                            "-no-canonical-prefixes",
                            "--target=" + ctx.attr.target_full_name,
                            "-fvisibility=hidden",
                        ] + ctx.attr.extra_compiler_flags + [
                            "-isystem",
                            ctx.attr.sysroot,
                        ],
                    ),
                ],
            ),
            flag_set(
                actions = all_compile_actions,
                flag_groups = [flag_group(flags = ["-DNDEBUG", "-O3"])],
                with_features = [with_feature_set(features = ["opt"])],
            ),
            flag_set(
                actions = all_compile_actions,
                flag_groups = [flag_group(flags = ["-g"])],
                with_features = [with_feature_set(features = ["dbg"])],
            ),
            flag_set(
                actions = all_compile_actions,
                flag_groups = [flag_group(flags = ["-O1"])],
                with_features = [with_feature_set(features = ["fastbuild"])],
            ),
        ],
    )

    features = [
        linker_flags,
        compiler_flags,
        sysroot_flags,
        feature(name = "dbg"),
        feature(name = "opt"),
    ]

    if "mingw" in ctx.attr.target_full_name:
        features.append(
            feature(
                name = "targets_windows",
                enabled = True,
            ),
        )
    else:
        features.append(
            feature(
                name = "supports_pic",
                enabled = True,
            ),
        )

    return cc_common.create_cc_toolchain_config_info(
        abi_libc_version = ctx.attr.abi_version,
        abi_version = ctx.attr.abi_version,
        artifact_name_patterns = artifact_name_patterns,
        ctx = ctx,
        compiler = "clang",
        cxx_builtin_include_directories = [
            ctx.attr.sysroot,
            ctx.attr.extra_include,
            "/usr/local/include",
            "/usr/local/lib/clang",
        ],
        features = features,
        host_system_name = "local",
        target_cpu = ctx.attr.target_cpu,
        target_libc = ctx.attr.target_cpu,
        target_system_name = ctx.attr.target_full_name,
        toolchain_identifier = ctx.attr.target_full_name,
        tool_paths = tool_paths,
    )

cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {
        "abi_version": attr.string(default = "local"),
        "extra_compiler_flags": attr.string_list(),
        "extra_include": attr.string(mandatory = False),
        "extra_linker_flags": attr.string_list(),
        "linker_path": attr.string(mandatory = True),
        "sysroot": attr.string(mandatory = False),
        "target_cpu": attr.string(mandatory = True, values = ["aarch64", "ppc64", "systemz", "x86_32", "x86_64"]),
        "target_full_name": attr.string(mandatory = True),
    },
    provides = [CcToolchainConfigInfo],
)