taocpp / taopq

C++ client library for PostgreSQL
Boost Software License 1.0
264 stars 40 forks source link

Failed to cooperate with vcpkg in CMake #52

Closed iyanging closed 3 years ago

iyanging commented 3 years ago

I have a CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.21.4)

set(CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# init dependencies
include(FetchContent)

# init vcpkg
FetchContent_Declare(
    vcpkg
    GIT_REPOSITORY  https://github.com/microsoft/vcpkg.git
    GIT_TAG         5bdb9d60123df20c247a1654e6a1def51d6c5140
    GIT_PROGRESS    TRUE
)
FetchContent_MakeAvailable(vcpkg)
execute_process(
    COMMAND sh ./bootstrap-vcpkg.sh --disableMetrics
    WORKING_DIRECTORY ${vcpkg_SOURCE_DIR}
    RESULTS_VARIABLE vcpkg_BUILD_RESULT
    COMMAND_ERROR_IS_FATAL ANY
)
set(CMAKE_TOOLCHAIN_FILE ${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")

project(xxx CXX)

# dep: taoPQ
execute_process(
    COMMAND ./vcpkg install libpq --clean-after-build
    WORKING_DIRECTORY ${vcpkg_SOURCE_DIR}
    COMMAND_ERROR_IS_FATAL ANY
)
find_package(PostgreSQL REQUIRED)

FetchContent_Declare(
   taopq
   GIT_REPOSITORY   https://github.com/taocpp/taopq
   GIT_TAG          8d9285f76726e312282ac2c1de70b3338b2c701d
   GIT_PROGRESS     TRUE
)
FetchContent_MakeAvailable(taopq)

...

But it failed with:

[cmake] -- Configuring done
[cmake] CMake Error in build/_deps/taopq-src/CMakeLists.txt:
[cmake]   Target "taopq" INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake] 
[cmake]     "/home/iyang/xxx/build/_deps/vcpkg-src/installed/x64-linux/include"
[cmake] 
[cmake]   which is prefixed in the build directory.
[cmake] 
[cmake] 
[cmake] CMake Error in build/_deps/taopq-src/CMakeLists.txt:
[cmake]   Target "taopq" INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake] 
[cmake]     "/home/iyang/xxx/build/_deps/vcpkg-src/installed/x64-linux/include"
[cmake] 
[cmake]   which is prefixed in the build directory.Target "taopq"
[cmake]   INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake] 
[cmake]     "/home/iyang/xxx/build/_deps/vcpkg-src/installed/x64-linux/include"
[cmake] 
[cmake]   which is prefixed in the source directory.
[cmake] 
[cmake] 
[cmake] CMake Error in build/_deps/taopq-src/CMakeLists.txt:
[cmake]   Target "taopq" INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake] 
[cmake]     "/home/iyang/xxx/build/_deps/vcpkg-src/installed/x64-linux/include/postgresql/server"
[cmake] 
[cmake]   which is prefixed in the build directory.
[cmake] 
[cmake] 
[cmake] CMake Error in build/_deps/taopq-src/CMakeLists.txt:
[cmake]   Target "taopq" INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake] 
[cmake]     "/home/iyang/xxx/build/_deps/vcpkg-src/installed/x64-linux/include/postgresql/server"
[cmake] 
[cmake]   which is prefixed in the build directory.Target "taopq"
[cmake]   INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake] 
[cmake]     "/home/iyang/xxx/build/_deps/vcpkg-src/installed/x64-linux/include/postgresql/server"
[cmake] 
[cmake]   which is prefixed in the source directory.
[cmake] 
[cmake] 
[cmake] -- Generating done

After I applied the following patch, CMake can be successfully configured.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 308c235..df1fc1a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -92,8 +92,8 @@ target_include_directories(taopq
   PUBLIC
     $<INSTALL_INTERFACE:include>
     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
-    ${PostgreSQL_INCLUDE_DIRS}
   PRIVATE
+    ${PostgreSQL_INCLUDE_DIRS}
     ${CMAKE_CURRENT_SOURCE_DIR}/src
 )

So I think maybe the change can directly happen on the code to avoid this patch ?

BTW, taoPQ is a very wonderful thing, THANKS for your libraries!

uilianries commented 3 years ago

It sounds reasonable as users should not touch PostgreSQL header directly, but use TaoPQ instead.

Thank you for your report, could you please open a pull request with your suggestion?

iyanging commented 3 years ago

It sounds reasonable as users should not touch PostgreSQL header directly, but use TaoPQ instead.

Thank you for your report, could you please open a pull request with your suggestion?

Ok! I'll open a PR.

d-frey commented 3 years ago

Maybe try removing the find_package(PostgreSQL REQUIRED) from your CMakeLists.txt, as this should come in transparently from taoPQ. Maybe there's a conflict there that two different versions are now installed/used? (I'm just guessing)

iyanging commented 3 years ago

In my CMakeLists.txt, vcpkg is installed in build directory by FetchContext. Then vcpkg installs the other dependencies in its source directory, which is still in my project's build directory. When taopq exports libpq's include directory in target_include_directory as public/interface, cmake will check if the path is prefixed in my project's build/source directory.

BOOM!

So, there is nothing wrong with taopq. Thanks very much for your kind helps!