Running `try_compile` within `Find<Package>.cmake`

For example:

add_library(Package::Target ALIAS PkgConfig::Package)
  SOURCES ${CMAKE_CURRENT_LIST_DIR}/test_package_api.f90
  LINK_LIBRARIES Package::Target

As far as I’ve tried this one fails because the target is not made available to the inner cmake project. Probably I could get the properties and feed them INCLUDE_DIRECTORIES and LINK_DIRECTORIES, but is there no better way to do this?

Is it a bug that either:

  • ALIAS targets are not resolved?
  • There is a clash when running within Find<Package>.cmake?
  • pkg-config target is not compatible?

Looking at the code in cmCoreTryCompile.cxx and the generated error it is quite weird that I am getting this error:

CMake Error at /.../TryCompile-DGCuOK/CMakeLists.txt:25 (target_link_libraries):
  Target "cmTC_aa9ca" links to:
  but the target was not found.  Possible reasons include:
    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.
CMake Error at cmake/FindPackage.cmake:33 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  CMakeLists.txt:237 (find_package)

It is a bug with ALIAS targets: