[libc-commits] [libc] 4dc205f - [libc] Add a convenience CMake function `add_unittest_framework_library`.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Wed May 17 14:14:00 PDT 2023


Author: Siva Chandra Reddy
Date: 2023-05-17T21:13:50Z
New Revision: 4dc205f016e3dd2eb1182886a77676f24e39e329

URL: https://github.com/llvm/llvm-project/commit/4dc205f016e3dd2eb1182886a77676f24e39e329
DIFF: https://github.com/llvm/llvm-project/commit/4dc205f016e3dd2eb1182886a77676f24e39e329.diff

LOG: [libc] Add a convenience CMake function `add_unittest_framework_library`.

This function is used to add unit test and hermetic test framework libraries.
It avoids the duplicated code to add compile options to each every test
framework libraries.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D150727

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCTestRules.cmake
    libc/test/UnitTest/CMakeLists.txt
    libc/utils/MPFRWrapper/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 820c21f56d315..ab1f46cfd2dc6 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -167,8 +167,15 @@ function(create_libc_unittest fq_target_name)
       CXX_STANDARD ${LIBC_UNITTEST_CXX_STANDARD}
   )
 
+  set(link_libraries ${link_object_files})
   # Test object files will depend on LINK_LIBRARIES passed down from `add_fp_unittest`
-  set(link_libraries ${link_object_files} ${LIBC_UNITTEST_LINK_LIBRARIES})
+  foreach(lib IN LISTS LIBC_UNITTEST_LINK_LIBRARIES)
+    if(TARGET ${lib}.unit)
+      list(APPEND link_libraries ${lib}.unit)
+    else()
+      list(APPEND link_libraries ${lib})
+    endif()
+  endforeach()
 
   set_target_properties(${fq_build_target_name}
     PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
@@ -179,7 +186,7 @@ function(create_libc_unittest fq_target_name)
   )
 
   # LibcUnitTest should not depend on anything in LINK_LIBRARIES.
-  list(APPEND link_libraries LibcUnitTest)
+  list(APPEND link_libraries LibcDeathTestExecutors.unit LibcTest.unit)
 
   target_link_libraries(${fq_build_target_name} PRIVATE ${link_libraries})
 
@@ -659,6 +666,15 @@ function(add_libc_hermetic_test test_name)
   target_compile_options(${fq_build_target_name}
       PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} ${HERMETIC_TEST_COMPILE_OPTIONS})
 
+  set(link_libraries "")
+  foreach(lib IN LISTS HERMETIC_TEST_LINK_LIBRARIES)
+    if(TARGET ${lib}.hermetic)
+      list(APPEND link_libraries ${lib}.hermetic)
+    else()
+      list(APPEND link_libraries ${lib})
+    endif()
+  endforeach()
+
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
   else()
@@ -667,13 +683,14 @@ function(add_libc_hermetic_test test_name)
   target_link_libraries(
     ${fq_build_target_name}
     PRIVATE
-      ${HERMETIC_TEST_LINK_LIBRARIES}
       libc.startup.${LIBC_TARGET_OS}.crt1
-      LibcHermeticTest
+      ${link_libraries}
+      LibcTest.hermetic
+      LibcHermeticTestSupport.hermetic
       # The NVIDIA 'nvlink' linker does not currently support static libraries.
       $<$<NOT:$<BOOL:${LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX}>>:${fq_target_name}.__libc__>)
   add_dependencies(${fq_build_target_name}
-                   LibcHermeticTest
+                   LibcTest.hermetic
                    ${fq_deps_list})
 
   # Tests on the GPU require an external loader utility to launch the kernel.

diff  --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 7f6e782815c85..52c7845859e37 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -1,144 +1,162 @@
-set(libc_test_srcs_common
-  CmakeFilePath.cpp
-  LibcTest.cpp
-  LibcTestMain.cpp
-  TestLogger.cpp
-  LibcTest.h
-  Test.h
-  TestLogger.h
-)
-
-set(libc_death_test_srcs ExecuteFunction.h)
-if(${LIBC_TARGET_OS} STREQUAL "linux")
-  list(APPEND libc_death_test_srcs 
-       LibcDeathTestExecutors.cpp ExecuteFunctionUnix.cpp)
-endif()
+function(add_unittest_framework_library name)
+  cmake_parse_arguments(
+    "TEST_LIB"
+    "" # No optional arguments
+    "" # No single value arguments
+    "SRCS;HDRS;DEPENDS" # Multi value arguments
+    ${ARGN}
+  )
+  if(NOT TEST_LIB_SRCS)
+    message(FATAL_ERROR "'add_unittest_framework_library' requires SRCS; for "
+                        "header only libraries, use 'add_header_library'")
+  endif()
 
-# The Nvidia 'nvlink' linker does not support static libraries.
-if(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
-  set(library_type OBJECT)
-else()
-  set(library_type STATIC)
-endif()
+  # The Nvidia 'nvlink' linker does not support static libraries.
+  if(LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
+    set(library_type OBJECT)
+  else()
+    set(library_type STATIC)
+  endif()
 
-add_library(
-  LibcUnitTest
-  ${library_type}
-  EXCLUDE_FROM_ALL
-  ${libc_test_srcs_common}
-  ${libc_death_test_srcs}
-)
+  foreach(lib IN ITEMS ${name}.unit ${name}.hermetic)
+    add_library(
+      ${lib}
+      ${library_type}
+      EXCLUDE_FROM_ALL
+      ${TEST_LIB_SRCS}
+      ${TEST_LIB_HDRS}
+    )
+    target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR})
+    target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti)
+  endforeach()
+  target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include)
+  target_compile_options(${name}.hermetic
+      PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} -ffreestanding -nostdinc++)
 
-add_library(
-  LibcHermeticTest
-  ${library_type}
-  EXCLUDE_FROM_ALL
-  ${libc_test_srcs_common}
-  HermeticTestUtils.cpp
-)
+  if(TEST_LIB_DEPENDS)
+    foreach(dep IN LISTS ${TEST_LIB_DEPENDS})
+      if(TARGET ${dep}.unit)
+        add_dependencies(${name}.unit ${dep}.unit)
+      else()
+        add_dependencies(${name}.unit ${dep})
+      endif()
+      if(TARGET ${dep}.hermetic)
+        add_dependencies(${name}.hermetic ${dep}.hermetic)
+      else()
+        add_dependencies(${name}.hermetic ${dep})
+      endif()
+    endforeach()
+  endif()
+endfunction()
 
-foreach(lib LibcUnitTest LibcHermeticTest)
-  target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR})
-  target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti)
-  add_dependencies(${lib}
+add_unittest_framework_library(
+  LibcTest
+  SRCS
+    CmakeFilePath.cpp
+    LibcTest.cpp
+    LibcTestMain.cpp
+    TestLogger.cpp
+  HDRS
+    LibcTest.h
+    Test.h
+    TestLogger.h
+  DEPENDS
     libc.src.__support.c_string
     libc.src.__support.CPP.string
     libc.src.__support.CPP.string_view
     libc.src.__support.CPP.type_traits
     libc.src.__support.OSUtil.osutil
     libc.src.__support.uint128
-  )
-endforeach()
+)
+
+set(libc_death_test_srcs LibcDeathTestExecutors.cpp)
+if(${LIBC_TARGET_OS} STREQUAL "linux")
+  list(APPEND libc_death_test_srcs ExecuteFunctionUnix.cpp)
+endif()
 
-target_include_directories(LibcHermeticTest PRIVATE ${LIBC_BUILD_DIR}/include)
-target_compile_options(LibcHermeticTest
-    PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} -ffreestanding -nostdlib -nostdlib++)
+add_unittest_framework_library(
+  LibcDeathTestExecutors
+  SRCS
+    ${libc_death_test_srcs}
+  HDRS
+    ExecuteFunction.h
+)
+
+add_unittest_framework_library(
+  LibcHermeticTestSupport
+  SRCS
+    HermeticTestUtils.cpp
+)
 
 add_header_library(
   string_utils
   HDRS
-  StringUtils.h
+    StringUtils.h
   DEPENDS
     libc.src.__support.CPP.string
     libc.src.__support.CPP.type_traits
 )
 
