CMake is unable to find OpenMP from llvmPackages_13

I’ve now filed a bug report: https://github.com/NixOS/nixpkgs/issues/161062

When inspecting the CMakeError.log I discovered lots of unknown type name errors which suggests that the standard library is not set up correctly.


Clang does not include the OpenMP runtime as part of the compiler like GCC does. It has to be installed separately and is packaged as llvmPackages.openmp. However, CMake is unable to find openmp despite adding it to buildInputs.

Below is an example consisting of three files that have to be placed in the same directory to reproduce.

omp_test.cpp

#include <cstdio>
#include <omp.h>
int main() {
    #pragma omp parallel
    #pragma omp single
    printf("%d\n", omp_get_num_threads());
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(omp_test CXX)
add_executable(omp_test omp_test.cpp)
find_package(OpenMP REQUIRED COMPONENTS CXX)
target_link_libraries(omp_test PRIVATE OpenMP::OpenMP_CXX)
install(TARGETS omp_test)

default.nix

{ pkgs ? import <nixpkgs> {} }:
pkgs.llvmPackages_13.libcxxStdenv.mkDerivation {
  name = "omp_test";
  src = ./.;
  nativeBuildInputs = [ pkgs.cmake ];
  buildInputs = [ pkgs.llvmPackages_13.openmp ];
}

This is the output of the builder:

$ nix-build
this derivation will be built:
  /nix/store/hyp76pzmfif3c6n6fxqm4dgrx5z244fq-omp_test.drv
building '/nix/store/hyp76pzmfif3c6n6fxqm4dgrx5z244fq-omp_test.drv'...
unpacking sources
unpacking source archive /nix/store/zw8kj8v9mx9zg1xccw46ll3l5sw90rsb-test
source root is test
patching sources
configuring
fixing cmake files...
cmake flags: -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_SKIP_BUILD_RPATH=ON -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/share/doc/omp_test -DCMAKE_INSTALL_INFODIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/include -DCMAKE_INSTALL_SBINDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_OSX_SYSROOT= -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/vmpdn2r9bnw6ygl1pzynppcl5mlmmfy4-clang-wrapper-13.0.0/bin/strip -DCMAKE_RANLIB=/nix/store/m78i23pkfzpgb5niln4nqmi4ial9cphs-binutils-2.35.2/bin/ranlib -DCMAKE_AR=/nix/store/m78i23pkfzpgb5niln4nqmi4ial9cphs-binutils-2.35.2/bin/ar -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_INSTALL_PREFIX=/nix/store/c6f8g7cdlfgsllm8h9l69h8wfynp090d-omp_test  
-- The CXX compiler identification is Clang 13.0.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /nix/store/vmpdn2r9bnw6ygl1pzynppcl5mlmmfy4-clang-wrapper-13.0.0/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /nix/store/nqmzmc7zzyl7dpsmn8zxcq4w66kb814n-cmake-3.21.2/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
Call Stack (most recent call first):
  /nix/store/nqmzmc7zzyl7dpsmn8zxcq4w66kb814n-cmake-3.21.2/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /nix/store/nqmzmc7zzyl7dpsmn8zxcq4w66kb814n-cmake-3.21.2/share/cmake-3.21/Modules/FindOpenMP.cmake:544 (find_package_handle_standard_args)
  CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!
See also "/build/test/build/CMakeFiles/CMakeOutput.log".
See also "/build/test/build/CMakeFiles/CMakeError.log".
error: builder for '/nix/store/hyp76pzmfif3c6n6fxqm4dgrx5z244fq-omp_test.drv' failed with exit code 1;
       last 10 log lines:
       >   Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
       > Call Stack (most recent call first):
       >   /nix/store/nqmzmc7zzyl7dpsmn8zxcq4w66kb814n-cmake-3.21.2/share/cmake-3.21/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
       >   /nix/store/nqmzmc7zzyl7dpsmn8zxcq4w66kb814n-cmake-3.21.2/share/cmake-3.21/Modules/FindOpenMP.cmake:544 (find_package_handle_standard_args)
       >   CMakeLists.txt:4 (find_package)
       >
       > 
       > -- Configuring incomplete, errors occurred!
       > See also "/build/test/build/CMakeFiles/CMakeOutput.log".
       > See also "/build/test/build/CMakeFiles/CMakeError.log".
       For full logs, run 'nix log /nix/store/hyp76pzmfif3c6n6fxqm4dgrx5z244fq-omp_test.drv'.
1 Like

The issue has been fixed on staging and the fix will appear in the channels in the next weeks.