# The umbrella-target fixture: the project carries, besides the extension,
#
# * a decoy target that is part of ALL and attached to nothing -- the stand-in for
#   the test executables and benchmarks a real project compiles into `all` and a
#   wheel never ships. A build restricted to cmake_pip_target must not run it.
# * a target declared in a SUBDIRECTORY and attached to the umbrella with the plain
#   add_dependencies() command -- the cross-directory contract cmake-pip promises.
# * a generated file, produced by a custom target attached to the umbrella and
#   declared through python_package_add_file(): the umbrella build materialises it,
#   the component install stages it into the distribution.

cmake_minimum_required(VERSION 3.21)

enable_testing()

project(TestCMakePIPUmbrella)

option(FIXTURE_DECLARE_PACKAGE "declare the extension to cmake-pip" ON)

find_package(Python3 COMPONENTS Interpreter Development.Module REQUIRED)
find_package(cmakepip REQUIRED)

python3_add_library(cmake_test MODULE WITH_SOABI
    src/python_header.hpp
    src/python_dummy_ext.cpp)

if(FIXTURE_DECLARE_PACKAGE)
  python_package_add_target(TARGET cmake_test
                            PYTHON_PACKAGE umbrella_test
                            DESTINATION ext_umbrella_only)

  # A file that exists only if its producing target was built: the custom target is
  # NOT in ALL, its only route into a build is the umbrella. install(FILES) at
  # staging time would fail had the target not run.
  add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/generated_data.txt"
    COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_BINARY_DIR}/generated_data.txt"
    COMMENT "generating the data file of the package")
  add_custom_target(generated_data
    DEPENDS "${CMAKE_BINARY_DIR}/generated_data.txt")
  add_dependencies(cmake_pip_target generated_data)

  python_package_add_file(FILES "${CMAKE_BINARY_DIR}/generated_data.txt"
                          PYTHON_PACKAGE umbrella_test
                          DESTINATION ext_umbrella_only)
endif()

# in ALL, attached to nothing: a build restricted to cmake_pip_target must not run it
add_custom_target(decoy_target ALL
  COMMAND ${CMAKE_COMMAND} -E touch "${CMAKE_BINARY_DIR}/decoy_built.stamp"
  COMMENT "decoy: stands in for the test executables a wheel does not need")

add_subdirectory(extra)
