File: IgnConfigureBuild.cmake

package info (click to toggle)
ignition-cmake 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,132 kB
  • sloc: xml: 35,671; python: 3,768; javascript: 2,308; sh: 172; ansic: 109; cpp: 64; makefile: 7
file content (392 lines) | stat: -rw-r--r-- 14,309 bytes parent folder | download
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#.rst
# IgnBuildProject
# -------------------
#
# ign_configure_build()
#
# Configures the build rules of an ignition library project.
#
#===============================================================================
# Copyright (C) 2017 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#################################################
# Configure the build of the ignition project
# Pass the argument HIDE_SYMBOLS_BY_DEFAULT to configure symbol visibility so
# that symbols are hidden unless explicitly marked as visible.
# Pass the argument QUIT_IF_BUILD_ERRORS to have this macro quit cmake when the
# build_errors
macro(ign_configure_build)

  #============================================================================
  # Parse the arguments that are passed in
  set(options HIDE_SYMBOLS_BY_DEFAULT QUIT_IF_BUILD_ERRORS)
  set(oneValueArgs)
  set(multiValueArgs COMPONENTS)
  cmake_parse_arguments(ign_configure_build "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

  #============================================================================
  # Examine the build type. If we do not recognize the type, we will generate
  # an error, so this must come before the error handling.
  ign_parse_build_type()

  #============================================================================
  # Ask whether we should make a shared or static library.
  option(BUILD_SHARED_LIBS "Set this to true to generate shared libraries (recommended), or false for static libraries" ON)

  #============================================================================
  # Print warnings and errors
  if(build_warnings)
    set(all_warnings " CONFIGURATION WARNINGS:")
    foreach (msg ${build_warnings})
      ign_string_append(all_warnings " -- ${msg}" DELIM "\n")
    endforeach ()
    message(WARNING "${all_warnings}")
  endif (build_warnings)

  if(build_errors)
    message(SEND_ERROR "-- BUILD ERRORS: These must be resolved before compiling.")
    foreach(msg ${build_errors})
      message(SEND_ERROR "-- ${msg}")
    endforeach()
    message(SEND_ERROR "-- END BUILD ERRORS\n")

    set(error_str "Errors encountered in build. Please see BUILD ERRORS above.")

    if(ign_configure_build_QUIT_IF_BUILD_ERRORS)
      message(FATAL_ERROR "${error_str}")
    else()
      message(WARNING "${error_str}")
    endif()

  endif()


  #============================================================================
  # If there are no build errors, try building
  if(NOT build_errors)

    #--------------------------------------
    # Turn on testing
    include(CTest)
    enable_testing()


    #--------------------------------------
    # Set up the compiler flags
    ign_set_compiler_flags()


    #--------------------------------------
    # Set up the compiler feature flags to help us choose our standard
    ign_set_cxx_feature_flags()


    #--------------------------------------
    # We want to include both the include directory from the source tree and
    # also the include directory that's generated in the build folder,
    # ${PROJECT_BINARY_DIR}, so that headers which are generated via cmake will
    # be visible to the compiler.
    #
    # TODO: We should consider removing this include_directories(~) command.
    # If these directories are needed by any targets, then we should specify it
    # for those targets directly.
    if(EXISTS "${PROJECT_SOURCE_DIR}/include")
      include_directories("${PROJECT_SOURCE_DIR}/include")
    endif()

    include_directories("${PROJECT_BINARY_DIR}/include")
    file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include")


    #--------------------------------------
    # Clear the test results directory
    #
    # TODO: This should probably be in a CI script instead of our build
    # configuration script.
    execute_process(COMMAND cmake -E remove_directory ${CMAKE_BINARY_DIR}/test_results)
    execute_process(COMMAND cmake -E make_directory ${CMAKE_BINARY_DIR}/test_results)


    #--------------------------------------
    # Create the "all" meta-target
    ign_create_all_target()


    #--------------------------------------
    # Add the source code directories of the core library
    if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/core")

      # If the directory structure has a subdirectory called "core", we will
      # use that instead of assuming that src, include, and test subdirectories
      # exist in the root directory.
      #
      # We treat "core" the same way as we treat the component subdirectories.
      # It's inserted into the beginning of the list to make sure that the core
      # subdirectory is handled before any other.
      list(INSERT ign_configure_build_COMPONENTS 0 core)

    else()

      add_subdirectory(src)
      _ign_find_include_script()

    endif()

    if(BUILD_TESTING AND EXISTS ${CMAKE_CURRENT_LIST_DIR}/test)
      add_subdirectory(test)
    endif()

    #--------------------------------------
    # Add the source, include, and test directories to the cppcheck dirs.
    # CPPCHECK_DIRS is used in IgnCodeCheck. The variable specifies the
    # directories static code analyzers should check. Additional directories
    # are added for each component.
    set (CPPCHECK_DIRS)
    set (potential_cppcheck_dirs
      ${CMAKE_SOURCE_DIR}/src
      ${CMAKE_SOURCE_DIR}/include
      ${CMAKE_SOURCE_DIR}/test/integration
      ${CMAKE_SOURCE_DIR}/test/regression
      ${CMAKE_SOURCE_DIR}/test/performance)
    foreach (dir ${potential_cppcheck_dirs})
      if (EXISTS ${dir})
        list (APPEND CPPCHECK_DIRS ${dir})
      endif()
    endforeach()

    # Includes for cppcheck. This sets include paths for cppcheck. Additional
    # directories are added for each component.
    set (CPPCHECK_INCLUDE_DIRS)
    set (potential_cppcheck_include_dirs
      ${CMAKE_BINARY_DIR}
      ${CMAKE_SOURCE_DIR}/include/${PROJECT_INCLUDE_DIR}
      ${CMAKE_SOURCE_DIR}/test/integration
      ${CMAKE_SOURCE_DIR}/test/regression
      ${CMAKE_SOURCE_DIR}/test/performance)
    foreach (dir ${potential_cppcheck_include_dirs})
      if (EXISTS ${dir})
        list (APPEND CPPCHECK_INCLUDE_DIRS ${dir})
      endif()
    endforeach()

    #--------------------------------------
    # Initialize the list of header directories that should be parsed by doxygen
    if(EXISTS "${CMAKE_SOURCE_DIR}/include")
      set(ign_doxygen_component_input_dirs "${CMAKE_SOURCE_DIR}/include")
    else()
      set(ign_doxygen_component_input_dirs "")
    endif()

    #--------------------------------------
    # Add the source code directories of each component if they exist
    foreach(component ${ign_configure_build_COMPONENTS})

      if(NOT SKIP_${component} AND NOT INTERNAL_SKIP_${component})

        set(found_${component}_src FALSE)

        # Append the component's include directory to both CPPCHECK_DIRS and
        # CPPCHECK_INCLUDE_DIRS
        list(APPEND CPPCHECK_DIRS ${CMAKE_CURRENT_LIST_DIR}/${component}/include)
        list(APPEND CPPCHECK_INCLUDE_DIRS
          ${CMAKE_CURRENT_LIST_DIR}/${component}/include)

        if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}/include")
          # Note: It seems we need to give the delimiter exactly this many
          # backslashes in order to get a \ plus a newline. This might be
          # dependent on the implementation of ign_string_append, so be careful
          # when changing the implementation of that function.
          ign_string_append(ign_doxygen_component_input_dirs
            "${CMAKE_CURRENT_LIST_DIR}/${component}/include"
            DELIM " \\\\\\\\\n  ")
        endif()

        # Append the component's source directory to CPPCHECK_DIRS.
        if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}/src")
          list(APPEND CPPCHECK_DIRS ${CMAKE_CURRENT_LIST_DIR}/${component}/src)
        endif()

        if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}/CMakeLists.txt")

          # If the component's directory has a top-level CMakeLists.txt, use
          # that.
          add_subdirectory(${component})
          set(found_${component}_src TRUE)

        else()

          # If the component's directory does not have a top-level
          # CMakeLists.txt, try to call the expected set of subdirectories
          # individually. This saves us from needing to create very redundant
          # CMakeLists.txt files that do nothing but redirect us to these
          # subdirectories.

          # Add the source files
          if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}/src/CMakeLists.txt")
            add_subdirectory(${component}/src)
            set(found_${component}_src TRUE)
          endif()

          _ign_find_include_script(COMPONENT ${component})

          # Add the tests
          if(BUILD_TESTING AND
             EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}/test/CMakeLists.txt")
            add_subdirectory(${component}/test)
          endif()
        endif()

        if(NOT found_${component}_src)
          message(AUTHOR_WARNING
            "Could not find a top-level CMakeLists.txt or src/CMakeLists.txt "
            "for the component [${component}]!")
        endif()

      else()

        set(skip_msg "Skipping the component [${component}]")
        if(SKIP_${component})
          ign_string_append(skip_msg "by user request")
        elseif(${component}_MISSING_DEPS)
          ign_string_append(skip_msg "because the following packages are missing: ${${component}_MISSING_DEPS}")
        endif()

        message(STATUS "${skip_msg}")

      endif()

    endforeach()

    #--------------------------------------
    # Export the "all" meta-target
    ign_export_target_all()

    #--------------------------------------
    # Create codecheck target
    include(IgnCodeCheck)
    ign_setup_target_for_codecheck()

    #--------------------------------------
    # If we made it this far, the configuration was successful
    message(STATUS "Build configuration successful")

  endif()

