Blame cmake/CreateCeresConfig.cmake

Packit ea1746
# Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
# Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
# http://ceres-solver.org/
Packit ea1746
#
Packit ea1746
# Redistribution and use in source and binary forms, with or without
Packit ea1746
# modification, are permitted provided that the following conditions are met:
Packit ea1746
#
Packit ea1746
# * Redistributions of source code must retain the above copyright notice,
Packit ea1746
#   this list of conditions and the following disclaimer.
Packit ea1746
# * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
#   this list of conditions and the following disclaimer in the documentation
Packit ea1746
#   and/or other materials provided with the distribution.
Packit ea1746
# * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
#   used to endorse or promote products derived from this software without
Packit ea1746
#   specific prior written permission.
Packit ea1746
#
Packit ea1746
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
# POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
#
Packit ea1746
# Author: alexs.mac@gmail.com (Alex Stewart)
Packit ea1746
Packit ea1746
# This must take place outside of CONFIGURE_CERES_CONFIG() in order that
Packit ea1746
# we can determine where *this* file is, and thus the relative path to
Packit ea1746
# config.h.in.  Inside of CONFIGURE_CERES_CONFIG(), CMAKE_CURRENT_LIST_DIR
Packit ea1746
# refers to the caller of CONFIGURE_CERES_CONFIG(), not this file.
Packit ea1746
set(CERES_CONFIG_IN_FILE "${CMAKE_CURRENT_LIST_DIR}/config.h.in")
Packit ea1746
Packit ea1746
# CreateCeresConfig.cmake - Create the config.h for Ceres.
Packit ea1746
#
Packit ea1746
# This function configures the Ceres config.h file based on the current
Packit ea1746
# compile options and copies it into the specified location.  It should be
Packit ea1746
# called before Ceres is built so that the correct config.h is used when
Packit ea1746
# Ceres is compiled.
Packit ea1746
#
Packit ea1746
# INPUTS:
Packit ea1746
#   CURRENT_CERES_COMPILE_OPTIONS: List of currently enabled Ceres compile
Packit ea1746
#                                  options. These are compared against the
Packit ea1746
#                                  full list of valid options, which are read
Packit ea1746
#                                  from config.h.in.  Any options present
Packit ea1746
#                                  which are not part of the valid set will
Packit ea1746
#                                  invoke an error.  Any valid option present
Packit ea1746
#                                  will be enabled in the resulting config.h,
Packit ea1746
#                                  all other options will be disabled.
Packit ea1746
#
Packit ea1746
#   CERES_CONFIG_OUTPUT_DIRECTORY: Path to output directory in which to save
Packit ea1746
#                                  the configured config.h.  Typically this
Packit ea1746
#                                  will be <src>/include/ceres/internal.
Packit ea1746
Packit ea1746
function(CREATE_CERES_CONFIG CURRENT_CERES_COMPILE_OPTIONS CERES_CONFIG_OUTPUT_DIRECTORY)
Packit ea1746
  # Create the specified output directory if it does not exist.
Packit ea1746
  if (NOT EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}")
Packit ea1746
    message(STATUS "Creating configured Ceres config.h output directory: "
Packit ea1746
      "${CERES_CONFIG_OUTPUT_DIRECTORY}")
Packit ea1746
    file(MAKE_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
Packit ea1746
  endif()
Packit ea1746
  if (EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}" AND
Packit ea1746
      NOT IS_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
Packit ea1746
    message(FATAL_ERROR "Ceres Bug: Specified CERES_CONFIG_OUTPUT_DIRECTORY: "
Packit ea1746
      "${CERES_CONFIG_OUTPUT_DIRECTORY} exists, but is not a directory.")
Packit ea1746
  endif()
Packit ea1746
Packit ea1746
  # Read all possible configurable compile options from config.h.in, this avoids
Packit ea1746
  # us having to hard-code in this file what the valid options are.
Packit ea1746
  file(READ ${CERES_CONFIG_IN_FILE} CERES_CONFIG_IN_CONTENTS)
Packit ea1746
  string(REGEX MATCHALL "@[^@ $]+@"
Packit ea1746
    ALL_CONFIGURABLE_CERES_OPTIONS "${CERES_CONFIG_IN_CONTENTS}")
Packit ea1746
  # Removing @ symbols at beginning and end of each option.
Packit ea1746
  string(REPLACE "@" ""
Packit ea1746
    ALL_CONFIGURABLE_CERES_OPTIONS "${ALL_CONFIGURABLE_CERES_OPTIONS}")
Packit ea1746
Packit ea1746
  # Ensure that there are no repetitions in the current compile options.
Packit ea1746
  list(REMOVE_DUPLICATES CURRENT_CERES_COMPILE_OPTIONS)
Packit ea1746
Packit ea1746
  foreach (CERES_OPTION ${ALL_CONFIGURABLE_CERES_OPTIONS})
Packit ea1746
    # Try and find the option in the list of current compile options, if it
Packit ea1746
    # is present, then the option is enabled, otherwise it is disabled.
Packit ea1746
    list(FIND CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION} OPTION_ENABLED)
Packit ea1746
Packit ea1746
    # list(FIND ..) returns -1 if the element was not in the list, but CMake
Packit ea1746
    # interprets if (VAR) to be true if VAR is any non-zero number, even
Packit ea1746
    # negative ones, hence we have to explicitly check for >= 0.
Packit ea1746
    if (OPTION_ENABLED GREATER -1)
Packit ea1746
      message(STATUS "Enabling ${CERES_OPTION} in Ceres config.h")
Packit ea1746
      set(${CERES_OPTION} "#define ${CERES_OPTION}")
Packit ea1746
Packit ea1746
      # Remove the item from the list of current options so that we can identify
Packit ea1746
      # any options that were in CURRENT_CERES_COMPILE_OPTIONS, but not in
Packit ea1746
      # ALL_CONFIGURABLE_CERES_OPTIONS (which is an error).
Packit ea1746
      list(REMOVE_ITEM CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION})
Packit ea1746
    else()
Packit ea1746
      set(${CERES_OPTION} "// #define ${CERES_OPTION}")
Packit ea1746
    endif()
Packit ea1746
  endforeach()
Packit ea1746
Packit ea1746
  # CURRENT_CERES_COMPILE_OPTIONS should now be an empty list, any elements
Packit ea1746
  # remaining were not present in ALL_CONFIGURABLE_CERES_OPTIONS read from
Packit ea1746
  # config.h.in.
Packit ea1746
  if (CURRENT_CERES_COMPILE_OPTIONS)
Packit ea1746
    message(FATAL_ERROR "Ceres Bug: CURRENT_CERES_COMPILE_OPTIONS contained "
Packit ea1746
      "the following options which were not present in config.h.in: "
Packit ea1746
      "${CURRENT_CERES_COMPILE_OPTIONS}")
Packit ea1746
  endif()
Packit ea1746
Packit ea1746
  configure_file(${CERES_CONFIG_IN_FILE}
Packit ea1746
    "${CERES_CONFIG_OUTPUT_DIRECTORY}/config.h" @ONLY)
Packit ea1746
endfunction()