Skip to content

Commit 80cf21d

Browse files
authored
[libc] Fix unit test compile flags propagation. (#106128)
With this change, I was able to build and test for aarch64 & riscv64 on x86-64 host as follow: Pre-requisite: - cross build toolchain for aarch64 ``` $ sudo apt install binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu g++-aarch64-linux-gnu ``` - cross build toolchain for riscv64 ``` $ sudo apt install binutils-riscv64-linux-gnu gcc-riscv64-linux-gnu g++-riscv64-linux-gnu ``` - qemu user: ``` $ sudo apt install qemu qemu-user qemu-user-static ``` CMake invocation: ``` $ cmake ../runtimes -GNinja -DLLVM_ENABLE_RUNTIMES=libc -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBC_TARGET_TRIPLE=<aarch64-linux-gnu/riscv64-linux-gnu> -DCMAKE_BUILD_TYPE=Release -DLIBC_TEST_COMPILE_OPTIONS_DEFAULT="-static" $ ninja libc $ ninja check-libc ```
1 parent 3edd21b commit 80cf21d

File tree

9 files changed

+74
-32
lines changed

9 files changed

+74
-32
lines changed

libc/CMakeLists.txt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
112112

113113
# Flags to pass down to the compiler while building the libc functions.
114114
set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
115+
set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.")
115116

116117
list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
117118

@@ -131,12 +132,31 @@ if(COMMAND_RETURN_CODE EQUAL 0)
131132
message(STATUS "Set COMPILER_RESOURCE_DIR to "
132133
"${COMPILER_RESOURCE_DIR} using --print-resource-dir")
133134
else()
134-
if (LIBC_TARGET_OS_IS_GPU)
135-
message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
136-
else()
137-
set(COMPILER_RESOURCE_DIR OFF)
138-
message(STATUS "COMPILER_RESOURCE_DIR not set
139-
--print-resource-dir not supported by host compiler")
135+
# Try with GCC option: -print-search-dirs, which will output in the form:
136+
# install: <path>
137+
# programs: ........
138+
# So we try to capture the <path> after "install: " in the first line of the
139+
# output.
140+
execute_process(
141+
OUTPUT_STRIP_TRAILING_WHITESPACE
142+
COMMAND ${CMAKE_CXX_COMPILER} -print-search-dirs
143+
RESULT_VARIABLE COMMAND_RETURN_CODE
144+
OUTPUT_VARIABLE COMPILER_RESOURCE_DIR
145+
)
146+
if(COMMAND_RETURN_CODE EQUAL 0)
147+
string(REPLACE " " ";" COMPILER_RESOURCE_DIR ${COMPILER_RESOURCE_DIR})
148+
string(REPLACE "\n" ";" COMPILER_RESOURCE_DIR "${COMPILER_RESOURCE_DIR}")
149+
list(GET COMPILER_RESOURCE_DIR 1 COMPILER_RESOURCE_DIR)
150+
message(STATUS "Set COMPILER_RESOURCE_DIR to "
151+
"${COMPILER_RESOURCE_DIR} using --print-search-dirs")
152+
else()
153+
if (LIBC_TARGET_OS_IS_GPU)
154+
message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
155+
else()
156+
set(COMPILER_RESOURCE_DIR OFF)
157+
message(STATUS "COMPILER_RESOURCE_DIR not set
158+
--print-resource-dir not supported by host compiler")
159+
endif()
140160
endif()
141161
endif()
142162

libc/cmake/modules/LLVMLibCArchitectures.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,5 @@ if(explicit_target_triple AND
207207
endif()
208208

209209
message(STATUS
210-
"Building libc for ${LIBC_TARGET_ARCHITECTURE} on ${LIBC_TARGET_OS}")
210+
"Building libc for ${LIBC_TARGET_ARCHITECTURE} on ${LIBC_TARGET_OS} with
211+
LIBC_COMPILE_OPTIONS_DEFAULT: ${LIBC_COMPILE_OPTIONS_DEFAULT}")

libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ elseif(${LIBC_TARGET_ARCHITECTURE_IS_AARCH64})
1313
set(LIBC_COMPILE_OPTIONS_NATIVE -mcpu=native)
1414
endif()
1515

16+
if(LIBC_CROSSBUILD)
17+
set(LIBC_COMPILE_OPTIONS_NATIVE ${LIBC_COMPILE_OPTIONS_DEFAULT})
18+
endif()
19+
1620
# Making sure ALL_CPU_FEATURES is sorted.
1721
list(SORT ALL_CPU_FEATURES)
1822

libc/cmake/modules/LLVMLibCCheckMPFR.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ else()
1111
LIBC_TESTS_CAN_USE_MPFR
1212
${CMAKE_CURRENT_BINARY_DIR}
1313
SOURCES
14-
${LIBC_SOURCE_DIR}/utils/MPFRWrapper/check_mpfr.cpp
14+
${LIBC_SOURCE_DIR}/utils/MPFRWrapper/check_mpfr.cpp
15+
COMPILE_DEFINITIONS
16+
${LIBC_COMPILE_OPTIONS_DEFAULT}
1517
LINK_LIBRARIES
1618
-lmpfr -lgmp -latomic
1719
)

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ endfunction()
188188
function(_get_common_test_compile_options output_var c_test flags)
189189
_get_compile_options_from_flags(compile_flags ${flags})
190190

191-
set(compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags})
191+
set(compile_options
192+
${LIBC_COMPILE_OPTIONS_DEFAULT}
193+
${LIBC_TEST_COMPILE_OPTIONS_DEFAULT}
194+
${compile_flags})
192195

193196
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
194197
list(APPEND compile_options "-fpie")
@@ -232,9 +235,12 @@ function(_get_common_test_compile_options output_var c_test flags)
232235
endfunction()
233236

234237
function(_get_hermetic_test_compile_options output_var flags)
235-
_get_compile_options_from_flags(compile_flags ${flags})
236-
list(APPEND compile_options ${LIBC_COMPILE_OPTIONS_DEFAULT} ${compile_flags}
237-
${flags} -fpie -ffreestanding -fno-exceptions -fno-rtti)
238+
_get_common_test_compile_options(compile_options "" "${flags}")
239+
240+
list(APPEND compile_options "-fpie")
241+
list(APPEND compile_options "-ffreestanding")
242+
list(APPEND compile_options "-fno-exceptions")
243+
list(APPEND compile_options "-fno-rtti")
238244

239245
# The GPU build requires overriding the default CMake triple and architecture.
240246
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
@@ -248,9 +254,5 @@ function(_get_hermetic_test_compile_options output_var flags)
248254
-nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit)
249255
endif()
250256

251-
if(LLVM_LIBC_FULL_BUILD)
252-
list(APPEND compile_options "-DLIBC_FULL_BUILD")
253-
endif()
254-
255257
set(${output_var} ${compile_options} PARENT_SCOPE)
256258
endfunction()

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ function(create_libc_unittest fq_target_name)
192192
target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
193193
target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
194194
target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
195+
target_link_options(${fq_build_target_name} PRIVATE ${compile_options})
195196

196197
if(NOT LIBC_UNITTEST_CXX_STANDARD)
197198
set(LIBC_UNITTEST_CXX_STANDARD ${CMAKE_CXX_STANDARD})
@@ -465,8 +466,9 @@ function(add_integration_test test_name)
465466
target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
466467
target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
467468

468-
_get_hermetic_test_compile_options(compile_options "${INTEGRATION_TEST_COMPILE_OPTIONS}")
469-
target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
469+
_get_hermetic_test_compile_options(compile_options "")
470+
target_compile_options(${fq_build_target_name} PRIVATE
471+
${compile_options} ${INTEGRATION_TEST_COMPILE_OPTIONS})
470472

471473
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
472474
target_link_options(${fq_build_target_name} PRIVATE
@@ -638,11 +640,12 @@ function(add_libc_hermetic test_name)
638640
#OUTPUT_NAME ${fq_target_name}
639641
)
640642

641-
_get_hermetic_test_compile_options(compile_options "${HERMETIC_TEST_COMPILE_OPTIONS}")
642643
target_include_directories(${fq_build_target_name} SYSTEM PRIVATE ${LIBC_INCLUDE_DIR})
643644
target_include_directories(${fq_build_target_name} PRIVATE ${LIBC_SOURCE_DIR})
644-
_get_hermetic_test_compile_options(compile_options "${HERMETIC_TEST_COMPILE_OPTIONS}")
645-
target_compile_options(${fq_build_target_name} PRIVATE ${compile_options})
645+
_get_hermetic_test_compile_options(compile_options "")
646+
target_compile_options(${fq_build_target_name} PRIVATE
647+
${compile_options}
648+
${HERMETIC_TEST_COMPILE_OPTIONS})
646649

647650
set(link_libraries "")
648651
foreach(lib IN LISTS HERMETIC_TEST_LINK_LIBRARIES)

libc/test/UnitTest/CMakeLists.txt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,27 @@ function(add_unittest_framework_library name)
1919
${TEST_LIB_SRCS}
2020
${TEST_LIB_HDRS}
2121
)
22-
target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR})
23-
list(APPEND compile_options -fno-exceptions -fno-rtti)
22+
target_include_directories(${lib} PRIVATE ${LIBC_SOURCE_DIR})
2423
if(TARGET libc.src.time.clock)
2524
target_compile_definitions(${lib} PRIVATE TARGET_SUPPORTS_CLOCK)
2625
endif()
27-
if(LIBC_COMPILER_HAS_FIXED_POINT)
28-
list(APPEND compile_options -ffixed-point)
29-
endif()
30-
target_compile_options(${lib} PUBLIC ${compile_options})
3126
endforeach()
32-
_get_hermetic_test_compile_options(compile_options -nostdinc++)
33-
target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include)
34-
target_compile_options(${name}.hermetic PRIVATE ${compile_options})
27+
28+
if(LLVM_LIBC_FULL_BUILD)
29+
# TODO: Build test framework with LIBC_FULL_BUILD in full build mode after
30+
# making LibcFPExceptionHelpers and LibcDeathTestExecutors hermetic.
31+
set(LLVM_LIBC_FULL_BUILD "")
32+
_get_common_test_compile_options(compile_options "" "")
33+
target_compile_options(${name}.unit PRIVATE ${compile_options})
34+
set(LLVM_LIBC_FULL_BUILD ON)
35+
else()
36+
_get_common_test_compile_options(compile_options "" "")
37+
target_compile_options(${name}.unit PRIVATE ${compile_options})
38+
endif()
39+
40+
_get_hermetic_test_compile_options(compile_options "")
41+
target_include_directories(${name}.hermetic PRIVATE ${LIBC_INCLUDE_DIR})
42+
target_compile_options(${name}.hermetic PRIVATE ${compile_options} -nostdinc++)
3543

