[Openmp-commits] [openmp] [OpenMP] Add libomp unit test infrastructure (PR #168063)

Michael Kruse via Openmp-commits openmp-commits at lists.llvm.org
Tue Dec 2 14:29:51 PST 2025


================
@@ -174,17 +174,28 @@ if(NOT WIN32)
 endif()
 
 # Add the OpenMP library
-libomp_get_ldflags(LIBOMP_CONFIGURED_LDFLAGS)
 
+# First, create an OBJECT library with all the runtime sources.
+# This allows both the main library and unit tests to link against the same
+# compiled objects.
+add_library(omp_objects OBJECT ${LIBOMP_SOURCE_FILES})
+set_property(TARGET omp_objects PROPERTY FOLDER "OpenMP/Libraries")
+set_property(TARGET omp_objects PROPERTY POSITION_INDEPENDENT_CODE ON)
+# Export the omp_objects target so unit tests can use it
+set(LIBOMP_OBJECTS_TARGET omp_objects PARENT_SCOPE)
+
+libomp_get_ldflags(LIBOMP_CONFIGURED_LDFLAGS)
 libomp_get_libflags(LIBOMP_CONFIGURED_LIBFLAGS)
-# Build libomp library. Add LLVMSupport dependency if building in-tree with libomptarget profiling enabled.
+
+# Build libomp library. Add LLVMSupport dependency if building in-tree with
+# libomptarget profiling enabled.
 if(OPENMP_STANDALONE_BUILD OR (NOT OPENMP_ENABLE_LIBOMP_PROFILING))
-  add_library(omp ${LIBOMP_LIBRARY_KIND} ${LIBOMP_SOURCE_FILES})
+  add_library(omp ${LIBOMP_LIBRARY_KIND} $<TARGET_OBJECTS:omp_objects>)
----------------
Meinersbur wrote:

[not a change request] According to the [documentation](https://cmake.org/cmake/help/latest/command/target_link_libraries.html#command:target_link_libraries), you can also
```cmake
target_link_libraries(omp PUBLIC omp_objects)
```
I had mixed success with it; keep `$<TARGET_OBJECTS:omp_objects>` if it works.

https://github.com/llvm/llvm-project/pull/168063


More information about the Openmp-commits mailing list