1# Ceres Solver - A fast non-linear least squares minimizer
2# Copyright 2014 Google Inc. All rights reserved.
3# http://code.google.com/p/ceres-solver/
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
31# This must take place outside of CONFIGURE_CERES_CONFIG() in order that
32# we can determine where *this* file is, and thus the relative path to
33# config.h.in.  Inside of CONFIGURE_CERES_CONFIG(), CMAKE_CURRENT_LIST_DIR
34# refers to the caller of CONFIGURE_CERES_CONFIG(), not this file.
35SET(CERES_CONFIG_IN_FILE "${CMAKE_CURRENT_LIST_DIR}/config.h.in")
36
37# CreateCeresConfig.cmake - Create the config.h for Ceres.
38#
39# This function configures the Ceres config.h file based on the current
40# compile options and copies it into the specified location.  It should be
41# called before Ceres is built so that the correct config.h is used when
42# Ceres is compiled.
43#
44# INPUTS:
45#   CURRENT_CERES_COMPILE_OPTIONS: List of currently enabled Ceres compile
46#                                  options. These are compared against the
47#                                  full list of valid options, which are read
48#                                  from config.h.in.  Any options present
49#                                  which are not part of the valid set will
50#                                  invoke an error.  Any valid option present
51#                                  will be enabled in the resulting config.h,
52#                                  all other options will be disabled.
53#
54#   CERES_CONFIG_OUTPUT_DIRECTORY: Path to output directory in which to save
55#                                  the configured config.h.  Typically this
56#                                  will be <src>/include/ceres/internal.
57
58FUNCTION(CREATE_CERES_CONFIG CURRENT_CERES_COMPILE_OPTIONS CERES_CONFIG_OUTPUT_DIRECTORY)
59  # Create the specified output directory if it does not exist.
60  IF (NOT EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}")
61    MESSAGE(STATUS "Creating configured Ceres config.h output directory: "
62      "${CERES_CONFIG_OUTPUT_DIRECTORY}")
63    FILE(MAKE_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
64  ENDIF()
65  IF (EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}" AND
66      NOT IS_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
67    MESSAGE(FATAL_ERROR "Ceres Bug: Specified CERES_CONFIG_OUTPUT_DIRECTORY: "
68      "${CERES_CONFIG_OUTPUT_DIRECTORY} exists, but is not a directory.")
69  ENDIF()
70
71  # Read all possible configurable compile options from config.h.in, this avoids
72  # us having to hard-code in this file what the valid options are.
73  FILE(READ ${CERES_CONFIG_IN_FILE} CERES_CONFIG_IN_CONTENTS)
74  STRING(REGEX MATCHALL "@[^@ $]+@"
75    ALL_CONFIGURABLE_CERES_OPTIONS "${CERES_CONFIG_IN_CONTENTS}")
76  # Removing @ symbols at beginning and end of each option.
77  STRING(REPLACE "@" ""
78    ALL_CONFIGURABLE_CERES_OPTIONS "${ALL_CONFIGURABLE_CERES_OPTIONS}")
79
80  # Ensure that there are no repetitions in the current compile options.
81  LIST(REMOVE_DUPLICATES CURRENT_CERES_COMPILE_OPTIONS)
82
83  FOREACH (CERES_OPTION ${ALL_CONFIGURABLE_CERES_OPTIONS})
84    # Try and find the option in the list of current compile options, if it
85    # is present, then the option is enabled, otherwise it is disabled.
86    LIST(FIND CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION} OPTION_ENABLED)
87
88    # list(FIND ..) returns -1 if the element was not in the list, but CMake
89    # interprets if (VAR) to be true if VAR is any non-zero number, even
90    # negative ones, hence we have to explicitly check for >= 0.
91    IF (OPTION_ENABLED GREATER -1)
92      MESSAGE(STATUS "Enabling ${CERES_OPTION} in Ceres config.h")
93      SET(${CERES_OPTION} "#define ${CERES_OPTION}")
94
95      # Remove the item from the list of current options so that we can identify
96      # any options that were in CURRENT_CERES_COMPILE_OPTIONS, but not in
97      # ALL_CONFIGURABLE_CERES_OPTIONS (which is an error).
98      LIST(REMOVE_ITEM CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION})
99    ELSE()
100      SET(${CERES_OPTION} "// #define ${CERES_OPTION}")
101    ENDIF()
102  ENDFOREACH()
103
104  # CURRENT_CERES_COMPILE_OPTIONS should now be an empty list, any elements
105  # remaining were not present in ALL_CONFIGURABLE_CERES_OPTIONS read from
106  # config.h.in.
107  IF (CURRENT_CERES_COMPILE_OPTIONS)
108    MESSAGE(FATAL_ERROR "Ceres Bug: CURRENT_CERES_COMPILE_OPTIONS contained "
109      "the following options which were not present in config.h.in: "
110      "${CURRENT_CERES_COMPILE_OPTIONS}")
111  ENDIF()
112
113  CONFIGURE_FILE(${CERES_CONFIG_IN_FILE}
114    "${CERES_CONFIG_OUTPUT_DIRECTORY}/config.h" @ONLY)
115ENDFUNCTION()
116