Skip to content

[vcpkg] Add special functions for wrappers#21783

Closed
dg0yt wants to merge 5 commits intomicrosoft:masterfrom
dg0yt:wrapper-utils
Closed

[vcpkg] Add special functions for wrappers#21783
dg0yt wants to merge 5 commits intomicrosoft:masterfrom
dg0yt:wrapper-utils

Conversation

@dg0yt
Copy link
Copy Markdown
Contributor

@dg0yt dg0yt commented Dec 1, 2021

This PR complements #21641 by an improved way for library lookup in cmake wrappers. If accepted, existing wrapper should be update for unified behaviour.

  • What does your PR fix?

    The vcpkg cmake toolchain respects VCPKG_PREFER_SYSTEM_LIBS when modifying CMAKE_PREFIX_PATH, CMAKE_LIBRARY_PATH and CMAKE_FIND_ROOT_PATH.
    However, it is hard to respect this preference correctly in wrappers when looking specifically for debug or release variants of libraries. Current wrappers provide explicit paths and so completely igore VCPKG_PREFER_SYSTEM_LIBS. This is inconsistent with behaviour for ports which do not need wrappers.

    In addition, it is difficult to deal with the fact that custom triplets may set a specific VCPKG_BUILD_TYPE for some ports.

    This PR adds two high level functions which can be used in wrappers before and after _find_package. Two low level functions are used instead of plain find_library in order to consistently respect VCPKG_PREFER_SYSTEM_LIBS.

    As an example and test case, port gdal's wrapper is updated to use these functions.

    Limitations: With VCPKG_PREFER_SYSTEM_LIBS activated, variations in library names may still lead to inconsistencies (e.g. release variant from system, debug variant from vcpkg). However, the user can directly initialize the cache variables to system variants if needed.)

  • Which triplets are supported/not supported? Have you updated the CI baseline?

    all, no

  • Does your PR follow the maintainer guide?

    yes

  • If you have added/updated a port: Have you run ./vcpkg x-add-version --all and committed the result?

    yes

@Cheney-W Cheney-W added the category:vcpkg-feature The issue is a new capability of the tool that doesn’t already exist and we haven’t committed label Dec 1, 2021
@Neumann-A
Copy link
Copy Markdown
Contributor

Is there a reason you use the complicated way instead of doing two find_library(${ARGN}) calls with the first one being with NO_DEFAULT_PATHS and only the vcpkg path as PATHS?

@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Dec 1, 2021

Is there a reason you use the complicated way instead of doing two find_library(${ARGN}) calls with the first one being with NO_DEFAULT_PATHS and only the vcpkg path as PATHS?

I hoped to explain it in the top post. Basically, I want to put the responsibility for correct vpckg library lookup into a single place, instead of scattering it over all wrappers.

Again taking the gdal example. We come from:

find_library(GDAL_LIBRARY_DEBUG
    NAMES gdal_d gdal_i_d gdal
    NAMES_PER_DIR
    PATHS "${CMAKE_CURRENT_LIST_DIR}/../../debug/lib"
    NO_DEFAULT_PATH)

Problem: Doesn't systematically respect VCPKG_PREFER_SYSTEM_LIBS. Doesn't systematically find the vcpkg lib when gdal is built only for release. (Needed to avoid picking a non-vcpkg debug lib. There will be another find_library call from _find_package.)

You propose (and I add gdal_i to cover all release lib names for gdal):

find_library(GDAL_LIBRARY_DEBUG
    NAMES gdal_d gdal_i_d gdal_i gdal
    NAMES_PER_DIR
    PATHS "${CMAKE_CURRENT_LIST_DIR}/../../debug/lib" "${CMAKE_CURRENT_LIST_DIR}/../../lib"
    NO_DEFAULT_PATH)
find_library(GDAL_LIBRARY_DEBUG
    NAMES gdal_d gdal_i_d gdal_i gdal
    NAMES_PER_DIR)

Problem: Doesn't systematically respect VCPKG_PREFER_SYSTEM_LIBS. Verbose syntax.

I propose:

z_vcpkg_find_library_debug(GDAL_LIBRARY_DEBUG
    NAMES gdal_d gdal_i_d gdal_i gdal
    NAMES_PER_DIR)

Problem: Needs helper functions. (This PR.)

... and everything duplicated for release, and done in every wrapper.

@Neumann-A
Copy link
Copy Markdown
Contributor

You propose

No i mean that you do two find_library calls in z_vcpkg_find_library_<config> instead of manipulating the lists.

@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Dec 1, 2021

You propose

No i mean that you do two find_library calls in z_vcpkg_find_library_<config> instead of manipulating the lists.

Okay.

This leaves the problem that it doesn't respect VCPKG_PREFER_SYSTEM_LIBS.
Guarding the first call with if(VCPKG_PREFER_SYSTEM_LIBS) doesn't help, because then we fall back to the default search path. The default search path contains the debug-release search order for the consuming project, not for the desired library. So it is wrong either for release or for debug.

