From 81bdc906492fa302c26eb200fe95cfe5bbb6e8ef Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 10 Jan 2025 15:31:09 -0800 Subject: [PATCH 1/5] [libc][cmake] move _get_hermetic_test_compile_options to LLVMLibCTestRules.cmake It's only used in that file, which is more appropriate than LLVMLibCCompileOptionRules.cmake since it's strictly related to tests. --- .../modules/LLVMLibCCompileOptionRules.cmake | 23 ----------------- libc/cmake/modules/LLVMLibCTestRules.cmake | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 8dcee1ec42246..785ea04944852 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -272,26 +272,3 @@ function(_get_common_test_compile_options output_var c_test flags) endif() set(${output_var} ${compile_options} PARENT_SCOPE) endfunction() - -function(_get_hermetic_test_compile_options output_var flags) - _get_common_test_compile_options(compile_options "" "${flags}") - - list(APPEND compile_options "-fpie") - list(APPEND compile_options "-ffreestanding") - list(APPEND compile_options "-fno-exceptions") - list(APPEND compile_options "-fno-rtti") - - # The GPU build requires overriding the default CMake triple and architecture. - if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) - list(APPEND compile_options - -Wno-multi-gpu -nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto - -mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION}) - elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX) - list(APPEND compile_options - "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false" - -Wno-multi-gpu --cuda-path=${LIBC_CUDA_ROOT} - -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit) - endif() - - set(${output_var} ${compile_options} PARENT_SCOPE) -endfunction() diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index 84f3125d557ea..fa9c83440a376 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -359,6 +359,31 @@ if(NOT MSVC AND NOT LIBC_CC_SUPPORTS_NOSTDLIBPP) string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION) endif() +function(_get_hermetic_test_compile_options output_var flags) + _get_common_test_compile_options(compile_options "" "${flags}") + + list(APPEND compile_options "-fpie") + list(APPEND compile_options "-ffreestanding") + list(APPEND compile_options "-fno-exceptions") + list(APPEND compile_options "-fno-rtti") + + # The GPU build requires overriding the default CMake triple and architecture. + if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) + list(APPEND compile_options + -Wno-multi-gpu -nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto + -mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION}) + elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX) + list(APPEND compile_options + "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false" + -Wno-multi-gpu --cuda-path=${LIBC_CUDA_ROOT} + -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit) + # elseif(explicit_target_triple AND NOT CMAKE_COMPILER_IS_GNUCXX) + # list(APPEND compile_options "--target=${explicit_target_triple}") + endif() + + set(${output_var} ${compile_options} PARENT_SCOPE) +endfunction() + # DEPRECATED: Use add_hermetic_test instead. # # Rule to add an integration test. An integration test is like a unit test From 61931049e819c0c6f1db4e041b7a77a546f19d9d Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 10 Jan 2025 15:44:05 -0800 Subject: [PATCH 2/5] fixup --- libc/cmake/modules/LLVMLibCTestRules.cmake | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index fa9c83440a376..ee32f331acb3a 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -359,8 +359,8 @@ if(NOT MSVC AND NOT LIBC_CC_SUPPORTS_NOSTDLIBPP) string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION) endif() -function(_get_hermetic_test_compile_options output_var flags) - _get_common_test_compile_options(compile_options "" "${flags}") +function(_get_hermetic_test_compile_options output_var) + _get_common_test_compile_options(compile_options "" "") list(APPEND compile_options "-fpie") list(APPEND compile_options "-ffreestanding") @@ -377,8 +377,6 @@ function(_get_hermetic_test_compile_options output_var flags) "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false" -Wno-multi-gpu --cuda-path=${LIBC_CUDA_ROOT} -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit) - # elseif(explicit_target_triple AND NOT CMAKE_COMPILER_IS_GNUCXX) - # list(APPEND compile_options "--target=${explicit_target_triple}") endif() set(${output_var} ${compile_options} PARENT_SCOPE) From ddd1164600539f4fe6d0c163b936141b6dc97554 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 10 Jan 2025 15:56:38 -0800 Subject: [PATCH 3/5] move _get_common_test_compile_options too --- .../modules/LLVMLibCCompileOptionRules.cmake | 59 ------------------- libc/cmake/modules/LLVMLibCTestRules.cmake | 58 ++++++++++++++++++ 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 785ea04944852..1795639011936 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -213,62 +213,3 @@ function(_get_common_compile_options output_var flags) endif() set(${output_var} ${compile_options} PARENT_SCOPE) endfunction() - -function(_get_common_test_compile_options output_var c_test flags) - _get_compile_options_from_flags(compile_flags ${flags}) - - set(compile_options - ${LIBC_COMPILE_OPTIONS_DEFAULT} - ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT} - ${compile_flags}) - - if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) - list(APPEND compile_options "-fpie") - - if(LLVM_LIBC_FULL_BUILD) - list(APPEND compile_options "-DLIBC_FULL_BUILD") - # Only add -ffreestanding flag in full build mode. - list(APPEND compile_options "-ffreestanding") - list(APPEND compile_options "-fno-exceptions") - list(APPEND compile_options "-fno-unwind-tables") - list(APPEND compile_options "-fno-asynchronous-unwind-tables") - if(NOT c_test) - list(APPEND compile_options "-fno-rtti") - endif() - endif() - - if(LIBC_COMPILER_HAS_FIXED_POINT) - list(APPEND compile_options "-ffixed-point") - endif() - - # list(APPEND compile_options "-Wall") - # list(APPEND compile_options "-Wextra") - # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror. - if(NOT LIBC_WNO_ERROR) - # list(APPEND compile_options "-Werror") - endif() - # list(APPEND compile_options "-Wconversion") - # list(APPEND compile_options "-Wno-sign-conversion") - # list(APPEND compile_options "-Wimplicit-fallthrough") - # list(APPEND compile_options "-Wwrite-strings") - # list(APPEND compile_options "-Wextra-semi") - # Silence this warning because _Complex is a part of C99. - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - if(NOT c_test) - list(APPEND compile_options "-fext-numeric-literals") - endif() - else() - list(APPEND compile_options "-Wno-c99-extensions") - list(APPEND compile_options "-Wno-gnu-imaginary-constant") - endif() - list(APPEND compile_options "-Wno-pedantic") - # if(NOT CMAKE_COMPILER_IS_GNUCXX) - # list(APPEND compile_options "-Wnewline-eof") - # list(APPEND compile_options "-Wnonportable-system-include-path") - # list(APPEND compile_options "-Wstrict-prototypes") - # list(APPEND compile_options "-Wthread-safety") - # list(APPEND compile_options "-Wglobal-constructors") - # endif() - endif() - set(${output_var} ${compile_options} PARENT_SCOPE) -endfunction() diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index ee32f331acb3a..642e48be5af9a 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -100,6 +100,64 @@ function(get_object_files_for_test result skipped_entrypoints_list) endfunction(get_object_files_for_test) +function(_get_common_test_compile_options output_var c_test flags) + _get_compile_options_from_flags(compile_flags ${flags}) + + set(compile_options + ${LIBC_COMPILE_OPTIONS_DEFAULT} + ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT} + ${compile_flags}) + + if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) + list(APPEND compile_options "-fpie") + + if(LLVM_LIBC_FULL_BUILD) + list(APPEND compile_options "-DLIBC_FULL_BUILD") + # Only add -ffreestanding flag in full build mode. + list(APPEND compile_options "-ffreestanding") + list(APPEND compile_options "-fno-exceptions") + list(APPEND compile_options "-fno-unwind-tables") + list(APPEND compile_options "-fno-asynchronous-unwind-tables") + if(NOT c_test) + list(APPEND compile_options "-fno-rtti") + endif() + endif() + + if(LIBC_COMPILER_HAS_FIXED_POINT) + list(APPEND compile_options "-ffixed-point") + endif() + + # list(APPEND compile_options "-Wall") + # list(APPEND compile_options "-Wextra") + # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror. + if(NOT LIBC_WNO_ERROR) + # list(APPEND compile_options "-Werror") + endif() + # list(APPEND compile_options "-Wconversion") + # list(APPEND compile_options "-Wno-sign-conversion") + # list(APPEND compile_options "-Wimplicit-fallthrough") + # list(APPEND compile_options "-Wwrite-strings") + # list(APPEND compile_options "-Wextra-semi") + # Silence this warning because _Complex is a part of C99. + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if(NOT c_test) + list(APPEND compile_options "-fext-numeric-literals") + endif() + else() + list(APPEND compile_options "-Wno-c99-extensions") + list(APPEND compile_options "-Wno-gnu-imaginary-constant") + endif() + list(APPEND compile_options "-Wno-pedantic") + # if(NOT CMAKE_COMPILER_IS_GNUCXX) + # list(APPEND compile_options "-Wnewline-eof") + # list(APPEND compile_options "-Wnonportable-system-include-path") + # list(APPEND compile_options "-Wstrict-prototypes") + # list(APPEND compile_options "-Wthread-safety") + # list(APPEND compile_options "-Wglobal-constructors") + # endif() + endif() + set(${output_var} ${compile_options} PARENT_SCOPE) +endfunction() # Rule to add a libc unittest. # Usage From 45a85c953b84229e10bbb28ae7854621362c3396 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 10 Jan 2025 16:00:14 -0800 Subject: [PATCH 4/5] move both functions to top of file, together --- libc/cmake/modules/LLVMLibCTestRules.cmake | 164 ++++++++++----------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index 642e48be5af9a..1cd4b4cb5b656 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -1,3 +1,85 @@ +function(_get_common_test_compile_options output_var c_test flags) + _get_compile_options_from_flags(compile_flags ${flags}) + + set(compile_options + ${LIBC_COMPILE_OPTIONS_DEFAULT} + ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT} + ${compile_flags}) + + if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) + list(APPEND compile_options "-fpie") + + if(LLVM_LIBC_FULL_BUILD) + list(APPEND compile_options "-DLIBC_FULL_BUILD") + # Only add -ffreestanding flag in full build mode. + list(APPEND compile_options "-ffreestanding") + list(APPEND compile_options "-fno-exceptions") + list(APPEND compile_options "-fno-unwind-tables") + list(APPEND compile_options "-fno-asynchronous-unwind-tables") + if(NOT c_test) + list(APPEND compile_options "-fno-rtti") + endif() + endif() + + if(LIBC_COMPILER_HAS_FIXED_POINT) + list(APPEND compile_options "-ffixed-point") + endif() + + # list(APPEND compile_options "-Wall") + # list(APPEND compile_options "-Wextra") + # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror. + if(NOT LIBC_WNO_ERROR) + # list(APPEND compile_options "-Werror") + endif() + # list(APPEND compile_options "-Wconversion") + # list(APPEND compile_options "-Wno-sign-conversion") + # list(APPEND compile_options "-Wimplicit-fallthrough") + # list(APPEND compile_options "-Wwrite-strings") + # list(APPEND compile_options "-Wextra-semi") + # Silence this warning because _Complex is a part of C99. + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if(NOT c_test) + list(APPEND compile_options "-fext-numeric-literals") + endif() + else() + list(APPEND compile_options "-Wno-c99-extensions") + list(APPEND compile_options "-Wno-gnu-imaginary-constant") + endif() + list(APPEND compile_options "-Wno-pedantic") + # if(NOT CMAKE_COMPILER_IS_GNUCXX) + # list(APPEND compile_options "-Wnewline-eof") + # list(APPEND compile_options "-Wnonportable-system-include-path") + # list(APPEND compile_options "-Wstrict-prototypes") + # list(APPEND compile_options "-Wthread-safety") + # list(APPEND compile_options "-Wglobal-constructors") + # endif() + endif() + set(${output_var} ${compile_options} PARENT_SCOPE) +endfunction() + +function(_get_hermetic_test_compile_options output_var) + _get_common_test_compile_options(compile_options "" "") + + list(APPEND compile_options "-fpie") + list(APPEND compile_options "-ffreestanding") + list(APPEND compile_options "-fno-exceptions") + list(APPEND compile_options "-fno-rtti") + + # The GPU build requires overriding the default CMake triple and architecture. + if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) + list(APPEND compile_options + -Wno-multi-gpu -nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto + -mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION}) + elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX) + list(APPEND compile_options + "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false" + -Wno-multi-gpu --cuda-path=${LIBC_CUDA_ROOT} + -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit) + endif() + + set(${output_var} ${compile_options} PARENT_SCOPE) +endfunction() + # This is a helper function and not a build rule. It is to be used by the # various test rules to generate the full list of object files # recursively produced by "add_entrypoint_object" and "add_object_library" @@ -100,65 +182,6 @@ function(get_object_files_for_test result skipped_entrypoints_list) endfunction(get_object_files_for_test) -function(_get_common_test_compile_options output_var c_test flags) - _get_compile_options_from_flags(compile_flags ${flags}) - - set(compile_options - ${LIBC_COMPILE_OPTIONS_DEFAULT} - ${LIBC_TEST_COMPILE_OPTIONS_DEFAULT} - ${compile_flags}) - - if(LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE) - list(APPEND compile_options "-fpie") - - if(LLVM_LIBC_FULL_BUILD) - list(APPEND compile_options "-DLIBC_FULL_BUILD") - # Only add -ffreestanding flag in full build mode. - list(APPEND compile_options "-ffreestanding") - list(APPEND compile_options "-fno-exceptions") - list(APPEND compile_options "-fno-unwind-tables") - list(APPEND compile_options "-fno-asynchronous-unwind-tables") - if(NOT c_test) - list(APPEND compile_options "-fno-rtti") - endif() - endif() - - if(LIBC_COMPILER_HAS_FIXED_POINT) - list(APPEND compile_options "-ffixed-point") - endif() - - # list(APPEND compile_options "-Wall") - # list(APPEND compile_options "-Wextra") - # -DLIBC_WNO_ERROR=ON if you can't build cleanly with -Werror. - if(NOT LIBC_WNO_ERROR) - # list(APPEND compile_options "-Werror") - endif() - # list(APPEND compile_options "-Wconversion") - # list(APPEND compile_options "-Wno-sign-conversion") - # list(APPEND compile_options "-Wimplicit-fallthrough") - # list(APPEND compile_options "-Wwrite-strings") - # list(APPEND compile_options "-Wextra-semi") - # Silence this warning because _Complex is a part of C99. - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - if(NOT c_test) - list(APPEND compile_options "-fext-numeric-literals") - endif() - else() - list(APPEND compile_options "-Wno-c99-extensions") - list(APPEND compile_options "-Wno-gnu-imaginary-constant") - endif() - list(APPEND compile_options "-Wno-pedantic") - # if(NOT CMAKE_COMPILER_IS_GNUCXX) - # list(APPEND compile_options "-Wnewline-eof") - # list(APPEND compile_options "-Wnonportable-system-include-path") - # list(APPEND compile_options "-Wstrict-prototypes") - # list(APPEND compile_options "-Wthread-safety") - # list(APPEND compile_options "-Wglobal-constructors") - # endif() - endif() - set(${output_var} ${compile_options} PARENT_SCOPE) -endfunction() - # Rule to add a libc unittest. # Usage # add_libc_unittest( @@ -417,29 +440,6 @@ if(NOT MSVC AND NOT LIBC_CC_SUPPORTS_NOSTDLIBPP) string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION) endif() -function(_get_hermetic_test_compile_options output_var) - _get_common_test_compile_options(compile_options "" "") - - list(APPEND compile_options "-fpie") - list(APPEND compile_options "-ffreestanding") - list(APPEND compile_options "-fno-exceptions") - list(APPEND compile_options "-fno-rtti") - - # The GPU build requires overriding the default CMake triple and architecture. - if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) - list(APPEND compile_options - -Wno-multi-gpu -nogpulib -mcpu=${LIBC_GPU_TARGET_ARCHITECTURE} -flto - -mcode-object-version=${LIBC_GPU_CODE_OBJECT_VERSION}) - elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX) - list(APPEND compile_options - "SHELL:-mllvm -nvptx-emit-init-fini-kernel=false" - -Wno-multi-gpu --cuda-path=${LIBC_CUDA_ROOT} - -nogpulib -march=${LIBC_GPU_TARGET_ARCHITECTURE} -fno-use-cxa-atexit) - endif() - - set(${output_var} ${compile_options} PARENT_SCOPE) -endfunction() - # DEPRECATED: Use add_hermetic_test instead. # # Rule to add an integration test. An integration test is like a unit test From 13b2064632eb0db416a4b8f89d98eb1ba39b5164 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Fri, 10 Jan 2025 16:07:42 -0800 Subject: [PATCH 5/5] remove duplication --- libc/cmake/modules/LLVMLibCTestRules.cmake | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake index 1cd4b4cb5b656..4dbe5e046cc68 100644 --- a/libc/cmake/modules/LLVMLibCTestRules.cmake +++ b/libc/cmake/modules/LLVMLibCTestRules.cmake @@ -60,11 +60,6 @@ endfunction() function(_get_hermetic_test_compile_options output_var) _get_common_test_compile_options(compile_options "" "") - list(APPEND compile_options "-fpie") - list(APPEND compile_options "-ffreestanding") - list(APPEND compile_options "-fno-exceptions") - list(APPEND compile_options "-fno-rtti") - # The GPU build requires overriding the default CMake triple and architecture. if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) list(APPEND compile_options