Blame cmake/AddCeresCXX11RequirementsToTarget.cmake

Packit ea1746
# Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
# Copyright 2017 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
# Adds Ceres' C++11 requirements to a target such that they are exported when
Packit ea1746
# the target is exported (if the version of CMake supports it).
Packit ea1746
#
Packit ea1746
#    add_ceres_cxx11_requirements_to_target( [target1 [target2 [...]]] )
Packit ea1746
function(add_ceres_cxx11_requirements_to_target)
Packit ea1746
  include(CheckCXXCompilerFlag)
Packit ea1746
  check_cxx_compiler_flag("-std=c++11" COMPILER_HAS_CXX11_FLAG)
Packit ea1746
Packit ea1746
  foreach(TARGET ${ARGN})
Packit ea1746
    if (NOT TARGET ${TARGET})
Packit ea1746
      message(FATAL_ERROR "Specified target to append Ceres C++11 requirements "
Packit ea1746
        "to: ${TARGET} is not a declared CMake target.")
Packit ea1746
    endif()
Packit ea1746
Packit ea1746
    if (COMPILER_HAS_CXX11_FLAG)
Packit ea1746
      # IMPORTANT: It is not sufficient to specify the
Packit ea1746
      #            CXX_STANDARD/CXX_STANDARD_REQUIRED target properties
Packit ea1746
      #            as these target properties are NOT exported.
Packit ea1746
      if (CMAKE_VERSION VERSION_LESS "2.8.12")
Packit ea1746
        # CMake version < 2.8.12 does not support target_compile_options(), warn
Packit ea1746
        # user that they will have to add compile flags to their own projects
Packit ea1746
        # manually.
Packit ea1746
        message(WARNING "-- Warning: Detected CMake version: ${CMAKE_VERSION} "
Packit ea1746
          "< 2.8.12, which is the minimum required for compile options to be "
Packit ea1746
          "included in an exported CMake target and the detected. compiler "
Packit ea1746
          "requires -std=c++11. The client is responsible for adding "
Packit ea1746
          "-std=c++11 when linking against: ${TARGET}.")
Packit ea1746
      elseif (COMMAND target_compile_features)
Packit ea1746
        # CMake >= 3.1, use new target_compile_features() to specify Ceres'
Packit ea1746
        # C++11 requirements as used in the public API.  This assumes that
Packit ea1746
        # C++11 STL features are available if the specified features are
Packit ea1746
        # available.  We do not use the cxx_std_11 feature to specify this as
Packit ea1746
        # this did not come in until CMake 3.8.
Packit ea1746
        #
Packit ea1746
        # The reason to prefer using target_compile_features() if it exists is
Packit ea1746
        # that this handles 'upgrading' of the C++ standard required more
Packit ea1746
        # gracefully, e.g. if a client of Ceres requires C++14, but Ceres was
Packit ea1746
        # compiled against C++11 then target_compile_options() may not work as
Packit ea1746
        # expected.
Packit ea1746
        target_compile_features(
Packit ea1746
          ${TARGET} PUBLIC cxx_alignas cxx_alignof cxx_constexpr)
Packit ea1746
      else()
Packit ea1746
        # CMake version >= 2.8.12 && < 3.1 supports target_compile_options()
Packit ea1746
        # but not target_compile_features(). For these intermediary versions,
Packit ea1746
        # we use target_compile_options() to manually specify the C++11 flag and
Packit ea1746
        # export it for client targets that depend on the target iff they are
Packit ea1746
        # NOT compiling for C.  We check for not C, rather than C++ as
Packit ea1746
        # LINKER_LANGUAGE is often NOTFOUND and then uses the default (C++).
Packit ea1746
        target_compile_options(${TARGET} PUBLIC
Packit ea1746
          $<$<NOT:$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,C>>:-std=c++11>)
Packit ea1746
      endif()
Packit ea1746
    endif()
Packit ea1746
  endforeach()
Packit ea1746
endfunction()