2 cmake_minimum_required(VERSION 2.8.5)
4 # guard against in-source builds
6 if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
7 message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt. ")
10 # guard against bad build-type strings
12 if (NOT CMAKE_BUILD_TYPE)
13 set(CMAKE_BUILD_TYPE "Release")
16 string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_tolower)
17 if( NOT cmake_build_type_tolower STREQUAL "debug"
18 AND NOT cmake_build_type_tolower STREQUAL "release"
19 AND NOT cmake_build_type_tolower STREQUAL "relwithdebinfo")
20 message(FATAL_ERROR "Unknown build type \"${CMAKE_BUILD_TYPE}\". Allowed values are Debug, Release, RelWithDebInfo (case-insensitive).")
24 #############################################################################
25 # retrieve version infomation #
26 #############################################################################
28 # automatically parse the version number
29 file(READ "${PROJECT_SOURCE_DIR}/Eigen/src/Core/util/Macros.h" _eigen_version_header)
30 string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen_world_version_match "${_eigen_version_header}")
31 set(EIGEN_WORLD_VERSION "${CMAKE_MATCH_1}")
32 string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen_major_version_match "${_eigen_version_header}")
33 set(EIGEN_MAJOR_VERSION "${CMAKE_MATCH_1}")
34 string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen_minor_version_match "${_eigen_version_header}")
35 set(EIGEN_MINOR_VERSION "${CMAKE_MATCH_1}")
36 set(EIGEN_VERSION_NUMBER ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})
38 # if the mercurial program is absent, this will leave the EIGEN_HG_CHANGESET string empty,
39 # but won't stop CMake.
40 execute_process(COMMAND hg tip -R ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE EIGEN_HGTIP_OUTPUT)
41 execute_process(COMMAND hg branch -R ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE EIGEN_BRANCH_OUTPUT)
43 # if this is the default (aka development) branch, extract the mercurial changeset number from the hg tip output...
44 if(EIGEN_BRANCH_OUTPUT MATCHES "default")
45 string(REGEX MATCH "^changeset: *[0-9]*:([0-9;a-f]+).*" EIGEN_HG_CHANGESET_MATCH "${EIGEN_HGTIP_OUTPUT}")
46 set(EIGEN_HG_CHANGESET "${CMAKE_MATCH_1}")
47 endif(EIGEN_BRANCH_OUTPUT MATCHES "default")
48 #...and show it next to the version number
49 if(EIGEN_HG_CHANGESET)
50 set(EIGEN_VERSION "${EIGEN_VERSION_NUMBER} (mercurial changeset ${EIGEN_HG_CHANGESET})")
51 else(EIGEN_HG_CHANGESET)
52 set(EIGEN_VERSION "${EIGEN_VERSION_NUMBER}")
53 endif(EIGEN_HG_CHANGESET)
56 include(CheckCXXCompilerFlag)
57 include(GNUInstallDirs)
59 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
61 #############################################################################
62 # find how to link to the standard libraries #
63 #############################################################################
65 find_package(StandardMathLibrary)
68 set(EIGEN_TEST_CUSTOM_LINKER_FLAGS "" CACHE STRING "Additional linker flags when linking unit tests.")
69 set(EIGEN_TEST_CUSTOM_CXX_FLAGS "" CACHE STRING "Additional compiler flags when compiling unit tests.")
71 set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "")
73 if(NOT STANDARD_MATH_LIBRARY_FOUND)
76 "Can't link to the standard math library. Please report to the Eigen developers, telling them about your platform.")
80 if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO)
81 set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO} ${STANDARD_MATH_LIBRARY}")
83 set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "${STANDARD_MATH_LIBRARY}")
88 if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO)
89 message(STATUS "Standard libraries to link to explicitly: ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}")
91 message(STATUS "Standard libraries to link to explicitly: none")
94 option(EIGEN_BUILD_BTL "Build benchmark suite" OFF)
96 option(EIGEN_BUILD_PKGCONFIG "Build pkg-config .pc file for Eigen" ON)
99 set(CMAKE_INCLUDE_CURRENT_DIR ON)
101 option(EIGEN_SPLIT_LARGE_TESTS "Split large tests into smaller executables" ON)
103 option(EIGEN_DEFAULT_TO_ROW_MAJOR "Use row-major as default matrix storage order" OFF)
104 if(EIGEN_DEFAULT_TO_ROW_MAJOR)
105 add_definitions("-DEIGEN_DEFAULT_TO_ROW_MAJOR")
108 set(EIGEN_TEST_MAX_SIZE "320" CACHE STRING "Maximal matrix/vector size, default is 320")
110 macro(ei_add_cxx_compiler_flag FLAG)
111 string(REGEX REPLACE "-" "" SFLAG ${FLAG})
112 check_cxx_compiler_flag(${FLAG} COMPILER_SUPPORT_${SFLAG})
113 if(COMPILER_SUPPORT_${SFLAG})
114 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}")
116 endmacro(ei_add_cxx_compiler_flag)
119 # We assume that other compilers are partly compatible with GNUCC
121 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
122 set(CMAKE_CXX_FLAGS_DEBUG "-g3")
123 set(CMAKE_CXX_FLAGS_RELEASE "-g0 -O2")
125 # clang outputs some warnings for unknwon flags that are not caught by check_cxx_compiler_flag
126 # adding -Werror turns such warnings into errors
127 check_cxx_compiler_flag("-Werror" COMPILER_SUPPORT_WERROR)
128 if(COMPILER_SUPPORT_WERROR)
129 set(CMAKE_REQUIRED_FLAGS "-Werror")
132 ei_add_cxx_compiler_flag("-pedantic")
133 ei_add_cxx_compiler_flag("-Wall")
134 ei_add_cxx_compiler_flag("-Wextra")
135 #ei_add_cxx_compiler_flag("-Weverything") # clang
137 ei_add_cxx_compiler_flag("-Wundef")
138 ei_add_cxx_compiler_flag("-Wcast-align")
139 ei_add_cxx_compiler_flag("-Wchar-subscripts")
140 ei_add_cxx_compiler_flag("-Wnon-virtual-dtor")
141 ei_add_cxx_compiler_flag("-Wunused-local-typedefs")
142 ei_add_cxx_compiler_flag("-Wpointer-arith")
143 ei_add_cxx_compiler_flag("-Wwrite-strings")
144 ei_add_cxx_compiler_flag("-Wformat-security")
146 ei_add_cxx_compiler_flag("-Wno-psabi")
147 ei_add_cxx_compiler_flag("-Wno-variadic-macros")
148 ei_add_cxx_compiler_flag("-Wno-long-long")
150 ei_add_cxx_compiler_flag("-fno-check-new")
151 ei_add_cxx_compiler_flag("-fno-common")
152 ei_add_cxx_compiler_flag("-fstrict-aliasing")
153 ei_add_cxx_compiler_flag("-wd981") # disable ICC's "operands are evaluated in unspecified order" remark
154 ei_add_cxx_compiler_flag("-wd2304") # disbale ICC's "warning #2304: non-explicit constructor with single argument may cause implicit type conversion" produced by -Wnon-virtual-dtor
156 # The -ansi flag must be added last, otherwise it is also used as a linker flag by check_cxx_compiler_flag making it fails
157 # Moreover we should not set both -strict-ansi and -ansi
158 check_cxx_compiler_flag("-strict-ansi" COMPILER_SUPPORT_STRICTANSI)
159 ei_add_cxx_compiler_flag("-Qunused-arguments") # disable clang warning: argument unused during compilation: '-ansi'
161 if(COMPILER_SUPPORT_STRICTANSI)
162 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -strict-ansi")
164 ei_add_cxx_compiler_flag("-ansi")
167 set(CMAKE_REQUIRED_FLAGS "")
169 option(EIGEN_TEST_SSE2 "Enable/Disable SSE2 in tests/examples" OFF)
171 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2")
172 message(STATUS "Enabling SSE2 in tests/examples")
175 option(EIGEN_TEST_SSE3 "Enable/Disable SSE3 in tests/examples" OFF)
177 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse3")
178 message(STATUS "Enabling SSE3 in tests/examples")
181 option(EIGEN_TEST_SSSE3 "Enable/Disable SSSE3 in tests/examples" OFF)
183 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mssse3")
184 message(STATUS "Enabling SSSE3 in tests/examples")
187 option(EIGEN_TEST_SSE4_1 "Enable/Disable SSE4.1 in tests/examples" OFF)
188 if(EIGEN_TEST_SSE4_1)
189 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")
190 message(STATUS "Enabling SSE4.1 in tests/examples")
193 option(EIGEN_TEST_SSE4_2 "Enable/Disable SSE4.2 in tests/examples" OFF)
194 if(EIGEN_TEST_SSE4_2)
195 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2")
196 message(STATUS "Enabling SSE4.2 in tests/examples")
199 option(EIGEN_TEST_ALTIVEC "Enable/Disable AltiVec in tests/examples" OFF)
200 if(EIGEN_TEST_ALTIVEC)
201 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maltivec -mabi=altivec")
202 message(STATUS "Enabling AltiVec in tests/examples")
205 option(EIGEN_TEST_NEON "Enable/Disable Neon in tests/examples" OFF)
207 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -mcpu=cortex-a8")
208 message(STATUS "Enabling NEON in tests/examples")
211 check_cxx_compiler_flag("-fopenmp" COMPILER_SUPPORT_OPENMP)
212 if(COMPILER_SUPPORT_OPENMP)
213 option(EIGEN_TEST_OPENMP "Enable/Disable OpenMP in tests/examples" OFF)
214 if(EIGEN_TEST_OPENMP)
215 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
216 message(STATUS "Enabling OpenMP in tests/examples")
222 # C4127 - conditional expression is constant
223 # C4714 - marked as __forceinline not inlined (I failed to deactivate it selectively)
224 # We can disable this warning in the unit tests since it is clear that it occurs
225 # because we are oftentimes returning objects that have a destructor or may
226 # throw exceptions - in particular in the unit tests we are throwing extra many
227 # exceptions to cover indexing errors.
228 # C4505 - unreferenced local function has been removed (impossible to deactive selectively)
229 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /wd4127 /wd4505 /wd4714")
231 # replace all /Wx by /W4
232 string(REGEX REPLACE "/W[0-9]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
234 check_cxx_compiler_flag("/openmp" COMPILER_SUPPORT_OPENMP)
235 if(COMPILER_SUPPORT_OPENMP)
236 option(EIGEN_TEST_OPENMP "Enable/Disable OpenMP in tests/examples" OFF)
237 if(EIGEN_TEST_OPENMP)
238 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /openmp")
239 message(STATUS "Enabling OpenMP in tests/examples")
243 option(EIGEN_TEST_SSE2 "Enable/Disable SSE2 in tests/examples" OFF)
246 # arch is not supported on 64 bit systems, SSE is enabled automatically.
247 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:SSE2")
248 endif(NOT CMAKE_CL_64)
249 message(STATUS "Enabling SSE2 in tests/examples")
250 endif(EIGEN_TEST_SSE2)
253 option(EIGEN_TEST_NO_EXPLICIT_VECTORIZATION "Disable explicit vectorization in tests/examples" OFF)
254 option(EIGEN_TEST_X87 "Force using X87 instructions. Implies no vectorization." OFF)
255 option(EIGEN_TEST_32BIT "Force generating 32bit code." OFF)
258 set(EIGEN_TEST_NO_EXPLICIT_VECTORIZATION ON)
259 if(CMAKE_COMPILER_IS_GNUCXX)
260 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=387")
261 message(STATUS "Forcing use of x87 instructions in tests/examples")
263 message(STATUS "EIGEN_TEST_X87 ignored on your compiler")
268 if(CMAKE_COMPILER_IS_GNUCXX)
269 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
270 message(STATUS "Forcing generation of 32-bit code in tests/examples")
272 message(STATUS "EIGEN_TEST_32BIT ignored on your compiler")
276 if(EIGEN_TEST_NO_EXPLICIT_VECTORIZATION)
277 add_definitions(-DEIGEN_DONT_VECTORIZE=1)
278 message(STATUS "Disabling vectorization in tests/examples")
281 option(EIGEN_TEST_NO_EXPLICIT_ALIGNMENT "Disable explicit alignment (hence vectorization) in tests/examples" OFF)
282 if(EIGEN_TEST_NO_EXPLICIT_ALIGNMENT)
283 add_definitions(-DEIGEN_DONT_ALIGN=1)
284 message(STATUS "Disabling alignment in tests/examples")
287 option(EIGEN_TEST_C++0x "Enables all C++0x features." OFF)
289 include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
291 # Backward compatibility support for EIGEN_INCLUDE_INSTALL_DIR
292 if(EIGEN_INCLUDE_INSTALL_DIR AND NOT INCLUDE_INSTALL_DIR)
293 set(INCLUDE_INSTALL_DIR ${EIGEN_INCLUDE_INSTALL_DIR}
294 CACHE PATH "The directory relative to CMAKE_PREFIX_PATH where Eigen header files are installed")
296 set(INCLUDE_INSTALL_DIR
297 "${CMAKE_INSTALL_INCLUDEDIR}/eigen3"
298 CACHE PATH "The directory relative to CMAKE_PREFIX_PATH where Eigen header files are installed"
302 set(CMAKEPACKAGE_INSTALL_DIR
303 "${CMAKE_INSTALL_LIBDIR}/cmake/eigen3"
304 CACHE PATH "The directory relative to CMAKE_PREFIX_PATH where Eigen3Config.cmake is installed"
306 set(PKGCONFIG_INSTALL_DIR
307 "${CMAKE_INSTALL_DATADIR}/pkgconfig"
308 CACHE PATH "The directory relative to CMAKE_PREFIX_PATH where eigen3.pc is installed"
311 # similar to set_target_properties but append the property instead of overwriting it
312 macro(ei_add_target_property target prop value)
314 get_target_property(previous ${target} ${prop})
315 # if the property wasn't previously set, ${previous} is now "previous-NOTFOUND" which cmake allows catching with plain if()
319 set_target_properties(${target} PROPERTIES ${prop} "${previous} ${value}")
320 endmacro(ei_add_target_property)
323 signature_of_eigen3_matrix_library
324 DESTINATION ${INCLUDE_INSTALL_DIR} COMPONENT Devel
327 if(EIGEN_BUILD_PKGCONFIG)
328 configure_file(eigen3.pc.in eigen3.pc @ONLY)
329 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/eigen3.pc
330 DESTINATION ${PKGCONFIG_INSTALL_DIR}
332 endif(EIGEN_BUILD_PKGCONFIG)
334 add_subdirectory(Eigen)
336 add_subdirectory(doc EXCLUDE_FROM_ALL)
338 include(EigenConfigureTesting)
340 # fixme, not sure this line is still needed:
341 enable_testing() # must be called from the root CMakeLists, see man page
344 if(EIGEN_LEAVE_TEST_IN_ALL_TARGET)
345 add_subdirectory(test) # can't do EXCLUDE_FROM_ALL here, breaks CTest
347 add_subdirectory(test EXCLUDE_FROM_ALL)
350 if(EIGEN_LEAVE_TEST_IN_ALL_TARGET)
351 add_subdirectory(blas)
352 add_subdirectory(lapack)
354 add_subdirectory(blas EXCLUDE_FROM_ALL)
355 add_subdirectory(lapack EXCLUDE_FROM_ALL)
358 add_subdirectory(unsupported)
360 add_subdirectory(demos EXCLUDE_FROM_ALL)
362 # must be after test and unsupported, for configuring buildtests.in
363 add_subdirectory(scripts EXCLUDE_FROM_ALL)
365 # TODO: consider also replacing EIGEN_BUILD_BTL by a custom target "make btl"?
367 add_subdirectory(bench/btl EXCLUDE_FROM_ALL)
368 endif(EIGEN_BUILD_BTL)
371 add_subdirectory(bench/spbench EXCLUDE_FROM_ALL)
374 configure_file(scripts/cdashtesting.cmake.in cdashtesting.cmake @ONLY)
376 ei_testing_print_summary()
379 message(STATUS "Configured Eigen ${EIGEN_VERSION_NUMBER}")
382 option(EIGEN_FAILTEST "Enable failtests." OFF)
384 add_subdirectory(failtest)
387 string(TOLOWER "${CMAKE_GENERATOR}" cmake_generator_tolower)
388 if(cmake_generator_tolower MATCHES "makefile")
389 message(STATUS "Some things you can do now:")
390 message(STATUS "--------------+--------------------------------------------------------------")
391 message(STATUS "Command | Description")
392 message(STATUS "--------------+--------------------------------------------------------------")
393 message(STATUS "make install | Install Eigen. Headers will be installed to:")
394 message(STATUS " | <CMAKE_INSTALL_PREFIX>/<INCLUDE_INSTALL_DIR>")
395 message(STATUS " | Using the following values:")
396 message(STATUS " | CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
397 message(STATUS " | INCLUDE_INSTALL_DIR: ${INCLUDE_INSTALL_DIR}")
398 message(STATUS " | Change the install location of Eigen headers using:")
399 message(STATUS " | cmake . -DCMAKE_INSTALL_PREFIX=yourprefix")
400 message(STATUS " | Or:")
401 message(STATUS " | cmake . -DINCLUDE_INSTALL_DIR=yourdir")
402 message(STATUS "make doc | Generate the API documentation, requires Doxygen & LaTeX")
403 message(STATUS "make check | Build and run the unit-tests. Read this page:")
404 message(STATUS " | http://eigen.tuxfamily.org/index.php?title=Tests")
405 message(STATUS "make blas | Build BLAS library (not the same thing as Eigen)")
406 message(STATUS "--------------+--------------------------------------------------------------")
408 message(STATUS "To build/run the unit tests, read this page:")
409 message(STATUS " http://eigen.tuxfamily.org/index.php?title=Tests")