Skip to content

Conversation

jvdp1
Copy link
Member

@jvdp1 jvdp1 commented Oct 14, 2025

Based on @perazz suggestion in jvdp1/stdlib#11

As statded in jvdp1/stdlib#11

Summary

Add if(NOT TARGET) guards around add_library calls to prevent conflicts when the target already exists.

Context

After PR fortran-lang/stdlib#1033, stdlib automatically creates namespaced ALIAS targets (e.g., fortran_stdlib::stdlib). The example code in Findtest-drive.cmake that users copy for integrating stdlib via FetchContent was trying to create these same targets, causing CMake errors.

Changes

  • Added guards in three locations (pkgconf, subproject, fetch methods)
  • Prevents duplicate target creation while maintaining backward compatibility

Testing

This allows the FetchContent pattern to work correctly with stdlib after #1033 without requiring users to manually remove the add_library lines.

Fixes the issue described in fortran-lang/stdlib#1036

@jvdp1 jvdp1 requested a review from perazz October 14, 2025 19:04
Copy link
Member

@perazz perazz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @jvdp1.

Copy link
Member

@awvwgk awvwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is the right place for this guard clause, especially for the subproject and fetch content include we don't want to fetch and include targets when the actual target is already available.

The finder already has this break clause

if(TARGET "${_lib}::${_lib}")
break()
endif()

Maybe we can run this before entering the loop?

@jvdp1
Copy link
Member Author

jvdp1 commented Oct 15, 2025

I don't think that is the right place for this guard clause, especially for the subproject and fetch content include we don't want to fetch and include targets when the actual target is already available.

The target does not exist before fetching it, but is created during fetching (?) with this rule.
This PR is due to the issue in fortran-lang/stdlib#1036

The finder already has this break clause

Indeed, however, I still get this error message:

CMake Error at config/cmake/Findfortran_stdlib.cmake:143 (add_library):
  add_library cannot create imported target "fortran_stdlib::fortran_stdlib"
  because another target with the same name already exists.
Call Stack (most recent call first):
  CMakeLists.txt:17 (find_package)


CMake Error at config/cmake/Findfortran_stdlib.cmake:144 (target_link_libraries):
  target_link_libraries can not be used on an ALIAS target.
Call Stack (most recent call first):
  CMakeLists.txt:17 (find_package)

Maybe we can run this before entering the loop?

Running this before won't work, because the target is created when fetching the content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants