Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pkgname_LOCAL variable #594

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,19 @@ function(cpm_find_package NAME VERSION)
YES
PARENT_SCOPE
)
set(${NAME}_LOCAL
YES
PARENT_SCOPE
)
else()
set(CPM_PACKAGE_FOUND
NO
PARENT_SCOPE
)
set(${NAME}_LOCAL
NO
PARENT_SCOPE
)
endif()
endfunction()

Expand Down Expand Up @@ -533,7 +541,8 @@ endfunction()
# method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload
# FetchContent calls. As these are internal cmake properties, this method should be used carefully
# and may need modification in future CMake versions. Source:
# https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake#L1152
# https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake
# #L1152
function(cpm_override_fetchcontent contentName)
cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "")
if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "")
Expand Down Expand Up @@ -731,6 +740,7 @@ function(CPMAddPackage)
)
endif()
endif()
set(${CPM_ARGS_NAME}_LOCAL NO)
endif()

CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}")
Expand Down Expand Up @@ -937,6 +947,10 @@ macro(cpm_export_variables name)
"${${name}_ADDED}"
PARENT_SCOPE
)
set(${name}_LOCAL
"${${name}_LOCAL}"
PARENT_SCOPE
)
set(CPM_LAST_PACKAGE_NAME
"${name}"
PARENT_SCOPE
Expand Down Expand Up @@ -1065,7 +1079,7 @@ function(
list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL)
endif()
if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25")
# https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM
# https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html #prop_dir:SYSTEM
list(APPEND addSubdirectoryExtraArgs SYSTEM)
endif()
if(OPTIONS)
Expand Down
58 changes: 58 additions & 0 deletions test/unit/is_local.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

include(${CPM_PATH}/testing.cmake)
include(CMakePackageConfigHelpers)

set(TEST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/is_local/)

configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/is_local/will_be_local/CMakeLists.txt.in"
"${CMAKE_CURRENT_LIST_DIR}/is_local/will_be_local/CMakeLists.txt"
INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/is_local/will_be_local/
)

execute_process(
COMMAND ${CMAKE_COMMAND} "-S${CMAKE_CURRENT_LIST_DIR}/is_local/will_be_local/"
"-B${TEST_BUILD_DIR}/will_be_local/" "-DCPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}"
RESULT_VARIABLE ret
)

assert_equal(${ret} "0")

execute_process(
COMMAND ${CMAKE_COMMAND} "--build" "${TEST_BUILD_DIR}/will_be_local/" "--verbose"
RESULT_VARIABLE ret
)

assert_equal(${ret} "0")

execute_process(
COMMAND ${CMAKE_COMMAND} "--install" "${TEST_BUILD_DIR}/will_be_local/" "--prefix"
"${TEST_BUILD_DIR}/install_dir" RESULT_VARIABLE ret
)

assert_equal(${ret} "0")

# It's better to have this layer of indirection, because we need to add
# -DCMAKE_PREFIX_PATH=binfolder/is_local/will_be_local/install_dir
configure_package_config_file(
"${CMAKE_CURRENT_LIST_DIR}/is_local/CMakeLists.txt.in"
"${CMAKE_CURRENT_LIST_DIR}/is_local/CMakeLists.txt"
INSTALL_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/is_local/
)

execute_process(
COMMAND
${CMAKE_COMMAND} "-S${CMAKE_CURRENT_LIST_DIR}/is_local/" "-B${TEST_BUILD_DIR}/"
"-DCMAKE_PREFIX_PATH=${TEST_BUILD_DIR}/install_dir/"
"-DCPM_SOURCE_CACHE=${CPM_SOURCE_CACHE_DIR}"
RESULT_VARIABLE ret
)

assert_equal(${ret} "0")

execute_process(
COMMAND ${CMAKE_COMMAND} "--build" "${TEST_BUILD_DIR}/" "--verbose" RESULT_VARIABLE ret
)

assert_equal(${ret} "0")
2 changes: 2 additions & 0 deletions test/unit/is_local/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/CMakeLists.txt
/package-lock.cmake
51 changes: 51 additions & 0 deletions test/unit/is_local/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

