# SPDX-License-Identifier: CC0-1.0
# SPDX-FileCopyrightText: 2019 Lynn Kirby

function(get_c_compiler_file_name name name_var)
    set(command ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} "-print-file-name=${name}")

    if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
        list(APPEND command "--target=${CMAKE_C_COMPILER_TARGET}")
    endif()

    string(REPLACE " " ";" command "${command}")

    execute_process(
        COMMAND ${command}
        RESULT_VARIABLE success OUTPUT_VARIABLE output)

    if(NOT ${success} EQUAL 0)
        message(
            FATAL_ERROR
            "Could not get C compiler library file ${name}:\n${output}")
    endif()

    string(STRIP "${output}" output)
    set(${name_var} "${output}" PARENT_SCOPE)
endfunction()
