Skip to content

Commit 0851512

Browse files
committed
Defer install module inclusion until after project() in CMake
Moved inclusion of cpp-library-install.cmake and related modules to after project() is called to avoid requiring language/architecture information too early. Updated documentation and comments to clarify this behavior. Improved CPM cache setup logic in README and setup.cmake. Removed the 'install' preset from CMakePresets.json.
1 parent 40f1197 commit 0851512

6 files changed

Lines changed: 22 additions & 27 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"doctest",
66
"MSVC",
77
"mylib"
8-
]
8+
],
9+
"cmake.ignoreCMakeListsMissing": true
910
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ Use `CPMAddPackage` to fetch cpp-library directly in your `CMakeLists.txt`:
102102
cmake_minimum_required(VERSION 3.24)
103103
104104
# Setup CPM cache before project()
105-
if(NOT CPM_SOURCE_CACHE AND NOT DEFINED ENV{CPM_SOURCE_CACHE})
105+
if(PROJECT_IS_TOP_LEVEL AND NOT CPM_SOURCE_CACHE AND NOT DEFINED ENV{CPM_SOURCE_CACHE})
106106
set(CPM_SOURCE_CACHE "${CMAKE_SOURCE_DIR}/.cache/cpm" CACHE PATH "CPM source cache")
107+
message(STATUS "Setting cpm cache dir to: ${CPM_SOURCE_CACHE}")
107108
endif()
108109
include(cmake/CPM.cmake)
109110

cmake/cpp-library-install.cmake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
# - Static libraries
99
# - Shared libraries (when BUILD_SHARED_LIBS is ON)
1010
# - CMake package config generation for find_package() support
11-
12-
include(GNUInstallDirs)
13-
include(CMakePackageConfigHelpers)
11+
#
12+
# Note: GNUInstallDirs and CMakePackageConfigHelpers are included inside
13+
# _cpp_library_setup_install() to avoid requiring project() to be called
14+
# when this module is loaded.
1415

1516
# System packages that don't require version constraints in find_dependency()
1617
# These are commonly available system libraries where version requirements are typically not specified.
@@ -337,6 +338,10 @@ function(_cpp_library_setup_install)
337338

338339
cmake_parse_arguments(ARG "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
339340

341+
# Include required CMake modules (deferred from top-level to avoid requiring project() before include)
342+
include(GNUInstallDirs)
343+
include(CMakePackageConfigHelpers)
344+
340345
# Validate required arguments
341346
if(NOT ARG_NAME)
342347
message(FATAL_ERROR "_cpp_library_setup_install: NAME is required")

cpp-library.cmake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ function(cpp_library_enable_dependency_tracking)
3535
endfunction()
3636

3737
# Include all the component modules
38-
# Note: CTest is NOT included here because it requires project() to be called first.
39-
# It will be included in cpp_library_setup() which is called after project().
38+
# Note: Some modules (CTest, cpp-library-install) require project() to be called first
39+
# because they need language/architecture information. These are included in
40+
# cpp_library_setup() which is called after project().
4041
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-setup.cmake")
4142
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-testing.cmake")
4243
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-docs.cmake")
43-
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-install.cmake")
4444
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-ci.cmake")
4545

4646
# Creates test or example executables and registers them with CTest.
@@ -165,8 +165,10 @@ function(cpp_library_setup)
165165
endif()
166166
set(ARG_NAME "${PROJECT_NAME}")
167167

168-
# Include CTest for testing support (must be after project())
168+
# Include modules that require project() to be called first
169+
# (CTest and GNUInstallDirs need language/architecture information)
169170
include(CTest)
171+
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-install.cmake")
170172

171173
# Calculate clean name (without namespace prefix) for target alias
172174
# If PROJECT_NAME starts with NAMESPACE-, strip it; otherwise use PROJECT_NAME as-is

setup.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ file(WRITE "${PROJECT_DIR}/CMakeLists.txt"
315315
"cmake_minimum_required(VERSION 3.24)
316316
317317
# Setup CPM cache before project()
318-
if(NOT CPM_SOURCE_CACHE AND NOT DEFINED ENV{CPM_SOURCE_CACHE})
319-
set(CPM_SOURCE_CACHE \"\${CMAKE_SOURCE_DIR}/.cache/cpm\" CACHE PATH \"CPM source cache\")
318+
if(PROJECT_IS_TOP_LEVEL AND NOT CPM_SOURCE_CACHE AND NOT DEFINED ENV{CPM_SOURCE_CACHE})
319+
set(CPM_SOURCE_CACHE "${CMAKE_SOURCE_DIR}/.cache/cpm" CACHE PATH "CPM source cache")
320+
message(STATUS "Setting cpm cache dir to: ${CPM_SOURCE_CACHE}")
320321
endif()
321322
include(cmake/CPM.cmake)
322323

templates/CMakePresets.json

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,14 @@
6868
"CMAKE_CXX_EXTENSIONS": "OFF",
6969
"CPP_LIBRARY_FORCE_INIT": "ON"
7070
}
71-
},
72-
{
73-
"name": "install",
74-
"displayName": "Local Install Test",
75-
"description": "Configuration for testing installation locally (installs to build/install/prefix)",
76-
"binaryDir": "${sourceDir}/build/install",
77-
"generator": "Ninja",
78-
"cacheVariables": {
79-
"CMAKE_BUILD_TYPE": "Release",
80-
"BUILD_TESTING": "OFF",
81-
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
82-
"CMAKE_CXX_EXTENSIONS": "OFF",
83-
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/install/prefix"
84-
}
8571
}
8672
],
8773
"buildPresets": [
8874
{ "name": "default", "displayName": "Default Build", "configurePreset": "default" },
8975
{ "name": "test", "displayName": "Build Tests", "configurePreset": "test" },
9076
{ "name": "docs", "displayName": "Build Docs", "configurePreset": "docs", "targets": "docs" },
9177
{ "name": "clang-tidy", "displayName": "Build with Clang-Tidy", "configurePreset": "clang-tidy" },
92-
{ "name": "init", "displayName": "Initialize Templates", "configurePreset": "init" },
93-
{ "name": "install", "displayName": "Build for Local Install", "configurePreset": "install" }
78+
{ "name": "init", "displayName": "Initialize Templates", "configurePreset": "init" }
9479
],
9580
"testPresets": [
9681
{ "name": "test", "displayName": "Run All Tests", "configurePreset": "test", "output": { "outputOnFailure": true } },

0 commit comments

Comments
 (0)