Skip to content

Commit c53417b

Browse files
authored
Small helper function for logging : (#422)
* Small helper function for logging : This PR add an helper function only when `message(STATUS ...)` as the other are `backtracing` and adding the helper function add on stack. It should fix #328 #342 with a few line of code. * Formatting
1 parent 50e9b9e commit c53417b

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

cmake/CPM.cmake

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@
2828

2929
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
3030

31+
# Initialize logging prefix
32+
if(NOT CPM_INDENT)
33+
set(CPM_INDENT
34+
"CPM:"
35+
CACHE INTERNAL ""
36+
)
37+
endif()
38+
39+
if(NOT COMMAND cpm_message)
40+
function(cpm_message)
41+
message(${ARGV})
42+
endfunction()
43+
endif()
44+
3145
set(CURRENT_CPM_VERSION 1.0.0-development-version)
3246

3347
get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH)
@@ -59,7 +73,8 @@ See https://github.com/cpm-cmake/CPM.cmake for more information."
5973
endif()
6074

6175
if(CURRENT_CPM_VERSION MATCHES "development-version")
62-
message(WARNING "Your project is using an unstable development version of CPM.cmake. \
76+
message(
77+
WARNING "${CPM_INDENT} Your project is using an unstable development version of CPM.cmake. \
6378
Please update to a recent release if possible. \
6479
See https://github.com/cpm-cmake/CPM.cmake for details."
6580
)
@@ -223,22 +238,14 @@ function(cpm_package_name_and_ver_from_url url outName outVer)
223238
endif()
224239
endfunction()
225240

226-
# Initialize logging prefix
227-
if(NOT CPM_INDENT)
228-
set(CPM_INDENT
229-
"CPM:"
230-
CACHE INTERNAL ""
231-
)
232-
endif()
233-
234241
function(cpm_find_package NAME VERSION)
235242
string(REPLACE " " ";" EXTRA_ARGS "${ARGN}")
236243
find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET)
237244
if(${CPM_ARGS_NAME}_FOUND)
238245
if(DEFINED ${CPM_ARGS_NAME}_VERSION)
239246
set(VERSION ${${CPM_ARGS_NAME}_VERSION})
240247
endif()
241-
message(STATUS "${CPM_INDENT} using local package ${CPM_ARGS_NAME}@${VERSION}")
248+
cpm_message(STATUS "${CPM_INDENT} Using local package ${CPM_ARGS_NAME}@${VERSION}")
242249
CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}")
243250
set(CPM_PACKAGE_FOUND
244251
YES
@@ -309,7 +316,7 @@ function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION)
309316
if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}")
310317
message(
311318
WARNING
312-
"${CPM_INDENT} requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})."
319+
"${CPM_INDENT} Requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})."
313320
)
314321
endif()
315322
cpm_get_fetch_properties(${CPM_ARGS_NAME})
@@ -366,7 +373,7 @@ function(cpm_parse_add_package_single_arg arg outArgs)
366373
set(packageType "git")
367374
else()
368375
# Give up
369-
message(FATAL_ERROR "CPM: Can't determine package type of '${arg}'")
376+
message(FATAL_ERROR "${CPM_INDENT} Can't determine package type of '${arg}'")
370377
endif()
371378
endif()
372379

@@ -386,7 +393,7 @@ function(cpm_parse_add_package_single_arg arg outArgs)
386393
else()
387394
# We should never get here. This is an assertion and hitting it means there's a bug in the code
388395
# above. A packageType was set, but not handled by this if-else.
389-
message(FATAL_ERROR "CPM: Unsupported package type '${packageType}' of '${arg}'")
396+
message(FATAL_ERROR "${CPM_INDENT} Unsupported package type '${packageType}' of '${arg}'")
390397
endif()
391398

392399
set(${outArgs}
@@ -419,7 +426,7 @@ function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean)
419426
)
420427
if(resultGitStatus)
421428
# not supposed to happen, assume clean anyway
422-
message(WARNING "Calling git status on folder ${repoPath} failed")
429+
message(WARNING "${CPM_INDENT} Calling git status on folder ${repoPath} failed")
423430
set(${isClean}
424431
TRUE
425432
PARENT_SCOPE
@@ -464,7 +471,7 @@ endfunction()
464471
function(cpm_override_fetchcontent contentName)
465472
cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "")
466473
if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "")
467-
message(FATAL_ERROR "Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}")
474+
message(FATAL_ERROR "${CPM_INDENT} Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}")
468475
endif()
469476

470477
string(TOLOWER ${contentName} contentNameLower)
@@ -600,7 +607,7 @@ function(CPMAddPackage)
600607
if(NOT DEFINED CPM_ARGS_NAME)
601608
message(
602609
FATAL_ERROR
603-
"CPM: 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'"
610+
"${CPM_INDENT} 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'"
604611
)
605612
endif()
606613

@@ -650,7 +657,7 @@ function(CPMAddPackage)
650657
if(CPM_LOCAL_PACKAGES_ONLY)
651658
message(
652659
SEND_ERROR
653-
"CPM: ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})"
660+
"${CPM_INDENT} ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})"
654661
)
655662
endif()
656663
endif()
@@ -716,7 +723,9 @@ function(CPMAddPackage)
716723
# warn if cache has been changed since checkout
717724
cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN)
718725
if(NOT ${IS_CLEAN})
719-
message(WARNING "Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty")
726+
message(
727+
WARNING "${CPM_INDENT} Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty"
728+
)
720729
endif()
721730
endif()
722731

@@ -762,8 +771,8 @@ function(CPMAddPackage)
762771
endif()
763772
endif()
764773

765-
message(
766-
STATUS "${CPM_INDENT} adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})"
774+
cpm_message(
775+
STATUS "${CPM_INDENT} Adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})"
767776
)
768777

769778
if(NOT CPM_SKIP_FETCH)
@@ -790,7 +799,7 @@ macro(CPMGetPackage Name)
790799
if(DEFINED "CPM_DECLARATION_${Name}")
791800
CPMAddPackage(NAME ${Name})
792801
else()
793-
message(SEND_ERROR "Cannot retrieve package ${Name}: no declaration available")
802+
message(SEND_ERROR "${CPM_INDENT} Cannot retrieve package ${Name}: no declaration available")
794803
endif()
795804
endmacro()
796805

@@ -880,7 +889,7 @@ endfunction()
880889
# declares a package in FetchContent_Declare
881890
function(cpm_declare_fetch PACKAGE VERSION INFO)
882891
if(${CPM_DRY_RUN})
883-
message(STATUS "${CPM_INDENT} package not declared (dry run)")
892+
cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)")
884893
return()
885894
endif()
886895

@@ -955,7 +964,7 @@ function(cpm_fetch_package PACKAGE populated)
955964
PARENT_SCOPE
956965
)
957966
if(${CPM_DRY_RUN})
958-
message(STATUS "${CPM_INDENT} package ${PACKAGE} not fetched (dry run)")
967+
cpm_message(STATUS "${CPM_INDENT} Package ${PACKAGE} not fetched (dry run)")
959968
return()
960969
endif()
961970

0 commit comments

Comments
 (0)