Skip to content

Commit c8fd8a6

Browse files
author
Sergio Sánchez Ramírez
committed
implement custom cc_toolchain_config rule
1 parent 8dd6bc0 commit c8fd8a6

File tree

1 file changed

+139
-102
lines changed

1 file changed

+139
-102
lines changed

src/BuildToolchains.jl

Lines changed: 139 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,11 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
251251
if platforms_match(p, platform)
252252
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false, clang_use_lld=clang_use_lld))
253253
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=false, clang_use_lld=clang_use_lld))
254-
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).bzl"), toolchain_file(Bazel{compiler}(), p, host_platform))
255254
end
256255
# Host toolchains
257256
if platforms_match(p, host_platform)
258257
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true, clang_use_lld=clang_use_lld))
259258
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).meson"), toolchain_file(Meson{compiler}(), p, envs; is_host=true, clang_use_lld=clang_use_lld))
260-
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).bzl"), toolchain_file(Bazel{compiler}(), p, host_platform))
261259
end
262260
end
263261

@@ -267,18 +265,24 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
267265
if prefer_clang(p)
268266
symlink_if_exists("host_$(aatriplet(p))_clang.cmake", joinpath(dir, "host_$(aatriplet(p)).cmake"))
269267
symlink_if_exists("host_$(aatriplet(p))_clang.meson", joinpath(dir, "host_$(aatriplet(p)).meson"))
270-
symlink_if_exists("host_$(aatriplet(p))_clang.bzl", joinpath(dir, "host_$(aatriplet(p)).bzl"))
271268
symlink_if_exists("target_$(aatriplet(p))_clang.cmake", joinpath(dir, "target_$(aatriplet(p)).cmake"))
272269
symlink_if_exists("target_$(aatriplet(p))_clang.meson", joinpath(dir, "target_$(aatriplet(p)).meson"))
273-
symlink_if_exists("target_$(aatriplet(p))_clang.bzl", joinpath(dir, "target_$(aatriplet(p)).bzl"))
274270
else
275271
symlink_if_exists("host_$(aatriplet(p))_gcc.cmake", joinpath(dir, "host_$(aatriplet(p)).cmake"))
276272
symlink_if_exists("host_$(aatriplet(p))_gcc.meson", joinpath(dir, "host_$(aatriplet(p)).meson"))
277-
symlink_if_exists("host_$(aatriplet(p))_gcc.bzl", joinpath(dir, "host_$(aatriplet(p)).bzl"))
278273
symlink_if_exists("target_$(aatriplet(p))_gcc.cmake", joinpath(dir, "target_$(aatriplet(p)).cmake"))
279274
symlink_if_exists("target_$(aatriplet(p))_gcc.meson", joinpath(dir, "target_$(aatriplet(p)).meson"))
280-
symlink_if_exists("target_$(aatriplet(p))_gcc.bzl", joinpath(dir, "target_$(aatriplet(p)).bzl"))
281275
end
276+
277+
# bazel (i.e. Google) doesn't like GCC
278+
if platforms_match(p, platform)
279+
write(joinpath(dir, "target_$(aatriplet(p))_clang.bzl"), toolchain_file(Bazel{:clang}(), p, host_platform; is_host=false))
280+
end
281+
if platforms_match(p, host_platform)
282+
write(joinpath(dir, "host_$(aatriplet(p))_clang.bzl"), toolchain_file(Bazel{:clang}(), p, host_platform; is_host=true))
283+
end
284+
symlink_if_exists("host_$(aatriplet(p))_clang.bzl", joinpath(dir, "host_$(aatriplet(p)).bzl"))
285+
symlink_if_exists("target_$(aatriplet(p))_clang.bzl", joinpath(dir, "target_$(aatriplet(p)).bzl"))
282286
end
283287
end
284288

@@ -307,130 +311,163 @@ function cargo_config_file!(dir::AbstractString, platform::AbstractPlatform;
307311
end
308312
end
309313

314+
function bazel_cpu(p::AbstractPlatform)
315+
if arch(p) == "x86_64"
316+
return "k8"
317+
else
318+
return arch(p)
319+
end
320+
end
321+
310322
# TODO distinguish between clang and gcc toolchains?
311-
# TODO implement our own cc_toolchain rule
323+
# TODO in _impl, get cpu, target_libc, abi_version and abi_libc_version from host + env info
312324
function toolchain_file(bt::Bazel, p::AbstractPlatform, host_platform::AbstractPlatform; is_host::Bool=false, clang_use_lld::Bool=false)
313-
target = triplet(p) # TODO fix this
314-
full_target = string(p) # TODO fix this
325+
target = triplet(p)
326+
aatarget = aatriplet(p)
315327
host_target = triplet(host_platform) # TODO fix this
316328

317329
return """
318330
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
319-
320-
def ygg_cc_toolchain():
321-
bb_target = "aarch64-linux-gnu"
322-
bb_full_target = "aarch64-linux-gnu-libgfortran5-cxx11-gpu+none-mode+opt"
323-
cpu = "aarch64"
324-
toolchain_identifier = "ygg_toolchain"
325-
target_system_name = ""
326-
supports_start_end_lib = False
327-
328-
cc_toolchain(
329-
name = "ygg_target_toolchain",
330-
all_files = ":empty",
331-
compiler_files = ":empty",
332-
dwp_files = ":empty",
333-
linker_files = ":empty",
334-
objcopy_files = ":empty",
335-
strip_files = ":empty",
336-
supports_param_files = 1,
337-
toolchain_config = ":ygg_target_toolchain_config",
338-
toolchain_identifier = "ygg_toolchain",
339-
)
340-
341-
cc_toolchain_config(
342-
name = "ygg_target_toolchain_config",
343-
cpu = cpu,
344-
compiler = "compiler",
345-
toolchain_identifier = toolchain_identifier,
346-
target_system_name = target_system_name,
347-
target_libc = "",
348-
abi_libc_version = "local",
349-
abi_version = "local",
331+
load(
332+
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
333+
"action_config",
334+
"artifact_name_pattern",
335+
"feature",
336+
"feature_set",
337+
"flag_group",
338+
"flag_set",
339+
"tool",
340+
"tool_path",
341+
"variable_with_value",
342+
"with_feature_set",
343+
)
344+
345+
def _impl(ctx):
346+
return cc_common.create_cc_toolchain_config_info(
347+
ctx = ctx,
348+
features = [...], # TODO
350349
cxx_builtin_include_directories = [
351-
"/opt/$(target)/lib/gcc/$(target)/10.2.0/include",
352-
"/opt/$(target)/lib/gcc/$(target)/10.2.0/include-fixed",
353-
"/opt/$(target)/$(target)/include",
354-
"/opt/$(target)/$(target)/sys-root/usr/include",
355-
"/opt/$(target)/$(target)/include/c++/10.2.0",
356-
"/opt/$(target)/$(target)/include/c++/10.2.0/$(target)",
357-
"/opt/$(target)/$(target)/include/c++/10.2.0/backward",
358-
"/opt/$(target)/$(target)/include/c++/10.2.0/parallel",
350+
# TODO maybe requires to add them to `compile_flags` with `-isystem`
351+
# TODO are these really required?
352+
# "/opt/$(aatarget)/lib/gcc/$(aatarget)/10.2.0/include",
353+
# "/opt/$(aatarget)/lib/gcc/$(aatarget)/10.2.0/include-fixed",
354+
# TODO these paths should use %sysroot% instead of hardcoding the sysroot
355+
# "/opt/$(aatarget)/$(aatarget)/include",
356+
# "/opt/$(aatarget)/$(aatarget)/sys-root/usr/include",
357+
# "/opt/$(aatarget)/$(aatarget)/include/c++/10.2.0",
358+
# "/opt/$(aatarget)/$(aatarget)/include/c++/10.2.0/$(aatarget)",
359+
# "/opt/$(aatarget)/$(aatarget)/include/c++/10.2.0/backward",
360+
# "/opt/$(aatarget)/$(aatarget)/include/c++/10.2.0/parallel",
361+
"%sysroot%/../include",
362+
"%sysroot%/usr/include",
363+
"%sysroot%/../include/c++/10.2.0",
364+
"%sysroot%/../include/c++/10.2.0/$(aatarget)",
365+
"%sysroot%/../include/c++/10.2.0/backward",
366+
"%sysroot%/../include/c++/10.2.0/parallel",
359367
],
360-
tool_paths = {
361-
"ar": "/opt/bin/$(full_target)/ar",
362-
"as": "/opt/bin/$(full_target)/as",
363-
"c++": "/opt/bin/$(full_target)/c++",
364-
"c++filt": "/opt/bin/$(full_target)/c++filt",
365-
"cc": "/opt/bin/$(full_target)/cc",
366-
"clang": "/opt/bin/$(full_target)/clang",
367-
"clang++": "/opt/bin/$(full_target)/clang++",
368-
"cpp": "/opt/bin/$(full_target)/cpp",
369-
"f77": "/opt/bin/$(full_target)/f77",
368+
toolchain_identifier = ctx.attr.toolchain_identifier,
369+
target_system_name = ctx.attr.target_system_name,
370+
target_cpu = "$(bazel_cpu(p))", # TODO
371+
target_libc = "unknown", # TODO
372+
compiler = "clang",
373+
abi_version = "unknown", # TODO
374+
abi_libc_version = "unknown", # TODO
375+
tool_paths = [
376+
tool_path(name = "ar", path = "/opt/bin/$(target)/ar"),
377+
tool_path(name = "as", path = "/opt/bin/$(target)/as"),
378+
tool_path(name = "c++", path = "/opt/bin/$(target)/c++"),
379+
tool_path(name = "c++filt", path = "/opt/bin/$(target)/c++filt"),
380+
tool_path(name = "cc", path = "/opt/bin/$(target)/cc"),
381+
tool_path(name = "clang", path = "/opt/bin/$(target)/clang"),
382+
tool_path(name = "clang++", path = "/opt/bin/$(target)/clang++"),
383+
tool_path(name = "cpp", path = "/opt/bin/$(target)/cpp"),
384+
tool_path(name = "f77", path = "/opt/bin/$(target)/f77"),
385+
370386
# WARN we force to use clang instead of gcc
371-
"g++": "/opt/bin/$(full_target)/clang++",
372-
"gcc": "/opt/bin/$(full_target)/clang",
373-
"gfortran": "/opt/bin/$(full_target)/gfortran",
374-
"ld": "/opt/bin/$(full_target)/ld",
375-
"ld.lld": "/opt/bin/$(full_target)/ld.lld",
376-
"libtool": "/opt/bin/$(full_target)/libtool",
377-
"lld": "/opt/bin/$(full_target)/lld",
378-
"nm": "/opt/bin/$(full_target)/nm",
379-
"objcopy": "/opt/bin/$(full_target)/objcopy",
380-
"patchelf": "/opt/bin/$(full_target)/patchelf",
381-
"ranlib": "/opt/bin/$(full_target)/ranlib",
382-
"readelf": "/opt/bin/$(full_target)/readelf",
383-
"strip": "/opt/bin/$(full_target)/strip",
387+
tool_path(name = "g++", path = "/opt/bin/$(target)/clang++"),
388+
tool_path(name = "gcc", path = "/opt/bin/$(target)/clang"),
389+
390+
tool_path(name = "gfortran", path = "/opt/bin/$(target)/gfortran"),
391+
tool_path(name = "ld", path = "/opt/bin/$(target)/ld"),
392+
tool_path(name = "ld.lld", path = "/opt/bin/$(target)/ld.lld"),
393+
tool_path(name = "libtool", path = "/opt/bin/$(target)/libtool"),
394+
tool_path(name = "lld", path = "/opt/bin/$(target)/lld"),
395+
tool_path(name = "nm", path = "/opt/bin/$(target)/nm"),
396+
tool_path(name = "objcopy", path = "/opt/bin/$(target)/objcopy"),
397+
tool_path(name = "patchelf", path = "/opt/bin/$(target)/patchelf"),
398+
tool_path(name = "ranlib", path = "/opt/bin/$(target)/ranlib"),
399+
tool_path(name = "readelf", path = "/opt/bin/$(target)/readelf"),
400+
tool_path(name = "strip", path = "/opt/bin/$(target)/strip"),
401+
384402
# from host
385-
"llvm-cov": "/opt/$(host_target)/bin/llvm-cov",
386-
"llvm-profdata": "/opt/$(host_target)/bin/llvm-profdata",
387-
"objdump": "/usr/bin/objdump",
388-
},
389-
compile_flags = [
403+
tool_path(name = "llvm-cov", path = "/opt/$(host_target)/bin/llvm-cov"),
404+
tool_path(name = "llvm-profdata", path = "/opt/$(host_target)/bin/llvm-profdata"),
405+
tool_path(name = "objdump", path = "/usr/bin/objdump"),
406+
],
407+
builtin_sysroot = "/opt/$(aatarget)/$(aatarget)/sys-root/",
408+
)
409+
410+
ygg_cc_toolchain_config = rule(
411+
implementation = _impl,
412+
attrs = {
413+
toolchain_identifier = attr.string(mandatory = True),
414+
target_system_name = attr.string(mandatory = True),
415+
compile_flags = attr.string_list(default = [
390416
"-fstack-protector",
391417
"-Wall",
392418
"-Wunused-but-set-parameter",
393419
"-Wno-free-nonheap-object",
394420
"-fno-omit-frame-pointer",
395-
# TODO cxx_builtin_include_directories doesn't seem to be working, so we add the INCLUDE_PATHs manually
396-
"-isystem /opt/$(target)/lib/gcc/$(target)/10.2.0/include",
397-
"-isystem /opt/$(target)/lib/gcc/$(target)/10.2.0/include-fixed",
398-
"-isystem /opt/$(target)/$(target)/include",
399-
"-isystem /opt/$(target)/$(target)/sys-root/usr/include",
400-
"-isystem /opt/$(target)/$(target)/include/c++/10.2.0",
401-
"-isystem /opt/$(target)/$(target)/include/c++/10.2.0/$(target)",
402-
"-isystem /opt/$(target)/$(target)/include/c++/10.2.0/backward",
403-
"-isystem /opt/$(target)/$(target)/include/c++/10.2.0/parallel",
404-
],
405-
opt_compile_flags = [
421+
]),
422+
opt_compile_flags = attr.string_list(default = [
406423
"-g0",
407424
"-O2",
408425
"-D_FORTIFY_SOURCE=1",
409426
"-DNDEBUG",
410427
"-ffunction-sections",
411428
"-fdata-sections",
412-
# "-stdlib=libstdc++",
413-
],
414-
dbg_compile_flags = ["-g"],
415-
link_flags = [],
416-
link_libs = [
417-
"-lstdc++",
418-
"-lm",
419-
],
420-
opt_link_flags = ["-Wl,--gc-sections"],
421-
unfiltered_compile_flags = [
429+
]),
430+
dbg_compile_flags = attr.string_list(["-g]),
431+
link_flags = attr.string_list(default = []),
432+
link_libs = attr.string_list(default = ["-lstdc++"]),
433+
opt_link_flags = attr.string_list(default = ["-Wl,--gc-sections"]),
434+
unfiltered_compile_flags = attr.string_list(default = [
422435
"-no-canonical-prefixes",
423436
"-Wno-builtin-macro-redefined",
424437
"-D__DATE__=\"redacted\"",
425438
"-D__TIMESTAMP__=\"redacted\"",
426439
"-D__TIME__=\"redacted\"",
427440
"-Wno-unused-command-line-argument",
428441
"-Wno-gnu-offsetof-extensions",
429-
],
430-
builtin_sysroot = "/opt/$(target)/$(target)/sys-root/",
431-
coverage_compile_flags = ["--coverage"],
432-
coverage_link_flags = ["--coverage"],
433-
host_system_name = "linux",
442+
]),
443+
coverage_compile_flags = attr.string_list(default = ["--coverage"]),
444+
coverage_link_flags = attr.string_list(default = ["--coverage"]),
445+
},
446+
provides = [CcToolchainConfigInfo],
447+
)
448+
449+
def ygg_cc_toolchain():
450+
cpu = "aarch64"
451+
toolchain_identifier = "ygg_$(is_host ? "host" : "target")_toolchain"
452+
supports_start_end_lib = False
453+
454+
cc_toolchain(
455+
name = "ygg_$(is_host ? "host" : "target")_toolchain",
456+
all_files = ":empty",
457+
compiler_files = ":empty",
458+
dwp_files = ":empty",
459+
linker_files = ":empty",
460+
objcopy_files = ":empty",
461+
strip_files = ":empty",
462+
supports_param_files = 1,
463+
toolchain_config = ":ygg_$(is_host ? "host" : "target")_toolchain_config",
464+
toolchain_identifier = toolchain_identifier,
465+
)
466+
467+
ygg_cc_toolchain_config(
468+
name = "ygg_target_toolchain_config",
469+
toolchain_identifier = toolchain_identifier,
470+
target_system_name = "$(os(p))"
434471
# TODO gcc doesn't support it, only put it on clang (maybe even only for clang on aarch64-darwin?)
435472
# supports_start_end_lib = supports_start_end_lib,
436473
)

0 commit comments

Comments
 (0)