Skip to content

Commit 087e705

Browse files
authored
[Runner] Adjust clang flags to make it work slightly better on Linux (#405)
* [Runner] Adjust `clang` flags to make it work slightly better on Linux * [Runner] Remove completely `${sysroot}/include` from headers search paths for clang Doesn't seem to be useful (it's empty for all platforms as far as I could see) and it's in the default search path anyway: ```console sandbox:${WORKSPACE} # /opt/x86_64-linux-musl/bin/clang -target ${target} --sysroot=/opt/${target}/${target}/sys-root -v -E -x c - < /dev/null clang version 18.1.7 (/home/tim/.cache/BinaryBuilder/downloads/clones/llvm-project.git-1df819a03ecf6890e3787b27bfd4f160aeeeeacd50a98d003be8b0893f11a9be 768118d1ad38bf13c545828f67bd6b474d61fc55) Target: aarch64-unknown-linux-gnu Thread model: posix InstalledDir: /opt/x86_64-linux-musl/bin (in-process) "/opt/x86_64-linux-musl/bin/clang-18" -cc1 -triple aarch64-unknown-linux-gnu -E -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=non-leaf -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu generic -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon -target-abi aapcs -debugger-tuning=gdb -fdebug-compilation-dir=/workspace -v -fcoverage-compilation-dir=/workspace -resource-dir /opt/x86_64-linux-musl/lib/clang/18 -isysroot /opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root -internal-isystem /opt/x86_64-linux-musl/lib/clang/18/include -internal-isystem /opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root/usr/local/include -internal-externc-isystem /opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root/include -internal-externc-isystem /opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root/usr/include -source-date-epoch 0 -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -target-feature +outline-atomics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o - -x c - clang -cc1 version 18.1.7 based upon LLVM 18.1.7 default target x86_64-alpine-linux-musl ignoring nonexistent directory "/opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root/usr/local/include" ignoring nonexistent directory "/opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root/include" /opt/x86_64-linux-musl/lib/clang/18/include /opt/aarch64-linux-gnu/aarch64-linux-gnu/sys-root/usr/include End of search list. ``` * [Runner] Shuffle some flags around to fix headers search paths order * Bump version
1 parent bcde7e8 commit 087e705

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BinaryBuilderBase"
22
uuid = "7f725544-6523-48cd-82d1-3fa08ff4056e"
33
authors = ["Elliot Saba <staticfloat@gmail.com>"]
4-
version = "1.35.0"
4+
version = "1.35.1"
55

66
[deps]
77
Bzip2_jll = "6e34b625-4abd-537c-b88f-471c36dfa7a0"

src/Runner.jl

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
394394
end
395395

396396
function sanitize_compile_flags!(p::AbstractPlatform, flags::Vector{String})
397-
san = sanitize(p)
398397
if sanitize(p) !== nothing
399398
if sanitize(p) == "memory"
400399
append!(flags, ["-fsanitize=memory"])
@@ -427,35 +426,52 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
427426
if lock_microarchitecture
428427
append!(flags, get_march_flags(arch(p), march(p), "clang"))
429428
end
429+
430+
append!(flags, [
431+
# We add `-Wno-unused-command-line-argument` so that if someone does something like
432+
# `clang -Werror -o foo a.o b.o`, it doesn't complain due to the fact that that is using
433+
# `clang` as a linker (and we have no real way to detect that in the wrapper), which
434+
# will cause `clang` to complain about compiler flags being passed in.
435+
"-Wno-unused-command-line-argument",
436+
# We need to override the typical C++ include search paths, because it always includes
437+
# the toolchain C++ headers first. Valentin tracked this down to:
438+
# https://github.com/llvm/llvm-project/blob/0378f3a90341d990236c44f297b923a32b35fab1/clang/lib/Driver/ToolChains/Darwin.cpp#L1944-L1978
439+
"-nostdinc++",
440+
# For systems other than macOS this directory doesn't exist out-of-the-box in our
441+
# toolchain, but you can put in there the headers of the C++ standard library for libc++
442+
# from LLLVMLibcxx_jll. This must come before GCC header files (added below).
443+
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/include/c++/v1",
444+
])
445+
430446
if Sys.isapple(p)
431447
macos_version_flags = clang_use_lld ? (min_macos_version_flags()[1],) : min_macos_version_flags()
432448
append!(flags, String[
433-
# On MacOS, we need to override the typical C++ include search paths, because it always includes
434-
# the toolchain C++ headers first. Valentin tracked this down to:
435-
# https://github.com/llvm/llvm-project/blob/0378f3a90341d990236c44f297b923a32b35fab1/clang/lib/Driver/ToolChains/Darwin.cpp#L1944-L1978
436-
"-nostdinc++",
437-
"-isystem",
438-
"/opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/include/c++/v1",
439-
# We also add `-Wno-unused-command-line-argument` so that if someone does something like
440-
# `clang -Werror -o foo a.o b.o`, it doesn't complain due to the fact that that is using
441-
# `clang` as a linker (and we have no real way to detect that in the wrapper), which will
442-
# cause `clang` to complain about compiler flags being passed in.
443-
"-Wno-unused-command-line-argument",
444449
macos_version_flags...,
445450
])
446451
end
452+
447453
sanitize_compile_flags!(p, flags)
448454
if Sys.isfreebsd(p)
449455
add_system_includedir(flags)
450456
end
451457

452-
if !Sys.isbsd(p) && !isnothing(gcc_version)
453-
append!(flags, String["-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)",
454-
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/$(aatriplet(p))",
455-
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/backward",
456-
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include",
457-
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/include"])
458+
if !Sys.isbsd(p)
459+
# GCC header files
460+
if !isnothing(gcc_version)
461+
append!(flags, [
462+
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)",
463+
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/$(aatriplet(p))",
464+
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/backward",
465+
])
466+
end
467+
# MinGW header files
468+
if Sys.iswindows(p)
469+
append!(flags, [
470+
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include",
471+
])
472+
end
458473
end
474+
459475
if Sys.iswindows(p) && nbits(p) == 32
460476
push!(flags, "-fsjlj-exceptions")
461477
end

test/runners.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ end
197197
end
198198

199199
# This tests only that compilers for all platforms can build and link simple C++ code
200-
@testset "Compilation - $(platform) - $(compiler)" for platform in platforms, compiler in ("c++", "g++", "clang++")
200+
# Note: we test the slightly weird `clang -x c++` as compiler driver because that's used
201+
# in some cases and we want to make sure it works correctly.
202+
@testset "Compilation - $(platform) - $(compiler)" for platform in platforms, (compiler, linker) in (("c++", "c++"), ("g++", "g++"), ("clang -x c++", "clang++"))
201203
mktempdir() do dir
202204
ur = preferred_runner()(dir; platform=platform)
203205
iobuff = IOBuffer()
@@ -221,13 +223,15 @@ end
221223
echo '$(test_cpp)' > test.cpp
222224
echo '$(main_cpp)' > main.cpp
223225
# Build object file
224-
$(compiler) -Werror -std=c++11 -c test.cpp -o test.o
225-
# Build shared library
226-
$(compiler) -Werror -std=c++11 $(needfpic) -shared test.cpp -o libtest.\${dlext}
226+
$(compiler) $(needfpic) -Werror -std=c++11 -c test.cpp -o test.o
227+
# Link shared library
228+
$(linker) -shared test.o -o libtest.\${dlext}
227229
# Build and link program with object file
228-
$(compiler) -Werror -std=c++11 -o main main.cpp test.o
229-
# Build and link program with shared library
230-
$(compiler) -Werror -std=c++11 -o main main.cpp -L. -ltest
230+
$(compiler) $(needfpic) -Werror -std=c++11 -c main.cpp -o main.o
231+
# Link main program with test object file
232+
$(linker) -o main main.o test.o
233+
# Link main program with shared library
234+
$(linker) -o main main.o -L. -ltest
231235
"""
232236
cmd = `/bin/bash -c "$(test_script)"`
233237
@test run(ur, cmd, iobuff; tee_stream=devnull)

0 commit comments

Comments
 (0)