Skip to content

Commit aa77c0e

Browse files
committed
cmake: add the Yokai CMake framework
1 parent d281e6a commit aa77c0e

14 files changed

Lines changed: 1648 additions & 0 deletions

‎cmake/Yokai/All.cmake‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Daemon BSD Source Code
2+
# Copyright (c) 2025-2026, Daemon Developers
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# * Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# * Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# * Neither the name of the Daemon developers nor the
13+
# names of its contributors may be used to endorse or promote products
14+
# derived from this software without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
20+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
################################################################################
28+
# Collection of reusable CMake helpers written for the Dæmon engine and related
29+
# projects.
30+
################################################################################
31+
32+
# Source generation and file embedding.
33+
include("${CMAKE_CURRENT_LIST_DIR}/SourceGenerator.cmake")
34+
35+
# System, architecture and compiler detection.
36+
include("${CMAKE_CURRENT_LIST_DIR}/Detection.cmake")

‎cmake/Yokai/Architecture.cmake‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Daemon BSD Source Code
2+
# Copyright (c) 2022-2026, Daemon Developers
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# * Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# * Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# * Neither the name of the Daemon developers nor the
13+
# names of its contributors may be used to endorse or promote products
14+
# derived from this software without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
20+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
################################################################################
28+
# Architecture detection.
29+
################################################################################
30+
31+
# When adding a new architecture, look at all the places YOKAI_TARGET_ARCH is used.
32+
33+
option(USE_ARCH_INTRINSICS "Enable custom code using intrinsics functions or asm declarations" ON)
34+
mark_as_advanced(USE_ARCH_INTRINSICS)
35+
36+
function(yokai_detect_arch)
37+
yokai_run_detection("TARGET" "ARCH" "Architecture.c" "")
38+
39+
set(YOKAI_TARGET_ARCH_NAME "${arch_name}" PARENT_SCOPE)
40+
set(YOKAI_TARGET_ARCH_NAME_UPPER "${arch_name_upper}" PARENT_SCOPE)
41+
42+
message(STATUS "Detected target architecture: ${arch_name}")
43+
44+
add_definitions(-DYOKAI_ARCH_${arch_name_upper})
45+
endfunction()
46+
47+
function(yokai_set_arch_intrinsics name)
48+
message(STATUS "Enabling ${name} architecture intrinsics")
49+
string(TOUPPER "${name}" name_upper)
50+
add_definitions(-DYOKAI_USE_ARCH_INTRINSICS_${name_upper})
51+
endfunction()
52+
53+
option(USE_ARCH_INTRINSICS "Enable custom code using intrinsics functions or asm declarations" ON)
54+
mark_as_advanced(USE_ARCH_INTRINSICS)
55+
56+
function(yokai_set_intrinsics)
57+
if (USE_ARCH_INTRINSICS)
58+
# Makes possible to do that in C++ code:
59+
# > if defined(YOKAI_USE_ARCH_INTRINSICS)
60+
add_definitions(-DYOKAI_USE_ARCH_INTRINSICS)
61+
62+
# Makes possible to do that in C++ code:
63+
# > if defined(YOKAI_USE_ARCH_INTRINSICS_AMD64)
64+
# > if defined(YOKAI_USE_ARCH_INTRINSICS_I686)
65+
yokai_set_arch_intrinsics("${YOKAI_TARGET_ARCH_NAME}")
66+
67+
set(amd64_PARENT "i686")
68+
set(arm64_PARENT "armhf")
69+
set(ppc64el_PARENT "ppc64")
70+
71+
if ("${YOKAI_TARGET_ARCH_NAME}_PARENT")
72+
yokai_set_arch_intrinsics("${${YOKAI_TARGET_ARCH_NAME}_PARENT}")
73+
endif()
74+
else()
75+
message(STATUS "Disabling ${YOKAI_TARGET_ARCH_NAME} architecture intrinsics")
76+
endif()
77+
endfunction()
78+
79+
yokai_detect_arch()
80+
yokai_set_intrinsics()
81+
82+
# Makes possible to do that in CMake code:
83+
# > if (YOKAI_TARGET_ARCH_ARM64)
84+
set("YOKAI_TARGET_ARCH_${YOKAI_TARGET_ARCH_NAME_UPPER}" ON)
85+
86+
if (YOKAI_SOURCE_GENERATOR)
87+
# Add printable strings to the executable.
88+
yokai_add_buildinfo("char*" "YOKAI_ARCH_STRING" "\"${YOKAI_TARGET_ARCH_NAME}\"")
89+
endif()

