Skip to content

Commit a2b7820

Browse files
committed
[Runner] Shuffle some flags around to fix headers search paths order
1 parent dc169b3 commit a2b7820

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

src/Runner.jl

Lines changed: 18 additions & 24 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,41 +426,36 @@ 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

452458
if !Sys.isbsd(p)
453-
# libc++ header files
454-
append!(flags, [
455-
# NOTE: this first directory doesn't exist out-of-the-box in our toolchain,
456-
# but you should put in there the C++ standard libraries for libc++ from
457-
# LLLVMLibcxx_jll. This must come before GCC header files (added below).
458-
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/include/c++/v1",
459-
# This directory contains C header files. Must come after the C++ ones, see
460-
# for example <https://github.com/llvm/llvm-project/blob/4bcdb26dac4cdadd7f8850a5f9b2e775b73aaf7f/libcxx/include/cmath#L336-L338>.
461-
# This directory is in default search paths, but we have to explicitly put
462-
# it here to give it higher precedence than the following directories.
463-
"-isystem /opt/$(aatriplet(p))/$(aatriplet(p))/sys-root/usr/include",
464-
])
465459
# GCC header files
466460
if !isnothing(gcc_version)
467461
append!(flags, [

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)