Skip to content

Commit

Permalink
[vcpkg_setup_pkgconfig_path] Add new functions to set or restore pkgc…
Browse files Browse the repository at this point in the history
…onfig related environment variables (#23429)

* [vcpkg_configure_meson] Fix append host path

* New functions vcpkg_setup_pkgconfig_path and vcpkg_restore_pkgconfig_path

* Fix bug, add cos

* Apply suggestions

* Apply suggestions

* Apply suggestion

* doc

* Update docs/maintainers/vcpkg_setup_pkgconfig_path.cmake.md

Co-authored-by: LilyWangLL <[email protected]>

* Update scripts/cmake/vcpkg_setup_pkgconfig_path.cmake

Co-authored-by: LilyWangLL <[email protected]>

* Update scripts/cmake/vcpkg_configure_make.cmake

* Apply suggestions

* Apply suggestion

* Apply suggestions

Co-authored-by: Victor Romero <[email protected]>
Co-authored-by: LilyWangLL <[email protected]>
  • Loading branch information
3 people authored May 10, 2022
1 parent 659b6b5 commit 0d7603c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 48 deletions.
11 changes: 11 additions & 0 deletions docs/maintainers/internal/z_vcpkg_setup_pkgconfig_path.cmake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# z_vcpkg_setup_pkgconfig_path

Setup the generated pkgconfig file path to PKG_CONFIG_PATH environment variable or restore PKG_CONFIG_PATH environment variable.

```cmake
z_vcpkg_setup_pkgconfig_path(BASE_DIRS <"${CURRENT_INSTALLED_DIR}" ...>)
z_vcpkg_restore_pkgconfig_path()
```

`z_vcpkg_setup_pkgconfig_path` prepends `lib/pkgconfig` and `share/pkgconfig` directories for the given `BASE_DIRS` to the `PKG_CONFIG_PATH` environment variable. It creates or updates a backup of the previous value.
`z_vcpkg_restore_pkgconfig_path` shall be called when leaving the scope which called `z_vcpkg_setup_pkgconfig_path` in order to restore the original value from the backup.
1 change: 1 addition & 0 deletions docs/maintainers/portfile-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- [z\_vcpkg\_function\_arguments](internal/z_vcpkg_function_arguments.md)
- [z\_vcpkg\_get\_cmake\_vars](internal/z_vcpkg_get_cmake_vars.md)
- [z\_vcpkg\_prettify\_command\_line](internal/z_vcpkg_prettify_command_line.md)
- [z\_vcpkg\_setup\_pkgconfig\_path](internal/z_vcpkg_setup_pkgconfig_path.md)

## Scripts from Ports

Expand Down
22 changes: 4 additions & 18 deletions scripts/cmake/vcpkg_configure_make.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,6 @@ function(vcpkg_configure_make)
endif()
endif()
debug_message("ENV{LIBS}:$ENV{LIBS}")
vcpkg_find_acquire_program(PKGCONFIG)
if(VCPKG_LIBRARY_LINKAGE STREQUAL "static" AND NOT PKGCONFIG STREQUAL "--static")
set(PKGCONFIG "${PKGCONFIG} --static") # Is this still required or was the PR changing the pc files accordingly merged?
endif()

# Run autoconf if necessary
if (arg_AUTOCONFIG OR requires_autoconfig)
Expand Down Expand Up @@ -767,13 +763,10 @@ function(vcpkg_configure_make)
endif()
# Setup PKG_CONFIG_PATH
set(pkgconfig_installed_dir "${CURRENT_INSTALLED_DIR}${path_suffix_${current_buildtype}}/lib/pkgconfig")
set(pkgconfig_installed_share_dir "${CURRENT_INSTALLED_DIR}/share/pkgconfig")
if(ENV{PKG_CONFIG_PATH})
set(backup_env_pkg_config_path_${current_buildtype} $ENV{PKG_CONFIG_PATH})
set(ENV{PKG_CONFIG_PATH} "${pkgconfig_installed_dir}${VCPKG_HOST_PATH_SEPARATOR}${pkgconfig_installed_share_dir}${VCPKG_HOST_PATH_SEPARATOR}$ENV{PKG_CONFIG_PATH}")
if ("${current_buildtype}" STREQUAL "DEBUG")
z_vcpkg_setup_pkgconfig_path(BASE_DIRS "${CURRENT_INSTALLED_DIR}/debug")
else()
set(ENV{PKG_CONFIG_PATH} "${pkgconfig_installed_dir}${VCPKG_HOST_PATH_SEPARATOR}${pkgconfig_installed_share_dir}")
z_vcpkg_setup_pkgconfig_path(BASE_DIRS "${CURRENT_INSTALLED_DIR}")
endif()
# Setup environment
Expand All @@ -794,7 +787,6 @@ function(vcpkg_configure_make)
set(link_config_backup "$ENV{_LINK_}")
set(ENV{_LINK_} "${LINK_ENV_${current_buildtype}}")
endif()
set(ENV{PKG_CONFIG} "${PKGCONFIG}")
vcpkg_list(APPEND lib_env_vars LIB LIBPATH LIBRARY_PATH) # LD_LIBRARY_PATH)
foreach(lib_env_var IN LISTS lib_env_vars)
Expand Down Expand Up @@ -834,13 +826,7 @@ function(vcpkg_configure_make)
file(RENAME "${target_dir}/config.log" "${CURRENT_BUILDTREES_DIR}/config.log-${TARGET_TRIPLET}-${short_name_${current_buildtype}}.log")
endif()
endif()
if(backup_env_pkg_config_path_${current_buildtype})
set(ENV{PKG_CONFIG_PATH} "${backup_env_pkg_config_path_${current_buildtype}}")
else()
unset(ENV{PKG_CONFIG_PATH})
endif()
unset(backup_env_pkg_config_path_${current_buildtype})
z_vcpkg_restore_pkgconfig_path()
if(link_config_backup)
set(ENV{_LINK_} "${link_config_backup}")
Expand Down
20 changes: 8 additions & 12 deletions scripts/cmake/vcpkg_configure_meson.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,6 @@ function(vcpkg_configure_meson)
vcpkg_list(APPEND arg_OPTIONS_RELEASE "-Dcmake_prefix_path=['${CURRENT_INSTALLED_DIR}','${CURRENT_INSTALLED_DIR}/debug']")
endif()

if(NOT arg_NO_PKG_CONFIG)
vcpkg_find_acquire_program(PKGCONFIG)
get_filename_component(PKGCONFIG_PATH ${PKGCONFIG} DIRECTORY)
vcpkg_add_to_path("${PKGCONFIG_PATH}")
set(pkgconfig_share_dir "${CURRENT_INSTALLED_DIR}/share/pkgconfig/")
endif()

set(buildtypes)
if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
set(buildname "DEBUG")
Expand All @@ -466,11 +459,12 @@ function(vcpkg_configure_meson)
message(STATUS "Configuring ${TARGET_TRIPLET}-${suffix_${buildtype}}")
file(MAKE_DIRECTORY "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-${suffix_${buildtype}}")
#setting up PKGCONFIG
vcpkg_backup_env_variables(VARS PKG_CONFIG PKG_CONFIG_PATH)
if(NOT arg_NO_PKG_CONFIG)
set(ENV{PKG_CONFIG} "${PKGCONFIG}") # Set via native file?
set(pkgconfig_installed_dir "${CURRENT_INSTALLED_DIR}/${path_suffix_${buildtype}}lib/pkgconfig/")
vcpkg_host_path_list(APPEND ENV{PKG_CONFIG_PATH} "${pkgconfig_installed_dir}" "${pkgconfig_share_dir}" "$ENV{PKG_CONFIG_PATH}")
if ("${buildtype}" STREQUAL "DEBUG")
z_vcpkg_setup_pkgconfig_path(BASE_DIRS "${CURRENT_INSTALLED_DIR}/debug")
else()
z_vcpkg_setup_pkgconfig_path(BASE_DIRS "${CURRENT_INSTALLED_DIR}")
endif()
endif()

vcpkg_execute_required_process(
Expand All @@ -494,7 +488,9 @@ function(vcpkg_configure_meson)
endif()
message(STATUS "Configuring ${TARGET_TRIPLET}-${suffix_${buildtype}} done")

vcpkg_restore_env_variables(VARS PKG_CONFIG PKG_CONFIG_PATH)
if(NOT arg_NO_PKG_CONFIG)
z_vcpkg_restore_pkgconfig_path()
endif()
endforeach()

vcpkg_restore_env_variables(VARS INCLUDE)
Expand Down
23 changes: 5 additions & 18 deletions scripts/cmake/vcpkg_configure_qmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,8 @@ function(vcpkg_configure_qmake)
set(ENV{QMAKE_MACOSX_DEPLOYMENT_TARGET} ${VCPKG_OSX_DEPLOYMENT_TARGET})
endif()

vcpkg_backup_env_variables(VARS PKG_CONFIG_PATH)

vcpkg_find_acquire_program(PKGCONFIG)
set(ENV{PKG_CONFIG} "${PKGCONFIG}")
get_filename_component(PKGCONFIG_PATH "${PKGCONFIG}" DIRECTORY)
vcpkg_add_to_path("${PKGCONFIG_PATH}")

if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release")
vcpkg_host_path_list(PREPEND ENV{PKG_CONFIG_PATH}
"${CURRENT_INSTALLED_DIR}/lib/pkgconfig"
"${CURRENT_INSTALLED_DIR}/share/pkgconfig"
"${CURRENT_PACKAGES_DIR}/lib/pkgconfig"
"${CURRENT_PACKAGES_DIR}/share/pkgconfig")
z_vcpkg_setup_pkgconfig_path(BASE_DIRS "${CURRENT_INSTALLED_DIR}" "${CURRENT_PACKAGES_DIR}")

set(current_binary_dir "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel")

Expand Down Expand Up @@ -98,15 +87,11 @@ function(vcpkg_configure_qmake)
file(RENAME "${current_binary_dir}/config.log" "${CURRENT_BUILDTREES_DIR}/internal-config-${TARGET_TRIPLET}-rel.log")
endif()

vcpkg_restore_env_variables(VARS PKG_CONFIG_PATH)
z_vcpkg_restore_pkgconfig_path()
endif()

if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug")
vcpkg_host_path_list(PREPEND ENV{PKG_CONFIG_PATH}
"${CURRENT_INSTALLED_DIR}/debug/lib/pkgconfig"
"${CURRENT_INSTALLED_DIR}/share/pkgconfig"
"${CURRENT_PACKAGES_DIR}/debug/lib/pkgconfig"
"${CURRENT_PACKAGES_DIR}/share/pkgconfig")
z_vcpkg_setup_pkgconfig_path(BASE_DIRS "${CURRENT_INSTALLED_DIR}/debug" "${CURRENT_PACKAGES_DIR}/debug")

set(current_binary_dir "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg")

Expand Down Expand Up @@ -135,6 +120,8 @@ function(vcpkg_configure_qmake)
file(REMOVE "${CURRENT_BUILDTREES_DIR}/internal-config-${TARGET_TRIPLET}-dbg.log")
file(RENAME "${current_binary_dir}/config.log" "${CURRENT_BUILDTREES_DIR}/internal-config-${TARGET_TRIPLET}-dbg.log")
endif()

z_vcpkg_restore_pkgconfig_path()
endif()

endfunction()
48 changes: 48 additions & 0 deletions scripts/cmake/z_vcpkg_setup_pkgconfig_path.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[===[
# z_vcpkg_setup_pkgconfig_path

`z_vcpkg_setup_pkgconfig_path` sets up environment variables to use `pkgconfig`, such as `PKG_CONFIG` and `PKG_CONFIG_PATH`.
The original values are restored with `z_vcpkg_restore_pkgconfig_path`. `BASE_DIRS` indicates the base directories to find `.pc` files; typically `${CURRENT_INSTALLED_DIR}`, or `${CURRENT_INSTALLED_DIR}/debug`.

```cmake
z_vcpkg_setup_pkgconfig_path(BASE_DIRS <"${CURRENT_INSTALLED_DIR}" ...>)
# Build process that may transitively invoke pkgconfig
z_vcpkg_restore_pkgconfig_path()
```

#]===]
function(z_vcpkg_setup_pkgconfig_path)
cmake_parse_arguments(PARSE_ARGV 0 "arg" "" "" "BASE_DIRS")

if(NOT DEFINED arg_BASE_DIRS OR "${arg_BASE_DIRS}" STREQUAL "")
message(FATAL_ERROR "BASE_DIRS is required.")
endif()
if(DEFINED arg_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()

vcpkg_backup_env_variables(VARS PKG_CONFIG PKG_CONFIG_PATH)

vcpkg_find_acquire_program(PKGCONFIG)
get_filename_component(pkgconfig_path "${PKGCONFIG}" DIRECTORY)
vcpkg_add_to_path("${pkgconfig_path}")

set(ENV{PKG_CONFIG} "${PKGCONFIG}") # Set via native file?

foreach(base_dir IN LISTS arg_BASE_DIRS)
vcpkg_host_path_list(PREPEND ENV{PKG_CONFIG_PATH} "${base_dir}/share/pkgconfig/")
endforeach()

foreach(base_dir IN LISTS arg_BASE_DIRS)
vcpkg_host_path_list(PREPEND ENV{PKG_CONFIG_PATH} "${base_dir}/lib/pkgconfig/")
endforeach()
endfunction()

function(z_vcpkg_restore_pkgconfig_path)
cmake_parse_arguments(PARSE_ARGV 0 "arg" "" "" "")
if(DEFINED arg_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION} was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()

vcpkg_restore_env_variables(VARS PKG_CONFIG PKG_CONFIG_PATH)
endfunction()
1 change: 1 addition & 0 deletions scripts/ports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ include("${SCRIPTS}/cmake/z_vcpkg_forward_output_variable.cmake")
include("${SCRIPTS}/cmake/z_vcpkg_function_arguments.cmake")
include("${SCRIPTS}/cmake/z_vcpkg_get_cmake_vars.cmake")
include("${SCRIPTS}/cmake/z_vcpkg_prettify_command_line.cmake")
include("${SCRIPTS}/cmake/z_vcpkg_setup_pkgconfig_path.cmake")

function(debug_message)
if(PORT_DEBUG)
Expand Down

0 comments on commit 0d7603c

Please sign in to comment.