Home | History | Annotate | Download | only in eigen
      1 project(Eigen)
      2 
      3 cmake_minimum_required(VERSION 2.8.2)
      4 
      5 # guard against in-source builds
      6 
      7 if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
      8   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. ")
      9 endif()
     10 
     11 # guard against bad build-type strings
     12 
     13 if (NOT CMAKE_BUILD_TYPE)
     14   set(CMAKE_BUILD_TYPE "Release")
     15 endif()
     16 
     17 string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_tolower)
     18 if(    NOT cmake_build_type_tolower STREQUAL "debug"
     19    AND NOT cmake_build_type_tolower STREQUAL "release"
     20    AND NOT cmake_build_type_tolower STREQUAL "relwithdebinfo")
     21   message(FATAL_ERROR "Unknown build type \"${CMAKE_BUILD_TYPE}\". Allowed values are Debug, Release, RelWithDebInfo (case-insensitive).")
     22 endif()
     23 
     24 
     25 #############################################################################
     26 # retrieve version infomation                                               #
     27 #############################################################################
     28 
     29 # automatically parse the version number
     30 file(READ "${PROJECT_SOURCE_DIR}/Eigen/src/Core/util/Macros.h" _eigen_version_header)
     31 string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen_world_version_match "${_eigen_version_header}")
     32 set(EIGEN_WORLD_VERSION "${CMAKE_MATCH_1}")
     33 string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen_major_version_match "${_eigen_version_header}")
     34 set(EIGEN_MAJOR_VERSION "${CMAKE_MATCH_1}")
     35 string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen_minor_version_match "${_eigen_version_header}")
     36 set(EIGEN_MINOR_VERSION "${CMAKE_MATCH_1}")
     37 set(EIGEN_VERSION_NUMBER ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})
     38 
     39 # if the mercurial program is absent, this will leave the EIGEN_HG_CHANGESET string empty,
     40 # but won't stop CMake.
     41 execute_process(COMMAND hg tip -R ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE EIGEN_HGTIP_OUTPUT)
     42 execute_process(COMMAND hg branch -R ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE EIGEN_BRANCH_OUTPUT)
     43 
     44 # if this is the default (aka development) branch, extract the mercurial changeset number from the hg tip output...
     45 if(EIGEN_BRANCH_OUTPUT MATCHES "default")
     46 string(REGEX MATCH "^changeset: *[0-9]*:([0-9;a-f]+).*" EIGEN_HG_CHANGESET_MATCH "${EIGEN_HGTIP_OUTPUT}")
     47 set(EIGEN_HG_CHANGESET "${CMAKE_MATCH_1}")
     48 endif(EIGEN_BRANCH_OUTPUT MATCHES "default")
     49 #...and show it next to the version number
     50 if(EIGEN_HG_CHANGESET)
     51   set(EIGEN_VERSION "${EIGEN_VERSION_NUMBER} (mercurial changeset ${EIGEN_HG_CHANGESET})")
     52 else(EIGEN_HG_CHANGESET)
     53   set(EIGEN_VERSION "${EIGEN_VERSION_NUMBER}")
     54 endif(EIGEN_HG_CHANGESET)
     55 
     56 
     57 include(CheckCXXCompilerFlag)
     58 
     59 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
     60 
     61 #############################################################################
     62 # find how to link to the standard libraries                                #
     63 #############################################################################
     64 
     65 find_package(StandardMathLibrary)
     66 
     67 
     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.")
     70 
     71 set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "")
     72 
     73 if(NOT STANDARD_MATH_LIBRARY_FOUND)
     74 
     75   message(FATAL_ERROR
     76     "Can't link to the standard math library. Please report to the Eigen developers, telling them about your platform.")
     77 
     78 else()
     79 
     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}")
     82   else()
     83     set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "${STANDARD_MATH_LIBRARY}")
     84   endif()
     85 
     86 endif()
     87 
     88 if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO)
     89   message(STATUS "Standard libraries to link to explicitly: ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}")
     90 else()
     91   message(STATUS "Standard libraries to link to explicitly: none")
     92 endif()
     93 
     94 option(EIGEN_BUILD_BTL "Build benchmark suite" OFF)
     95 if(NOT WIN32)
     96   option(EIGEN_BUILD_PKGCONFIG "Build pkg-config .pc file for Eigen" ON)
     97 endif(NOT WIN32)
     98 
     99 set(CMAKE_INCLUDE_CURRENT_DIR ON)
    100 
    101 option(EIGEN_SPLIT_LARGE_TESTS "Split large tests into smaller executables" ON)
    102 
    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")
    106 endif()
    107 
    108 set(EIGEN_TEST_MAX_SIZE "320" CACHE STRING "Maximal matrix/vector size, default is 320")
    109 
    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}")
    115   endif()
    116 endmacro(ei_add_cxx_compiler_flag)
    117 
    118 if(NOT MSVC)
    119   # We assume that other compilers are partly compatible with GNUCC
    120   
    121   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions")
    122   set(CMAKE_CXX_FLAGS_DEBUG "-g3")
    123   set(CMAKE_CXX_FLAGS_RELEASE "-g0 -O2")
    124   
    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")
    130   endif()
    131   
    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
    136   
    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")
    145   
    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")
    149   
    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
    155   
    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'
    160   
    161   if(COMPILER_SUPPORT_STRICTANSI)
    162     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -strict-ansi")
    163   else()
    164     ei_add_cxx_compiler_flag("-ansi")
    165   endif()
    166   
    167   set(CMAKE_REQUIRED_FLAGS "")
    168 
    169   option(EIGEN_TEST_SSE2 "Enable/Disable SSE2 in tests/examples" OFF)
    170   if(EIGEN_TEST_SSE2)
    171     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2")
    172     message(STATUS "Enabling SSE2 in tests/examples")
    173   endif()
    174 
    175   option(EIGEN_TEST_SSE3 "Enable/Disable SSE3 in tests/examples" OFF)
    176   if(EIGEN_TEST_SSE3)
    177     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse3")
    178     message(STATUS "Enabling SSE3 in tests/examples")
    179   endif()
    180 
    181   option(EIGEN_TEST_SSSE3 "Enable/Disable SSSE3 in tests/examples" OFF)
    182   if(EIGEN_TEST_SSSE3)
    183     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mssse3")
    184     message(STATUS "Enabling SSSE3 in tests/examples")
    185   endif()
    186 
    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")
    191   endif()
    192 
    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")
    197   endif()
    198 
    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")
    203   endif()
    204 
    205   option(EIGEN_TEST_NEON "Enable/Disable Neon in tests/examples" OFF)
    206   if(EIGEN_TEST_NEON)
    207     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -mcpu=cortex-a8")
    208     message(STATUS "Enabling NEON in tests/examples")
    209   endif()
    210 
    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")
    217     endif()
    218   endif()
    219 
    220 else(NOT MSVC)
    221 
    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")
    230 
    231   # replace all /Wx by /W4
    232   string(REGEX REPLACE "/W[0-9]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    233 
    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")
    240     endif()
    241   endif()
    242 
    243   option(EIGEN_TEST_SSE2 "Enable/Disable SSE2 in tests/examples" OFF)
    244   if(EIGEN_TEST_SSE2)
    245     if(NOT CMAKE_CL_64)
    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)
    251 endif(NOT MSVC)
    252 
    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)
    256 
    257 if(EIGEN_TEST_X87)
    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")
    262   else()
    263     message(STATUS "EIGEN_TEST_X87 ignored on your compiler")
    264   endif()
    265 endif()
    266 
    267 if(EIGEN_TEST_32BIT)
    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")
    271   else()
    272     message(STATUS "EIGEN_TEST_32BIT ignored on your compiler")
    273   endif()
    274 endif()
    275 
    276 if(EIGEN_TEST_NO_EXPLICIT_VECTORIZATION)
    277   add_definitions(-DEIGEN_DONT_VECTORIZE=1)
    278   message(STATUS "Disabling vectorization in tests/examples")
    279 endif()
    280 
    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")
    285 endif()
    286 
    287 option(EIGEN_TEST_C++0x "Enables all C++0x features." OFF)
    288 
    289 include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
    290 
    291 # the user modifiable install path for header files
    292 set(EIGEN_INCLUDE_INSTALL_DIR ${EIGEN_INCLUDE_INSTALL_DIR} CACHE PATH "The directory where we install the header files (optional)")
    293 
    294 # set the internal install path for header files which depends on wether the user modifiable
    295 # EIGEN_INCLUDE_INSTALL_DIR has been set by the user or not.
    296 if(EIGEN_INCLUDE_INSTALL_DIR)
    297   set(INCLUDE_INSTALL_DIR
    298     ${EIGEN_INCLUDE_INSTALL_DIR}
    299     CACHE INTERNAL
    300     "The directory where we install the header files (internal)"
    301   )
    302 else()
    303   set(INCLUDE_INSTALL_DIR
    304     "${CMAKE_INSTALL_PREFIX}/include/eigen3"
    305     CACHE INTERNAL
    306     "The directory where we install the header files (internal)"
    307   )
    308 endif()
    309 
    310 # similar to set_target_properties but append the property instead of overwriting it
    311 macro(ei_add_target_property target prop value)
    312 
    313   get_target_property(previous ${target} ${prop})
    314   # if the property wasn't previously set, ${previous} is now "previous-NOTFOUND" which cmake allows catching with plain if()
    315   if(NOT previous)
    316     set(previous "")
    317   endif(NOT previous)
    318   set_target_properties(${target} PROPERTIES ${prop} "${previous} ${value}")
    319 endmacro(ei_add_target_property)
    320 
    321 install(FILES
    322   signature_of_eigen3_matrix_library
    323   DESTINATION ${INCLUDE_INSTALL_DIR} COMPONENT Devel
    324   )
    325 
    326 if(EIGEN_BUILD_PKGCONFIG)
    327     SET(path_separator ":")
    328     STRING(REPLACE ${path_separator} ";" pkg_config_libdir_search "$ENV{PKG_CONFIG_LIBDIR}")
    329     message(STATUS "searching for 'pkgconfig' directory in PKG_CONFIG_LIBDIR ( $ENV{PKG_CONFIG_LIBDIR} ), ${CMAKE_INSTALL_PREFIX}/share, and ${CMAKE_INSTALL_PREFIX}/lib")
    330     FIND_PATH(pkg_config_libdir pkgconfig ${pkg_config_libdir_search} ${CMAKE_INSTALL_PREFIX}/share ${CMAKE_INSTALL_PREFIX}/lib ${pkg_config_libdir_search})
    331     if(pkg_config_libdir)
    332         SET(pkg_config_install_dir ${pkg_config_libdir})
    333         message(STATUS "found ${pkg_config_libdir}/pkgconfig" )
    334     else(pkg_config_libdir)
    335         SET(pkg_config_install_dir ${CMAKE_INSTALL_PREFIX}/share)
    336         message(STATUS "pkgconfig not found; installing in ${pkg_config_install_dir}" )
    337     endif(pkg_config_libdir)
    338 
    339     configure_file(eigen3.pc.in eigen3.pc)
    340     install(FILES ${CMAKE_CURRENT_BINARY_DIR}/eigen3.pc
    341         DESTINATION ${pkg_config_install_dir}/pkgconfig
    342         )
    343 endif(EIGEN_BUILD_PKGCONFIG)
    344 
    345 add_subdirectory(Eigen)
    346 
    347 add_subdirectory(doc EXCLUDE_FROM_ALL)
    348 
    349 include(EigenConfigureTesting)
    350 
    351 # fixme, not sure this line is still needed:
    352 enable_testing() # must be called from the root CMakeLists, see man page
    353 
    354 
    355 if(EIGEN_LEAVE_TEST_IN_ALL_TARGET)
    356   add_subdirectory(test) # can't do EXCLUDE_FROM_ALL here, breaks CTest
    357 else()
    358   add_subdirectory(test EXCLUDE_FROM_ALL)
    359 endif()
    360 
    361 if(EIGEN_LEAVE_TEST_IN_ALL_TARGET)
    362   add_subdirectory(blas)
    363   add_subdirectory(lapack)
    364 else()
    365   add_subdirectory(blas EXCLUDE_FROM_ALL)
    366   add_subdirectory(lapack EXCLUDE_FROM_ALL)
    367 endif()
    368 
    369 add_subdirectory(unsupported)
    370 
    371 add_subdirectory(demos EXCLUDE_FROM_ALL)
    372 
    373 # must be after test and unsupported, for configuring buildtests.in
    374 add_subdirectory(scripts EXCLUDE_FROM_ALL)
    375 
    376 # TODO: consider also replacing EIGEN_BUILD_BTL by a custom target "make btl"?
    377 if(EIGEN_BUILD_BTL)
    378   add_subdirectory(bench/btl EXCLUDE_FROM_ALL)
    379 endif(EIGEN_BUILD_BTL)
    380 
    381 if(NOT WIN32)
    382   add_subdirectory(bench/spbench EXCLUDE_FROM_ALL)
    383 endif(NOT WIN32)
    384 
    385 configure_file(scripts/cdashtesting.cmake.in cdashtesting.cmake @ONLY)
    386 
    387 ei_testing_print_summary()
    388 
    389 message(STATUS "")
    390 message(STATUS "Configured Eigen ${EIGEN_VERSION_NUMBER}")
    391 message(STATUS "")
    392 
    393 option(EIGEN_FAILTEST "Enable failtests." OFF)
    394 if(EIGEN_FAILTEST)
    395   add_subdirectory(failtest)
    396 endif()
    397 
    398 string(TOLOWER "${CMAKE_GENERATOR}" cmake_generator_tolower)
    399 if(cmake_generator_tolower MATCHES "makefile")
    400   message(STATUS "Some things you can do now:")
    401   message(STATUS "--------------+--------------------------------------------------------------")
    402   message(STATUS "Command       |   Description")
    403   message(STATUS "--------------+--------------------------------------------------------------")
    404   message(STATUS "make install  | Install to ${CMAKE_INSTALL_PREFIX}. To change that:")
    405   message(STATUS "              |     cmake . -DCMAKE_INSTALL_PREFIX=yourpath")
    406   message(STATUS "              |   Eigen headers will then be installed to:")
    407   message(STATUS "              |     ${INCLUDE_INSTALL_DIR}")
    408   message(STATUS "              |   To install Eigen headers to a separate location, do:")
    409   message(STATUS "              |     cmake . -DEIGEN_INCLUDE_INSTALL_DIR=yourpath")
    410   message(STATUS "make doc      | Generate the API documentation, requires Doxygen & LaTeX")
    411   message(STATUS "make check    | Build and run the unit-tests. Read this page:")
    412   message(STATUS "              |   http://eigen.tuxfamily.org/index.php?title=Tests")
    413   message(STATUS "make blas     | Build BLAS library (not the same thing as Eigen)")
    414   message(STATUS "--------------+--------------------------------------------------------------")
    415 else()
    416   message(STATUS "To build/run the unit tests, read this page:")
    417   message(STATUS "  http://eigen.tuxfamily.org/index.php?title=Tests")
    418 endif()
    419 
    420 message(STATUS "")
    421