Skip to content

Fix TBB transitive dependencies for libblake3#460

Merged
BurningEnlightenment merged 3 commits intoBLAKE3-team:masterfrom
silvanshade:fix-tbb-transitive-deps
Apr 2, 2025
Merged

Fix TBB transitive dependencies for libblake3#460
BurningEnlightenment merged 3 commits intoBLAKE3-team:masterfrom
silvanshade:fix-tbb-transitive-deps

Conversation

@silvanshade
Copy link
Copy Markdown
Contributor

This PR fixes the transitive dependencies for TBB when libblake3 is built with BLAKE3_USE_TBB=1 which I unfortunately overlooked in the original PR.

This PR introduces two changes:

  • when BLAKE3_USE_TBB=1 then tbb is added to the Requires: field for pkg-config
  • when BLAKE3_USE_TBB=1 then CMake find_package(blake3) automatically finds and configures the TBB::tbb target

Additionally, TBB functionality can be specifically requested through the CMake config with the following (which will fail with an informative error if not available):

find_package(blake3 COMPONENTS TBB)

An alternative design to this fix would be the approach suggested by @Ericson2314 in this comment.

@silvanshade silvanshade force-pushed the fix-tbb-transitive-deps branch 2 times, most recently from 7ea6e31 to 6e6e835 Compare April 1, 2025 01:04
Copy link
Copy Markdown
Collaborator

@BurningEnlightenment BurningEnlightenment left a comment

Choose a reason for hiding this comment

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

I'm a little pressed for time at the moment., so don't consider this a review.

Comment thread c/blake3-config.cmake.in Outdated
endmacro()

if(BLAKE3_USE_TBB)
find_package(TBB QUIET)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AFAIK find_dependency should be used in package configs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@BurningEnlightenment I considered using find_dependency but it doesn't seem to be appropriate here. find_dependency is meant for required dependencies but here TBB is a conditional dependency.

See this comment from the CMake devs discussing a similar situation:

find_dependency is meant for required dependencies and is supposed to handle the QUIET and REQUIRED options from the caller automatically. Don't pass them explicitly.

In a given build of polo, CURL is either a dependency or it is not. The polo-config.cmake file should be generated in a way that requires CURL or does not even search for CURL depending on how the project was built.

The way we simulate forwarding REQUIRED for the find_package(TBB) call is by specifying TBB via the COMPONENTS option (as opposed to OPTIONAL_COMPONENTS):

find_package(blake3 COMPONENTS TBB)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@silvanshade maybe I'm wrong, but if blake3 has been statically built with TBB enabled you should get link errors if you don't also link against TBB, because blake3.c refers to symbols from blake3_tbb.cpp therefore linker shouldn't be able to omit the objects.

Copy link
Copy Markdown
Contributor Author

@silvanshade silvanshade Apr 1, 2025

Choose a reason for hiding this comment

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

@BurningEnlightenment You are right that the downstream project must link against TBB (if BLAKE3 was built with BLAKE3_USE_TBB), but the downstream project should not need to manually do so, it should be handled automatically through the appropriate CMake transitive target mechanisms. That is what this PR fixes.

This is achieved in the code snippets highlighted below:

https://github.com/silvanshade/BLAKE3/blob/b67759a6bd97db787b95b524b2cdcce5eda6c8e5/c/blake3-config.cmake.in#L35-L42

  • add_library(BLAKE3::tbb INTERFACE IMPORTED) creates a logical target for the TBB that BLAKE3 links.
  • target_link_libraries(BLAKE3::tbb INTERFACE TBB::tbb) adds TBB::tbb to the link interface for BLAKE3::tbb.

The second line above ensures any target linking BLAKE3::tbb also links TBB::tbb.

https://github.com/silvanshade/BLAKE3/blob/b67759a6bd97db787b95b524b2cdcce5eda6c8e5/c/blake3-config.cmake.in#L52-L56

  • target_link_libraries(BLAKE3::blake3 INTERFACE BLAKE3::tbb) adds BLAKE3::tbb to the link interface for BLAKE3::blake3.

Now any target linking BLAKE3::blake3 also links TBB::tbb because of how the link interface is configured.

I've tested and this does work as expected. You can try the example here: https://gist.github.com/silvanshade/1e1ae78cc0ad576d5f5060ceeafde11f

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We shouldn't need to target_link_libraries(BLAKE3::tbb INTERFACE TBB::tbb) in the config.cmake because we target_link_libraries(blake3 PUBLIC TBB::tbb) at CMakeLists.txt:232 which should propagate into the exported targets. I.e. the following should be necessary and sufficient:

if(@BLAKE3_USE_TBB@)
  find_dependency(TBB)
endif()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I.e. the following should be necessary and sufficient:

if(@BLAKE3_USE_TBB@)
  find_dependency(TBB)
endif()

You are right -- that is enough given the earlier use of the PUBLIC target link.

I've done things more elaborately because I was considering how usage of the CMake config would look from the perspective of a downstream project needing libblake3 with TBB support.

With the way I've designed it now, you can specify:

find_package(blake3 COMPONENTS TBB)

If TBB isn't found, or isn't found with a compatible version, you get an error and an informative message.

With the minimal approach, extra checking and error logic is required:

find_package(blake3)
if(NOT BLAKE3_USE_TBB)
    message(FATAL_ERROR "Expected TBB")
endif()

I think the COMPONENTS version is more direct and the additional code I've implemented around that is designed to be reusable for other features in the future.

But I think it's important to settle on a design quickly in order to get a fix released so I'll simplify it as you suggest.

@silvanshade silvanshade force-pushed the fix-tbb-transitive-deps branch from 6e6e835 to b67759a Compare April 1, 2025 17:57
@silvanshade silvanshade force-pushed the fix-tbb-transitive-deps branch from b67759a to 510ae9d Compare April 2, 2025 15:06
The `join_pkg_config_requires` function would misbehave in a few edge cases. Though these edge cases aren't triggered by our current usage.
Copy link
Copy Markdown
Collaborator

@BurningEnlightenment BurningEnlightenment left a comment

Choose a reason for hiding this comment

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

LGTM as soon as CI greenlights this. Thanks!

@BurningEnlightenment BurningEnlightenment merged commit 339abc5 into BLAKE3-team:master Apr 2, 2025
60 checks passed
@BurningEnlightenment
Copy link
Copy Markdown
Collaborator

@oconnor663 can you cut a patch release soon? (As usual) I totally forgot the config.cmake when reviewing #445

@silvanshade
Copy link
Copy Markdown
Contributor Author

I added one additional fix for pkg-config at #461

oconnor663 added a commit that referenced this pull request Apr 3, 2025
Changes since 1.8.0:
- [CMake] Fix transitive dependencies for TBB when libblake3 is built
  with BLAKE3_USE_TBB=1 (#460 and #461).
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