cpm icon indicating copy to clipboard operation
cpm copied to clipboard

Building CPM modules independently from parent

Open SteffenL opened this issue 10 years ago • 1 comments

I have adapted a C++ library I have made, for CPM. The library/module builds without issues when added to a (top-level) parent; however, attempting to build the module by itself yields in error messages, e.g. the following when building using Visual Studio 2013:

[snip]\cpm-packages\tests\ghost-git-repo\main.cpp(2): fatal error C1083: Cannot open include file: 'cpm-test-01/module.hpp': No such file or directory [[snip].vcxproj]

For some reason, CPM's tests are added to my VS project so that it attempts bulding the tests before my own code.

My CMakeLists.txt file currently looks mostly like this:

cmake_minimum_required(VERSION 3.2 FATAL_ERROR)


#-----------------------------------------------------------------------
# CPM configuration
#-----------------------------------------------------------------------

set(CPM_MODULE_NAME "my_library")
set(CPM_LIB_TARGET_NAME ${CPM_MODULE_NAME})

if ((DEFINED CPM_DIR) AND (DEFINED CPM_UNIQUE_ID) AND (DEFINED CPM_TARGET_NAME))
    set(CPM_LIB_TARGET_NAME ${CPM_TARGET_NAME})
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CPM_DIR})
    include(CPM)
else()
    set(CPM_DIR "${CMAKE_CURRENT_BINARY_DIR}/cpm-packages" CACHE TYPE STRING)
    find_package(Git)
    if(NOT GIT_FOUND)
        message(FATAL_ERROR "CPM requires Git.")
    endif()
    if (NOT EXISTS ${CPM_DIR}/CPM.cmake)
        message(STATUS "Cloning repo (https://github.com/iauns/cpm)")
        execute_process(
        COMMAND "${GIT_EXECUTABLE}" clone https://github.com/iauns/cpm ${CPM_DIR}
        RESULT_VARIABLE error_code
        OUTPUT_QUIET ERROR_QUIET)
        if(error_code)
            message(FATAL_ERROR "CPM failed to get the hash for HEAD")
        endif()
    endif()
    include(${CPM_DIR}/CPM.cmake)
endif()

# Include CPM modules or externals here (with CPM_AddModule).

CPM_AddModule("dep_name"
  GIT_REPOSITORY "repo_url"
  GIT_TAG "tag_name")

CPM_InitModule(${CPM_MODULE_NAME})
CPM_ExportAdditionalLibraryTarget(${CPM_LIB_TARGET_NAME})


#-----------------------------------------------------------------------
# Source
#-----------------------------------------------------------------------

file(GLOB_RECURSE Sources src
  "*.cpp"
)


#-----------------------------------------------------------------------
# Library setup
#-----------------------------------------------------------------------

add_library(${CPM_LIB_TARGET_NAME} SHARED ${Sources})
target_link_libraries(${CPM_LIB_TARGET_NAME} ${CPM_LIBRARIES})

Thank you for any assistance you can provide.

SteffenL avatar Aug 04 '15 17:08 SteffenL

I finally figured out that this issue only exists when the current working directory (when running cmake and the build command) is inside the module's root directory).. So I guess CMake automatically picks up the tests in module root/build dir/cpm-packages/tests. I don't know why this doesn't happen when there is a parent project.

My "solution" so far is to not build inside the module's directory whenever I build the module separately from a parent.

Example:

projects
  ├─ my_module/CMakeLists.txt
  └─ my_module-build

Is this the intended way to go about it?

SteffenL avatar Aug 05 '15 02:08 SteffenL