From d21a4a7741677e1518168cd60dd4ba816d547ac0 Mon Sep 17 00:00:00 2001 From: suda-morris <362953310@qq.com> Date: Tue, 27 Oct 2026 19:10:37 +0800 Subject: [PATCH] CMake: Add option ENABLE_INSTALL install() always ran, including install(EXPORT). Embedders that add_subdirectory() TinyCBOR then write headers into the parent prefix, and CMake may refuse the export when the tinycbor target inherited foreign LINK_LIBRARIES. Route every install() through tinycbor_install so the option can skip those rules. Default is ON. --- CMakeLists.txt | 14 +++++++++++--- cmake/PackageConfig.cmake | 4 ++-- tools/cbordump/CMakeLists.txt | 2 +- tools/json2cbor/CMakeLists.txt | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 985d0434..19cebad1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,14 @@ if(WITH_FLOATING_POINT AND NOT WITH_FREESTANDING) option(BUILD_TOOLS "Compile the TinyCBOR tools" ON) endif() +# Allow disabling installation when TinyCBOR is used via add_subdirectory(). +option(ENABLE_INSTALL "Install TinyCBOR files" ON) +macro(tinycbor_install) + if(ENABLE_INSTALL) + install(${ARGN}) + endif() +endmacro() + # Include additional modules that are used unconditionally include(GNUInstallDirs) include(GenerateExportHeader) @@ -184,17 +192,17 @@ target_include_directories(tinycbor PUBLIC "$" ) -install(FILES +tinycbor_install(FILES ${PROJECT_SOURCE_DIR}/src/cbor.h ${PROJECT_BINARY_DIR}/tinycbor-version.h ${PROJECT_BINARY_DIR}/tinycbor-export.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tinycbor ) -install(FILES +tinycbor_install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tinycbor.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) -install( +tinycbor_install( TARGETS tinycbor EXPORT "${TARGETS_EXPORT_NAME}" ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # import library diff --git a/cmake/PackageConfig.cmake b/cmake/PackageConfig.cmake index 73cc9138..964a1e15 100644 --- a/cmake/PackageConfig.cmake +++ b/cmake/PackageConfig.cmake @@ -19,7 +19,7 @@ write_basic_package_version_file( COMPATIBILITY SameMinorVersion ) -install(FILES +tinycbor_install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake" COMPONENT devel @@ -27,7 +27,7 @@ install(FILES ) if (PROJECT_LIBRARIES OR PROJECT_STATIC_LIBRARIES) - install( + tinycbor_install( EXPORT "${TARGETS_EXPORT_NAME}" FILE ${TARGETS_EXPORT_NAME}.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} diff --git a/tools/cbordump/CMakeLists.txt b/tools/cbordump/CMakeLists.txt index c03fc5ef..bee2361f 100644 --- a/tools/cbordump/CMakeLists.txt +++ b/tools/cbordump/CMakeLists.txt @@ -2,5 +2,5 @@ # SPDX-License-Identifier: MIT if(NOT WIN32) tinycbor_add_executable(cbordump cbordump.c) - install(TARGETS cbordump) + tinycbor_install(TARGETS cbordump) endif() diff --git a/tools/json2cbor/CMakeLists.txt b/tools/json2cbor/CMakeLists.txt index a8731144..d02e98a8 100644 --- a/tools/json2cbor/CMakeLists.txt +++ b/tools/json2cbor/CMakeLists.txt @@ -6,5 +6,5 @@ if(LIBCJSON_FOUND) tinycbor_add_executable(json2cbor json2cbor.c) target_include_directories(json2cbor SYSTEM PUBLIC ${LIBCJSON_INCLUDE_DIRS}) target_link_libraries(json2cbor ${LIBCJSON_LIBRARIES}) - install(TARGETS json2cbor) + tinycbor_install(TARGETS json2cbor) endif()