3644
if(TEST_LIB_DEPENDS)
3745
foreach(dep IN ITEMS ${TEST_LIB_DEPENDS})

libc/test/integration/startup/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function(add_startup_test target_name)
3232
PRIVATE
3333
${LIBC_SOURCE_DIR}
3434
${LIBC_BUILD_DIR}
35-
${LIBC_BUILD_DIR}/include
35+
${LIBC_INCLUDE_DIR}
3636
)
3737

3838
if(ADD_STARTUP_TEST_DEPENDS)

libc/utils/MPFRWrapper/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ if(LIBC_TESTS_CAN_USE_MPFR)
44
MPFRUtils.h
55
mpfr_inc.h
66
)
7-
target_compile_options(libcMPFRWrapper PRIVATE -O3)
7+
_get_common_test_compile_options(compile_options "" "")
8+
target_compile_options(libcMPFRWrapper PRIVATE -O3 ${compile_options})
89
add_dependencies(
910
libcMPFRWrapper
1011
libc.src.__support.CPP.array
@@ -19,6 +20,7 @@ if(LIBC_TESTS_CAN_USE_MPFR)
1920
target_include_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/include)
2021
target_link_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/lib)
2122
endif()
23+
target_include_directories(libcMPFRWrapper PUBLIC ${LIBC_SOURCE_DIR})
2224
target_link_libraries(libcMPFRWrapper PUBLIC LibcFPTestHelpers.unit LibcTest.unit mpfr gmp)
2325
elseif(NOT LIBC_TARGET_OS_IS_GPU AND NOT LLVM_LIBC_FULL_BUILD)
2426
message(WARNING "Math tests using MPFR will be skipped.")

0 commit comments

Comments
 (0)