cmake: reuse existing asio target if present #1682
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I’m using standalone Asio via vcpkg in an existing project, with CMake configured like this:
find_package(asio CONFIG REQUIRED)When I tried to integrate
stdexecandasioexecinto the same project using CPM:CPMAddPackage( NAME stdexec GITHUB_REPOSITORY NVIDIA/stdexec GIT_TAG main OPTIONS "STDEXEC_ENABLE_ASIO ON" "STDEXEC_ASIO_IMPLEMENTATION standalone" )CMake failed with the following error:
This happens because
vcpkgalready provides anasiotarget, andstdexec’s CMake code tries to create anotherasiotarget inimport_standalone_asio.cmake, which CMake rejects.There are a few ways to work around this in user code:
stdexec, orfind_package(asio CONFIG REQUIRED)+ vcpkg-provided Asio and only use theasiotarget created bystdexec.However, it is more convenient and less surprising if
stdexeccan reuse an existingasiotarget instead of failing.To avoid the conflict, I changed the
import_standalone_asiofunction to detect an existingasiotarget and reuse it:With this change:
asiotarget,stdexecwill reuse it and skip creating its own target.asiotarget exists,stdexecbehaves as before and imports standalone Asio normally.This makes it easier to integrate
stdexecinto existing CMake projects that already depend on standalone Asio.