Skip to content

FindOpenMP: Add support for GNU-like Clang targeting MSVC ABI

Markus Mützel requested to merge mmuetzel/cmake:openmp into master

Use the code path from !4327 (merged) with the GNU-like frontend too.

Original Description

FindOpenMP.cmake doesn't check for the libraries needed to link to OpenMP when using Clang (with GNU compatible CLI) targeting MSVC.

This issue occurred for SuiteSparse downstream: https://github.com/DrTimothyAldenDavis/SuiteSparse/issues/750

Quoting that issue:

The build errors like the following:

[4/5605] Linking C shared library SuiteSparse_config\Release\suitesparseconfig.dll
FAILED: SuiteSparse_config/Release/suitesparseconfig.dll SuiteSparse_config/Release/suitesparseconfig.lib 
cmd.exe /C "cmd.exe /C ""C:\Program Files\CMake\bin\cmake.exe" -E __create_def D:\a\SuiteSparse\SuiteSparse\build\SuiteSparse_config\CMakeFiles\SuiteSparseConfig.dir\Release\exports.def D:\a\SuiteSparse\SuiteSparse\build\SuiteSparse_config\CMakeFiles\SuiteSparseConfig.dir\Release\exports.def.objs --nm="C:\Program Files\LLVM\bin\llvm-nm.exe" && cd D:\a\SuiteSparse\SuiteSparse\build" && C:\PROGRA~1\LLVM\bin\clang.exe -fuse-ld=lld-link -nostartfiles -nostdlib -O3 -DNDEBUG -Wno-extra-semi-stmt -Wno-extra-semi-stmt -D_DLL -D_MT -Xclang --dependent-lib=msvcrt  -Xlinker /DEF:SuiteSparse_config\CMakeFiles\SuiteSparseConfig.dir\Release\exports.def -shared -o SuiteSparse_config\Release\suitesparseconfig.dll  -Xlinker /MANIFEST:EMBED -Xlinker /implib:SuiteSparse_config\Release\suitesparseconfig.lib -Xlinker /pdb:SuiteSparse_config\Release\suitesparseconfig.pdb -Xlinker /version:7.7 SuiteSparse_config/CMakeFiles/SuiteSparseConfig.dir/Release/SuiteSparse_config.c.obj  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames  && cd ."
lld-link: error: undefined symbol: omp_get_wtime
>>> referenced by SuiteSparse_config/CMakeFiles/SuiteSparseConfig.dir/Release/SuiteSparse_config.c.obj:(SuiteSparse_tic)
>>> referenced by SuiteSparse_config/CMakeFiles/SuiteSparseConfig.dir/Release/SuiteSparse_config.c.obj:(SuiteSparse_toc)
>>> referenced by SuiteSparse_config/CMakeFiles/SuiteSparseConfig.dir/Release/SuiteSparse_config.c.obj:(SuiteSparse_time)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The error probably occurs because the OpenMP libraries aren't set for some reason by FindOpenMP.cmake:

-- OpenMP C libraries:       
-- OpenMP C include:         
-- OpenMP C flags:          -fopenmp=libomp 

IIUC, they should be set to the same as when building with clang-cl:

-- OpenMP C libraries:      C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.37.32822/lib/x64/libomp.lib 
-- OpenMP C include:         
-- OpenMP C flags:          -Xclang -fopenmp 

The proposed change leads to the OpenMP libraries being detected also in that configuration. (See: https://github.com/DrTimothyAldenDavis/SuiteSparse/pull/760 )

Backport: release
Topic-rename: FindOpenMP-clang-windows

Edited by Brad King

Merge request reports