Skip to content

Commit 0425a5d

Browse files
authored
[libc] Add hooks for extra options in running hermetic tests (#147931)
Part of #145349. These hooks are required downstream in order to run hermetic tests. See arm/arm-toolchain#420 for more context on how this PR will be used.
1 parent 339a1f2 commit 0425a5d

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

libc/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ add_compile_definitions(LIBC_NAMESPACE=${LIBC_NAMESPACE})
7676
set(LIBC_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Architecture to tell clang to optimize for (e.g. -march=... or -mcpu=...)")
7777
set(LIBC_TEST_COMPILE_OPTIONS_DEFAULT "" CACHE STRING "Common compile options for all the tests.")
7878

79+
set(LIBC_LINK_OPTIONS_DEFAULT "" CACHE STRING "Arguments used when linking.")
80+
set(LIBC_TEST_LINK_OPTIONS_DEFAULT "" CACHE STRING "Common link options for all the tests.")
81+
82+
set(LIBC_TEST_CMD "" CACHE STRING
83+
"The full test command in the form <command> binary=@BINARY@, if using another program to test (e.g. QEMU)")
84+
set(LIBC_TEST_HERMETIC_ONLY "" OFF CACHE BOOL "Only enable hermetic tests.")
85+
7986
list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT ${LIBC_COMMON_TUNE_OPTIONS})
8087

8188
# Check --print-resource-dir to find the compiler resource dir if this flag

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ function(create_libc_unittest fq_target_name)
233233
_get_common_test_compile_options(compile_options "${LIBC_UNITTEST_C_TEST}"
234234
"${LIBC_UNITTEST_FLAGS}")
235235
# TODO: Ideally we would have a separate function for link options.
236-
set(link_options ${compile_options})
236+
set(link_options
237+
${compile_options}
238+
${LIBC_LINK_OPTIONS_DEFAULT}
239+
${LIBC_TEST_LINK_OPTIONS_DEFAULT}
240+
)
237241
list(APPEND compile_options ${LIBC_UNITTEST_COMPILE_OPTIONS})
238242

239243
if(SHOW_INTERMEDIATE_OBJECTS)
@@ -580,12 +584,26 @@ function(add_integration_test test_name)
580584
-march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
581585
"--cuda-path=${LIBC_CUDA_ROOT}")
582586
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
583-
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
587+
set(link_options
588+
-nolibc
589+
-nostartfiles
590+
-nostdlib++
591+
-static
592+
${LIBC_LINK_OPTIONS_DEFAULT}
593+
${LIBC_TEST_LINK_OPTIONS_DEFAULT}
594+
)
595+
target_link_options(${fq_build_target_name} PRIVATE ${link_options})
584596
else()
585597
# Older version of gcc does not support `nostdlib++` flag. We use
586598
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
587-
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
588-
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
599+
set(link_options
600+
-nolibc
601+
-nostartfiles
602+
-static
603+
${LIBC_LINK_OPTIONS_DEFAULT}
604+
${LIBC_TEST_LINK_OPTIONS_DEFAULT}
605+
)
606+
target_link_options(${fq_build_target_name} PRIVATE ${link_options})
589607
endif()
590608
target_link_libraries(
591609
${fq_build_target_name}
@@ -774,11 +792,26 @@ function(add_libc_hermetic test_name)
774792
-march=${LIBC_GPU_TARGET_ARCHITECTURE} -nostdlib -static
775793
"--cuda-path=${LIBC_CUDA_ROOT}")
776794
elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
777-
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
795+
set(link_options
796+
-nolibc
797+
-nostartfiles
798+
-nostdlib++
799+
-static
800+
${LIBC_LINK_OPTIONS_DEFAULT}
801+
${LIBC_TEST_LINK_OPTIONS_DEFAULT}
802+
)
803+
target_link_options(${fq_build_target_name} PRIVATE ${link_options})
778804
else()
779805
# Older version of gcc does not support `nostdlib++` flag. We use
780806
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
781-
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
807+
set(link_options
808+
-nolibc
809+
-nostartfiles
810+
-static
811+
${LIBC_LINK_OPTIONS_DEFAULT}
812+
${LIBC_TEST_LINK_OPTIONS_DEFAULT}
813+
)
814+
target_link_options(${fq_build_target_name} PRIVATE ${link_options})
782815
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
783816
endif()
784817
target_link_libraries(
@@ -809,9 +842,16 @@ function(add_libc_hermetic test_name)
809842
endif()
810843

811844
if(NOT HERMETIC_TEST_NO_RUN_POSTBUILD)
812-
set(test_cmd ${HERMETIC_TEST_ENV}
845+
if (LIBC_TEST_CMD)
846+
# In the form of "<command> binary=@BINARY@", e.g. "qemu-system-arm -loader$<COMMA>file=@BINARY@"
847+
string(REPLACE "@BINARY@" "$<TARGET_FILE:${fq_build_target_name}>" test_cmd_parsed ${LIBC_TEST_CMD})
848+
string(REPLACE " " ";" test_cmd "${test_cmd_parsed}")
849+
else()
850+
set(test_cmd ${HERMETIC_TEST_ENV}
813851
$<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${HERMETIC_TEST_LOADER_ARGS}
814852
$<TARGET_FILE:${fq_build_target_name}> ${HERMETIC_TEST_ARGS})
853+
endif()
854+
815855
add_custom_target(
816856
${fq_target_name}
817857
DEPENDS ${fq_target_name}.__cmd__
@@ -863,7 +903,9 @@ function(add_libc_test test_name)
863903
# Tests like the file tests perform file operations on disk file. If we
864904
# don't chain up the unit test and hermetic test, then those tests will
865905
# step on each other's files.
866-
add_dependencies(${fq_test_name}.__hermetic__ ${fq_test_name}.__unit__)
906+
if(NOT LIBC_TEST_HERMETIC_ONLY)
907+
add_dependencies(${fq_test_name}.__hermetic__ ${fq_test_name}.__unit__)
908+
endif()
867909
endif()
868910
endif()
869911
endfunction(add_libc_test)

0 commit comments

Comments
 (0)