Skip to content

Commit 802f7f9

Browse files
authored
Merge pull request #150 from aminya/multi-project [skip ci]
2 parents 5e077f5 + 0eb9b71 commit 802f7f9

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ It accepts the following named flags:
221221

222222
It gets the following named parameters that can have different values in front of them:
223223

224+
- `PREFIX`: the optional prefix that is used to define `${PREFIX}_project_options` and `${PREFIX}_project_warnings` targets when the function is used in a multi-project fashion.
224225
- `DOXYGEN_THEME`: the name of the Doxygen theme to use. Supported themes:
225226
- `awesome-sidebar` (default)
226227
- `awesome`

src/Index.cmake

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ include("${ProjectOptions_SRC_DIR}/Vcpkg.cmake")
4141

4242
#
4343
# Params:
44+
# - PREFIX: the optional prefix to be prepended to the `project_options` and `project_warnings` targets when the function is used in a multi-project fashion.
4445
# - WARNINGS_AS_ERRORS: Treat compiler warnings as errors
4546
# - ENABLE_CPPCHECK: Enable static analysis with cppcheck
4647
# - ENABLE_CLANG_TIDY: Enable static analysis with clang-tidy
@@ -94,7 +95,11 @@ macro(project_options)
9495
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
9596
ENABLE_SANITIZER_THREAD
9697
ENABLE_SANITIZER_MEMORY)
97-
set(oneValueArgs LINKER VS_ANALYSIS_RULESET CONAN_PROFILE)
98+
set(oneValueArgs
99+
PREFIX
100+
LINKER
101+
VS_ANALYSIS_RULESET
102+
CONAN_PROFILE)
98103
set(multiValueArgs
99104
DOXYGEN_THEME
100105
MSVC_WARNINGS
@@ -121,8 +126,26 @@ macro(project_options)
121126

122127
common_project_options()
123128

124-
# Link this 'library' to set the c++ standard / compile-time options requested
125-
add_library(project_options INTERFACE)
129+
# Add an interface library for the options
130+
set(_options_target project_options)
131+
set(_warnings_target project_warnings)
132+
if(NOT
133+
"${ProjectOptions_PREFIX}"
134+
STREQUAL
135+
"")
136+
set(_options_target "${ProjectOptions_PREFIX}_project_options")
137+
set(_warnings_target "${ProjectOptions_PREFIX}_project_warnings")
138+
else()
139+
if(TARGET project_options)
140+
message(
141+
FATAL
142+
"Multiple calls to `project_options` in the same `project` detected, but the argument `PREFIX` that is prepended to `project_options` and `project_warnings` is not set."
143+
)
144+
endif()
145+
endif()
146+
147+
add_library(${_options_target} INTERFACE)
148+
add_library(${_warnings_target} INTERFACE)
126149

127150
# fix mingw
128151
mingw_unicode()
@@ -135,46 +158,43 @@ macro(project_options)
135158
set(ProjectOptions_ENABLE_INTERPROCEDURAL_OPTIMIZATION ${ProjectOptions_ENABLE_IPO})
136159
endif()
137160
if(${ProjectOptions_ENABLE_INTERPROCEDURAL_OPTIMIZATION})
138-
enable_interprocedural_optimization(project_options)
161+
enable_interprocedural_optimization(${_options_target})
139162
endif()
140163

141164
if(${ProjectOptions_ENABLE_NATIVE_OPTIMIZATION})
142-
enable_native_optimization(project_options)
165+
enable_native_optimization(${_options_target})
143166
endif()
144167

145168
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
146169
if(ProjectOptions_ENABLE_BUILD_WITH_TIME_TRACE)
147-
target_compile_options(project_options INTERFACE -ftime-trace)
170+
target_compile_options(${_options_target} INTERFACE -ftime-trace)
148171
endif()
149172
endif()
150173

151-
# Link this 'library' to use the warnings specified in CompilerWarnings.cmake
152-
add_library(project_warnings INTERFACE)
153-
154174
if(${ProjectOptions_ENABLE_CACHE})
155175
# enable cache system
156176
enable_cache()
157177
endif()
158178

159179
# use the linker
160-
configure_linker(project_options "${ProjectOptions_LINKER}")
180+
configure_linker(${_options_target} "${ProjectOptions_LINKER}")
161181

162182
# standard compiler warnings
163183
set_project_warnings(
164-
project_warnings
184+
${_warnings_target}
165185
"${WARNINGS_AS_ERRORS}"
166186
"${ProjectOptions_MSVC_WARNINGS}"
167187
"${ProjectOptions_CLANG_WARNINGS}"
168188
"${ProjectOptions_GCC_WARNINGS}"
169189
"${ProjectOptions_CUDA_WARNINGS}")
170190

171191
if(${ProjectOptions_ENABLE_COVERAGE})
172-
enable_coverage(project_options)
192+
enable_coverage(${_options_target})
173193
endif()
174194

175195
# sanitizer options if supported by compiler
176196
enable_sanitizers(
177-
project_options
197+
${_options_target}
178198
${ProjectOptions_ENABLE_SANITIZER_ADDRESS}
179199
${ProjectOptions_ENABLE_SANITIZER_LEAK}
180200
${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
@@ -211,7 +231,7 @@ macro(project_options)
211231
<map>
212232
<utility>)
213233
endif()
214-
target_precompile_headers(project_options INTERFACE ${ProjectOptions_PCH_HEADERS})
234+
target_precompile_headers(${_options_target} INTERFACE ${ProjectOptions_PCH_HEADERS})
215235
endif()
216236

217237
if(${ProjectOptions_ENABLE_VCPKG})
@@ -224,7 +244,7 @@ macro(project_options)
224244

225245
if(${ProjectOptions_ENABLE_UNITY})
226246
# Add for any project you want to apply unity builds for
227-
set_target_properties(project_options PROPERTIES UNITY_BUILD ON)
247+
set_target_properties(${_options_target} PROPERTIES UNITY_BUILD ON)
228248
endif()
229249

230250
endmacro()

0 commit comments

Comments
 (0)