Import std in clang with custom stdlibc++

Merge request 10727 added support for import std using clang and stdlibc++. I use clang with a custom gcc installation passed through a flag --gcc-install-dir via CMAKE_CXX_FLAGS.

In the Clang-CXX-CXXImportStd.cmake file, there is the following call:

execute_process(
    COMMAND
      "${CMAKE_CXX_COMPILER}"
      ${CMAKE_CXX_COMPILER_ID_ARG1}
      "-print-file-name=${_clang_modules_json_impl}.modules.json"
    OUTPUT_VARIABLE _clang_libcxx_modules_json_file
    ERROR_VARIABLE _clang_libcxx_modules_json_file_err
    RESULT_VARIABLE _clang_libcxx_modules_json_file_res
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_STRIP_TRAILING_WHITESPACE)

This call ignores CMAKE_CXX_FLAGS so it uses the gcc12 installed on my system and libstdc++.modules.json isn’t found. Is this a bug or can I somehow pass --gcc-install-dir to this call or specify a path to libstdc++.modules.json by hand?

Hmm. Does adding ${CMAKE_CXX_FLAGS} to the argument list fix it?

Yes, adding CMAKE_CXX_FLAGS fixes it.
My CMAKE_CXX_FLAGS looks like this: set(CMAKE_CXX_FLAGS "--gcc-install-dir=/usr/local/lib/gcc/x86_64-linux-gnu/16.0.0 --stdlib=libstdc++") so that’s what I did to get it to work:

separate_arguments(_cxx_flags_list NATIVE_COMMAND "${CMAKE_CXX_FLAGS}")

execute_process(
  COMMAND
    "${CMAKE_CXX_COMPILER}"
    ${CMAKE_CXX_COMPILER_ID_ARG1}
    ${_cxx_flags_list}
    "-print-file-name=${_clang_modules_json_impl}.modules.json"
  OUTPUT_VARIABLE _clang_libcxx_modules_json_file
  ERROR_VARIABLE _clang_libcxx_modules_json_file_err
  RESULT_VARIABLE _clang_libcxx_modules_json_file_res
  OUTPUT_STRIP_TRAILING_WHITESPACE
  ERROR_STRIP_TRAILING_WHITESPACE)

I saw CMAKE_CXX_COMPILER_ID_FLAGS_LIST used in another place, but it was empty in my context.