include(@CPM_PATH@/CPM.cmake)
include(@CPM_PATH@/testing.cmake)

set(CPM_LOCAL_PACKAGES_ONLY 1)

# Expected that project substractor is installed and can be found. In case of the test, since we do
# not want to make tests be sudo-required, we use CMAKE_INSTALL_PREFIX to a folder in which the
# project was installed via `cmake --install build_folder_substractor --prefix a_folder`

CPMAddPackage(NAME substractor VERSION 1.0.0.0)

assert_defined(substractor_ADDED)
assert_equal("${substractor_ADDED}" "") # it's broken in this case.
assert_defined(substractor_LOCAL)
assert_equal("${substractor_LOCAL}" "YES")

# just check the logic 10 times. Can be any number, but 10 is sufficient.
foreach(loop_var RANGE 1 10 1)
CPMAddPackage(NAME substractor VERSION 1.0.0.0)

assert_equal("${substractor_ADDED}" "NO")
assert_equal("${substractor_LOCAL}" "YES")
endforeach()

# now same logic but for downloaded package.
set(CPM_LOCAL_PACKAGES_ONLY 0)
set(CPM_DOWNLOAD_ONLY 1)

# testpack-fibonacci is chosen as not local target.
set(fibonachi_VERSION 2.0)
CPMAddPackage(
NAME fibonachi
VERSION ${fibonachi_VERSION}
URL https://github.com/cpm-cmake/testpack-fibonacci/archive/refs/tags/v${fibonachi_VERSION}.tar.gz
)

assert_equal("${fibonachi_ADDED}" "YES")
assert_equal("${fibonachi_LOCAL}" "NO")

foreach(loop_var RANGE 1 10 1)
CPMAddPackage(
NAME fibonachi
VERSION ${fibonachi_VERSION}
URL https://github.com/cpm-cmake/testpack-fibonacci/archive/refs/tags/v${fibonachi_VERSION}.tar.gz
)

assert_equal("${fibonachi_ADDED}" "NO")
assert_equal("${fibonachi_LOCAL}" "NO")
endforeach()
2 changes: 2 additions & 0 deletions test/unit/is_local/will_be_local/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/CMakeLists.txt
/package-lock.cmake
41 changes: 41 additions & 0 deletions test/unit/is_local/will_be_local/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(
substractor
VERSION 1.0.0.0
LANGUAGES C
)

add_library(${PROJECT_NAME} src/substractor.c include/substractor.h)
target_include_directories(
${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)

include(@CPM_PATH@/CPM.cmake)

set(packageProject_VERSION 1.11.2)
CPMAddPackage(
NAME PackageProject.cmake
VERSION ${packageProject_VERSION}
URL "https://github.com/TheLartians/PackageProject.cmake/archive/refs/tags/v${packageProject_VERSION}.zip"
)

string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
string(TOLOWER ${PROJECT_NAME}/export.h EXPORT_HEADER_LOCATION)

set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${PROJECT_VERSION})
set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION 1)

packageProject(
NAME ${PROJECT_NAME}
VERSION ${PROJECT_VERSION}
NAMESPACE ${PROJECT_NAME}
BINARY_DIR ${PROJECT_BINARY_DIR}
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
INCLUDE_DESTINATION include/${PROJECT_NAME}
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
EXPORT_HEADER "${EXPORT_HEADER_LOCATION}"
COMPATIBILITY "AnyNewerVersion"
DISABLE_VERSION_SUFFIX ON
)
6 changes: 6 additions & 0 deletions test/unit/is_local/will_be_local/include/substractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef INCLUDE_WILL_BE_LOCAL_SUBSTRACTOR_H_
#define INCLUDE_WILL_BE_LOCAL_SUBSTRACTOR_H_

long substract(int, int);

#endif // INCLUDE_WILL_BE_LOCAL_SUBSTRACTOR_H_
1 change: 1 addition & 0 deletions test/unit/is_local/will_be_local/src/substractor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
long substract(int a, int b) { return (long)a - b; }