[libc-commits] [libc] [libc] Replace -nostdlib++ flag when building with gcc and add placement new operator to HermeticTestUtils.cpp. (PR #78906)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 22 14:47:47 PST 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/78906

>From cc8d6a4a5a43c1b0a6826379ba530fa9ba06e7b1 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Sun, 21 Jan 2024 13:53:07 -0500
Subject: [PATCH 1/3] [libc] Replace -nostdlib++ flag when building with gcc
 and add placement new operator to HermeticTestUtils.cpp.

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 8 ++++++--
 libc/test/UnitTest/HermeticTestUtils.cpp   | 4 ++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 9b7e9180275414..95163a2898d9bb 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -564,8 +564,10 @@ function(add_integration_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  else()
+  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
+  else()
+    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
   endif()
   target_link_libraries(
     ${fq_build_target_name}
@@ -741,8 +743,10 @@ function(add_libc_hermetic_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  else()
+  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
+  else()
+    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
   endif()
   target_link_libraries(
     ${fq_build_target_name}
diff --git a/libc/test/UnitTest/HermeticTestUtils.cpp b/libc/test/UnitTest/HermeticTestUtils.cpp
index 68e31f478d79ab..349c182ff2379f 100644
--- a/libc/test/UnitTest/HermeticTestUtils.cpp
+++ b/libc/test/UnitTest/HermeticTestUtils.cpp
@@ -104,6 +104,8 @@ void *__dso_handle = nullptr;
 
 } // extern "C"
 
+void *operator new(unsigned long size, void *ptr) { return ptr; }
+
 void *operator new(size_t size) { return malloc(size); }
 
 void *operator new[](size_t size) { return malloc(size); }
@@ -113,3 +115,5 @@ void operator delete(void *) {
   // we just trap here to catch any such accidental usages.
   __builtin_trap();
 }
+
+void operator delete(void *ptr, size_t size) { __builtin_trap(); }

>From b7f526465adae2bd6acb0ab743291e6fafe5d6f9 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Mon, 22 Jan 2024 16:59:41 -0500
Subject: [PATCH 2/3] Sync and add check for -nostdlib++ support.

---
 .../cmake/modules/CheckCompilerFeatures.cmake |  2 ++
 libc/cmake/modules/LLVMLibCTestRules.cmake    | 21 +++++++++++++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index 1ad81bbb5f666c..8c51292235a424 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -57,3 +57,5 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
 endforeach()
 
 message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
+
+check_cxx_compiler_flag("-nostdlib++" LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 95163a2898d9bb..392ef9faff7d07 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -435,6 +435,13 @@ function(add_libc_fuzzer target_name)
 
 endfunction(add_libc_fuzzer)
 
+# Get libgcc_s to be used in hermetic and integration tests.
+if(NOT LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
+  execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc_s.so.1
+                  OUTPUT_VARIABLE LIBGCC_S_LOCATION)
+  string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION)
+endif()
+
 # DEPRECATED: Use add_hermetic_test instead.
 #
 # Rule to add an integration test. An integration test is like a unit test
@@ -564,10 +571,13 @@ function(add_integration_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
+  elseif(LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
   else()
-    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
+    # Older version of gcc does not support `nostdlib++` flag.  We use
+    # `nostdlib` and link against libgcc_s, which cannot be linked statically.
+    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
+    list(APPEND link_libraries ${LIBGCC_S_LOCATION})
   endif()
   target_link_libraries(
     ${fq_build_target_name}
@@ -743,10 +753,13 @@ function(add_libc_hermetic_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
+  elseif(LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
   else()
-    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
+    # Older version of gcc does not support `nostdlib++` flag.  We use
+    # `nostdlib` and link against libgcc_s, which cannot be linked statically.
+    target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
+    list(APPEND link_libraries ${LIBGCC_S_LOCATION})
   endif()
   target_link_libraries(
     ${fq_build_target_name}

>From f1ac4e2a55f6f5f7b9a5250e7a1b53c454703041 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Mon, 22 Jan 2024 17:47:26 -0500
Subject: [PATCH 3/3] Update the flag name.

---
 libc/cmake/modules/CheckCompilerFeatures.cmake | 2 +-
 libc/cmake/modules/LLVMLibCTestRules.cmake     | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index 8c51292235a424..6bfeb48313c602 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -58,4 +58,4 @@ endforeach()
 
 message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
 
-check_cxx_compiler_flag("-nostdlib++" LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
+check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 392ef9faff7d07..46ffc8316fc822 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -436,7 +436,7 @@ function(add_libc_fuzzer target_name)
 endfunction(add_libc_fuzzer)
 
 # Get libgcc_s to be used in hermetic and integration tests.
-if(NOT LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
+if(NOT LIBC_CC_SUPPORTS_NOSTDLIBPP)
   execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc_s.so.1
                   OUTPUT_VARIABLE LIBGCC_S_LOCATION)
   string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION)
@@ -571,7 +571,7 @@ function(add_integration_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  elseif(LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
+  elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
   else()
     # Older version of gcc does not support `nostdlib++` flag.  We use
@@ -753,7 +753,7 @@ function(add_libc_hermetic_test test_name)
 
   if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
     target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
-  elseif(LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
+  elseif(LIBC_CC_SUPPORTS_NOSTDLIBPP)
     target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
   else()
     # Older version of gcc does not support `nostdlib++` flag.  We use



More information about the libc-commits mailing list