Skip to content

[Runner] Adjust clang flags to make it work slightly better on Linux #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BinaryBuilderBase"
uuid = "7f725544-6523-48cd-82d1-3fa08ff4056e"
authors = ["Elliot Saba <staticfloat@gmail.com>"]
version = "1.35.0"
version = "1.35.1"

[deps]
Bzip2_jll = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
Expand Down
52 changes: 34 additions & 18 deletions src/Runner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
end

function sanitize_compile_flags!(p::AbstractPlatform, flags::Vector{String})
san = sanitize(p)
if sanitize(p) !== nothing
if sanitize(p) == "memory"
append!(flags, ["-fsanitize=memory"])
Expand Down Expand Up @@ -427,35 +426,52 @@ function generate_compiler_wrappers!(platform::AbstractPlatform; bin_path::Abstr
if lock_microarchitecture
append!(flags, get_march_flags(arch(p), march(p), "clang"))
end

append!(flags, [
# We add `-Wno-unused-command-line-argument` so that if someone does something like
# `clang -Werror -o foo a.o b.o`, it doesn't complain due to the fact that that is using
# `clang` as a linker (and we have no real way to detect that in the wrapper), which
# will cause `clang` to complain about compiler flags being passed in.
"-Wno-unused-command-line-argument",
# We need to override the typical C++ include search paths, because it always includes
# the toolchain C++ headers first. Valentin tracked this down to:
# https://github.com/llvm/llvm-project/blob/0378f3a90341d990236c44f297b923a32b35fab1/clang/lib/Driver/ToolChains/Darwin.cpp#L1944-L1978
"-nostdinc++",
# For systems other than macOS this directory doesn't exist out-of-the-box in our
# toolchain, but you can put in there the headers of the C++ standard library for libc++
# from LLLVMLibcxx_jll. This must come before GCC header files (added below).
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/include/c++/v1",
])

if Sys.isapple(p)
macos_version_flags = clang_use_lld ? (min_macos_version_flags()[1],) : min_macos_version_flags()
append!(flags, String[
# On MacOS, we need to override the typical C++ include search paths, because it always includes
# the toolchain C++ headers first. Valentin tracked this down to:
# https://github.com/llvm/llvm-project/blob/0378f3a90341d990236c44f297b923a32b35fab1/clang/lib/Driver/ToolChains/Darwin.cpp#L1944-L1978
"-nostdinc++",
"-isystem",
"/opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/include/c++/v1",
# We also add `-Wno-unused-command-line-argument` so that if someone does something like
# `clang -Werror -o foo a.o b.o`, it doesn't complain due to the fact that that is using
# `clang` as a linker (and we have no real way to detect that in the wrapper), which will
# cause `clang` to complain about compiler flags being passed in.
"-Wno-unused-command-line-argument",
macos_version_flags...,
])
end

sanitize_compile_flags!(p, flags)
if Sys.isfreebsd(p)
add_system_includedir(flags)
end

if !Sys.isbsd(p) && !isnothing(gcc_version)
append!(flags, String["-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)",
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/$(aatriplet(p))",
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/backward",
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include",
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/include"])
if !Sys.isbsd(p)
# GCC header files
if !isnothing(gcc_version)
append!(flags, [
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)",
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/$(aatriplet(p))",
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include/c++/$(gcc_version)/backward",
])
end
# MinGW header files
if Sys.iswindows(p)
append!(flags, [
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/include",
])
end
end

if Sys.iswindows(p) && nbits(p) == 32
push!(flags, "-fsjlj-exceptions")
end
Expand Down
18 changes: 11 additions & 7 deletions test/runners.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ end
end

# This tests only that compilers for all platforms can build and link simple C++ code
@testset "Compilation - $(platform) - $(compiler)" for platform in platforms, compiler in ("c++", "g++", "clang++")
# Note: we test the slightly weird `clang -x c++` as compiler driver because that's used
# in some cases and we want to make sure it works correctly.
@testset "Compilation - $(platform) - $(compiler)" for platform in platforms, (compiler, linker) in (("c++", "c++"), ("g++", "g++"), ("clang -x c++", "clang++"))
mktempdir() do dir
ur = preferred_runner()(dir; platform=platform)
iobuff = IOBuffer()
Expand All @@ -221,13 +223,15 @@ end
echo '$(test_cpp)' > test.cpp
echo '$(main_cpp)' > main.cpp
# Build object file
$(compiler) -Werror -std=c++11 -c test.cpp -o test.o
# Build shared library
$(compiler) -Werror -std=c++11 $(needfpic) -shared test.cpp -o libtest.\${dlext}
$(compiler) $(needfpic) -Werror -std=c++11 -c test.cpp -o test.o
# Link shared library
$(linker) -shared test.o -o libtest.\${dlext}
# Build and link program with object file
$(compiler) -Werror -std=c++11 -o main main.cpp test.o
# Build and link program with shared library
$(compiler) -Werror -std=c++11 -o main main.cpp -L. -ltest
$(compiler) $(needfpic) -Werror -std=c++11 -c main.cpp -o main.o
# Link main program with test object file
$(linker) -o main main.o test.o
# Link main program with shared library
$(linker) -o main main.o -L. -ltest
"""
cmd = `/bin/bash -c "$(test_script)"`
@test run(ur, cmd, iobuff; tee_stream=devnull)
Expand Down
Loading