‎cmake/Yokai/Compiler.cmake‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# Daemon BSD Source Code
2+
# Copyright (c) 2024-2026, Daemon Developers
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# * Redistributions of source code must retain the above copyright
8+
# notice, this list of conditions and the following disclaimer.
9+
# * Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
# * Neither the name of the Daemon developers nor the
13+
# names of its contributors may be used to endorse or promote products
14+
# derived from this software without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
# DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
20+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
################################################################################
28+
# Compiler detection.
29+
################################################################################
30+
31+
# When adding a new compiler, look at all the places YOKAI_C_COMPILER
32+
# and YOKAI_CXX_COMPILER are used.
33+
34+
function(yokai_detect_compiler lang)
35+
set(C_NAME "C")
36+
set(CXX_NAME "C++")
37+
set(C_EXT ".c")
38+
set(CXX_EXT ".cpp")
39+
40+
get_filename_component(compiler_basename "${CMAKE_${lang}_COMPILER}" NAME)
41+
42+
yokai_run_detection("${lang}" "COMPILER" "Compiler${${lang}_EXT}" "GCC;Clang;generic")
43+
44+
if (compiler_name STREQUAL "Unknown")
45+
if (CMAKE_${lang}_COMPILER_ID)
46+
set(compiler_name "${CMAKE_${lang}_COMPILER_ID}")
47+
# Compiler version is done below.
48+
else()
49+
message(WARNING "Unknown ${${lang}_NAME} compiler")
50+
endif()
51+
endif()
52+
53+
# AOCC
54+
if (compiler_Clang_version_string)
55+
set(aocc_version_regex ".*CLANG: AOCC_([^ )]+).*")
56+
if (compiler_Clang_version_string MATCHES ${aocc_version_regex})
57+
set(compiler_name "AOCC")
58+
string(REGEX REPLACE ${aocc_version_regex} "\\1"
59+
compiler_AOCC_version "${compiler_Clang_version_string}")
60+
string(REGEX REPLACE "(.*)-Build.*" "\\1"
61+
compiler_AOCC_version "${compiler_AOCC_version}")
62+
endif()
63+
endif()
64+
65+
# Zig
66+
if (compiler_Clang_version_string)
67+
set(zig_version_regex ".*[(]https://github.com/ziglang/zig-bootstrap .*[)]")
68+
if (compiler_Clang_version_string MATCHES ${zig_version_regex})
69+
set(compiler_name "Zig")
70+
endif()
71+
72+
# Parse “zig version”
73+
execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" version
74+
OUTPUT_VARIABLE CUSTOM_${lang}_ZIG_OUTPUT
75+
RESULT_VARIABLE CUSTOM_${lang}_ZIG_RETURN_CODE
76+
ERROR_QUIET
77+
OUTPUT_STRIP_TRAILING_WHITESPACE)
78+
79+
if (NOT CUSTOM_${lang}_ZIG_RETURN_CODE) # Success
80+
set(compiler_Zig_version "${CUSTOM_${lang}_ZIG_OUTPUT}")
81+
endif()
82+
endif()
83+
84+
# Compilers that use the underlying Clang version as their own version.
85+
foreach(name in AppleClang)
86+
if (compiler_name STREQUAL "${name}")
87+
set(compiler_${name}_version "${compiler_Clang_version}")
88+
endif()
89+
endforeach()
90+
91+
# Compilers that write the version number at the beginning of the VERSION string.
92+
set(string_version_regex "([^ ]+).*")
93+
foreach(name in PNaCl)
94+
if (compiler_name STREQUAL "${name}")
95+
if (compiler_generic_version_string)
96+
if (compiler_generic_version_string MATCHES ${string_version_regex})
97+
string(REGEX REPLACE ${string_version_regex} "\\1"
98+
compiler_${name}_version "${compiler_generic_version_string}")
99+
endif()
100+
endif()
101+
endif()
102+
endforeach()
103+
104+
if (compiler_ARMClang_version_string)
105+
# There is no __armclang_patchlevel__ so we should parse __armclang_version__ to get it.
106+
if (compiler_ARMClang_version_string MATCHES ${string_version_regex})
107+
string(REGEX REPLACE ${string_version_regex} "\\1"
108+
compiler_ARMClang_version "${compiler_ARMClang_version_string}")
109+
endif()
110+
endif()
111+
112+
if (compiler_ICX_version)
113+
# 20240000 becomes 2024.0.0
114+
string(REGEX REPLACE "(....)(..)(..)" "\\1.\\2.\\3"
115+
compiler_ICX_version "${compiler_ICX_version}")
116+
string(REGEX REPLACE "\\.0" "."
117+
compiler_ICX_version "${compiler_ICX_version}")
118+
endif()
119+
120+
if (compiler_${compiler_name}_version)
121+
set(compiler_version "${compiler_${compiler_name}_version}")
122+
elseif (CMAKE_${lang}_COMPILER_VERSION)
123+
set(compiler_version "${CMAKE_${lang}_COMPILER_VERSION}")
124+
else()
125+
set(compiler_version "Unknown")
126+
message(WARNING "Unknown ${${lang}_NAME} compiler version")
127+
endif()
128+
129+
set(YOKAI_${lang}_COMPILER_BASENAME "${compiler_basename}" PARENT_SCOPE)
130+
set(YOKAI_${lang}_COMPILER_NAME "${compiler_name}" PARENT_SCOPE)
131+
set(YOKAI_${lang}_COMPILER_NAME_UPPER "${compiler_name_upper}" PARENT_SCOPE)
132+
set(YOKAI_${lang}_COMPILER_VERSION "${compiler_version}" PARENT_SCOPE)
133+
endfunction()
134+
135+
message(STATUS "CMake generator: ${CMAKE_GENERATOR}")
136+
137+
foreach(lang C;CXX)
138+
set(C_NAME "C")
139+
set(CXX_NAME "C++")
140+
141+
if (MSVC)
142+
set(YOKAI_${lang}_COMPILER_MSVC_COMPATIBILITY ON)
143+
endif()
144+
145+
# When MSVC is ON, ${CMAKE_${lang}_COMPILER_ID} can be either MSVC
146+
# or Clang (clang-cl), Only MSVC is true MSVC.
147+
if (MSVC AND "${CMAKE_${lang}_COMPILER_ID}" STREQUAL "MSVC")
148+
set(YOKAI_${lang}_COMPILER_NAME "MSVC")
149+
set(YOKAI_${lang}_COMPILER_NAME_UPPER "MSVC")
150+
151+
# Let CMake do the job, it does it well,
152+
set(YOKAI_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}")
153+
154+
get_filename_component(YOKAI_${lang}_COMPILER_BASENAME "${CMAKE_${lang}_COMPILER}" NAME)
155+
else()
156+
yokai_detect_compiler(${lang})
157+
158+
if (YOKAI_${lang}_COMPILER_CLANG_COMPATIBILITY)
159+
if (NOT YOKAI_${lang}_COMPILER_NAME STREQUAL "Clang")
160+
set(YOKAI_${lang}_COMPILER_EXTENDED_VERSION
161+
"${YOKAI_${lang}_COMPILER_VERSION}/Clang_${YOKAI_${lang}_COMPILER_CLANG_VERSION}")
162+
endif()
163+
elseif (YOKAI_${lang}_COMPILER_GCC_COMPATIBILITY)
164+
if (NOT YOKAI_${lang}_COMPILER_NAME STREQUAL "GCC")
165+
# Almost all compilers on Earth pretend to be GCC compatible.
166+
# So we first have to check it's really a GCC variant.
167+
# Parse “<compiler> -v”
168+
execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -v
169+
ERROR_VARIABLE CUSTOM_${lang}_GCC_OUTPUT
170+
RESULT_VARIABLE CUSTOM_${lang}_GCC_RETURN_CODE
171+
OUTPUT_QUIET)
172+
173+
if (NOT CUSTOM_${lang}_GCC_RETURN_CODE) # Success
174+
# The existence of this string tells us it's a GCC variant.
175+
# The version in this string is the same as __VERSION__,
176+
# the version of the GCC variant, not the version of the upstream
177+
# GCC we are looking for.
178+
if ("${CUSTOM_${lang}_GCC_OUTPUT}" MATCHES "\ngcc version ")
179+
set(YOKAI_${lang}_COMPILER_EXTENDED_VERSION
180+
"${YOKAI_${lang}_COMPILER_VERSION}/GCC_${YOKAI_${lang}_COMPILER_GCC_VERSION}")
181+
endif()
182+
endif()
183+
endif()
184+
endif()
185+
endif()
186+
187+
if (NOT YOKAI_${lang}_COMPILER_EXTENDED_VERSION)
188+
set(YOKAI_${lang}_COMPILER_EXTENDED_VERSION "${YOKAI_${lang}_COMPILER_VERSION}")
189+
endif()
190+
191+
set(YOKAI_${lang}_COMPILER_STRING
192+
"${YOKAI_${lang}_COMPILER_NAME}_${YOKAI_${lang}_COMPILER_EXTENDED_VERSION}:${YOKAI_${lang}_COMPILER_BASENAME}")
193+
194+
if (CMAKE_CXX_COMPILER_ARG1)
195+
set(YOKAI_${lang}_COMPILER_STRING "${YOKAI_${lang}_COMPILER_STRING}:${CMAKE_CXX_COMPILER_ARG1}")
196+
endif()
197+
198+
message(STATUS "Detected ${${lang}_NAME} compiler: ${YOKAI_${lang}_COMPILER_STRING}")
199+
200+
# Makes possible to do that in C++ code:
201+
# > if defined(YOKAI_CXX_COMPILER_CLANG)
202+
set(compiler_var_name "YOKAI_${lang}_COMPILER_${YOKAI_${lang}_COMPILER_NAME_UPPER}")
203+
add_definitions(-D${compiler_var_name})
204+
205+
# Makes possible to do that in CMake code:
206+
# > if (YOKAI_CXX_COMPILER_CLANG)
207+
set("${compiler_var_name}" ON)
208+
209+
if (YOKAI_SOURCE_GENERATOR)
210+
# Add printable string to the executable.
211+
yokai_add_buildinfo("char*" "YOKAI_${lang}_COMPILER_STRING" "\"${YOKAI_${lang}_COMPILER_STRING}\"")
212+
endif()
213+
endforeach()

0 commit comments

Comments
 (0)