endmacro()

macro(ign_set_cxx_feature_flags)

  set(IGN_KNOWN_CXX_STANDARDS 11 14 17)

  set(IGN_CXX_11_FEATURES cxx_std_11)
  set(IGN_CXX_14_FEATURES cxx_std_14)
  set(IGN_CXX_17_FEATURES cxx_std_17)

endmacro()

function(_ign_find_include_script)

  #------------------------------------
  # Define the expected arguments
  set(options) # Unused
  set(oneValueArgs COMPONENT)
  set(multiValueArgs) # Unused

  #------------------------------------
  # Parse the arguments
  cmake_parse_arguments(_ign_find_include_script "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

  #------------------------------------
  # Set the starting point
  set(include_start "${CMAKE_CURRENT_LIST_DIR}")

  if(_ign_find_include_script_COMPONENT)
    ign_string_append(include_start ${_ign_find_include_script_COMPONENT} DELIM "/")
  endif()

  # Check each level of depth to find the first CMakeLists.txt. This allows us
  # to have custom behavior for each include directory structure while also
  # allowing us to just have one leaf CMakeLists.txt file if a project doesn't
  # need any custom configuration in its include directories.
  if(EXISTS "${include_start}/include")
    if(EXISTS "${include_start}/include/CMakeLists.txt")
      add_subdirectory("${include_start}/include")
    elseif(EXISTS "${include_start}/include/ignition/CMakeLists.txt")
      add_subdirectory("${include_start}/include/ignition")
    elseif(EXISTS "${include_start}/include/${PROJECT_INCLUDE_DIR}/CMakeLists.txt")
      add_subdirectory("${include_start}/include/${PROJECT_INCLUDE_DIR}")
    else()
      message(AUTHOR_WARNING
        "You have an include directory [${include_start}/include] without a "
        "CMakeLists.txt. This means its headers will not get installed!")
    endif()
  endif()

endfunction()

macro(ign_parse_build_type)

  #============================================================================
  # If a build type is not specified, set it to RelWithDebInfo by default
  if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "RelWithDebInfo")
  endif()

  # Handle NONE in MSVC as blank and default to RelWithDebInfo
  if (MSVC AND CMAKE_BUILD_TYPE_UPPERCASE STREQUAL "NONE")
    set(CMAKE_BUILD_TYPE "RelWithDebInfo")
  endif()

  # Convert to uppercase in order to support arbitrary capitalization
  string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPERCASE)

  #============================================================================
  # Set variables based on the build type
  set(BUILD_TYPE_PROFILE FALSE)
  set(BUILD_TYPE_RELEASE FALSE)
  set(BUILD_TYPE_RELWITHDEBINFO FALSE)
  set(BUILD_TYPE_MINSIZEREL FALSE)
  set(BUILD_TYPE_NONE FALSE)
  set(BUILD_TYPE_DEBUG FALSE)

  if("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "DEBUG")
    set(BUILD_TYPE_DEBUG TRUE)
  elseif("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "RELEASE")
    set(BUILD_TYPE_RELEASE TRUE)
  elseif("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "RELWITHDEBINFO")
    set(BUILD_TYPE_RELWITHDEBINFO TRUE)
  elseif("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "MINSIZEREL")
    set(BUILD_TYPE_MINSIZEREL TRUE)
  elseif("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "NONE")
    set(BUILD_TYPE_NONE TRUE)
  elseif("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "COVERAGE")
    include(IgnCodeCoverage)
    set(BUILD_TYPE_DEBUG TRUE)
    ign_setup_target_for_coverage(
      OUTPUT_NAME coverage
      TARGET_NAME coverage
      TEST_RUNNER ctest)
    ign_setup_target_for_coverage(
      BRANCH_COVERAGE
      OUTPUT_NAME coverage-branch
      TARGET_NAME coverage-branch
      TEST_RUNNER ctest)
  elseif("${CMAKE_BUILD_TYPE_UPPERCASE}" STREQUAL "PROFILE")
    set(BUILD_TYPE_PROFILE TRUE)
  else()
    ign_build_error("CMAKE_BUILD_TYPE [${CMAKE_BUILD_TYPE}] unknown. Valid options are: Debug Release RelWithDebInfo MinSizeRel Profile Check")
  endif()

endmacro()