Skip to content

Commit

Permalink
Add Basic Checks For Clang Compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
CuriousTommy committed Aug 25, 2023
1 parent a12ec5d commit 1ff27a4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ 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)
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}")
Expand Down
42 changes: 42 additions & 0 deletions cmake/clang_version_check.cmake
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1ff27a4

Please sign in to comment.