Skip to content
Snippets Groups Projects
CMakeLists.txt 7.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    ###########################################################################
    
    # top-level CMakeLists.txt for building BornAgain
    
    ############################################################################
    
    
    cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    set(CMAKE_MODULE_PATH
        ${CMAKE_SOURCE_DIR}/cmake
        ${CMAKE_SOURCE_DIR}/cmake/find
        ${CMAKE_SOURCE_DIR}/cmake/pack)
    
    set(CONFIGURABLES_DIR ${CMAKE_SOURCE_DIR}/cmake/configurables)
    
    include(commons/PreventInSourceBuilds)
    
        VERSION 1.19.0
    
        DESCRIPTION "BornAgain: simulate and fit scattering at grazing incidence."
        HOMEPAGE_URL https://www.bornagainproject.org
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        ) # LANGUAGES CXX)
         # TODO modernize FindCerf to get rid of AssertLibraryFunction to restore restriction to CXX
    
    set(CMAKE_C_STANDARD 11)
    
    Beerwerth, Randolf's avatar
    Beerwerth, Randolf committed
    set(CMAKE_CXX_STANDARD 17)
    
    ## Show info about generator type; set CMAKE_BUILD_TYPE if not given
    
    if(CMAKE_CONFIGURATION_TYPES)
    
    Matthias Puchner's avatar
    Matthias Puchner committed
        message(STATUS "Generator type: Multi-configuration generator")
    else()
        message(STATUS "Generator type: Single-configuration generator")
    endif()
    message("    CMAKE_CONFIGURATION_TYPES: ${CMAKE_CONFIGURATION_TYPES}")
    
    # The following is not correct/does not have any effect for multi-configuration generators.
    
    # But when correcting this, be aware that CMAKE_BUILD_TYPE is used in more scripts!
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
        message("    CMAKE_BUILD_TYPE type not given - forced to 'Release'")
    
    else()
        string(TOUPPER "${CMAKE_BUILD_TYPE}" _upper_build_type)
        set(BUILD_${_upper_build_type} 1)
    
    Matthias Puchner's avatar
    Matthias Puchner committed
    message("    CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
    
    configure_file("${PROJECT_SOURCE_DIR}/cmake/configurables/config_build.h.in"
        "${PROJECT_BINARY_DIR}/inc/config_build.h"
        @ONLY)
    
    # GoogleTest: `gtest_discover_tests` discovers tests by asking the _compiled test executable_
    # to enumerate its tests. `PRE_TEST` delays test discovery until just prior to test execution;
    # this avoids calling the executables during the build phase.
    set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)
    
    
    include(CTest) # equivalent to "enable_testing() ???
    
    add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -LE Fullcheck)
       # => 'make check' is an alias for 'ctest'
    add_custom_target(fullcheck COMMAND ${CMAKE_CTEST_COMMAND})
       # => 'make check' is an alias for 'ctest'
    
    ## Project-wide user flags
    
    # options that are on by default (switch off for accelerated builds of limited scope)
    
    option(BORNAGAIN_PYTHON "Build with python support" ON)
    
    option(BORNAGAIN_GUI "Build a graphical user interface" ON)
    option(BORNAGAIN_TIFF_SUPPORT "Tiff files read/write support" ON)
    
    
    # options that are off by default (switch on for additional functionality)
    
    option(BORNAGAIN_MPI "Build with MPI support" OFF)
    
    
    option(CONFIGURE_BINDINGS "Generate python bindings during build (requires swig)" OFF)
    option(CONFIGURE_PYTHON_DOCS "Generate python documentation from the doxygen comments" OFF)
    option(CONFIGURE_MANPAGE "Build Unix man page" OFF)
    option(CONFIGURE_DOXY "Configure Doxygen files" OFF)
    option(ALLCONFIG "Regenerate Py docs, Py wrappers, man page, Doxyfiles" OFF)
    
    Pospelov, Gennady's avatar
    Pospelov, Gennady committed
    option(BUILD_DEBIAN "Build a debian package" OFF)
    
    option(BORNAGAIN_APPLE_BUNDLE "Create a Mac OS X bundle" OFF)
    
    option(ZERO_TOLERANCE "Terminate compilation on warnings" OFF)
    
    option(BORNAGAIN_COVERAGE "Build with test coverage information" OFF)
    
    option(BORNAGAIN_DEBUG_OPTIMIZATION "Build with debug optimization (gcc only)" OFF)
    
    option(BORNAGAIN_TIDY "Invokes clang-tidy" OFF)
    
    option(ALGORITHM_DIAGNOSTIC "Let some algorithms set diagnostic variables" OFF)
    
    # note on two non-options:
    # macro SWIG is defined when code is processed by the tool Swig.
    # macros SWIG and USER_API are set by Doc/Doxygen/user/Doxyfile.in
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    if(BORNAGAIN_TIDY AND BORNAGAIN_PYTHON)
        message(FATAL_ERROR "BORNAGAIN_TIDY is incompatible with BORNAGAIN_PYTHON")
    endif()
    
    
    if(BORNAGAIN_GUI AND NOT BORNAGAIN_PYTHON AND NOT BORNAGAIN_TIDY)
        message(FATAL_ERROR "BORNAGAIN_GUI without BORNAGAIN_PYTHON is currently unsupported"
            " (except with BORNAGAIN_TIDY)")
    endif()
    
    
    if(ALLCONFIG)
        set(CONFIGURE_MANPAGE ON)
        set(CONFIGURE_PYTHON_DOCS ON)
        set(CONFIGURE_BINDINGS ON)
        set(CONFIGURE_DOXY ON)
    
    ## Various CMake settings (include order is of critical importance)
    
    # function definitions
    
    include(commons/GetFilenameComponent) # fct get_filename_component (overwrites CMake's built-in)
    include(BornAgain/PythonDocs) # fct GeneratePythonDocs
    include(BornAgain/SwigLib) # fct SwigLib
    include(BornAgain/MakeLib) # fct MakeLib
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    
    # main settings
    
    
    include(BornAgain/Directories)
    
    if(UNIX)
        if(CMAKE_SYSTEM_NAME MATCHES Linux)
            include(BornAgain/Linux)
        elseif(APPLE)
            include(BornAgain/MacOS)
        else()
            message(FATAL_ERROR "Operating system ${CMAKE_SYSTEM_NAME} not supported")
        endif()
    
        include(BornAgain/NixInstall)
    
    elseif(WIN32)
        include(BornAgain/Windows)
    
    else()
        message(FATAL_ERROR "Operating system ${CMAKE_SYSTEM_NAME} is neither UNIX nor WIN32")
    
    endif()
    
    include(BornAgain/Compiler)
    
        string(APPEND CMAKE_CXX_FLAGS " -Werror -Wfatal-errors")
    
    if(ALGORITHM_DIAGNOSTIC)
        string(APPEND CMAKE_CXX_FLAGS " -DALGORITHM_DIAGNOSTIC=ON")
    endif()
    
    
    include(BornAgain/Dependences)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    if(BORNAGAIN_GUI)
    
        include(BornAgain/Qt)
    
        string(APPEND CMAKE_CXX_FLAGS " -DHAVE_QT=ON")
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    endif()
    
    include(BornAgain/Config)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    if(BORNAGAIN_PYTHON)
        include(BornAgain/PythonAPI)
    endif()
    
    # debug optimization
    if(BORNAGAIN_DEBUG_OPTIMIZATION)
    
      include(commons/DebugOptimization)
    
    add_subdirectory(Fit)
    add_subdirectory(Fit/Test/Unit)
    
    add_subdirectory(Fit/Test/Functional)
    
    ## recurse into the given subdirectories
    
    
    Matthias's avatar
    Matthias committed
    # BornAgain components are built as separate shared libraries
    
    set(CoreComponents "Base;Param;Sample;Resample;Device;Core")
    
    set(AllComponents "${CoreComponents};GUI")
    
    
    # code analysis
    include(BornAgain/LineLength)
    
    if(BORNAGAIN_COVERAGE)
    
      include(commons/CoverageFunction)
    
      include(BornAgain/Coverage)
    
    # third-party code
    
    add_subdirectory(3rdparty/common)
    add_subdirectory(3rdparty/Core)
    
    if(BORNAGAIN_GUI)
    
        add_subdirectory(3rdparty/GUI/qcustomplot)
    
    # from here on our own code, occasionally scrutinized by clang-tidy
    
    if(BORNAGAIN_TIDY)
    
        set(CMAKE_CXX_CLANG_TIDY "clang-tidy") # has effect only if compiler is clang; uses .clang-tidy
    
    # core components
    foreach(lib ${CoreComponents})
        add_subdirectory(${lib})
    endforeach()
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    if(BORNAGAIN_PYTHON)
        add_dependencies(BornAgainFit swig_runtime)
    endif()
    
    # core tests
    
    add_subdirectory(Tests/Unit/Base)
    
    add_subdirectory(Tests/Unit/Param)
    add_subdirectory(Tests/Unit/Sample)
    add_subdirectory(Tests/Unit/Device)
    add_subdirectory(Tests/Unit/Resample)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    add_subdirectory(Tests/Unit/Core)
    add_subdirectory(Tests/Unit/Numeric)
    
    add_subdirectory(Tests/Examples)
    
        add_subdirectory(GUI)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        add_subdirectory(App)
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        add_subdirectory(Tests/Unit/GUI)
    
    # TODO: split Core / GUI
    
    add_subdirectory(Tests/Functional)
    
    # documentation
    if(BORNAGAIN_USERMANUAL)
        add_subdirectory(Doc/UserManual)
    endif()
    if(CONFIGURE_MANPAGE)
        add_subdirectory(Doc/man)
    endif()
    if(CONFIGURE_DOXY)
        add_subdirectory(Doc/Doxygen)
    endif()
    
    
    # after-install message
    
    add_subdirectory(cmake)
    
    include(BornAgain/Pack)
    
    
    message(STATUS "CMake done")