diff --git a/docs/users/manifests.md b/docs/users/manifests.md index 6d94cd40ce..ae0ee7eedc 100644 --- a/docs/users/manifests.md +++ b/docs/users/manifests.md @@ -425,6 +425,12 @@ Defaults to `OFF`. This variable can be set to a list of additional command line parameters to pass to the vcpkg tool during automatic installation. +#### `VCPKG_PREFER_SYSTEM_LIBS` + +This variable controls whether vcpkg will appends instead of prepends its paths to `CMAKE_PREFIX_PATH`, `CMAKE_LIBRARY_PATH` and `CMAKE_FIND_ROOT_PATH` so that vcpkg libraries/packages are found after toolchain/system libraries/packages. + +Defaults to `OFF`. + #### `VCPKG_FEATURE_FLAGS` This variable can be set to a list of feature flags to pass to the vcpkg tool during automatic installation to opt-in to diff --git a/scripts/buildsystems/vcpkg.cmake b/scripts/buildsystems/vcpkg.cmake index 446e64f23c..58d3da956a 100644 --- a/scripts/buildsystems/vcpkg.cmake +++ b/scripts/buildsystems/vcpkg.cmake @@ -47,6 +47,7 @@ mark_as_advanced(VCPKG_VERBOSE) option(VCPKG_APPLOCAL_DEPS "Automatically copy dependencies into the output directory for executables." ON) option(X_VCPKG_APPLOCAL_DEPS_SERIALIZED "(experimental) Add USES_TERMINAL to VCPKG_APPLOCAL_DEPS to force serialization." OFF) option(X_VCPKG_APPLOCAL_DEPS_INSTALL "(experimental) Automatically copy dependencies into the install target directory for executables." OFF) +option(VCPKG_PREFER_SYSTEM_LIBS "Appends the vcpkg paths to CMAKE_PREFIX_PATH, CMAKE_LIBRARY_PATH and CMAKE_FIND_ROOT_PATH so that vcpkg libraries/packages are found after toolchain/system libraries/packages." OFF) # Manifest options and settings if(NOT DEFINED VCPKG_MANIFEST_DIR) @@ -379,29 +380,35 @@ set(_VCPKG_INSTALLED_DIR "${VCPKG_INSTALLED_DIR}" CACHE PATH "The directory which contains the installed libraries for each triplet" FORCE) +if(VCPKG_PREFER_SYSTEM_LIBS) + set(Z_VCPKG_PATH_LIST_OP APPEND) +else() + set(Z_VCPKG_PATH_LIST_OP PREPEND) +endif() + if(CMAKE_BUILD_TYPE MATCHES "^[Dd][Ee][Bb][Uu][Gg]$" OR NOT DEFINED CMAKE_BUILD_TYPE) #Debug build: Put Debug paths before Release paths. - list(APPEND CMAKE_PREFIX_PATH + list(${Z_VCPKG_PATH_LIST_OP} CMAKE_PREFIX_PATH "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug" "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}" ) - list(APPEND CMAKE_LIBRARY_PATH + list(${Z_VCPKG_PATH_LIST_OP} CMAKE_LIBRARY_PATH "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/lib/manual-link" "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/manual-link" ) - list(APPEND CMAKE_FIND_ROOT_PATH + list(${Z_VCPKG_PATH_LIST_OP} CMAKE_FIND_ROOT_PATH "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug" "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}" ) else() #Release build: Put Release paths before Debug paths. Debug Paths are required so that CMake generates correct info in autogenerated target files. - list(APPEND CMAKE_PREFIX_PATH + list(${Z_VCPKG_PATH_LIST_OP} CMAKE_PREFIX_PATH "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}" "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug" ) - list(APPEND CMAKE_LIBRARY_PATH + list(${Z_VCPKG_PATH_LIST_OP} CMAKE_LIBRARY_PATH "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/manual-link" "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug/lib/manual-link" ) - list(APPEND CMAKE_FIND_ROOT_PATH + list(${Z_VCPKG_PATH_LIST_OP} CMAKE_FIND_ROOT_PATH "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}" "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/debug" )