-add_library(
+add_unittest_framework_library(
   LibcFPTestHelpers
-  FPMatcher.h
-  RoundingModeUtils.cpp
-  RoundingModeUtils.h
-)
-add_dependencies(
-  LibcFPTestHelpers
-  LibcUnitTest
-  libc.test.UnitTest.string_utils
-  libc.src.__support.FPUtil.fp_bits
-  libc.src.__support.FPUtil.fenv_impl
-  libc.test.UnitTest.string_utils
-)
-add_library(
-  LibcFPExceptionHelpers
-  FPExceptMatcher.cpp
-  FPExceptMatcher.h
+  SRCS
+    RoundingModeUtils.cpp
+  HDRS
+    FPMatcher.h
+    RoundingModeUtils.h
+  DEPENDS
+    LibcTest
+    libc.test.UnitTest.string_utils
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.fenv_impl
+    libc.test.UnitTest.string_utils
 )
-add_dependencies(
+
+add_unittest_framework_library(
   LibcFPExceptionHelpers
-  LibcUnitTest
-  libc.src.__support.FPUtil.fp_bits
-  libc.src.__support.FPUtil.fenv_impl
+  SRCS
+    FPExceptMatcher.cpp
+  HDRS
+    FPExceptMatcher.h
+  DEPENDS
+    LibcTest
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.fenv_impl
 )
 
-add_library(
+add_unittest_framework_library(
   LibcMemoryHelpers
-  ${library_type}
-  MemoryMatcher.h
-  MemoryMatcher.cpp
-)
-add_dependencies(
-  LibcMemoryHelpers
-  LibcUnitTest
-  libc.src.__support.CPP.span
+  SRCS
+    MemoryMatcher.cpp
+  HDRS
+    MemoryMatcher.h
+  DEPENDS
+    LibcTest
+    libc.src.__support.CPP.span
 )
 
-add_library(
-  LibcPrintfHelpers
-  PrintfMatcher.h
-  PrintfMatcher.cpp
-)
-add_dependencies(
+add_unittest_framework_library(
   LibcPrintfHelpers
-  LibcUnitTest
-  libc.src.__support.FPUtil.fp_bits
-  libc.src.stdio.printf_core.core_structs
-  libc.test.UnitTest.string_utils
+  SRCS
+    PrintfMatcher.cpp
+  HDRS
+    PrintfMatcher.h
+  DEPENDS
+    LibcTest
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.stdio.printf_core.core_structs
+    libc.test.UnitTest.string_utils
 )
 
-add_library(
+add_unittest_framework_library(
   LibcScanfHelpers
-  ScanfMatcher.h
-  ScanfMatcher.cpp
-)
-add_dependencies(
-  LibcScanfHelpers
-  LibcUnitTest
-  libc.src.__support.FPUtil.fp_bits
-  libc.src.stdio.scanf_core.core_structs
-  libc.test.UnitTest.string_utils
+  SRCS
+    ScanfMatcher.cpp
+  HDRS
+    ScanfMatcher.h
+  DEPENDS
+    LibcTest
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.stdio.scanf_core.core_structs
+    libc.test.UnitTest.string_utils
 )
-
-foreach(lib LibcFPTestHelpers LibcFPExceptionHelpers LibcMemoryHelpers 
-            LibcPrintfHelpers LibcScanfHelpers)
-  target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR})
-  target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti)
-  target_link_libraries(${lib} LibcUnitTest)
-endforeach()
-
-# The GPU needs these flags applied to override the system triple.
-if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
-  foreach(lib LibcMemoryHelpers)
-    target_include_directories(${lib} PRIVATE ${LIBC_BUILD_DIR}/include)
-    target_compile_options(${lib}
-        PRIVATE ${LIBC_HERMETIC_TEST_COMPILE_OPTIONS} -ffreestanding -nostdlib -nostdlib++)
-  endforeach()
-endif()

diff  --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index 6447d06b7bb5e..f1fcf26b6f0bd 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -12,13 +12,13 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     libc.src.__support.CPP.type_traits 
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.platform_defs
-    LibcUnitTest
+    LibcTest.unit
   )
   if(EXISTS ${LLVM_LIBC_MPFR_INSTALL_PATH})
     target_include_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/include)
     target_link_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/lib)
   endif()
-  target_link_libraries(libcMPFRWrapper LibcFPTestHelpers LibcUnitTest mpfr gmp)
+  target_link_libraries(libcMPFRWrapper LibcFPTestHelpers.unit LibcTest.unit mpfr gmp)
 elseif(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
   message(WARNING "Math tests using MPFR will be skipped.")
 endif()


        


More information about the libc-commits mailing list