Skip to content

Commit

Permalink
fix: add is_clang utility
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 12, 2024
1 parent 9ba4921 commit 8505a53
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
50 changes: 50 additions & 0 deletions src/Clang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
include_guard()

set(ProjectOptions_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}")

# detect clang
function(is_clang value)
if(clang
OR ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
OR ("${DETECTED_CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND "${DETECTED_CMAKE_C_COMPILER_ID}"
STREQUAL "Clang")
)
set(${value} ON PARENT_SCOPE)
return()
endif()

# if the compiler is unknown by CMake
if(NOT CMAKE_CXX_COMPILER
AND NOT CMAKE_C_COMPILER
AND NOT CMAKE_CXX_COMPILER_ID
AND NOT CMAKE_C_COMPILER_ID
)

# if clang is inferred by cmake later
include("${ProjectOptions_SRC_DIR}/DetectCompiler.cmake")
detect_compiler()

if((DETECTED_CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND DETECTED_CMAKE_C_COMPILER_ID STREQUAL
"Clang")
)
set(${value} ON PARENT_SCOPE)
return()
endif()

endif()

set(${value} OFF PARENT_SCOPE)
endfunction()

# configure clang toolchain for vcpkg
macro(configure_clang_vcpkg)
if(WIN32)
is_clang(_is_clang)
if(${_is_clang})
# Disable /utf-8 flag in the vcpkg toolchain file
# https://github.com/microsoft/vcpkg/blob/e590c2b30c08caf1dd8d612ec602a003f9784b7d/scripts/toolchains/windows.cmake#L68
message(STATUS "Disabling /utf-8 flag in the vcpkg toolchain file for Clang")
set(VCPKG_SET_CHARSET_FLAG "OFF" CACHE STRING "Vcpkg set charset flag" FORCE)
endif()
endif()
endmacro()
55 changes: 33 additions & 22 deletions src/DetectCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,41 @@ set(ProjectOptions_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}")
# includes a separate CMakeLists.txt file to detect the CXX/C compilers before project is called
# Using a separate file ensures that the current scope is not contaminated by the variable
macro(detect_compiler)
find_program(CMAKE_EXECUTABLE cmake)
execute_process(
COMMAND
"${CMAKE_EXECUTABLE}" -S "${ProjectOptions_SRC_DIR}/detect_compiler" -B
"${CMAKE_CURRENT_BINARY_DIR}/detect_compiler" -G "${CMAKE_GENERATOR}" "--log-level=ERROR"
"-Wno-dev"
OUTPUT_QUIET
if(NOT
(DETECTED_CMAKE_CXX_COMPILER
AND DETECTED_CMAKE_C_COMPILER
AND DETECTED_CMAKE_CXX_COMPILER_ID
AND DETECTED_CMAKE_C_COMPILER_ID
AND DETECTED_CMAKE_SYSTEM_PROCESSOR
AND DETECTED_CMAKE_HOST_SYSTEM_PROCESSOR)
)

# parse the detected compilers from the cache
set(cache_variables
CMAKE_CXX_COMPILER
CMAKE_CXX_COMPILER_ID
CMAKE_C_COMPILER
CMAKE_C_COMPILER_ID
CMAKE_SYSTEM_PROCESSOR
CMAKE_HOST_SYSTEM_PROCESSOR
)
foreach(cache_var ${cache_variables})
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/detect_compiler/CMakeCache.txt"
"DETECTED_${cache_var}" REGEX "^${cache_var}:STRING=(.*)$"
find_program(CMAKE_EXECUTABLE cmake)
execute_process(
COMMAND
"${CMAKE_EXECUTABLE}" -S "${ProjectOptions_SRC_DIR}/detect_compiler" -B
"${CMAKE_CURRENT_BINARY_DIR}/detect_compiler" -G "${CMAKE_GENERATOR}" "--log-level=ERROR"
"-Wno-dev"
OUTPUT_QUIET
)
string(REGEX REPLACE "^${cache_var}:STRING=(.*)$" "\\1" "DETECTED_${cache_var}"
"${DETECTED_${cache_var}}"

# parse the detected compilers from the cache
set(cache_variables
CMAKE_CXX_COMPILER
CMAKE_CXX_COMPILER_ID
CMAKE_C_COMPILER
CMAKE_C_COMPILER_ID
CMAKE_SYSTEM_PROCESSOR
CMAKE_HOST_SYSTEM_PROCESSOR
)
endforeach()
foreach(cache_var ${cache_variables})
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/detect_compiler/CMakeCache.txt"
"DETECTED_${cache_var}" REGEX "^${cache_var}:STRING=(.*)$"
)
string(REGEX REPLACE "^${cache_var}:STRING=(.*)$" "\\1" "DETECTED_${cache_var}"
"${DETECTED_${cache_var}}"
)
endforeach()

endif()
endmacro()
1 change: 1 addition & 0 deletions src/Index.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ include("${ProjectOptions_SRC_DIR}/Doxygen.cmake")
include("${ProjectOptions_SRC_DIR}/StaticAnalyzers.cmake")
include("${ProjectOptions_SRC_DIR}/VCEnvironment.cmake")
include("${ProjectOptions_SRC_DIR}/MinGW.cmake")
include("${ProjectOptions_SRC_DIR}/Clang.cmake")
include("${ProjectOptions_SRC_DIR}/DetectCompiler.cmake")
include("${ProjectOptions_SRC_DIR}/CrossCompiler.cmake")
include("${ProjectOptions_SRC_DIR}/DynamicProjectOptions.cmake")
Expand Down
1 change: 1 addition & 0 deletions src/Vcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ macro(run_vcpkg)
_checkout_vcpkg_repository()

configure_mingw_vcpkg()
# configure_clang_vcpkg()

# add the vcpkg toolchain
_add_vcpkg_toolchain()
Expand Down

0 comments on commit 8505a53

Please sign in to comment.