summaryrefslogtreecommitdiff
path: root/cmake/DemoCommon.cmake
blob: 1c63b2608b66efa37a9c9d7ad566d162a5ec8663 (plain)
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
# CMake Common Utilities
# Shared macros and functions for the demo64k build system

# =============================================================================
# demo_set_conditional_sources(VAR HEADLESS_LIST STRIP_LIST NORMAL_LIST)
# =============================================================================
# Simplifies 3-branch conditional source lists based on build mode
#
# Arguments:
#   VAR           - Output variable name (e.g., GPU_SOURCES)
#   HEADLESS_LIST - Sources for DEMO_HEADLESS mode
#   STRIP_LIST    - Sources for DEMO_STRIP_EXTERNAL_LIBS mode
#   NORMAL_LIST   - Sources for normal builds
#
# Usage:
#   demo_set_conditional_sources(GPU_SOURCES
#       "src/gpu/headless_gpu.cc;src/gpu/effect.cc"
#       "src/gpu/stub_gpu.cc"
#       "src/gpu/gpu.cc;src/gpu/effect.cc"
#   )
function(demo_set_conditional_sources VAR HEADLESS_LIST STRIP_LIST NORMAL_LIST)
    if(DEMO_HEADLESS)
        set(${VAR} ${HEADLESS_LIST} PARENT_SCOPE)
    elseif(DEMO_STRIP_EXTERNAL_LIBS)
        set(${VAR} ${STRIP_LIST} PARENT_SCOPE)
    else()
        set(${VAR} ${NORMAL_LIST} PARENT_SCOPE)
    endif()
endfunction()

# =============================================================================
# demo_apply_size_optimizations(TARGET)
# =============================================================================
# Applies platform-specific size optimization flags to a target
#
# Arguments:
#   TARGET - CMake target name
#
# Behavior:
#   - Only applies if DEMO_SIZE_OPT is enabled
#   - MSVC:  /Os /GS- /OPT:REF /OPT:ICF
#   - Apple: -Os -Wl,-dead_strip
#   - Linux: -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -s
#
# Usage:
#   demo_apply_size_optimizations(demo64k)
function(demo_apply_size_optimizations TARGET)
    if(DEMO_SIZE_OPT)
        if(MSVC)
            target_compile_options(${TARGET} PRIVATE /Os /GS-)
            target_link_options(${TARGET} PRIVATE /OPT:REF /OPT:ICF /INCREMENTAL:NO)
        elseif(APPLE)
            target_compile_options(${TARGET} PRIVATE -Os)
            target_link_options(${TARGET} PRIVATE -Wl,-dead_strip)
        else()
            target_compile_options(${TARGET} PRIVATE -Os -ffunction-sections -fdata-sections)
            target_link_options(${TARGET} PRIVATE -Wl,--gc-sections -s)
        endif()
    endif()
endfunction()

# =============================================================================
# demo_link_libraries_ordered(TARGET)
# =============================================================================
# Links subsystem libraries in correct order with platform-specific quirks
#
# Arguments:
#   TARGET - CMake target name
#
# Link Order:
#   - Circular dependencies on Linux require --start-group/--end-group
#   - Apple uses direct linking
#   - DEMO_STRIP_EXTERNAL_LIBS mode excludes DEMO_LIBS
#
# Usage:
#   demo_link_libraries_ordered(demo64k)
function(demo_link_libraries_ordered TARGET)
    if(DEMO_STRIP_EXTERNAL_LIBS)
        # Size measurement mode: No external libraries
        if(APPLE)
            target_link_libraries(${TARGET} PRIVATE 3d gpu audio procedural util)
        else()
            target_link_libraries(${TARGET} PRIVATE
                -Wl,--start-group 3d gpu audio procedural util -Wl,--end-group
                pthread m)
        endif()
    else()
        # Normal mode: Link external libraries
        if(APPLE)
            target_link_libraries(${TARGET} PRIVATE 3d gpu audio procedural util ${DEMO_LIBS})
        else()
            target_link_libraries(${TARGET} PRIVATE
                -Wl,--start-group 3d gpu audio procedural util -Wl,--end-group
                ${DEMO_LIBS})
        endif()
    endif()
endfunction()

# =============================================================================
# demo_add_test_with_deps(NAME TEST_NAME LABEL SOURCE [SOURCES...]
#                          [LINK libs...] [DEPENDS targets...]
#                          [DEFINITIONS defs...] [INCLUDES dirs...])
# =============================================================================
# Streamlined test registration with automatic dependency handling
#
# Arguments:
#   NAME        - Executable target name
#   TEST_NAME   - CTest test name
#   LABEL       - Test label for subsystem grouping (audio, gpu, 3d, assets, util)
#   SOURCE      - First source file (required)
#   SOURCES...  - Additional source files (parsed until keyword)
#
# Optional Keywords:
#   LINK libs...        - Libraries to link (parsed until next keyword)
#   DEPENDS targets...  - Target dependencies (parsed until next keyword)
#   DEFINITIONS defs... - Compile definitions (parsed until next keyword)
#   INCLUDES dirs...    - Include directories (parsed until next keyword)
#
# Usage:
#   demo_add_test_with_deps(test_window HammingWindowTest audio
#       src/tests/audio/test_window.cc
#       ${GEN_DEMO_CC}
#       LINK audio util procedural ${DEMO_LIBS}
#       DEPENDS generate_demo_assets
#   )
function(demo_add_test_with_deps NAME TEST_NAME LABEL)
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs LINK DEPENDS DEFINITIONS INCLUDES)

    # Collect sources until we hit a keyword
    set(SOURCES "")
    set(current_list SOURCES)
    set(keyword_mode FALSE)

    foreach(arg ${ARGN})
        if(arg STREQUAL "LINK" OR arg STREQUAL "DEPENDS" OR
           arg STREQUAL "DEFINITIONS" OR arg STREQUAL "INCLUDES")
            set(current_list ${arg})
            set(keyword_mode TRUE)
        else()
            list(APPEND ${current_list} ${arg})
        endif()
    endforeach()

    # Create test executable
    add_executable(${NAME} ${SOURCES})
    add_test(NAME ${TEST_NAME} COMMAND ${NAME})
    set_tests_properties(${TEST_NAME} PROPERTIES LABELS "${LABEL}")

    # Apply optional parameters
    if(DEFINED LINK)
        target_link_libraries(${NAME} PRIVATE ${LINK})
    endif()

    if(DEFINED DEPENDS)
        add_dependencies(${NAME} ${DEPENDS})
    endif()

    if(DEFINED DEFINITIONS)
        target_compile_definitions(${NAME} PRIVATE ${DEFINITIONS})
    endif()

    if(DEFINED INCLUDES)
        target_include_directories(${NAME} PRIVATE ${INCLUDES})
    endif()
endfunction()

# =============================================================================
# add_demo_executable(NAME SOURCES...)
# =============================================================================
# Creates an executable for the demo
#
# Arguments:
#   NAME     - Executable target name
#   SOURCES  - Source files
#
# Note: target_link_libraries must be called manually to ensure correct order
macro(add_demo_executable NAME)
    add_executable(${NAME} ${ARGN})
endmacro()

# =============================================================================
# add_demo_test(NAME TEST_NAME LABEL SOURCES...)
# =============================================================================
# Creates a test executable and registers it with CTest (legacy macro)
#
# Arguments:
#   NAME      - Executable target name
#   TEST_NAME - CTest test name
#   LABEL     - Test label for subsystem grouping
#   SOURCES   - Source files
#
# Note: Use demo_add_test_with_deps() for new tests
macro(add_demo_test NAME TEST_NAME LABEL)
    add_executable(${NAME} ${ARGN})
    add_test(NAME ${TEST_NAME} COMMAND ${NAME})
    set_tests_properties(${TEST_NAME} PROPERTIES LABELS "${LABEL}")
endmacro()