Replace (MSVC/GCC/CLANG)_VERSION with CMAKE_CXX_COMPILER_VERSION.
- As raised in issue #377, GCC_VERSION is not always defined, in which
case we were not enabling compiler optimisations.
- CMAKE_CXX_COMPILER_VERSION is the more modern, uniform method to
verify the compiler version.
- Also removes legacy Apple-GCC (i.e. Apple's fork of GCC prior to their
switch to Clang) logic, and Xcode 4.x logic (in June 2018, Xcode 9.x
is the current version).
- Also removes addition of -march=native on Linux (was not added for
OS X). If users wish to append -march=native (which will affect the
portability of the resulting output) they can do so manually as now
explained in the docs.
- Fixes check for Clang on OS X. As per CMP0025 (CMake 3.0+)
CMAKE_CXX_COMPILER_ID reports AppleClang on OS X, not Clang. Thus
using: if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") would fail for
AppleClang. Instead, MATCHES should be used instead of STREQUAL to
support both Clang & AppleClang.
Change-Id: I9647030b76f4b85a9ef2deea82d80ed79812ae33
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 500ce02..f1f59ff 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -57,9 +57,9 @@
mark_as_advanced(CMAKE_CXX_STANDARD_REQUIRED)
# MSVC versions < 2013 did not fully support >= C++11.
-if (MSVC AND MSVC_VERSION VERSION_LESS 1800)
- message(FATAL_ERROR "Invalid MSVC_VERSION: ${MSVC_VERSION}. Ceres requires at "
- "least MSVC 2013 Update 4+")
+if (MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12.0)
+ message(FATAL_ERROR "Invalid CMAKE_CXX_COMPILER_VERSION: "
+ "${CMAKE_CXX_COMPILER_VERSION}. Ceres requires at least MSVC 2013 Update 4+")
endif()
project(Ceres C CXX)
@@ -524,35 +524,6 @@
endif (CMAKE_BUILD_TYPE STREQUAL "Debug")
endif (NOT CMAKE_BUILD_TYPE)
-# Set the default Ceres flags to an empty string.
-set (CERES_CXX_FLAGS)
-
-if (CMAKE_BUILD_TYPE STREQUAL "Release")
- if (CMAKE_COMPILER_IS_GNUCXX)
- # Linux
- if (CMAKE_SYSTEM_NAME MATCHES "Linux")
- if (NOT GCC_VERSION VERSION_LESS 4.2)
- set (CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -march=native -mtune=native")
- endif (NOT GCC_VERSION VERSION_LESS 4.2)
- endif (CMAKE_SYSTEM_NAME MATCHES "Linux")
- # Mac OS X
- if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
- set (CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -msse3")
- # Use of -fast only applicable for Apple's GCC
- # Assume this is being used if GCC version < 4.3 on OSX
- execute_process(COMMAND ${CMAKE_C_COMPILER}
- ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
- OUTPUT_VARIABLE GCC_VERSION
- OUTPUT_STRIP_TRAILING_WHITESPACE)
- if (GCC_VERSION VERSION_LESS 4.3)
- set (CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -fast")
- endif (GCC_VERSION VERSION_LESS 4.3)
- endif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
- endif (CMAKE_COMPILER_IS_GNUCXX)
-endif (CMAKE_BUILD_TYPE STREQUAL "Release")
-
-set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CERES_CXX_FLAGS}")
-
if (MINGW)
# MinGW produces code that segfaults when performing matrix multiplications
# in Eigen when compiled with -O3 (see [1]), as such force the use of -O2
@@ -644,7 +615,7 @@
# resulting in an unreasonably slow version of the blas routines. The
# -Qunused-arguments is needed because CMake passes the inline
# threshold to the linker and clang complains about it and dies.
-if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Matches Clang & AppleClang.
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Qunused-arguments -mllvm -inline-threshold=600")
@@ -658,24 +629,6 @@
endif(HAVE_RETURN_TYPE_C_LINKAGE)
endif ()
-# Xcode 4.5.x used Clang 4.1 (Apple version), this has a bug that prevents
-# compilation of Ceres.
-if (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
- execute_process(COMMAND ${CMAKE_CXX_COMPILER}
- ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
- OUTPUT_VARIABLE CLANG_VERSION
- OUTPUT_STRIP_TRAILING_WHITESPACE)
- # Use version > 4.0 & < 4.2 to catch all 4.1(.x) versions.
- if (CLANG_VERSION VERSION_GREATER 4.0 AND
- CLANG_VERSION VERSION_LESS 4.2)
- message(FATAL_ERROR "You are attempting to build Ceres on OS X using Xcode "
- "4.5.x (Clang version: ${CLANG_VERSION}). This version of Clang has a "
- "bug that prevents compilation of Ceres, please update to "
- "Xcode >= 4.6.3.")
- endif (CLANG_VERSION VERSION_GREATER 4.0 AND
- CLANG_VERSION VERSION_LESS 4.2)
-endif (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
-
# Configure the Ceres config.h compile options header using the current
# compile options and put the configured header into the Ceres build
# directory. Note that the ceres/internal subdir in <build>/config where
diff --git a/docs/source/installation.rst b/docs/source/installation.rst
index f9a7701..e7951bf 100644
--- a/docs/source/installation.rst
+++ b/docs/source/installation.rst
@@ -577,6 +577,34 @@
``CMake`` GUI. If they are not present in the *Standard View*,
toggle to the *Advanced View* with ``<t>``.
+
+Modifying default compilation flags
+-----------------------------------
+
+The ``CMAKE_CXX_FLAGS`` variable can be used to define additional
+default compilation flags for all build types. Any flags specified
+in ``CMAKE_CXX_FLAGS`` will be used in addition to the default
+flags used by Ceres for the current build type.
+
+For example, if you wished to build Ceres with `-march=native
+<https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html>`_ which is not
+enabled by default (even if ``CMAKE_BUILD_TYPE=Release``) you would invoke
+CMake with:
+
+.. code-block:: bash
+
+ cmake -DCMAKE_CXX_FLAGS="-march=native" <PATH_TO_CERES_SOURCE>
+
+.. NOTE ::
+
+ The use of ``-march=native`` will limit portability, as it will tune the
+ implementation to the specific CPU of the compiling machine (e.g. use of
+ AVX if available). Run-time segfaults may occur if you then tried to
+ run the resulting binaries on a machine with a different processor, even
+ if it is from the same family (e.g. x86) if the specific options available
+ are different. Note that the performance gains from the use of
+ ``-march=native`` are not guaranteed to be significant.
+
.. _options-controlling-ceres-configuration:
Options controlling Ceres configuration