From 2c1efec642a74f66ff26e842b40ffb715de2e464 Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Wed, 25 Oct 2023 18:23:19 +0200 Subject: [PATCH 1/2] Explicit and smart CMake target --- libmamba/CMakeLists.txt | 20 +++++++++++++++----- libmamba/libmambaConfig.cmake.in | 19 +++++++++++++------ libmamba/tests/CMakeLists.txt | 10 ++-------- libmambapy/CMakeLists.txt | 6 +++--- mamba_package/CMakeLists.txt | 22 +++------------------- micromamba/CMakeLists.txt | 6 +++--- 6 files changed, 39 insertions(+), 44 deletions(-) diff --git a/libmamba/CMakeLists.txt b/libmamba/CMakeLists.txt index 65cf867e24..8dc442bd38 100644 --- a/libmamba/CMakeLists.txt +++ b/libmamba/CMakeLists.txt @@ -600,6 +600,7 @@ macro(libmamba_create_target target_name linkage output_name) endif () list(APPEND libmamba_targets ${target_name}) + add_library(mamba::${target_name} ALIAS ${target_name}) endmacro() @@ -607,7 +608,7 @@ set(libmamba_targets "") if (BUILD_SHARED) message(STATUS "Adding shared libmamba target") - libmamba_create_target(libmamba SHARED libmamba) + libmamba_create_target(libmamba-dyn SHARED libmamba) endif () if (BUILD_STATIC) @@ -620,9 +621,15 @@ if (BUILD_STATIC) else () libmamba_create_target(libmamba-static STATIC libmamba) endif () -endif () +endif() -if (NOT (BUILD_SHARED OR BUILD_STATIC)) +if(BUILD_SHARED_LIBS AND BUILD_SHARED) + add_library(mamba::libmamba ALIAS libmamba-dyn) +elseif(BUILD_STATIC) + add_library(mamba::libmamba ALIAS libmamba-static) +elseif(BUILD_SHARED) + add_library(mamba::libmamba ALIAS libmamba-dyn) +else() message(FATAL_ERROR "Select at least a build variant for libmamba") endif () @@ -652,8 +659,11 @@ install(DIRECTORY "${LIBMAMBA_INCLUDE_DIR}/" PATTERN "*.h") # Makes the project importable from the build directory -export(EXPORT ${PROJECT_NAME}-targets - FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake") +export( + EXPORT ${PROJECT_NAME}-targets + FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake" + NAMESPACE mamba:: +) # Configure 'mambaConfig.cmake' for a build tree set(MAMBA_CONFIG_CODE "####### Expanded from \@MAMBA_CONFIG_CODE\@ #######\n") diff --git a/libmamba/libmambaConfig.cmake.in b/libmamba/libmambaConfig.cmake.in index e927a13353..d5fab88a51 100644 --- a/libmamba/libmambaConfig.cmake.in +++ b/libmamba/libmambaConfig.cmake.in @@ -34,13 +34,20 @@ find_dependency(yaml-cpp) if(NOT (TARGET libmamba OR TARGET libmamba-static)) include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") - if (TARGET libmamba-static) + if (TARGET mamba::libmamba-dyn) + get_target_property(@PROJECT_NAME@_INCLUDE_DIR libmamba INTERFACE_INCLUDE_DIRECTORIES) + get_target_property(@PROJECT_NAME@_LIBRARY libmamba LOCATION) + endif() + + if (TARGET mamba::libmamba-static) get_target_property(@PROJECT_NAME@_INCLUDE_DIR libmamba-static INTERFACE_INCLUDE_DIRECTORIES) get_target_property(@PROJECT_NAME@_STATIC_LIBRARY libmamba-static LOCATION) - endif () + endif() + + if(BUILD_SHARED_LIBS AND TARGET mamba::libmamba-dyn) + add_library(mamba::libmamba ALIAS mamba::libmamba-dyn) + else() + add_library(mamba::libmamba ALIAS mamba::libmamba-static) + endif() - if (TARGET libmamba) - get_target_property(@PROJECT_NAME@_INCLUDE_DIR libmamba INTERFACE_INCLUDE_DIRECTORIES) - get_target_property(@PROJECT_NAME@_LIBRARY libmamba LOCATION) - endif () endif() diff --git a/libmamba/tests/CMakeLists.txt b/libmamba/tests/CMakeLists.txt index d21f9c3560..8870f3af58 100644 --- a/libmamba/tests/CMakeLists.txt +++ b/libmamba/tests/CMakeLists.txt @@ -1,14 +1,8 @@ cmake_minimum_required(VERSION 3.16) -if (BUILD_SHARED) - set(libmamba_target "libmamba") -else () - set(libmamba_target "libmamba-static") -endif() - add_executable(testing_libmamba_lock libmamba_lock/lock.cpp) -target_link_libraries(testing_libmamba_lock PUBLIC ${libmamba_target}) +target_link_libraries(testing_libmamba_lock PUBLIC mamba::libmamba) target_compile_features(testing_libmamba_lock PUBLIC cxx_std_17) @@ -97,7 +91,7 @@ find_package(Threads REQUIRED) target_link_libraries( test_libmamba PUBLIC - ${libmamba_target} + mamba::libmamba reproc reproc++ PRIVATE diff --git a/libmambapy/CMakeLists.txt b/libmambapy/CMakeLists.txt index 760dd73217..cdb641d4b6 100644 --- a/libmambapy/CMakeLists.txt +++ b/libmambapy/CMakeLists.txt @@ -11,9 +11,9 @@ cmake_policy(SET CMP0057 NEW) project(libmambapy) -if(NOT TARGET libmamba) +if(NOT TARGET mamba::libmamba) find_package(libmamba REQUIRED) -endif () +endif() find_package(Python COMPONENTS Interpreter Development) find_package(pybind11 REQUIRED) @@ -25,7 +25,7 @@ pybind11_add_module(bindings mamba_target_add_compile_warnings(bindings WARNING_AS_ERROR ${MAMBA_WARNING_AS_ERROR}) -target_link_libraries(bindings PUBLIC pybind11::pybind11 libmamba) +target_link_libraries(bindings PRIVATE pybind11::pybind11 mamba::libmamba) set_property(TARGET bindings PROPERTY CXX_STANDARD 17) install(TARGETS bindings diff --git a/mamba_package/CMakeLists.txt b/mamba_package/CMakeLists.txt index c61e9fdef9..9724ebe231 100644 --- a/mamba_package/CMakeLists.txt +++ b/mamba_package/CMakeLists.txt @@ -10,18 +10,6 @@ cmake_policy(SET CMP0077 NEW) # Introduced in cmake 3.13 project(mamba-package) -# Build options -# ============= - -if (BUILD_STATIC AND NOT BUILD_SHARED) - set(MAMBA_PACKAGE_LINKAGE "STATIC" CACHE STRING "mamba-package linkage against libraries") -else () - set(MAMBA_PACKAGE_LINKAGE "DYNAMIC" CACHE STRING "mamba-package linkage against libraries") -endif () - -string(TOUPPER "${MAMBA_PACKAGE_LINKAGE}" linkage_upper) -message(STATUS "'mamba-package' linkage: ${MAMBA_PACKAGE_LINKAGE}") - # Source files # ============ @@ -37,7 +25,7 @@ set(MAMBA_PACKAGE_HEADERS # Dependencies # ============ -if(NOT (TARGET libmamba OR TARGET libmamba-static)) +if(NOT TARGET mamba::libmamba) find_package(libmamba REQUIRED) endif () @@ -47,12 +35,8 @@ endif () add_executable(mamba-package ${MAMBA_PACKAGE_SRCS} ${MAMBA_PACKAGE_HEADERS}) mamba_target_add_compile_warnings(mamba-package WARNING_AS_ERROR ${MAMBA_WARNING_AS_ERROR}) -if (${MAMBA_PACKAGE_LINKAGE} STREQUAL "STATIC") - target_link_libraries(mamba-package PRIVATE libmamba-static) -else () - target_link_libraries(mamba-package PRIVATE libmamba) -endif () +target_link_libraries(mamba-package PRIVATE mamba::libmamba) -set_property(TARGET mamba-package PROPERTY CXX_STANDARD 17) +set_target_properties(mamba-package PROPERTIES CXX_STANDARD 17) install(TARGETS mamba-package) diff --git a/micromamba/CMakeLists.txt b/micromamba/CMakeLists.txt index 950b98ea02..5938960411 100644 --- a/micromamba/CMakeLists.txt +++ b/micromamba/CMakeLists.txt @@ -65,7 +65,7 @@ add_executable(micromamba ${MICROMAMBA_SRCS} ${MICROMAMBA_HEADERS}) mamba_target_add_compile_warnings(micromamba WARNING_AS_ERROR ${MAMBA_WARNING_AS_ERROR}) mamba_target_set_lto(micromamba MODE ${MAMBA_LTO}) -if(NOT (TARGET libmamba OR TARGET libmamba-static)) +if(NOT (TARGET mamba::libmamba-dyn OR TARGET mamba::libmamba-static)) find_package(libmamba REQUIRED) endif () @@ -73,9 +73,9 @@ string(TOUPPER "${MICROMAMBA_LINKAGE}" linkage_upper) message(STATUS "Micromamba linkage: ${MICROMAMBA_LINKAGE}") if (${MICROMAMBA_LINKAGE} STREQUAL "STATIC") - set(libmamba_target "libmamba-static") + set(libmamba_target mamba::libmamba-static) else () - set(libmamba_target "libmamba") + set(libmamba_target mamba::libmamba-dyn) endif () find_package(Threads REQUIRED) From 0a9c023e36b7c0e3e777dc5d3ade7b777a4ee33e Mon Sep 17 00:00:00 2001 From: AntoinePrv Date: Tue, 31 Oct 2023 10:29:19 +0100 Subject: [PATCH 2/2] Only expose explicit CMake targets --- libmamba/libmambaConfig.cmake.in | 7 ------- 1 file changed, 7 deletions(-) diff --git a/libmamba/libmambaConfig.cmake.in b/libmamba/libmambaConfig.cmake.in index d5fab88a51..35834de762 100644 --- a/libmamba/libmambaConfig.cmake.in +++ b/libmamba/libmambaConfig.cmake.in @@ -43,11 +43,4 @@ if(NOT (TARGET libmamba OR TARGET libmamba-static)) get_target_property(@PROJECT_NAME@_INCLUDE_DIR libmamba-static INTERFACE_INCLUDE_DIRECTORIES) get_target_property(@PROJECT_NAME@_STATIC_LIBRARY libmamba-static LOCATION) endif() - - if(BUILD_SHARED_LIBS AND TARGET mamba::libmamba-dyn) - add_library(mamba::libmamba ALIAS mamba::libmamba-dyn) - else() - add_library(mamba::libmamba ALIAS mamba::libmamba-static) - endif() - endif()