From dfea2628498dd49192b185f01ee60e04d89e0aff Mon Sep 17 00:00:00 2001 From: william-dawson Date: Fri, 16 Nov 2018 21:14:11 +0900 Subject: [PATCH] Improvements to the build system (#86) * Minor cleanups to build * Script cleanup * Simplified python building * Capitalization consistency... * Only mac autopath for python is needed * Mac without toolchain * Test for linux with no toolchain * Static/Shared division * Reverting feature that still doesn't work. --- CMakeLists.txt | 38 ++++++++++++++++++++++---------- Documentation/CMakeLists.txt | 4 ++-- Source/CPlusPlus/CMakeLists.txt | 2 +- Source/CPlusPlus/EigenBounds.cc | 1 - Source/CPlusPlus/MatrixMapper.cc | 2 -- Source/Fortran/CMakeLists.txt | 6 ++++- Source/Swig/CMakeLists.txt | 34 ++++++++++++++-------------- Source/Swig/ConfigPython.cmake | 20 +++++++++++++++++ Source/Wrapper/CMakeLists.txt | 2 +- Targets/Mac-python2.cmake | 13 ----------- Targets/Mac-python3.cmake | 13 ----------- UnitTests/CMakeLists.txt | 1 + UnitTests/travis_run_cmake.sh | 14 ++++++------ 13 files changed, 81 insertions(+), 69 deletions(-) create mode 100644 Source/Swig/ConfigPython.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 45e991fe..46b9c897 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,14 @@ enable_language(Fortran) ################################################################################ ## Packages -find_package(MPI REQUIRED) +if (NOT CMAKE_TOOLCHAIN_FILE) + message(WARNING "Building without a toolchain file. " + "If this does not work, please see the example toolchain files in " + "the Targets directory and set the parameters that match your " + "system.") + find_package(MPI REQUIRED) + find_package(BLAS) +endif() find_package(SWIG 3.0) ################################################################################ @@ -14,7 +21,7 @@ find_package(SWIG 3.0) if (NOT FORTRAN_ONLY) enable_testing() else() - MESSAGE(WARNING "Fortran only! No local testing will be generated.") + message(WARNING "Fortran only! No local testing will be generated.") endif() ################################################################################ @@ -23,33 +30,40 @@ set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include) -install(DIRECTORY ${CMAKE_Fortran_MODULE_DIRECTORY}/ DESTINATION include) +install(DIRECTORY ${CMAKE_Fortran_MODULE_DIRECTORY}/ + DESTINATION ${CMAKE_INSTALL_PREFIX}/include) file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/scratch) ################################################################################ ## Compiler Flags -set(CMAKE_Fortran_FLAGS_DEBUG ${F_TOOLCHAINFLAGS_DEBUG}) -set(CMAKE_CXX_FLAGS_DEBUG ${CXX_TOOLCHAINFLAGS_DEBUG}) -set(CMAKE_Fortran_FLAGS_RELEASE ${F_TOOLCHAINFLAGS_RELEASE}) -set(CMAKE_CXX_FLAGS_RELEASE ${CXX_TOOLCHAINFLAGS_RELEASE}) +if (CMAKE_TOOLCHAIN_FILE) + set(CMAKE_Fortran_FLAGS_DEBUG ${F_TOOLCHAINFLAGS_DEBUG}) + set(CMAKE_CXX_FLAGS_DEBUG ${CXX_TOOLCHAINFLAGS_DEBUG}) + set(CMAKE_Fortran_FLAGS_RELEASE ${F_TOOLCHAINFLAGS_RELEASE}) + set(CMAKE_CXX_FLAGS_RELEASE ${CXX_TOOLCHAINFLAGS_RELEASE}) +else() + find_package(OpenMP) + set(CMAKE_Fortran_FLAGS_RELEASE + "${CMAKE_Fortran_FLAGS_RELEASE} ${OpenMP_Fortran_FLAGS}") +endif() if (NOT CMAKE_BUILD_TYPE) - set(CMAKE_Fortran_FLAGS ${F_TOOLCHAINFLAGS_RELEASE}) - set(CMAKE_CXX_FLAGS ${CXX_TOOLCHAINFLAGS_RELEASE}) + set(CMAKE_Fortran_FLAGS ${CMAKE_Fortran_FLAGS_RELEASE}) + set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_RELEASE}) endif() ################################################################################ ## Check MPI Features option(USE_MPIH OFF) -if (NOT ${MPI_Fortran_HAVE_F90_MODULE}) +if (USE_MPIH OR NOT ${MPI_Fortran_HAVE_F90_MODULE}) add_definitions(-DUSE_MPIH) endif() option(NOIALLGATHER OFF) if (NOIALLGATHER) add_definitions(-DNOIALLGATHER) - MESSAGE(STATUS "IAllgather replacement activated.") - MESSAGE(STATUS "Note that this may reduce parallel performance.") + message(STATUS "IAllgather replacement activated.") + message(STATUS "Note that this may reduce parallel performance.") endif() ################################################################################ diff --git a/Documentation/CMakeLists.txt b/Documentation/CMakeLists.txt index 14b0427e..51ed90a0 100644 --- a/Documentation/CMakeLists.txt +++ b/Documentation/CMakeLists.txt @@ -12,7 +12,7 @@ if ( FORD ) add_custom_target(fdoc "${FORD}" Ford) add_dependencies(doc fdoc) else() - MESSAGE(WARNING + message(WARNING "FORD not found! No fortran documentation will be generated.") endif() @@ -23,5 +23,5 @@ if (NOT FORTRAN_ONLY AND DOXYGEN_FOUND) add_custom_target(cdoc ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile) add_dependencies(doc cdoc) elseif(NOT DOXYGEN_FOUND) - MESSAGE(WARNING "Doxygen not found! No documentation will be generated.") + message(WARNING "Doxygen not found! No documentation will be generated.") endif() diff --git a/Source/CPlusPlus/CMakeLists.txt b/Source/CPlusPlus/CMakeLists.txt index 2510b70a..f9ec48f8 100644 --- a/Source/CPlusPlus/CMakeLists.txt +++ b/Source/CPlusPlus/CMakeLists.txt @@ -55,7 +55,7 @@ set(Chead ) ################################################################################ -add_library(NTPolyCPP STATIC ${Csrc} ${Chead}) +add_library(NTPolyCPP ${Csrc} ${Chead}) target_link_libraries(NTPolyCPP NTPolyWrapper) set_target_properties(NTPolyCPP PROPERTIES PUBLIC_HEADER "${Chead}") diff --git a/Source/CPlusPlus/EigenBounds.cc b/Source/CPlusPlus/EigenBounds.cc index f12b4a19..e45b3cf1 100644 --- a/Source/CPlusPlus/EigenBounds.cc +++ b/Source/CPlusPlus/EigenBounds.cc @@ -1,6 +1,5 @@ #include "EigenBounds.h" using namespace NTPoly; -#include //////////////////////////////////////////////////////////////////////////////// extern "C" { diff --git a/Source/CPlusPlus/MatrixMapper.cc b/Source/CPlusPlus/MatrixMapper.cc index 244fdb1e..f918d026 100644 --- a/Source/CPlusPlus/MatrixMapper.cc +++ b/Source/CPlusPlus/MatrixMapper.cc @@ -4,8 +4,6 @@ #include "TripletList.h" #include "Wrapper.h" -#include - //////////////////////////////////////////////////////////////////////////////// extern "C" { #include "PSMatrix_c.h" diff --git a/Source/Fortran/CMakeLists.txt b/Source/Fortran/CMakeLists.txt index 1d26d8f8..372edbdd 100644 --- a/Source/Fortran/CMakeLists.txt +++ b/Source/Fortran/CMakeLists.txt @@ -39,7 +39,11 @@ set(Fsrc ) ################################################################################ -add_library(NTPoly STATIC ${Fsrc}) +add_library(NTPoly ${Fsrc}) +target_link_libraries(NTPoly ${MPI_Fortran_LIBRARIES} + ${OpenMP_Fortran_LIBRARIES} ${BLAS_LIBRARIES}) +target_include_directories(NTPoly PUBLIC ${MPI_Fortran_INCLUDE_PATH}) + include(GNUInstallDirs) install(TARGETS NTPoly EXPORT ntpoly-export diff --git a/Source/Swig/CMakeLists.txt b/Source/Swig/CMakeLists.txt index a5a48e8c..9255b559 100644 --- a/Source/Swig/CMakeLists.txt +++ b/Source/Swig/CMakeLists.txt @@ -1,23 +1,25 @@ ################################################################################ if (SWIG_FOUND) - # Determine python path using python's distutils module + # Probe some information about python if(NOT DEFINED PYTHON_EXECUTABLE) find_package(PythonInterp REQUIRED) endif() - execute_process( - COMMAND ${PYTHON_EXECUTABLE} -c - "from distutils.sysconfig import get_python_inc; print(get_python_inc())" - OUTPUT_VARIABLE PYTHON_INCLUDE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE) - MESSAGE(STATUS "Using Python Include Path:" ${PYTHON_INCLUDE_PATH}) - MESSAGE(STATUS "Using Python Library Path:" ${PYTHON_LIBRARIES}) - INCLUDE(${SWIG_USE_FILE}) - INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) - INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) - INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/Source/C) - INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/Source/CPlusPlus) + include(ConfigPython.cmake) + if (APPLE) + get_py_lib() + message(STATUS "Using Python Library Path:" ${PYTHON_LIBRARIES}) + endif() + get_py_include() + message(STATUS "Using Python Include Path:" ${PYTHON_INCLUDE_PATH}) + + include_directories(${PYTHON_INCLUDE_PATH}) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + include_directories(${CMAKE_SOURCE_DIR}/Source/C) + include_directories(${CMAKE_SOURCE_DIR}/Source/CPlusPlus) - SET(CMAKE_SWIG_FLAGS "") + include(${SWIG_USE_FILE}) + set(CMAKE_SWIG_FLAGS "") set(Swigsrc NTPolySwig.i @@ -27,13 +29,13 @@ if (SWIG_FOUND) SET_SOURCE_FILES_PROPERTIES(${file} PROPERTIES CPLUSPLUS ON) endforeach(file) set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR}/python) - SWIG_ADD_MODULE(NTPolySwig python ${Swigsrc}) - SWIG_LINK_LIBRARIES(NTPolySwig NTPolyCPP NTPolyWrapper NTPoly + swig_add_module(NTPolySwig python ${Swigsrc}) + swig_link_libraries(NTPolySwig NTPolyCPP NTPolyWrapper NTPoly ${PYTHON_LIBRARIES} ${TOOLCHAIN_LIBS}) set_target_properties(_NTPolySwig PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/python LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/python ) else() - MESSAGE(WARNING "Swig not found! No python bindings will be generated.") + message(WARNING "Swig not found! No python bindings will be generated.") endif() diff --git a/Source/Swig/ConfigPython.cmake b/Source/Swig/ConfigPython.cmake new file mode 100644 index 00000000..439c35cb --- /dev/null +++ b/Source/Swig/ConfigPython.cmake @@ -0,0 +1,20 @@ +################################################################################ +# Determine python include path using python's distutils module +macro(get_py_include) + execute_process( + COMMAND ${PYTHON_EXECUTABLE} -c + "from distutils.sysconfig import get_python_inc; print(get_python_inc())" + OUTPUT_VARIABLE PYTHON_INCLUDE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE) +endmacro() + +################################################################################ +# Determine Python Library Path +macro(get_py_lib) + execute_process( + COMMAND ${PYTHON_EXECUTABLE} -c +" +from distutils.sysconfig import get_python_lib +path=get_python_lib(standard_lib=True)+\"/../../Python\" +print(path)" + OUTPUT_VARIABLE PYTHON_LIBRARIES OUTPUT_STRIP_TRAILING_WHITESPACE) +endmacro() diff --git a/Source/Wrapper/CMakeLists.txt b/Source/Wrapper/CMakeLists.txt index 56523fa6..5afb1332 100644 --- a/Source/Wrapper/CMakeLists.txt +++ b/Source/Wrapper/CMakeLists.txt @@ -28,7 +28,7 @@ set(Wsrc ) ################################################################################ -add_library(NTPolyWrapper STATIC ${Wsrc}) +add_library(NTPolyWrapper ${Wsrc}) target_link_libraries(NTPolyWrapper NTPoly) include(GNUInstallDirs) diff --git a/Targets/Mac-python2.cmake b/Targets/Mac-python2.cmake index f95d51a8..7af13c8e 100644 --- a/Targets/Mac-python2.cmake +++ b/Targets/Mac-python2.cmake @@ -1,24 +1,11 @@ ################################################################################ # Target file for a Mac computer. -# Unfortunately, installing on the mac is a bit tricky. The problem is that -# when you install SWIG, it might link against the wrong version of python. -# That's why I've included custom set variables for the python dynamic library, -# which is determined by your choice of python executable. set(CMAKE_SYSTEM_NAME Darwin) set(CMAKE_C_COMPILER mpicc) set(CMAKE_Fortran_COMPILER mpif90) set(CMAKE_CXX_COMPILER mpicxx) set(PYTHON_EXECUTABLE python2) -# Determine Python Library Path -execute_process( - COMMAND ${PYTHON_EXECUTABLE} -c -" -from distutils.sysconfig import get_python_lib -path=get_python_lib(standard_lib=True)+\"/../../Python\" -print path" - OUTPUT_VARIABLE PYTHON_LIBRARIES OUTPUT_STRIP_TRAILING_WHITESPACE) - # Library Files set(TOOLCHAIN_LIBS "-framework Accelerate -lgomp") diff --git a/Targets/Mac-python3.cmake b/Targets/Mac-python3.cmake index 640a9395..22c35957 100644 --- a/Targets/Mac-python3.cmake +++ b/Targets/Mac-python3.cmake @@ -1,24 +1,11 @@ ################################################################################ # Target file for a Mac computer. -# Unfortunately, installing on the mac is a bit tricky. The problem is that -# when you install SWIG, it might link against the wrong version of python. -# That's why I've included custom set variables for the python dynamic library, -# which is determined by your choice of python executable. set(CMAKE_SYSTEM_NAME Darwin) set(CMAKE_C_COMPILER mpicc) set(CMAKE_Fortran_COMPILER mpif90) set(CMAKE_CXX_COMPILER mpicxx) set(PYTHON_EXECUTABLE python3) -# Determine Python Library Path -execute_process( - COMMAND ${PYTHON_EXECUTABLE} -c -" -from distutils.sysconfig import get_python_lib -path=get_python_lib(standard_lib=True)+\"/../../Python\" -print(path)" - OUTPUT_VARIABLE PYTHON_LIBRARIES OUTPUT_STRIP_TRAILING_WHITESPACE) - # Library Files set(TOOLCHAIN_LIBS "-framework Accelerate -lgomp") diff --git a/UnitTests/CMakeLists.txt b/UnitTests/CMakeLists.txt index 202b8f6b..1a361056 100644 --- a/UnitTests/CMakeLists.txt +++ b/UnitTests/CMakeLists.txt @@ -14,6 +14,7 @@ set(TestFiles find_package(PythonInterp REQUIRED) ################################################################################ +find_package(MPI REQUIRED) execute_process(COMMAND ${MPIEXEC} --version OUTPUT_VARIABLE mpiversion OUTPUT_STRIP_TRAILING_WHITESPACE) if (${mpiversion} MATCHES "OpenRTE") diff --git a/UnitTests/travis_run_cmake.sh b/UnitTests/travis_run_cmake.sh index 5fad5d3b..13df3c49 100644 --- a/UnitTests/travis_run_cmake.sh +++ b/UnitTests/travis_run_cmake.sh @@ -1,15 +1,15 @@ if [[ "$TESTOS" == "OSX" ]]; then - cmake -DCMAKE_TOOLCHAIN_FILE=../Targets/Mac-python2.cmake \ - -DCMAKE_BUILD_TYPE=Release .. ; + cmake .. -DCMAKE_TOOLCHAIN_FILE=../Targets/Mac-python2.cmake \ + -DCMAKE_BUILD_TYPE=Release ; else if [ ! -z ${NOIALLGATHER+x} ]; then - cmake -DCMAKE_TOOLCHAIN_FILE=../Targets/Linux.cmake \ - -DNOIALLGATHER=YES .. -DCMAKE_BUILD_TYPE=Release ; + cmake .. -DCMAKE_TOOLCHAIN_FILE=../Targets/Linux.cmake \ + -DNOIALLGATHER=YES -DCMAKE_BUILD_TYPE=Release ; elif [ -z ${FORTRAN_ONLY+x} ]; then - cmake -DCMAKE_TOOLCHAIN_FILE=../Targets/Linux.cmake .. \ + cmake .. -DCMAKE_TOOLCHAIN_FILE=../Targets/Linux.cmake \ -DCMAKE_BUILD_TYPE=Release ; else - cmake -DCMAKE_TOOLCHAIN_FILE=../Targets/Linux.cmake \ - -DFORTRAN_ONLY=YES .. -DCMAKE_BUILD_TYPE=Release ; + cmake .. -DCMAKE_TOOLCHAIN_FILE=../Targets/Linux.cmake \ + -DFORTRAN_ONLY=YES -DCMAKE_BUILD_TYPE=Release ; fi fi