Add Basic Checks For Clang Compiler

This commit is contained in:
Thomas A 2023-08-24 21:54:14 -07:00
parent 5d5a364a79
commit 252d7f75b1
2 changed files with 47 additions and 0 deletions

View File

@ -56,6 +56,7 @@ enable_language(ASM)
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "core")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(clang_version_check)
include(InstallSymlink)
include(MacroEnsureOutOfSourceBuild)
include(dsym)
@ -63,6 +64,10 @@ include(xcproj)
include(architecture)
include(create_symlink)
set(CLANG_RECOMMENDED_MINIMUM_VERSION 11)
clang_version_check(${CMAKE_C_COMPILER} c ${CLANG_RECOMMENDED_MINIMUM_VERSION})
clang_version_check(${CMAKE_CXX_COMPILER} cpp ${CLANG_RECOMMENDED_MINIMUM_VERSION})
MACRO_ENSURE_OUT_OF_SOURCE_BUILD()
set(DARLING_TOP_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

View File

@ -0,0 +1,42 @@
set(MANUALLY_SET_COMPILER_ERROR_MESSAGE
"If you already have a supported version of clang installed, you may need to \
manually set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER. Refer to the Darling docs \
for more details."
)
macro(clang_version_check compiler source_type clang_minimum_version)
if (compiler STREQUAL "")
message(FATAL_ERROR "Unable to find a compatible compiler")
endif (compiler STREQUAL "")
file(WRITE "${CMAKE_BINARY_DIR}/clang_major.${source_type}" "#include <stdio.h>\n"
"#if !__clang__\n" "#error \"Not running on a clang compiler!\"\n" "#endif\n" "int main() { printf(\"%d\", __clang_major__); }")
execute_process(COMMAND "${compiler}" "${CMAKE_BINARY_DIR}/clang_major.${source_type}" "-o" "clang_${source_type}_major"
RESULT_VARIABLE BUILD_CLANG_TEST_RESULT
OUTPUT_VARIABLE BUILD_CLANG_TEST_OUTPUT
COMMAND_ECHO NONE
)
if (BUILD_CLANG_TEST_RESULT)
message(FATAL_ERROR
"Failed to build ${CMAKE_BINARY_DIR}/clang_major.${source_type}\n"
"This could indicate that `${compiler}` is either not a clang compiler, "
"or the path does not exist. ${MANUALLY_SET_COMPILER_ERROR_MESSAGE}")
endif (BUILD_CLANG_TEST_RESULT)
execute_process(COMMAND "${CMAKE_BINARY_DIR}/clang_${source_type}_major"
RESULT_VARIABLE CLANG_MAJOR_VERSION_RESULT
OUTPUT_VARIABLE CLANG_MAJOR_VERSION_OUTPUT
)
if (CLANG_MAJOR_VERSION_RESULT)
# This should normally never fail...
message(FATAL_ERROR "Failed to check clang major version")
endif (CLANG_MAJOR_VERSION_RESULT)
if ("${CLANG_MAJOR_VERSION_OUTPUT}" LESS ${clang_minimum_version})
message(FATAL_ERROR
"Your clang version (${CLANG_MAJOR_VERSION_OUTPUT}) is below the recommend supported version (${clang_minimum_version})\n"
"${MANUALLY_SET_COMPILER_ERROR_MESSAGE}")
endif ("${CLANG_MAJOR_VERSION_OUTPUT}" LESS ${clang_minimum_version})
endmacro()