This search order is the problem which is reliably addressed by locally inserting the configuration-specific item before the first occurence of the wrong item. This construction doesn't need particular parameters to find_library, allowing to transparently forward all arguments to find_library.

@Neumann-A
Copy link
Copy Markdown
Contributor

Guarding the first call with if(VCPKG_PREFER_SYSTEM_LIBS) doesn't help, because then we fall back to the default search path.

I would say people should use empty overlays in this case since it is the only way to not screw up the binary cache ...

@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Dec 1, 2021

I would say people should use empty overlays in this case since it is the only way to not screw up the binary cache ...

I tend to agree. Mixing system libraries with vcpkg invites undefined behaviour at runtime. But VCPKG_PREFER_SYSTEM_LIBS was merged nevertheless.
However, we can get rid of VCPKG_PREFER_SYSTEM_LIBS later, without changing the interface which is added in this PR.

@Cheney-W Cheney-W requested a review from BillyONeal December 3, 2021 02:08
@Cheney-W Cheney-W added the requires:vcpkg-team-review This PR or issue requires someone on the vcpkg team to take a further look. label Dec 3, 2021
@Cheney-W Cheney-W requested a review from JackBoosY December 3, 2021 08:45
@Cheney-W
Copy link
Copy Markdown
Contributor

Cheney-W commented Dec 3, 2021

@dg0yt Could you please handle the conflict?

@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Dec 5, 2021

@dg0yt Could you please handle the conflict?

Done. But this is a PR for discussion. Merge conflicts can be resolved after discussion.

Added two more functions.
The test port is gdal because it does have some dependening ports. Note that the even the curent version of FindGDAL.cmake uses GDAL_LIBRARY as a cache variable (filepath) and doesn't use select_library_configurations.

@dg0yt dg0yt changed the title Add special find_library functions for wrappers Add special functions for wrappers Dec 5, 2021
@PhoebeHui PhoebeHui changed the title Add special functions for wrappers [vcpkg] Add special functions for wrappers Dec 6, 2021
@ras0219-msft
Copy link
Copy Markdown
Contributor

Mixing system libraries with vcpkg invites undefined behaviour at runtime. But VCPKG_PREFER_SYSTEM_LIBS was merged nevertheless.

I think this (and linked issue) has exposed fundamental problems with VCPKG_PREFER_SYSTEM_LIBS. This suggests to me that we should instead deprecate it, emit a warning, and instruct users that they should use empty overlay ports to correctly stub out system libraries.

github-actions[bot]
github-actions bot previously approved these changes Dec 19, 2021
@JackBoosY
Copy link
Copy Markdown
Contributor

Ping @ras0219-msft for review again.

@Cheney-W Cheney-W removed the requires:vcpkg-team-review This PR or issue requires someone on the vcpkg team to take a further look. label Jun 24, 2022
@Cheney-W
Copy link
Copy Markdown
Contributor

@dg0yt Could you please handle the conflicts? Thank you!

@Cheney-W
Copy link
Copy Markdown
Contributor

Cheney-W commented Jul 1, 2022

Pinging @dg0yt for response. Is work still being done for this PR?

@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Jul 1, 2022

I'm going to resolve the merge conflicts soon. But this PR is really waiting for feedback from @ras0219-msft since December.

@Cheney-W
Copy link
Copy Markdown
Contributor

@dg0yt Could you please handle the conflicts? Thank you!

@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Jul 30, 2022

I tend to withdraw the proposed special functions.
However, the deprecation of VCPKG_PREFER_SYSTEM_LIBS could be merged via a separate PR.

@Cheney-W Cheney-W added the info:needs-maintainer-attention Lets the current 'on rotation' vcpkg maintainer know they need to look at this. label Aug 1, 2022
@ras0219-msft
Copy link
Copy Markdown
Contributor

If we consider defining VCPKG_BUILD_TYPE to release and then building in debug to be "UB", are these special functions still required? My understanding of the status quo (calling find_library() twice) is that the release library will be found and correctly used but the debug library will be one of {system copy, not found}. Then using that debug library is ambiguous and undefined for customers.

Adding these special functions helps to make this illegal scenario less ambiguous, but I'm not sure they're worth their weight in that scenario since there are no longer other policies they need to implement and regularize.

I would like to merge the VCPKG_PREFER_SYSTEM_LIBS deprecations you've put into this PR however. Would you be willing to shrink this PR to just those or open a different one?

@ras0219-msft ras0219-msft added requires:author-response and removed info:needs-maintainer-attention Lets the current 'on rotation' vcpkg maintainer know they need to look at this. labels Aug 3, 2022
@dg0yt
Copy link
Copy Markdown
Contributor Author

dg0yt commented Aug 21, 2022

I tend to withdraw the proposed special functions.
However, the deprecation of VCPKG_PREFER_SYSTEM_LIBS could be merged via a separate PR.

I would like to merge the VCPKG_PREFER_SYSTEM_LIBS deprecations you've put into this PR however. Would you be willing to shrink this PR to just those or open a different one?

-> #26449

@dg0yt dg0yt closed this Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category:vcpkg-feature The issue is a new capability of the tool that doesn’t already exist and we haven’t committed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants