summaryrefslogtreecommitdiff
path: root/libs/assimp/contrib/draco/cmake/draco_targets.cmake
blob: 0456c4d7b283d5aec182ef0b4695edc602f15aeb (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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
if(DRACO_CMAKE_DRACO_TARGETS_CMAKE_)
  return()
endif() # DRACO_CMAKE_DRACO_TARGETS_CMAKE_
set(DRACO_CMAKE_DRACO_TARGETS_CMAKE_ 1)

# Resets list variables used to track draco targets.
macro(draco_reset_target_lists)
  unset(draco_targets)
  unset(draco_exe_targets)
  unset(draco_lib_targets)
  unset(draco_objlib_targets)
  unset(draco_module_targets)
  unset(draco_sources)
  unset(draco_test_targets)
endmacro()

# Creates an executable target. The target name is passed as a parameter to the
# NAME argument, and the sources passed as a parameter to the SOURCES argument:
# draco_add_executable(NAME <name> SOURCES <sources> [optional args])
#
# Optional args:
# cmake-format: off
#   - OUTPUT_NAME: Override output file basename. Target basename defaults to
#     NAME.
#   - TEST: Flag. Presence means treat executable as a test.
#   - DEFINES: List of preprocessor macro definitions.
#   - INCLUDES: list of include directories for the target.
#   - COMPILE_FLAGS: list of compiler flags for the target.
#   - LINK_FLAGS: List of linker flags for the target.
#   - OBJLIB_DEPS: List of CMake object library target dependencies.
#   - LIB_DEPS: List of CMake library dependencies.
# cmake-format: on
#
# Sources passed to this macro are added to $draco_test_sources when TEST is
# specified. Otherwise sources are added to $draco_sources.
#
# Targets passed to this macro are always added to the $draco_targets list. When
# TEST is specified targets are also added to the $draco_test_targets list.
# Otherwise targets are added to $draco_exe_targets.
macro(draco_add_executable)
  unset(exe_TEST)
  unset(exe_TEST_DEFINES_MAIN)
  unset(exe_NAME)
  unset(exe_OUTPUT_NAME)
  unset(exe_SOURCES)
  unset(exe_DEFINES)
  unset(exe_INCLUDES)
  unset(exe_COMPILE_FLAGS)
  unset(exe_LINK_FLAGS)
  unset(exe_OBJLIB_DEPS)
  unset(exe_LIB_DEPS)
  set(optional_args TEST)
  set(single_value_args NAME OUTPUT_NAME)
  set(multi_value_args SOURCES DEFINES INCLUDES COMPILE_FLAGS LINK_FLAGS
                       OBJLIB_DEPS LIB_DEPS)

  cmake_parse_arguments(exe "${optional_args}" "${single_value_args}"
                        "${multi_value_args}" ${ARGN})

  if(DRACO_VERBOSE GREATER 1)
    message("--------- draco_add_executable ---------\n"
            "exe_TEST=${exe_TEST}\n"
            "exe_TEST_DEFINES_MAIN=${exe_TEST_DEFINES_MAIN}\n"
            "exe_NAME=${exe_NAME}\n"
            "exe_OUTPUT_NAME=${exe_OUTPUT_NAME}\n"
            "exe_SOURCES=${exe_SOURCES}\n"
            "exe_DEFINES=${exe_DEFINES}\n"
            "exe_INCLUDES=${exe_INCLUDES}\n"
            "exe_COMPILE_FLAGS=${exe_COMPILE_FLAGS}\n"
            "exe_LINK_FLAGS=${exe_LINK_FLAGS}\n"
            "exe_OBJLIB_DEPS=${exe_OBJLIB_DEPS}\n"
            "exe_LIB_DEPS=${exe_LIB_DEPS}\n"
            "------------------------------------------\n")
  endif()

  if(NOT (exe_NAME AND exe_SOURCES))
    message(FATAL_ERROR "draco_add_executable: NAME and SOURCES required.")
  endif()

  list(APPEND draco_targets ${exe_NAME})
  if(exe_TEST)
    list(APPEND draco_test_targets ${exe_NAME})
    list(APPEND draco_test_sources ${exe_SOURCES})
  else()
    list(APPEND draco_exe_targets ${exe_NAME})
    list(APPEND draco_sources ${exe_SOURCES})
  endif()

  add_executable(${exe_NAME} ${exe_SOURCES})
  set_target_properties(${exe_NAME} PROPERTIES VERSION ${DRACO_VERSION})

  if(exe_OUTPUT_NAME)
    set_target_properties(${exe_NAME} PROPERTIES OUTPUT_NAME ${exe_OUTPUT_NAME})
  endif()

  draco_process_intrinsics_sources(TARGET ${exe_NAME} SOURCES ${exe_SOURCES})

  if(exe_DEFINES)
    target_compile_definitions(${exe_NAME} PRIVATE ${exe_DEFINES})
  endif()

  if(exe_INCLUDES)
    target_include_directories(${exe_NAME} PRIVATE ${exe_INCLUDES})
  endif()

  if(exe_COMPILE_FLAGS OR DRACO_CXX_FLAGS)
    target_compile_options(${exe_NAME}
                           PRIVATE ${exe_COMPILE_FLAGS} ${DRACO_CXX_FLAGS})
  endif()

  if(exe_LINK_FLAGS OR DRACO_EXE_LINKER_FLAGS)
    if(${CMAKE_VERSION} VERSION_LESS "3.13")
      list(APPEND exe_LINK_FLAGS "${DRACO_EXE_LINKER_FLAGS}")
      # LINK_FLAGS is managed as a string.
      draco_set_and_stringify(SOURCE "${exe_LINK_FLAGS}" DEST exe_LINK_FLAGS)
      set_target_properties(${exe_NAME}
                            PROPERTIES LINK_FLAGS "${exe_LINK_FLAGS}")
    else()
      target_link_options(${exe_NAME} PRIVATE ${exe_LINK_FLAGS}
                          ${DRACO_EXE_LINKER_FLAGS})
    endif()
  endif()

  if(exe_OBJLIB_DEPS)
    foreach(objlib_dep ${exe_OBJLIB_DEPS})
      target_sources(${exe_NAME} PRIVATE $<TARGET_OBJECTS:${objlib_dep}>)
    endforeach()
  endif()

  if(CMAKE_THREAD_LIBS_INIT)
    list(APPEND exe_LIB_DEPS ${CMAKE_THREAD_LIBS_INIT})
  endif()

  if(BUILD_SHARED_LIBS AND (MSVC OR WIN32))
    target_compile_definitions(${exe_NAME} PRIVATE "DRACO_BUILDING_DLL=0")
  endif()

  if(exe_LIB_DEPS)
    unset(exe_static)
    if("${CMAKE_EXE_LINKER_FLAGS} ${DRACO_EXE_LINKER_FLAGS}" MATCHES "static")
      set(exe_static ON)
    endif()

    if(exe_static AND CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
      # Third party dependencies can introduce dependencies on system and test
      # libraries. Since the target created here is an executable, and CMake
      # does not provide a method of controlling order of link dependencies,
      # wrap all of the dependencies of this target in start/end group flags to
      # ensure that dependencies of third party targets can be resolved when
      # those dependencies happen to be resolved by dependencies of the current
      # target.
      list(INSERT exe_LIB_DEPS 0 -Wl,--start-group)
      list(APPEND exe_LIB_DEPS -Wl,--end-group)
    endif()
    target_link_libraries(${exe_NAME} PRIVATE ${exe_LIB_DEPS})
  endif()
endmacro()

# Creates a library target of the specified type. The target name is passed as a
# parameter to the NAME argument, the type as a parameter to the TYPE argument,
# and the sources passed as a parameter to the SOURCES argument:
# draco_add_library(NAME <name> TYPE <type> SOURCES <sources> [optional args])
#
# Optional args:
# cmake-format: off
#   - OUTPUT_NAME: Override output file basename. Target basename defaults to
#     NAME. OUTPUT_NAME is ignored when BUILD_SHARED_LIBS is enabled and CMake
#     is generating a build for which MSVC is true. This is to avoid output
#     basename collisions with DLL import libraries.
#   - TEST: Flag. Presence means treat library as a test.
#   - DEFINES: List of preprocessor macro definitions.
#   - INCLUDES: list of include directories for the target.
#   - COMPILE_FLAGS: list of compiler flags for the target.
#   - LINK_FLAGS: List of linker flags for the target.
#   - OBJLIB_DEPS: List of CMake object library target dependencies.
#   - LIB_DEPS: List of CMake library dependencies.
#   - PUBLIC_INCLUDES: List of include paths to export to dependents.
# cmake-format: on
#
# Sources passed to the macro are added to the lists tracking draco sources:
# cmake-format: off
#   - When TEST is specified sources are added to $draco_test_sources.
#   - Otherwise sources are added to $draco_sources.
# cmake-format: on
#
# Targets passed to this macro are added to the lists tracking draco targets:
# cmake-format: off
#   - Targets are always added to $draco_targets.
#   - When the TEST flag is specified, targets are added to
#     $draco_test_targets.
#   - When TEST is not specified:
#     - Libraries of type SHARED are added to $draco_dylib_targets.
#     - Libraries of type OBJECT are added to $draco_objlib_targets.
#     - Libraries of type STATIC are added to $draco_lib_targets.
# cmake-format: on
macro(draco_add_library)
  unset(lib_TEST)
  unset(lib_NAME)
  unset(lib_OUTPUT_NAME)
  unset(lib_TYPE)
  unset(lib_SOURCES)
  unset(lib_DEFINES)
  unset(lib_INCLUDES)
  unset(lib_COMPILE_FLAGS)
  unset(lib_LINK_FLAGS)
  unset(lib_OBJLIB_DEPS)
  unset(lib_LIB_DEPS)
  unset(lib_PUBLIC_INCLUDES)
  unset(lib_TARGET_PROPERTIES)
  set(optional_args TEST)
  set(single_value_args NAME OUTPUT_NAME TYPE)
  set(multi_value_args SOURCES DEFINES INCLUDES COMPILE_FLAGS LINK_FLAGS
                       OBJLIB_DEPS LIB_DEPS PUBLIC_INCLUDES TARGET_PROPERTIES)

  cmake_parse_arguments(lib "${optional_args}" "${single_value_args}"
                        "${multi_value_args}" ${ARGN})

  if(DRACO_VERBOSE GREATER 1)
    message("--------- draco_add_library ---------\n"
            "lib_TEST=${lib_TEST}\n"
            "lib_NAME=${lib_NAME}\n"
            "lib_OUTPUT_NAME=${lib_OUTPUT_NAME}\n"
            "lib_TYPE=${lib_TYPE}\n"
            "lib_SOURCES=${lib_SOURCES}\n"
            "lib_DEFINES=${lib_DEFINES}\n"
            "lib_INCLUDES=${lib_INCLUDES}\n"
            "lib_COMPILE_FLAGS=${lib_COMPILE_FLAGS}\n"
            "lib_LINK_FLAGS=${lib_LINK_FLAGS}\n"
            "lib_OBJLIB_DEPS=${lib_OBJLIB_DEPS}\n"
            "lib_LIB_DEPS=${lib_LIB_DEPS}\n"
            "lib_PUBLIC_INCLUDES=${lib_PUBLIC_INCLUDES}\n"
            "---------------------------------------\n")
  endif()

  if(NOT (lib_NAME AND lib_TYPE))
    message(FATAL_ERROR "draco_add_library: NAME and TYPE required.")
  endif()

  list(APPEND draco_targets ${lib_NAME})
  if(lib_TEST)
    list(APPEND draco_test_targets ${lib_NAME})
    list(APPEND draco_test_sources ${lib_SOURCES})
  else()
    list(APPEND draco_sources ${lib_SOURCES})
    if(lib_TYPE STREQUAL MODULE)
      list(APPEND draco_module_targets ${lib_NAME})
    elseif(lib_TYPE STREQUAL OBJECT)
      list(APPEND draco_objlib_targets ${lib_NAME})
    elseif(lib_TYPE STREQUAL SHARED)
      list(APPEND draco_dylib_targets ${lib_NAME})
    elseif(lib_TYPE STREQUAL STATIC)
      list(APPEND draco_lib_targets ${lib_NAME})
    else()
      message(WARNING "draco_add_library: Unhandled type: ${lib_TYPE}")
    endif()
  endif()

  add_library(${lib_NAME} ${lib_TYPE} ${lib_SOURCES})
  if(lib_SOURCES)
    draco_process_intrinsics_sources(TARGET ${lib_NAME} SOURCES ${lib_SOURCES})
  endif()

  if(lib_OUTPUT_NAME)
    if(NOT (BUILD_SHARED_LIBS AND MSVC))
      set_target_properties(${lib_NAME}
                            PROPERTIES OUTPUT_NAME ${lib_OUTPUT_NAME})
    endif()
  endif()

  if(lib_DEFINES)
    target_compile_definitions(${lib_NAME} PRIVATE ${lib_DEFINES})
  endif()

  if(lib_INCLUDES)
    target_include_directories(${lib_NAME} PRIVATE ${lib_INCLUDES})
  endif()

  if(lib_PUBLIC_INCLUDES)
    target_include_directories(${lib_NAME} PUBLIC ${lib_PUBLIC_INCLUDES})
  endif()

  if(lib_COMPILE_FLAGS OR DRACO_CXX_FLAGS)
    target_compile_options(${lib_NAME}
                           PRIVATE ${lib_COMPILE_FLAGS} ${DRACO_CXX_FLAGS})
  endif()

  if(lib_LINK_FLAGS)
    set_target_properties(${lib_NAME} PROPERTIES LINK_FLAGS ${lib_LINK_FLAGS})
  endif()

  if(lib_OBJLIB_DEPS)
    foreach(objlib_dep ${lib_OBJLIB_DEPS})
      target_sources(${lib_NAME} PRIVATE $<TARGET_OBJECTS:${objlib_dep}>)
    endforeach()
  endif()

  if(lib_LIB_DEPS)
    if(lib_TYPE STREQUAL STATIC)
      set(link_type PUBLIC)
    else()
      set(link_type PRIVATE)
      if(lib_TYPE STREQUAL SHARED AND CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
        # The draco shared object uses the static draco as input to turn it into
        # a shared object. Include everything from the static library in the
        # shared object.
        if(APPLE)
          list(INSERT lib_LIB_DEPS 0 -Wl,-force_load)
        else()
          list(INSERT lib_LIB_DEPS 0 -Wl,--whole-archive)
          list(APPEND lib_LIB_DEPS -Wl,--no-whole-archive)
        endif()
      endif()
    endif()
    target_link_libraries(${lib_NAME} ${link_type} ${lib_LIB_DEPS})
  endif()

  if(NOT MSVC AND lib_NAME MATCHES "^lib")
    # Non-MSVC generators prepend lib to static lib target file names. Libdraco
    # already includes lib in its name. Avoid naming output files liblib*.
    set_target_properties(${lib_NAME} PROPERTIES PREFIX "")
  endif()

  # VERSION and SOVERSION as necessary
  if(NOT lib_TYPE STREQUAL STATIC AND NOT lib_TYPE STREQUAL MODULE)
    set_target_properties(${lib_NAME} PROPERTIES VERSION ${DRACO_VERSION})
    if(NOT MSVC)
      set_target_properties(${lib_NAME} PROPERTIES SOVERSION ${DRACO_SOVERSION})
    endif()
  endif()

  if(BUILD_SHARED_LIBS AND (MSVC OR WIN32))
    if(lib_TYPE STREQUAL SHARED)
      target_compile_definitions(${lib_NAME} PRIVATE "DRACO_BUILDING_DLL=1")
    else()
      target_compile_definitions(${lib_NAME} PRIVATE "DRACO_BUILDING_DLL=0")
    endif()
  endif()

  # Determine if $lib_NAME is a header only target.
  unset(sources_list)
  if(lib_SOURCES)
    set(sources_list ${lib_SOURCES})
    list(FILTER sources_list INCLUDE REGEX cc$)
  endif()

  if(NOT sources_list)
    if(NOT XCODE)
      # This is a header only target. Tell CMake the link language.
      set_target_properties(${lib_NAME} PROPERTIES LINKER_LANGUAGE CXX)
    else()
      # The Xcode generator ignores LINKER_LANGUAGE. Add a dummy cc file.
      draco_create_dummy_source_file(TARGET ${lib_NAME} BASENAME ${lib_NAME})
    endif()
  endif()
endmacro()