From 7a801b7dfb38c2f9f77fbda0f45ed88b70ca4b87 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 1 Nov 2018 13:21:45 -0400 Subject: FindPostgreSQL: Modernize documentation layout --- Modules/FindPostgreSQL.cmake | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Modules/FindPostgreSQL.cmake b/Modules/FindPostgreSQL.cmake index 77fa4ed..d59ba50 100644 --- a/Modules/FindPostgreSQL.cmake +++ b/Modules/FindPostgreSQL.cmake @@ -7,14 +7,21 @@ FindPostgreSQL Find the PostgreSQL installation. -This module defines - -:: - - PostgreSQL_LIBRARIES - the PostgreSQL libraries needed for linking - PostgreSQL_INCLUDE_DIRS - the directories of the PostgreSQL headers - PostgreSQL_LIBRARY_DIRS - the link directories for PostgreSQL libraries - PostgreSQL_VERSION_STRING - the version of PostgreSQL found (since CMake 2.8.8) +Result Variables +^^^^^^^^^^^^^^^^ + +This module will set the following variables in your project: + +``PostgreSQL_FOUND`` + True if PostgreSQL is found. +``PostgreSQL_LIBRARIES`` + the PostgreSQL libraries needed for linking +``PostgreSQL_INCLUDE_DIRS`` + the directories of the PostgreSQL headers +``PostgreSQL_LIBRARY_DIRS`` + the link directories for PostgreSQL libraries +``PostgreSQL_VERSION_STRING`` + the version of PostgreSQL found #]=======================================================================] # ---------------------------------------------------------------------------- -- cgit v0.12 From d2235fd2538eca934f542ff82f21249c3ff314be Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 31 Oct 2018 14:36:27 -0400 Subject: FindPostgreSQL: add an imported target --- Help/release/dev/FindPostgreSQL-target.rst | 4 ++++ Modules/FindPostgreSQL.cmake | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 Help/release/dev/FindPostgreSQL-target.rst diff --git a/Help/release/dev/FindPostgreSQL-target.rst b/Help/release/dev/FindPostgreSQL-target.rst new file mode 100644 index 0000000..84f8d1a --- /dev/null +++ b/Help/release/dev/FindPostgreSQL-target.rst @@ -0,0 +1,4 @@ +FindPostgreSQL-target +--------------------- + +* The :module:`FindPostgreSQL` module now provides an imported target. diff --git a/Modules/FindPostgreSQL.cmake b/Modules/FindPostgreSQL.cmake index d59ba50..6832bbe 100644 --- a/Modules/FindPostgreSQL.cmake +++ b/Modules/FindPostgreSQL.cmake @@ -7,6 +7,12 @@ FindPostgreSQL Find the PostgreSQL installation. +IMPORTED Targets +^^^^^^^^^^^^^^^^ + +This module defines :prop_tgt:`IMPORTED` target ``PostgreSQL::PostgreSQL`` +if PostgreSQL has been found. + Result Variables ^^^^^^^^^^^^^^^^ @@ -56,6 +62,8 @@ This module will set the following variables in your project: # PostgreSQL_LIBRARY_DIRS - Link directories for PostgreSQL libraries # PostgreSQL_LIBRARIES - The PostgreSQL libraries. # +# The ``PostgreSQL::PostgreSQL`` imported target is also created. +# # ---------------------------------------------------------------------------- # If you have installed PostgreSQL in a non-standard location. # (Please note that in the following comments, it is assumed that @@ -187,6 +195,12 @@ set(PostgreSQL_FOUND ${POSTGRESQL_FOUND}) # Now try to get the include and library path. if(PostgreSQL_FOUND) + if (NOT TARGET PostgreSQL::PostgreSQL) + add_library(PostgreSQL::PostgreSQL UNKNOWN IMPORTED) + set_target_properties(PostgreSQL::PostgreSQL PROPERTIES + IMPORTED_LOCATION "${PostgreSQL_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${PostgreSQL_INCLUDE_DIR};${PostgreSQL_TYPE_INCLUDE_DIR}") + endif () set(PostgreSQL_INCLUDE_DIRS ${PostgreSQL_INCLUDE_DIR} ${PostgreSQL_TYPE_INCLUDE_DIR} ) set(PostgreSQL_LIBRARY_DIRS ${PostgreSQL_LIBRARY_DIR} ) set(PostgreSQL_LIBRARIES ${PostgreSQL_LIBRARY}) -- cgit v0.12 From 7413f29fe63296cf34176c5fc68c2c5354b43202 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 31 Oct 2018 15:06:45 -0400 Subject: Tests/FindPostgreSQL: add a test for FindPostgreSQL --- Tests/CMakeLists.txt | 4 ++++ Tests/FindPostgreSQL/CMakeLists.txt | 10 ++++++++++ Tests/FindPostgreSQL/Test/CMakeLists.txt | 16 ++++++++++++++++ Tests/FindPostgreSQL/Test/main.c | 15 +++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 Tests/FindPostgreSQL/CMakeLists.txt create mode 100644 Tests/FindPostgreSQL/Test/CMakeLists.txt create mode 100644 Tests/FindPostgreSQL/Test/main.c diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index c7cfa86..f74defb 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1461,6 +1461,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release add_subdirectory(FindPatch) endif() + if(CMake_TEST_FindPostgreSQL) + add_subdirectory(FindPostgreSQL) + endif() + if(CMake_TEST_FindProtobuf) add_subdirectory(FindProtobuf) endif() diff --git a/Tests/FindPostgreSQL/CMakeLists.txt b/Tests/FindPostgreSQL/CMakeLists.txt new file mode 100644 index 0000000..50151ee --- /dev/null +++ b/Tests/FindPostgreSQL/CMakeLists.txt @@ -0,0 +1,10 @@ +add_test(NAME FindPostgreSQL.Test COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindPostgreSQL/Test" + "${CMake_BINARY_DIR}/Tests/FindPostgreSQL/Test" + ${build_generator_args} + --build-project TestFindPostgreSQL + --build-options ${build_options} + --test-command ${CMAKE_CTEST_COMMAND} -V -C $ + ) diff --git a/Tests/FindPostgreSQL/Test/CMakeLists.txt b/Tests/FindPostgreSQL/Test/CMakeLists.txt new file mode 100644 index 0000000..374e147 --- /dev/null +++ b/Tests/FindPostgreSQL/Test/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.10) +project(TestFindPostgreSQL C) +include(CTest) + +find_package(PostgreSQL REQUIRED) + +add_definitions(-DCMAKE_EXPECTED_POSTGRESQL_VERSION="${PostgreSQL_VERSION_STRING}") + +add_executable(test_tgt main.c) +target_link_libraries(test_tgt PostgreSQL::PostgreSQL) +add_test(NAME test_tgt COMMAND test_tgt) + +add_executable(test_var main.c) +target_include_directories(test_var PRIVATE ${PostgreSQL_INCLUDE_DIRS}) +target_link_libraries(test_var PRIVATE ${PostgreSQL_LIBRARIES}) +add_test(NAME test_var COMMAND test_var) diff --git a/Tests/FindPostgreSQL/Test/main.c b/Tests/FindPostgreSQL/Test/main.c new file mode 100644 index 0000000..2cfeed0 --- /dev/null +++ b/Tests/FindPostgreSQL/Test/main.c @@ -0,0 +1,15 @@ +#include +#include +#include + +int main() +{ + int version = PQlibVersion(); + int major = version / 10000; + int minor = version % 10000; + char version_string[100]; + snprintf(version_string, sizeof(version_string), "%d.%d", major, minor); + printf("Found PostgreSQL version %s, expected version %s\n", version_string, + CMAKE_EXPECTED_POSTGRESQL_VERSION); + return strcmp(version_string, CMAKE_EXPECTED_POSTGRESQL_VERSION); +} -- cgit v0.12 From 575e2cc35da7cef40c521bccf0512483012bc136 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 1 Nov 2018 14:45:25 -0400 Subject: FindPostgreSQL: extract the actual version number The `PG_VERSION` variable can be mangled by distributions to indicate things like the name of the distro and package build numbers. However, `PG_VERSION_NUM` is new in 8.2 (2006), so keep the old extraction code around for old versions. --- Modules/FindPostgreSQL.cmake | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Modules/FindPostgreSQL.cmake b/Modules/FindPostgreSQL.cmake index 6832bbe..4b5e60e 100644 --- a/Modules/FindPostgreSQL.cmake +++ b/Modules/FindPostgreSQL.cmake @@ -175,14 +175,34 @@ if (PostgreSQL_INCLUDE_DIR) foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS}) if(EXISTS "${_PG_CONFIG_HEADER}") file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str - REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"") + REGEX "^#define[\t ]+PG_VERSION_NUM[\t ]+.*") if(pgsql_version_str) - string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*" - "\\1" PostgreSQL_VERSION_STRING "${pgsql_version_str}") + string(REGEX REPLACE "^#define[\t ]+PG_VERSION_NUM[\t ]+([0-9]*).*" + "\\1" _PostgreSQL_VERSION_NUM "${pgsql_version_str}") break() endif() endif() endforeach() + if (_PostgreSQL_VERSION_NUM) + math(EXPR _PostgreSQL_major_version "${_PostgreSQL_VERSION_NUM} / 10000") + math(EXPR _PostgreSQL_minor_version "${_PostgreSQL_VERSION_NUM} % 10000") + set(PostgreSQL_VERSION_STRING "${_PostgreSQL_major_version}.${_PostgreSQL_minor_version}") + unset(_PostgreSQL_major_version) + unset(_PostgreSQL_minor_version) + else () + foreach(_PG_CONFIG_HEADER ${_PG_CONFIG_HEADERS}) + if(EXISTS "${_PG_CONFIG_HEADER}") + file(STRINGS "${_PG_CONFIG_HEADER}" pgsql_version_str + REGEX "^#define[\t ]+PG_VERSION[\t ]+\".*\"") + if(pgsql_version_str) + string(REGEX REPLACE "^#define[\t ]+PG_VERSION[\t ]+\"([^\"]*)\".*" + "\\1" PostgreSQL_VERSION_STRING "${pgsql_version_str}") + break() + endif() + endif() + endforeach() + endif () + unset(_PostgreSQL_VERSION_NUM) unset(pgsql_version_str) endif() -- cgit v0.12