CreateCeresConfig.cmake 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Ceres Solver - A fast non-linear least squares minimizer
  2. # Copyright 2022 Google Inc. All rights reserved.
  3. # http://ceres-solver.org/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. # * Neither the name of Google Inc. nor the names of its contributors may be
  14. # used to endorse or promote products derived from this software without
  15. # specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. # Author: alexs.mac@gmail.com (Alex Stewart)
  30. # This must take place outside of CONFIGURE_CERES_CONFIG() in order that
  31. # we can determine where *this* file is, and thus the relative path to
  32. # config.h.in. Inside of CONFIGURE_CERES_CONFIG(), CMAKE_CURRENT_LIST_DIR
  33. # refers to the caller of CONFIGURE_CERES_CONFIG(), not this file.
  34. set(CERES_CONFIG_IN_FILE "${CMAKE_CURRENT_LIST_DIR}/config.h.in")
  35. # CreateCeresConfig.cmake - Create the config.h for Ceres.
  36. #
  37. # This function configures the Ceres config.h file based on the current
  38. # compile options and copies it into the specified location. It should be
  39. # called before Ceres is built so that the correct config.h is used when
  40. # Ceres is compiled.
  41. #
  42. # INPUTS:
  43. # CURRENT_CERES_COMPILE_OPTIONS: List of currently enabled Ceres compile
  44. # options. These are compared against the
  45. # full list of valid options, which are read
  46. # from config.h.in. Any options present
  47. # which are not part of the valid set will
  48. # invoke an error. Any valid option present
  49. # will be enabled in the resulting config.h,
  50. # all other options will be disabled.
  51. #
  52. # CERES_CONFIG_OUTPUT_DIRECTORY: Path to output directory in which to save
  53. # the configured config.h. Typically this
  54. # will be <src>/include/ceres/internal.
  55. function(CREATE_CERES_CONFIG CURRENT_CERES_COMPILE_OPTIONS CERES_CONFIG_OUTPUT_DIRECTORY)
  56. # Create the specified output directory if it does not exist.
  57. if (NOT EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}")
  58. message(STATUS "Creating configured Ceres config.h output directory: "
  59. "${CERES_CONFIG_OUTPUT_DIRECTORY}")
  60. file(MAKE_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
  61. endif()
  62. if (EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}" AND
  63. NOT IS_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
  64. message(FATAL_ERROR "Ceres Bug: Specified CERES_CONFIG_OUTPUT_DIRECTORY: "
  65. "${CERES_CONFIG_OUTPUT_DIRECTORY} exists, but is not a directory.")
  66. endif()
  67. # Read all possible configurable compile options from config.h.in, this avoids
  68. # us having to hard-code in this file what the valid options are.
  69. file(READ ${CERES_CONFIG_IN_FILE} CERES_CONFIG_IN_CONTENTS)
  70. string(REGEX MATCHALL "@[^@ $]+@"
  71. ALL_CONFIGURABLE_CERES_OPTIONS "${CERES_CONFIG_IN_CONTENTS}")
  72. # Removing @ symbols at beginning and end of each option.
  73. string(REPLACE "@" ""
  74. ALL_CONFIGURABLE_CERES_OPTIONS "${ALL_CONFIGURABLE_CERES_OPTIONS}")
  75. # Ensure that there are no repetitions in the current compile options.
  76. list(REMOVE_DUPLICATES CURRENT_CERES_COMPILE_OPTIONS)
  77. foreach (CERES_OPTION ${ALL_CONFIGURABLE_CERES_OPTIONS})
  78. # Try and find the option in the list of current compile options, if it
  79. # is present, then the option is enabled, otherwise it is disabled.
  80. list(FIND CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION} OPTION_ENABLED)
  81. # list(FIND ..) returns -1 if the element was not in the list, but CMake
  82. # interprets if (VAR) to be true if VAR is any non-zero number, even
  83. # negative ones, hence we have to explicitly check for >= 0.
  84. if (OPTION_ENABLED GREATER -1)
  85. message(STATUS "Enabling ${CERES_OPTION} in Ceres config.h")
  86. set(${CERES_OPTION} "#define ${CERES_OPTION}")
  87. # Remove the item from the list of current options so that we can identify
  88. # any options that were in CURRENT_CERES_COMPILE_OPTIONS, but not in
  89. # ALL_CONFIGURABLE_CERES_OPTIONS (which is an error).
  90. list(REMOVE_ITEM CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION})
  91. else()
  92. set(${CERES_OPTION} "// #define ${CERES_OPTION}")
  93. endif()
  94. endforeach()
  95. # CURRENT_CERES_COMPILE_OPTIONS should now be an empty list, any elements
  96. # remaining were not present in ALL_CONFIGURABLE_CERES_OPTIONS read from
  97. # config.h.in.
  98. if (CURRENT_CERES_COMPILE_OPTIONS)
  99. message(FATAL_ERROR "Ceres Bug: CURRENT_CERES_COMPILE_OPTIONS contained "
  100. "the following options which were not present in config.h.in: "
  101. "${CURRENT_CERES_COMPILE_OPTIONS}")
  102. endif()
  103. configure_file(${CERES_CONFIG_IN_FILE}
  104. "${CERES_CONFIG_OUTPUT_DIRECTORY}/config.h" @ONLY)
  105. endfunction()