xref: /netbsd-src/external/apache2/llvm/dist/llvm/cmake/modules/GetErrcMessages.cmake (revision 82d56013d7b633d116a93943de88e08335357a7c)
1# This function returns the messages of various POSIX error codes as they are returned by std::error_code.
2# The purpose of this function is to supply those error messages to llvm-lit using the errc_messages config.
3# Currently supplied and needed error codes: ENOENT, EISDIR, EINVAL and EACCES.
4# Messages are semi colon separated.
5# Keep amount, order and tested error codes in sync with llvm/utils/lit/lit/llvm/config.py.
6function(get_errc_messages outvar)
7    if(CMAKE_CROSSCOMPILING AND NOT CMAKE_CROSSCOMPILING_EMULATOR AND NOT DEFINED errc_exit_code)
8        set(${outvar} "" PARENT_SCOPE)
9        message(STATUS "Can't get errc messages in cross-compilation mode")
10        return()
11    endif()
12
13    set(errc_test_code ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/getErrc.cpp)
14
15    file(WRITE ${errc_test_code} "
16        #include <cerrno>
17        #include <iostream>
18        #include <string>
19        #include <system_error>
20
21        std::string getMessageFor(int err) {
22            return std::make_error_code(static_cast<std::errc>(err)).message();
23        }
24
25        int main() {
26            std::cout << getMessageFor(ENOENT) << ';' << getMessageFor(EISDIR);
27            std::cout << ';' << getMessageFor(EINVAL) << ';' << getMessageFor(EACCES);
28        }
29    ")
30
31    try_run(errc_exit_code
32            errc_compiled
33            ${CMAKE_BINARY_DIR}
34            ${errc_test_code}
35            RUN_OUTPUT_VARIABLE errc_result
36            COMPILE_OUTPUT_VARIABLE errc_compile_errors)
37    if (errc_compiled AND "${errc_exit_code}" STREQUAL "0")
38        set(${outvar} ${errc_result} PARENT_SCOPE)
39    else()
40        set(${outvar} "" PARENT_SCOPE)
41        message(STATUS "Failed to get errc messages")
42    endif ()
43endfunction()
44