Skip to content

Commit a514e8d

Browse files
committed
Flatten main CMakeLists
1 parent c135969 commit a514e8d

7 files changed

Lines changed: 283 additions & 225 deletions

File tree

CMake/build-from-src.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ function(build_from_src dep)
313313
COMMAND ${CMAKE_COMMAND} -E copy <SOURCE_DIR>/ikcp.h ${BINARY_DIR}/include/
314314
)
315315
ExternalProject_Get_Property(kcp SOURCE_DIR)
316-
set(kcp_SOURCE_DIR ${SOURCE_DIR} PARENT_SCOPE)
317-
set(kcp_static ${BINARY_DIR}/lib/libikcp.a PARENT_SCOPE)
316+
set(KCP_INCLUDE_DIRS ${SOURCE_DIR} PARENT_SCOPE)
317+
set(KCP_LIBRARIES ${BINARY_DIR}/lib/libikcp.a PARENT_SCOPE)
318318

319319
endif ()
320320

CMake/photon-deps.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Central registry that exposes each resolved dependency as an internal target
2+
## Photon::<name>, so include dirs / definitions / link libraries propagate
3+
## through the target graph instead of loose <DEP>_INCLUDE_DIRS / <DEP>_LIBRARIES
4+
## variables.
5+
##
6+
## Both dependency acquisition paths -- find_package (the Find<pkg>.cmake modules)
7+
## and build_from_src (ExternalProject) -- publish the same conventional
8+
## variables. The top-level dependency loop feeds those into photon_declare_dep(),
9+
## so a dependency looks identical to consumers regardless of where it came from.
10+
##
11+
## Note: targets carry compile usage requirements (include dirs, definitions) plus
12+
## the dependency's link libraries. Photon's own libraries still assemble their
13+
## final link line manually (whole-archive re-export cannot be expressed with
14+
## targets under CMake 3.14), so the link libraries recorded here are informative
15+
## for consumers rather than the source Photon itself links from.
16+
include_guard(GLOBAL)
17+
18+
# photon_declare_dep(<name> [INCLUDES <dir>...] [LIBRARIES <lib>...] [DEFINITIONS <def>...])
19+
#
20+
# Creates the INTERFACE library _photon_dep_<name> holding the usage
21+
# requirements and exposes it as the alias Photon::<name>. All keyword groups
22+
# are optional; empty groups are ignored, so it is safe to call with whatever
23+
# the acquisition path happened to publish.
24+
function(photon_declare_dep name)
25+
cmake_parse_arguments(_dep "" "" "INCLUDES;LIBRARIES;DEFINITIONS" ${ARGN})
26+
set(_tgt _photon_dep_${name})
27+
if (NOT TARGET ${_tgt})
28+
add_library(${_tgt} INTERFACE)
29+
add_library(Photon::${name} ALIAS ${_tgt})
30+
endif ()
31+
if (_dep_INCLUDES)
32+
target_include_directories(${_tgt} INTERFACE ${_dep_INCLUDES})
33+
endif ()
34+
if (_dep_LIBRARIES)
35+
target_link_libraries(${_tgt} INTERFACE ${_dep_LIBRARIES})
36+
endif ()
37+
if (_dep_DEFINITIONS)
38+
target_compile_definitions(${_tgt} INTERFACE ${_dep_DEFINITIONS})
39+
endif ()
40+
endfunction()

CMake/photon-helpers.cmake

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Declarative helpers that replace the repetitive option / source-URL / feature
2+
# / dependency if-chains in the top-level CMakeLists.txt with one readable line
3+
# each. Behavior is intentionally identical to the hand-written blocks they
4+
# supersede; they only remove boilerplate.
5+
6+
# A user-facing cache knob, declared with one uniform form instead of mixing
7+
# option() (booleans) with set(... CACHE STRING) (string values).
8+
# photon_option(<name> BOOL|STRING <default> <help>)
9+
# option(X "help" OFF) is exactly set(X OFF CACHE BOOL "help"), so BOOL knobs
10+
# behave identically to the option() they replace.
11+
macro(photon_option name type default help)
12+
set(${name} ${default} CACHE ${type} "${help}")
13+
endmacro ()
14+
15+
# A build-from-source download location, collapsing the repetitive
16+
# set(PHOTON_..._SOURCE "url" CACHE STRING "") table into one line each.
17+
# <key> carries the suffix (SOURCE or GIT) so the resulting cache variable name
18+
# (PHOTON_<key>) matches the existing convention.
19+
macro(photon_source key url)
20+
set(PHOTON_${key} "${url}" CACHE STRING "")
21+
endmacro ()
22+
23+
# Append <name> to the `dependencies` list. Any trailing arguments form a
24+
# condition evaluated by if(); with no condition the dependency is
25+
# unconditional. Keeps each dependency on a single declarative line with its
26+
# enabling condition inline, in place of a long if() / list(APPEND) chain.
27+
macro(photon_dependency name)
28+
if (${ARGC} GREATER 1)
29+
if (${ARGN})
30+
list(APPEND dependencies ${name})
31+
endif ()
32+
else ()
33+
list(APPEND dependencies ${name})
34+
endif ()
35+
endmacro ()
36+
37+
# target_compile_definitions(photon_features INTERFACE <defs>), guarded by a
38+
# condition:
39+
# photon_feature(IF <cond...> DEFS <defs...>)
40+
# <cond...> is any expression if() accepts; the definitions are added only when
41+
# it holds. Mirrors the per-feature if() blocks with one line each.
42+
function(photon_feature)
43+
cmake_parse_arguments(_pf "" "" "IF;DEFS" ${ARGN})
44+
if (${_pf_IF})
45+
target_compile_definitions(photon_features INTERFACE ${_pf_DEFS})
46+
endif ()
47+
endfunction ()
48+
49+
# list(APPEND <list> <items...>), guarded by a condition:
50+
# photon_append_if(<list> IF <cond...> ITEMS <items...>)
51+
# The items (possibly empty, e.g. an unset *_LIBRARIES) are appended only when
52+
# <cond...> holds. Replaces the if() / list(APPEND) blocks that assemble the
53+
# link-time dependency lists while preserving their append order.
54+
function(photon_append_if listvar)
55+
cmake_parse_arguments(_pa "" "" "IF;ITEMS" ${ARGN})
56+
if (${_pa_IF})
57+
list(APPEND ${listvar} ${_pa_ITEMS})
58+
set(${listvar} "${${listvar}}" PARENT_SCOPE)
59+
endif ()
60+
endfunction ()
61+
62+
# list(REMOVE_ITEM <list> <items...>), guarded by a condition -- the opt-out
63+
# counterpart of photon_append_if:
64+
# photon_remove_if(<list> IF <cond...> ITEMS <items...>)
65+
# Used where a file is globbed in by default and dropped when a feature is off.
66+
function(photon_remove_if listvar)
67+
cmake_parse_arguments(_pr "" "" "IF;ITEMS" ${ARGN})
68+
if (${_pr_IF})
69+
list(REMOVE_ITEM ${listvar} ${_pr_ITEMS})
70+
set(${listvar} "${${listvar}}" PARENT_SCOPE)
71+
endif ()
72+
endfunction ()

0 commit comments

Comments
 (0)