# Copyright 2026, Raffi Enficiaud.
#
# An extension linking a co-located shared library: the case that CANNOT work without
# the post-install relocation. python_package_add_target walks the link dependencies
# and installs dep_library next to the extension, but CMake strips the build rpath at
# install -- without the relocation rewriting it to $ORIGIN, the import of the
# installed extension fails to locate libdep_library.

cmake_minimum_required(VERSION 3.21)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

project(TestCMakePIPSharedDependency)

# Development.Module: headers and platform-correct linking for extension modules;
# Python3_EXECUTABLE is pinned by cmake-pip to the driving interpreter.
find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)

find_package(cmakepip REQUIRED)

# the shared library the extension depends on
add_library(dep_library SHARED src/dep_library.cpp)

# the extension itself; WITH_SOABI appends the canonical platform suffix
python3_add_library(ext_with_dep MODULE WITH_SOABI
    src/python_header.hpp
    src/python_dep_ext.cpp)
target_link_libraries(ext_with_dep PRIVATE dep_library)

# dep_library is picked up by the dependency walk and installed next to the extension;
# the runtime dependency set and the relocation are emitted by cmake-pip at the end of
# the configure phase.
python_package_add_target(TARGET ext_with_dep
                          PYTHON_PACKAGE dep_test
                          DESTINATION ext_with_shared_dependency)
