cmake_minimum_required(VERSION 3.13)
project(pkgconf_cmake_generate_pc_test VERSION 1.2.3 DESCRIPTION "generated pc test" LANGUAGES C)

find_package(pkgconf REQUIRED CONFIG)

add_library(pkgconf::publicdep INTERFACE IMPORTED)
set_target_properties(pkgconf::publicdep PROPERTIES PKGCONF_MODULES "publicdep >= 1")

add_library(pkgconf::privatedep INTERFACE IMPORTED)
set_target_properties(pkgconf::privatedep PROPERTIES PKGCONF_MODULES "privatedep")

add_library(sample STATIC sample.c)
set_target_properties(sample PROPERTIES
  OUTPUT_NAME sampleout
  VERSION 2.4.6
  DESCRIPTION "sample target")
target_link_libraries(sample PUBLIC pkgconf::publicdep PRIVATE pkgconf::privatedep)

set(_generated_pc "${CMAKE_CURRENT_BINARY_DIR}/sample.pc")
pkgconf_generate_pc(sample
  NAME sample
  DESCRIPTION "Sample library"
  URL "https://example.invalid/sample"
  LICENSE "MIT"
  MAINTAINER "Pkgconf Maintainers <pkgconf@example.invalid>"
  COPYRIGHT "Copyright 2026 pkgconf"
  RELOCATABLE
  VARIABLES "customdir=\${prefix}/custom"
  CONFLICTS "oldsample < 1"
  PROVIDES "sample-virtual = 2.4.6"
  CFLAGS "-DSAMPLE=1"
  CFLAGS_PRIVATE "-DSAMPLE_PRIVATE=1"
  LIBS_PRIVATE "-lm"
  OUTPUT "${_generated_pc}")

file(READ "${_generated_pc}" _pc)
string(FIND "${_pc}"
  "# Generated by pkgconf's CMake support (pkgconf_generate_pc()); do not edit.\n\nprefix="
  _found)
if(_found EQUAL -1)
  message(FATAL_ERROR "generated pc file has an invalid header:\n${_pc}")
endif()
foreach(_expected
    "prefix=\${pcfiledir}/../../"
    "exec_prefix=\${prefix}"
    "libdir=\${exec_prefix}/lib"
    "includedir=\${prefix}/include"
    "customdir=\${prefix}/custom"
    "Name: sample"
    "Description: Sample library"
    "URL: https://example.invalid/sample"
    "Version: 2.4.6"
    "Maintainer: Pkgconf Maintainers <pkgconf@example.invalid>"
    "License: MIT"
    "Copyright: Copyright 2026 pkgconf"
    "Requires: publicdep >= 1"
    "Requires.private: privatedep"
    "Conflicts: oldsample < 1"
    "Provides: sample-virtual = 2.4.6"
    "Cflags: -I\${includedir} -DSAMPLE=1"
    "Cflags.private: -DSAMPLE_PRIVATE=1"
    "Libs: -L\${libdir} -lsampleout"
    "Libs.private: -lm")
  string(FIND "${_pc}" "${_expected}" _found)
  if(_found EQUAL -1)
    message(FATAL_ERROR "generated pc file is missing expected text: ${_expected}\n---\n${_pc}")
  endif()
endforeach()

add_library(header_only INTERFACE)
set(_header_only_pc "${CMAKE_CURRENT_BINARY_DIR}/header-only.pc")
pkgconf_generate_pc(header_only
  NAME header-only
  VERSION 1
  NO_DEFAULT_INCLUDE
  OUTPUT "${_header_only_pc}")

file(READ "${_header_only_pc}" _header_only)
if(_header_only MATCHES "\nLibs:")
  message(FATAL_ERROR "header-only pc file unexpectedly contains Libs:\n${_header_only}")
endif()
if(_header_only MATCHES "\nCflags:")
  message(FATAL_ERROR "NO_DEFAULT_INCLUDE pc file unexpectedly contains Cflags:\n${_header_only}")
endif()

set(_no_default_libs_pc "${CMAKE_CURRENT_BINARY_DIR}/no-default-libs.pc")
pkgconf_generate_pc(sample
  NAME no-default-libs
  VERSION 1
  NO_DEFAULT_LIBS
  LIBS "-Wl,--as-needed"
  OUTPUT "${_no_default_libs_pc}")

file(READ "${_no_default_libs_pc}" _no_default_libs)
if(_no_default_libs MATCHES "-L[$][{]libdir[}]" OR _no_default_libs MATCHES "-lsampleout")
  message(FATAL_ERROR "NO_DEFAULT_LIBS pc file contains default library flags:\n${_no_default_libs}")
endif()
string(FIND "${_no_default_libs}" "Libs: -Wl,--as-needed" _found)
if(_found EQUAL -1)
  message(FATAL_ERROR "NO_DEFAULT_LIBS pc file is missing explicit LIBS:\n${_no_default_libs}")
endif()

set(_no_infer_pc "${CMAKE_CURRENT_BINARY_DIR}/no-infer.pc")
pkgconf_generate_pc(sample
  NAME no-infer
  VERSION 1
  NO_INFER_REQUIRES
  REQUIRES explicit-public
  REQUIRES_PRIVATE explicit-private
  OUTPUT "${_no_infer_pc}")

file(READ "${_no_infer_pc}" _no_infer)
foreach(_unexpected "publicdep" "privatedep")
  string(FIND "${_no_infer}" "${_unexpected}" _found)
  if(NOT _found EQUAL -1)
    message(FATAL_ERROR "NO_INFER_REQUIRES pc file contains inferred dependency ${_unexpected}:\n${_no_infer}")
  endif()
endforeach()
foreach(_expected
    "Requires: explicit-public"
    "Requires.private: explicit-private")
  string(FIND "${_no_infer}" "${_expected}" _found)
  if(_found EQUAL -1)
    message(FATAL_ERROR "NO_INFER_REQUIRES pc file is missing explicit dependency ${_expected}:\n${_no_infer}")
  endif()
endforeach()
