From 84d1b2ae3a7187f418b1cbbec03eabda372ea699 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Mon, 19 Apr 2010 11:19:22 -0400 Subject: add platform check for how to link to the standard math library. This allows to support QNX. --- CMakeLists.txt | 37 +++++++++++++++++++ blas/CMakeLists.txt | 6 ++++ cmake/EigenTesting.cmake | 3 ++ cmake/FindEigen3.cmake | 3 +- cmake/FindStandardMathLibrary.cmake | 64 +++++++++++++++++++++++++++++++++ doc/examples/CMakeLists.txt | 33 +++++++++-------- doc/snippets/CMakeLists.txt | 29 ++++++++------- unsupported/doc/examples/CMakeLists.txt | 25 +++++++------ unsupported/doc/snippets/CMakeLists.txt | 3 ++ 9 files changed, 163 insertions(+), 40 deletions(-) create mode 100644 cmake/FindStandardMathLibrary.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f5f10ab81..5418a06ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,17 @@ project(Eigen) cmake_minimum_required(VERSION 2.6.2) + +# guard against in-source builds + if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR}) 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 ") endif() +############################################################################# +# retrieve version infomation # +############################################################################# + # automatically parse the version number file(READ "${CMAKE_SOURCE_DIR}/Eigen/src/Core/util/Macros.h" _eigen_version_header) string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen_world_version_match "${_eigen_version_header}") @@ -33,10 +40,40 @@ else(EIGEN_HG_CHANGESET) set(EIGEN_VERSION "${EIGEN_VERSION_NUMBER}") endif(EIGEN_HG_CHANGESET) + include(CheckCXXCompilerFlag) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +############################################################################# +# find how to link to the standard libraries # +############################################################################# + +find_package(StandardMathLibrary) + +set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "") + +if(NOT STANDARD_MATH_LIBRARY_FOUND) + + message(FATAL_ERROR + "Can't link to the standard math library. Please report to the Eigen developers, telling them about your platform.") + +else() + + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO} ${STANDARD_MATH_LIBRARY}") + else() + set(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO "${STANDARD_MATH_LIBRARY}") + endif() + +endif() + +if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + message(STATUS "Standard libraries to link to explicitly: ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}") +else() + message(STATUS "Standard libraries to link to explicitly: none") +endif() + option(EIGEN_BUILD_BTL "Build benchmark suite" OFF) if(NOT WIN32) option(EIGEN_BUILD_PKGCONFIG "Build pkg-config .pc file for Eigen" ON) diff --git a/blas/CMakeLists.txt b/blas/CMakeLists.txt index d1f5b5676..a752d2ea2 100644 --- a/blas/CMakeLists.txt +++ b/blas/CMakeLists.txt @@ -6,6 +6,12 @@ set(EigenBlas_SRCS single.cpp double.cpp complex_single.cpp complex_double.cpp x add_library(eigen_blas_static ${EigenBlas_SRCS}) add_library(eigen_blas SHARED ${EigenBlas_SRCS}) + +if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(eigen_blas_static ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + target_link_libraries(eigen_blas ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) +endif() + add_dependencies(blas eigen_blas eigen_blas_static) install(TARGETS eigen_blas diff --git a/cmake/EigenTesting.cmake b/cmake/EigenTesting.cmake index 3bb9aed2b..4b2ee9f6f 100644 --- a/cmake/EigenTesting.cmake +++ b/cmake/EigenTesting.cmake @@ -40,6 +40,9 @@ macro(ei_add_test_internal testname testname_with_suffix) ei_add_target_property(${targetname} COMPILE_FLAGS "${ARGV2}") endif(${ARGC} GREATER 2) + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(${targetname} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + endif() if(EXTERNAL_LIBS) target_link_libraries(${targetname} ${EXTERNAL_LIBS}) endif() diff --git a/cmake/FindEigen3.cmake b/cmake/FindEigen3.cmake index b87ebff4b..9c546a05d 100644 --- a/cmake/FindEigen3.cmake +++ b/cmake/FindEigen3.cmake @@ -12,7 +12,8 @@ # Copyright (c) 2006, 2007 Montel Laurent, # Copyright (c) 2008, 2009 Gael Guennebaud, -# Redistribution and use is allowed according to the terms of the BSD license. +# Copyright (c) 2009 Benoit Jacob +# Redistribution and use is allowed according to the terms of the 2-clause BSD license. if(NOT Eigen3_FIND_VERSION) if(NOT Eigen3_FIND_VERSION_MAJOR) diff --git a/cmake/FindStandardMathLibrary.cmake b/cmake/FindStandardMathLibrary.cmake new file mode 100644 index 000000000..711b0e4b4 --- /dev/null +++ b/cmake/FindStandardMathLibrary.cmake @@ -0,0 +1,64 @@ +# - Try to find how to link to the standard math library, if anything at all is needed to do. +# On most platforms this is automatic, but for example it's not automatic on QNX. +# +# Once done this will define +# +# STANDARD_MATH_LIBRARY_FOUND - we found how to successfully link to the standard math library +# STANDARD_MATH_LIBRARY - the name of the standard library that one has to link to. +# -- this will be left empty if it's automatic (most platforms). +# -- this will be set to "m" on platforms where one must explicitly +# pass the "-lm" linker flag. +# +# Copyright (c) 2010 Benoit Jacob +# Redistribution and use is allowed according to the terms of the 2-clause BSD license. + + +include(CheckCXXSourceCompiles) + +# a little test program for c++ math functions. +# notice the std:: is required on some platforms such as QNX + +set(find_standard_math_library_test_program +"#include +int main() { std::sin(0.0); std::log(0.0f); }") + +# first try compiling/linking the test program without any linker flags + +set(CMAKE_REQUIRED_FLAGS "") +set(CMAKE_REQUIRED_LIBRARIES "") +CHECK_CXX_SOURCE_COMPILES( + "${find_standard_math_library_test_program}" + standard_math_library_linked_to_automatically +) + +if(standard_math_library_linked_to_automatically) + + # the test program linked successfully without any linker flag. + set(STANDARD_MATH_LIBRARY "") + set(STANDARD_MATH_LIBRARY_FOUND TRUE) + +else() + + # the test program did not link successfully without any linker flag. + # This is a very uncommon case that so far we only saw on QNX. The next try is the + # standard name 'm' for the standard math library. + + set(CMAKE_REQUIRED_LIBRARIES "m") + CHECK_CXX_SOURCE_COMPILES( + "${find_standard_math_library_test_program}" + standard_math_library_linked_to_as_m) + + if(standard_math_library_linked_to_as_m) + + # the test program linked successfully when linking to the 'm' library + set(STANDARD_MATH_LIBRARY "m") + set(STANDARD_MATH_LIBRARY_FOUND TRUE) + + else() + + # the test program still doesn't link successfully + set(STANDARD_MATH_LIBRARY_FOUND FALSE) + + endif() + +endif() diff --git a/doc/examples/CMakeLists.txt b/doc/examples/CMakeLists.txt index 29cf078c1..13ec0c123 100644 --- a/doc/examples/CMakeLists.txt +++ b/doc/examples/CMakeLists.txt @@ -1,17 +1,20 @@ -FILE(GLOB examples_SRCS "*.cpp") +file(GLOB examples_SRCS "*.cpp") -ADD_CUSTOM_TARGET(all_examples) +add_custom_target(all_examples) -FOREACH(example_src ${examples_SRCS}) -GET_FILENAME_COMPONENT(example ${example_src} NAME_WE) -ADD_EXECUTABLE(${example} ${example_src}) -GET_TARGET_PROPERTY(example_executable - ${example} LOCATION) -ADD_CUSTOM_COMMAND( - TARGET ${example} - POST_BUILD - COMMAND ${example_executable} - ARGS >${CMAKE_CURRENT_BINARY_DIR}/${example}.out -) -ADD_DEPENDENCIES(all_examples ${example}) -ENDFOREACH(example_src) +foreach(example_src ${examples_SRCS}) + get_filename_component(example ${example_src} NAME_WE) + add_executable(${example} ${example_src}) + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(${example} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + endif() + get_target_property(example_executable + ${example} LOCATION) + add_custom_command( + TARGET ${example} + POST_BUILD + COMMAND ${example_executable} + ARGS >${CMAKE_CURRENT_BINARY_DIR}/${example}.out + ) + add_dependencies(all_examples ${example}) +endforeach(example_src) diff --git a/doc/snippets/CMakeLists.txt b/doc/snippets/CMakeLists.txt index 87eae62a2..fbddf2226 100644 --- a/doc/snippets/CMakeLists.txt +++ b/doc/snippets/CMakeLists.txt @@ -1,25 +1,28 @@ -FILE(GLOB snippets_SRCS "*.cpp") +file(GLOB snippets_SRCS "*.cpp") -ADD_CUSTOM_TARGET(all_snippets) +add_custom_target(all_snippets) -FOREACH(snippet_src ${snippets_SRCS}) - GET_FILENAME_COMPONENT(snippet ${snippet_src} NAME_WE) - SET(compile_snippet_target compile_${snippet}) - SET(compile_snippet_src ${compile_snippet_target}.cpp) - FILE(READ ${snippet_src} snippet_source_code) - CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/compile_snippet.cpp.in +foreach(snippet_src ${snippets_SRCS}) + get_filename_component(snippet ${snippet_src} NAME_WE) + set(compile_snippet_target compile_${snippet}) + set(compile_snippet_src ${compile_snippet_target}.cpp) + file(READ ${snippet_src} snippet_source_code) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/compile_snippet.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src}) - ADD_EXECUTABLE(${compile_snippet_target} + add_executable(${compile_snippet_target} ${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src}) - GET_TARGET_PROPERTY(compile_snippet_executable + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(${compile_snippet_target} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + endif() + get_target_property(compile_snippet_executable ${compile_snippet_target} LOCATION) - ADD_CUSTOM_COMMAND( + add_custom_command( TARGET ${compile_snippet_target} POST_BUILD COMMAND ${compile_snippet_executable} ARGS >${CMAKE_CURRENT_BINARY_DIR}/${snippet}.out ) - ADD_DEPENDENCIES(all_snippets ${compile_snippet_target}) + add_dependencies(all_snippets ${compile_snippet_target}) set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src} PROPERTIES OBJECT_DEPENDS ${snippet_src}) -ENDFOREACH(snippet_src) +endforeach(snippet_src) diff --git a/unsupported/doc/examples/CMakeLists.txt b/unsupported/doc/examples/CMakeLists.txt index cb42b2ab9..61af3e5a5 100644 --- a/unsupported/doc/examples/CMakeLists.txt +++ b/unsupported/doc/examples/CMakeLists.txt @@ -3,15 +3,18 @@ FILE(GLOB examples_SRCS "*.cpp") ADD_CUSTOM_TARGET(unsupported_examples) FOREACH(example_src ${examples_SRCS}) -GET_FILENAME_COMPONENT(example ${example_src} NAME_WE) -ADD_EXECUTABLE(example_${example} ${example_src}) -GET_TARGET_PROPERTY(example_executable - example_${example} LOCATION) -ADD_CUSTOM_COMMAND( - TARGET example_${example} - POST_BUILD - COMMAND ${example_executable} - ARGS >${CMAKE_CURRENT_BINARY_DIR}/${example}.out -) -ADD_DEPENDENCIES(unsupported_examples example_${example}) + GET_FILENAME_COMPONENT(example ${example_src} NAME_WE) + ADD_EXECUTABLE(example_${example} ${example_src}) + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(example_${example} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + endif() + GET_TARGET_PROPERTY(example_executable + example_${example} LOCATION) + ADD_CUSTOM_COMMAND( + TARGET example_${example} + POST_BUILD + COMMAND ${example_executable} + ARGS >${CMAKE_CURRENT_BINARY_DIR}/${example}.out + ) + ADD_DEPENDENCIES(unsupported_examples example_${example}) ENDFOREACH(example_src) diff --git a/unsupported/doc/snippets/CMakeLists.txt b/unsupported/doc/snippets/CMakeLists.txt index 19e9b1a1f..4a4157933 100644 --- a/unsupported/doc/snippets/CMakeLists.txt +++ b/unsupported/doc/snippets/CMakeLists.txt @@ -11,6 +11,9 @@ FOREACH(snippet_src ${snippets_SRCS}) ${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src}) ADD_EXECUTABLE(${compile_snippet_target} ${CMAKE_CURRENT_BINARY_DIR}/${compile_snippet_src}) + if(EIGEN_STANDARD_LIBRARIES_TO_LINK_TO) + target_link_libraries(${compile_snippet_target} ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO}) + endif() GET_TARGET_PROPERTY(compile_snippet_executable ${compile_snippet_target} LOCATION) ADD_CUSTOM_COMMAND( -- cgit v1.2.3