-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
CXXModules.cmake
234 lines (188 loc) · 7.72 KB
/
CXXModules.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# ########################################################################## #
# Copyright (c) 2018 Jiří Fatka
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ########################################################################## #
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# Clang
set(CXX_MODULES_CHECK -fmodules-ts)
set(CXX_MODULES_FLAGS -fmodules-ts)
set(CXX_MODULES_EXT pcm)
set(CXX_MODULES_CREATE_FLAGS -fmodules-ts -x c++-module --precompile)
set(CXX_MODULES_USE_FLAG -fmodule-file=)
set(CXX_MODULES_OUTPUT_FLAG -o)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# GCC
message(FATAL_ERROR "GCC is not supported yet")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# MSVC
# compiler flags for modules were changed at version 16.8
if (MSVC_VERSION LESS 1928)
set(CXX_MODULES_CHECK /experimental:module)
set(CXX_MODULES_FLAGS /experimental:module /module:interface)
set(CXX_MODULES_EXT ifc)
set(CXX_MODULES_CREATE_FLAGS -c)
set(CXX_MODULES_USE_FLAG /module:reference)
set(CXX_MODULES_OUTPUT_FLAG /module:output)
else()
set(CXX_MODULES_CHECK /experimental:module)
set(CXX_MODULES_FLAGS /experimental:module /interface)
set(CXX_MODULES_EXT ifc)
set(CXX_MODULES_CREATE_FLAGS -c)
set(CXX_MODULES_USE_FLAG /reference)
set(CXX_MODULES_OUTPUT_FLAG /ifcOutput)
endif()
else ()
message(FATAL_ERROR "Unsupported compiler")
endif ()
# ########################################################################## #
# Compiler flags support tests
include(CheckCXXCompilerFlag)
include(CMakePushCheckState)
# Check if used compiler version supports modules
check_cxx_compiler_flag(${CXX_MODULES_CHECK} CXX_MODULES)
# ########################################################################## #
##
## Check if current compiler supports C++ modules. If compiler doesn't support
## modules it fails with fatal error.
##
function (_check_cxx_modules_support)
if (NOT CXX_MODULES)
message(FATAL_ERROR "Compiler doesn't support C++ modules (TS)")
endif ()
endfunction ()
# ########################################################################## #
##
## Enable C++ modules for project.
##
## This function adds appropriate compiler flags to the target.
##
function (target_enable_cxx_modules TARGET)
_check_cxx_modules_support()
# Add modules flag
target_compile_options(${TARGET} PRIVATE ${CXX_MODULES_FLAGS})
endfunction ()
# ########################################################################## #
##
## Create an executable with C++ support
##
function (add_module_executable TARGET)
_check_cxx_modules_support()
add_executable(${TARGET} ${ARGN})
# Enable modules for target
target_enable_cxx_modules(${TARGET})
endfunction ()
# ########################################################################## #
##
## Create C++ module library.
##
## Sets target property CXX_MODULES_INTERFACE_FILES and CXX_MODULES_INTERFACE_TARGETS
##
function (add_module_library TARGET)
_check_cxx_modules_support()
# Get sources
set(_sources)
# Filter source files
foreach (_arg ${ARGN})
list(FIND "STATIC;SHARED;MODULE;EXCLUDE_FROM_ALL;OBJECT;UNKNOWN;IMPORTED" ${_arg} _skip)
if (${_skip} GREATER_EQUAL 0)
continue ()
endif ()
if (${_arg} MATCHES "ALIAS")
message(FATAL_ERROR "Alias library is not supported")
endif ()
# TODO: limit sources extensions?
list(APPEND _sources ${_arg})
endforeach ()
# Allow to use CXX compiler on C++ module files
set_source_files_properties(${_sources} PROPERTIES LANGUAGE CXX)
# Create normal library
add_library(${TARGET} ${ARGN})
# Enable modules for target
target_enable_cxx_modules(${TARGET})
set(_interface_files)
set(_interface_targets)
# Create targets for interface files
foreach (_source ${_sources})
set(_o_file ${_source}.${CXX_MODULES_EXT})
set(_i_file ${CMAKE_CURRENT_SOURCE_DIR}/${_source})
# TODO: CXX flags might be different
set(_cmd ${CMAKE_CXX_COMPILER} "$<JOIN:$<TARGET_PROPERTY:${TARGET},COMPILE_OPTIONS>,\t>" ${CXX_MODULES_CREATE_FLAGS} ${_i_file} ${CXX_MODULES_OUTPUT_FLAG} ${_o_file})
get_filename_component(_o_file_dir ${_o_file} DIRECTORY)
if (_o_file_dir)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_o_file_dir})
endif()
# Create interface build target
add_custom_command(
OUTPUT ${_o_file}
COMMAND ${_cmd}
DEPENDS ${_i_file}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
# Replace directory separators with something else
string(REPLACE "/" "__" _o_file_target ${_o_file})
string(PREPEND _o_file_target "module_")
# Create interface build target
add_custom_target(${_o_file_target}
COMMAND ${_cmd}
DEPENDS ${_o_file}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
list(APPEND _interface_files ${_o_file})
list(APPEND _interface_targets ${_o_file_target})
endforeach ()
# Store property with interface files
set_target_properties(${TARGET}
PROPERTIES CXX_MODULES_INTERFACE_FILES "${_interface_files}"
)
set_target_properties(${TARGET}
PROPERTIES CXX_MODULES_INTERFACE_TARGETS "${_interface_targets}"
)
endfunction ()
# ########################################################################## #
##
## Link a (C++ module) library to (C++ module) target.
##
## Use target property CXX_MODULES_INTERFACE_FILES and CXX_MODULES_INTERFACE_TARGETS
##
function (target_link_module_libraries TARGET)
_check_cxx_modules_support()
# Enable modules for target
target_enable_cxx_modules(${TARGET})
foreach (_arg ${ARGN})
list(FIND "PUBLIC;PRIVATE;INTERFACE" ${_arg} _skip)
if (${_skip} GREATER_EQUAL 0)
continue ()
endif ()
# Get interface files from library
get_target_property(_interface_targets ${_arg} CXX_MODULES_INTERFACE_TARGETS)
foreach (_target ${_interface_targets})
add_dependencies(${TARGET} ${_target})
endforeach ()
# Get interface files from library
get_target_property(_interface_files ${_arg} CXX_MODULES_INTERFACE_FILES)
foreach (_file ${_interface_files})
# TODO: might be different on different compilers
target_compile_options(${TARGET} PRIVATE ${CXX_MODULES_USE_FLAG}${CMAKE_CURRENT_BINARY_DIR}/${_file})
endforeach ()
endforeach ()
# Normal link
target_link_libraries(${TARGET} ${ARGN})
endfunction ()
# ########################################################################## #