Skip to content
Snippets Groups Projects
CMakeLists.txt 4.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • vonopr's avatar
    vonopr committed
    cmake_minimum_required(VERSION 3.14)
    project(exple_cmake_build CXX)
    
    
    Vladimir Onoprienko's avatar
    Vladimir Onoprienko committed
    option(DOWNLOAD_DEPS "Enable automatic download of dependencies" ON)
    
    vonopr's avatar
    vonopr committed
    
    
    set(COMMON_LIB_URL "http://tesla.parallel.ru/vonopr/explelibx-common.git"
      CACHE STRING "Git repository URL of the downloaded 'explelibx-common' dependency; used if DOWNLOAD_DEPS enabled")
    set(COMMON_LIB_TAG "e45b60782a6414125c7300d9b7a77d34a8404d6e"
      CACHE STRING "Specify the branch name, tag name or commit hash of the downloaded 'explelibx-common' dependency; used if DOWNLOAD_DEPS enabled")
    
    set(GRID_LIB_URL "http://tesla.parallel.ru/vonopr/explelibx-mgrid.git"
      CACHE STRING "Git repository URL of the downloaded 'explelibx-mgrid' dependency; used if DOWNLOAD_DEPS enabled")
    set(GRID_LIB_TAG "c345859b9ebca2227e3b3faea4cc2380fdbb054f"
      CACHE STRING "Specify the branch name, tag name or commit hash of the downloaded 'explelibx-mgrid' dependency; used if DOWNLOAD_DEPS enabled")
    
    
    # download dependency 'explelibx-common' or find it locally
    
    vonopr's avatar
    vonopr committed
    if (DOWNLOAD_DEPS)
      include(FetchContent)
      FetchContent_Declare(
    
        GIT_REPOSITORY "${COMMON_LIB_URL}"
        GIT_TAG "${COMMON_LIB_TAG}")
    
      FetchContent_MakeAvailable(explelibx-common)
      set(COMMON_DIR "${explelibx-common_SOURCE_DIR}")
    
    vonopr's avatar
    vonopr committed
    else()
      if (NOT DEFINED COMMON_DIR)
        file(REAL_PATH "${PROJECT_SOURCE_DIR}/../../explelibx-common" COMMON_DIR)
      endif()
      if (NOT EXISTS ${COMMON_DIR})
        message(WARNING "Failed to locate 'explelibx-common' project. Variable COMMON_DIR points to non-existent directory.\
        Set COMMON_DIR to point a valid directory or enable dependencies download with '-DDOWNLOAD_DEPS=ON' (e.g. cmake . -DDOWNLOAD_DEPS=ON)")
      endif()
      message("-- Path to 'explelibx-common' project")
      message("    COMMON_DIR: ${COMMON_DIR}")
    endif()
    
    
    # download dependency 'explelibx-mgrid' or find it locally
    
    vonopr's avatar
    vonopr committed
    if (DOWNLOAD_DEPS)
      include(FetchContent)
      FetchContent_Declare(
    
        GIT_REPOSITORY "${GRID_LIB_URL}"
        GIT_TAG "${GRID_LIB_TAG}")
    
      FetchContent_MakeAvailable(explelibx-mgrid)
      set(GRID_DIR "${explelibx-mgrid_SOURCE_DIR}")
    
    vonopr's avatar
    vonopr committed
    else()
      if (NOT DEFINED GRID_DIR)
        file(REAL_PATH "${PROJECT_SOURCE_DIR}/../../explelibx-mgrid" GRID_DIR)
      endif()
      if (NOT EXISTS ${GRID_DIR})
        message(WARNING "Failed to locate 'explelibx-mgrid' project. Variable GRID_DIR points to non-existent directory.\
        Set GRID_DIR to point a valid directory or enable dependencies download with '-DDOWNLOAD_DEPS=ON' (e.g. cmake . -DDOWNLOAD_DEPS=ON)")
      endif()
      message("-- Path to 'explelibx-mgrid' project")
      message("    GRID_DIR: ${GRID_DIR}")
    endif()
    
    # set paths to 'explelibx-common' sources
    set(COMMON_SOURCE_DIR "${COMMON_DIR}/explelibx-common")
    set(COMMON_HEADER_DIR "${COMMON_DIR}/explelibx-common")
    set(COMMON_SOURCES exple-add.cpp)
    set(COMMON_HEADERS exple-add.h)
    list(TRANSFORM COMMON_SOURCES PREPEND "${COMMON_SOURCE_DIR}/")
    list(TRANSFORM COMMON_HEADERS PREPEND "${COMMON_HEADER_DIR}/")
    
    # set paths to 'explelibx-mgrid' sources
    set(GRID_SOURCE_DIR "${GRID_DIR}/explelibx-mgrid")
    set(GRID_HEADER_DIR "${GRID_DIR}/explelibx-mgrid")
    set(GRID_SOURCES exple-mul.cpp)
    set(GRID_HEADERS exple-mul.h)
    list(TRANSFORM GRID_SOURCES PREPEND "${GRID_SOURCE_DIR}/")
    list(TRANSFORM GRID_HEADERS PREPEND "${GRID_HEADER_DIR}/")
    
    # set paths to current project's sources
    set(SOURCE_DIR "${PROJECT_SOURCE_DIR}/exple-cmake-build")
    set(HEADER_DIR "${PROJECT_SOURCE_DIR}/exple-cmake-build")
    set(CURRENT_SOURCES main.cpp)
    set(CURRENT_HEADERS exple-expr.hpp)
    list(TRANSFORM CURRENT_SOURCES PREPEND "${SOURCE_DIR}/")
    list(TRANSFORM CURRENT_HEADERS PREPEND "${HEADER_DIR}/")
    
    # set targets
    
    add_executable(exple-add-numbers
    
    vonopr's avatar
    vonopr committed
      "${CURRENT_SOURCES}" 
      "${CURRENT_HEADERS}"
      "${COMMON_SOURCES}" 
      "${COMMON_HEADERS}" 
      "${GRID_SOURCES}"
      "${GRID_HEADERS}")  # headers should be explicitly added to the target to be shown in IDEs after mentioned in source_group()
                         # if using Visual Studio switch to project's target representation
    
    target_include_directories(exple-add-numbers PRIVATE
    
    vonopr's avatar
    vonopr committed
      "${HEADER_DIR}"
      "${COMMON_HEADER_DIR}"
      "${GRID_HEADER_DIR}")
    
    
    Vladimir Onoprienko's avatar
    Vladimir Onoprienko committed
    add_custom_command(TARGET exple-add-numbers POST_BUILD
    
    Vladimir Onoprienko's avatar
    Vladimir Onoprienko committed
      COMMAND ${CMAKE_COMMAND} -E copy_directory
    
    Vladimir Onoprienko's avatar
    Vladimir Onoprienko committed
      "${PROJECT_SOURCE_DIR}/exple-cmake-build/config-examples" "$<TARGET_FILE_DIR:exple-add-numbers>/config-examples")
    
    vonopr's avatar
    vonopr committed
    
    # set groups for better source representation in IDEs
    source_group("Common\\Sources" FILES "${COMMON_SOURCES}")
    source_group("Common\\Headers" FILES "${COMMON_HEADERS}")
    source_group("Grid\\Sources" FILES "${GRID_SOURCES}")
    source_group("Grid\\Headers" FILES "${GRID_HEADERS}")
    source_group("CMake-Build\\Sources" FILES "${CURRENT_SOURCES}")
    source_group("CMake-Build\\Headers" FILES "${CURRENT_HEADERS}")