Thanks to visit codestin.com
Credit goes to github.com

Skip to content

make sure reflectcpp library namespace is also available when not installing #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

BestITUserEUW
Copy link
Contributor

What is the problem ?

When using reflect-cpp without installing it, the CMake target reflectcpp::reflectcpp is not automatically provided. This namespaced target is only made available through installation.

Abstract Example:

if(MY_LIB_BUILD_DEPS)
    FetchContent_Declare(
        reflectcpp
        GIT_REPOSITORY https://github.com/getml/reflect-cpp.git
        GIT_TAG 2a0fdeabe35bdab0e07e05d4f869ca88bd6d2b8c
        OVERRIDE_FIND_PACKAGE
    )
    // Here we get just reflectcpp
    FetchContent_MakeAvailable(reflectcpp)

     // Manually create an alias to have a common interface
    add_library(reflectcpp::reflectcpp ALIAS reflectcpp)
else()
     // Here we get the relfectcpp::reflectcpp target 
    find_package(reflectcpp REQUIRED)
endif()

...

target_link_libraries(${PROJECT_NAME} PRIVATE reflectcpp::reflectcpp)

What does this solve ?

This setup allows consistent usage of reflectcpp::reflectcpp whether reflect-cpp is:

  1. Built from source (FetchContent or add_subdirectory)
  2. Used as a pre-installed library (via find_package)

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @BestITUserEUW, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an inconsistency in how the reflect-cpp library's CMake target is exposed. Previously, the reflectcpp::reflectcpp namespaced target was only available when the library was installed. This change ensures that this target is consistently available even when reflect-cpp is integrated directly into a project via FetchContent or add_subdirectory, simplifying client code that links against it.

Highlights

  • CMake Configuration: Introduced an ALIAS target reflectcpp::reflectcpp for the reflectcpp library in CMakeLists.txt. This ensures that the namespaced target is always available, regardless of whether the library is installed or built directly from source.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a CMake alias reflectcpp::reflectcpp for the reflectcpp target. This is a great improvement as it allows consumers of the library to use a consistent target name whether they are using an installed version or building from source via FetchContent. My review includes a suggestion to make this change more robust by checking for the existence of the alias target before creating it. This will prevent build failures for users who have already implemented a similar workaround in their own build scripts.

@@ -134,6 +134,8 @@ else()
add_library(reflectcpp STATIC)
endif()

add_library(reflectcpp::reflectcpp ALIAS reflectcpp)

Choose a reason for hiding this comment

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

medium

This is a great addition for improving the consumer experience! However, adding this alias unconditionally could break builds for users who have already implemented the workaround described in the pull request description (i.e., they have their own add_library(reflectcpp::reflectcpp ALIAS reflectcpp)).

To make this change more robust and backward-compatible, it's a good practice to check if the target already exists before creating the alias.

if(NOT TARGET reflectcpp::reflectcpp)
    add_library(reflectcpp::reflectcpp ALIAS reflectcpp)
endif()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gemini-code-assist Will not work if alias library is declared after add_subdirectory or FetchContentMakeAvailable. So even with that a breaking change.

Choose a reason for hiding this comment

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

You raise a valid point, BestITUserEUW. The if(NOT TARGET reflectcpp::reflectcpp) check primarily prevents redefinition errors if the target were to be defined within the reflectcpp project's own CMake processing or if a user's workaround was placed before the reflectcpp project's inclusion in their top-level CMakeLists.txt.

However, you are correct that if a user has an add_library(reflectcpp::reflectcpp ALIAS reflectcpp) workaround in their own CMakeLists.txt that is processed after reflectcpp's CMakeLists.txt (which now defines this alias), it will indeed lead to a redefinition error. This is an unavoidable consequence of the library now officially providing the reflectcpp::reflectcpp target.

The intent of this PR is to standardize the availability of reflectcpp::reflectcpp. When a library starts providing a target that was previously a common workaround, users who implemented that workaround will need to remove it from their build scripts. While this might require a minor adjustment for some existing users, it ultimately leads to a more consistent and robust build experience for everyone consuming reflectcpp.

@BestITUserEUW
Copy link
Contributor Author

As gemini already pointed out anyone using the approach i showed in my example his build will crash:

CMake Error at CMakeLists.txt:39 (add_library):
add_library cannot create ALIAS target "reflectcpp::reflectcpp" because
another target with the same name already exists.

@BestITUserEUW
Copy link
Contributor Author

@liuzicheng1987 Seems like windows test timed out. "Process terminated due to timeout". Is possible to rerun this test ?

Normally this change should not affect the current tests at all

@liuzicheng1987 liuzicheng1987 merged commit c496f14 into getml:main Jul 14, 2025
13 of 14 checks passed
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.

2 participants