Skip to content

Commit 8dd6bc0

Browse files
author
Sergio Sánchez Ramírez
committed
prototype bazel toolchain
1 parent a79769d commit 8dd6bc0

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

src/BuildToolchains.jl

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ abstract type AbstractBuildToolchain{C} end
22

33
struct CMake{C} <: AbstractBuildToolchain{C} end
44
struct Meson{C} <: AbstractBuildToolchain{C} end
5+
struct Bazel{C} <: AbstractBuildToolchain{C} end
56

67
c_compiler(::AbstractBuildToolchain{:clang}) = "clang"
78
cxx_compiler(::AbstractBuildToolchain{:clang}) = "clang++"
@@ -250,11 +251,13 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
250251
if platforms_match(p, platform)
251252
write(joinpath(dir, "target_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=false, clang_use_lld=clang_use_lld))
252253
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))
253255
end
254256
# Host toolchains
255257
if platforms_match(p, host_platform)
256258
write(joinpath(dir, "host_$(aatriplet(p))_$(compiler).cmake"), toolchain_file(CMake{compiler}(), p, host_platform; is_host=true, clang_use_lld=clang_use_lld))
257259
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))
258261
end
259262
end
260263

@@ -264,13 +267,17 @@ function generate_toolchain_files!(platform::AbstractPlatform, envs::Dict{String
264267
if prefer_clang(p)
265268
symlink_if_exists("host_$(aatriplet(p))_clang.cmake", joinpath(dir, "host_$(aatriplet(p)).cmake"))
266269
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"))
267271
symlink_if_exists("target_$(aatriplet(p))_clang.cmake", joinpath(dir, "target_$(aatriplet(p)).cmake"))
268272
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"))
269274
else
270275
symlink_if_exists("host_$(aatriplet(p))_gcc.cmake", joinpath(dir, "host_$(aatriplet(p)).cmake"))
271276
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"))
272278
symlink_if_exists("target_$(aatriplet(p))_gcc.cmake", joinpath(dir, "target_$(aatriplet(p)).cmake"))
273279
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"))
274281
end
275282
end
276283
end
@@ -299,3 +306,133 @@ function cargo_config_file!(dir::AbstractString, platform::AbstractPlatform;
299306
""")
300307
end
301308
end
309+
310+
# TODO distinguish between clang and gcc toolchains?
311+
# TODO implement our own cc_toolchain rule
312+
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
315+
host_target = triplet(host_platform) # TODO fix this
316+
317+
return """
318+
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",
350+
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",
359+
],
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",
370+
# 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",
384+
# 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 = [
390+
"-fstack-protector",
391+
"-Wall",
392+
"-Wunused-but-set-parameter",
393+
"-Wno-free-nonheap-object",
394+
"-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 = [
406+
"-g0",
407+
"-O2",
408+
"-D_FORTIFY_SOURCE=1",
409+
"-DNDEBUG",
410+
"-ffunction-sections",
411+
"-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 = [
422+
"-no-canonical-prefixes",
423+
"-Wno-builtin-macro-redefined",
424+
"-D__DATE__=\"redacted\"",
425+
"-D__TIMESTAMP__=\"redacted\"",
426+
"-D__TIME__=\"redacted\"",
427+
"-Wno-unused-command-line-argument",
428+
"-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",
434+
# TODO gcc doesn't support it, only put it on clang (maybe even only for clang on aarch64-darwin?)
435+
# supports_start_end_lib = supports_start_end_lib,
436+
)
437+
"""
438+
end

0 commit comments

Comments
 (0)