Skip to content

Commit

Permalink
build: CMake pqxx improvements (#249)
Browse files Browse the repository at this point in the history
* build: cmake: libpqxx: fix indentation format

Previously, 4 spaces were used for both indentation and line continuations. This uses 4 spaces for indentation and tabs for line continuations.

 * src/CMakeLists.txt{,.template}: Use tabs for line continuations

* build: cmake: libpqxx: remove separate shared and static library targets

Previously, there were separate shared and static library targets defined for libpqxx. This works alright on Linux, but on Windows the shared library interface (i.e. `libpqxx.lib`) which coincides with the actual dynamic runtime library (i.e. `libpqxx.dll`) will clobber the static library (i.e. `libpqxx.lib`). Instead, one library target should be defined, and the user will select which to build using `-DBUILD_SHARED_LIBS`. This is the recommended approach for CMake in any case.

 * src/CMakeLists.txt{,.template}: define a single library target for libpqxx
 * test/{,unit/}CMakeLists.txt{,.template}: use the pqxx target

* build: cmake: libpqxx: create and install library symlinks

This creates and installs a symlink for e.g. `libpqxx.so -> libpqxx-$major.$minor.so`.

 * src/CMakeLists.txt{,.template}: create and install library symlinks

* build: cmake: libpqxx: install pkg-config file

This installs the pkg-config file for libpqxx when using CMake, like with the autotools build.

 * libpqxx.pc.in: remove library and include directories for postgresql (private dependency)
 * src/CMakeLists.txt{,.template}: install pkg-config file with libpqxx
  • Loading branch information
mkrupcale authored and jtv committed Jan 10, 2020
1 parent d3e0f3c commit 4519227
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 40 deletions.
4 changes: 2 additions & 2 deletions libpqxx.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ includedir=@includedir@
Name: libpqxx
Description: C++ client API for the PostgreSQL database management system.
Version: @VERSION@
Libs: -L${libdir} -L@with_postgres_lib@ -lpqxx
Cflags: -I${includedir} -I@with_postgres_include@
Libs: -L${libdir} -lpqxx
Cflags: -I${includedir}
58 changes: 41 additions & 17 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ macro(library_target_setup tgt)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
get_target_property(name ${tgt} NAME)
get_target_property(output_name ${tgt} OUTPUT_NAME)
if(NOT name STREQUAL output_name)
# Create library symlink
get_target_property(target_type ${tgt} TYPE)
if(target_type STREQUAL "SHARED_LIBRARY")
set(library_prefix ${CMAKE_SHARED_LIBRARY_PREFIX})
set(library_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX})
elseif(target_type STREQUAL "STATIC_LIBRARY")
set(library_prefix ${CMAKE_STATIC_LIBRARY_PREFIX})
set(library_suffix ${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
add_custom_target(library_symlink ALL
${CMAKE_COMMAND} -E create_symlink
${library_prefix}${output_name}${library_suffix}
${library_prefix}${name}${library_suffix}
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${library_prefix}${name}${library_suffix}
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
endif()
endmacro()

file(
Expand Down Expand Up @@ -67,23 +88,26 @@ file(
version.cxx
)

if(NOT SKIP_PQXX_SHARED)
set(CXX_SHARED_EXTRA_SOURCE)
add_library(pqxx ${CXX_SOURCES})

# Build a shared library
add_library(pqxx_shared SHARED ${CXX_SOURCES} ${CXX_SHARED_EXTRA_SOURCE})
target_compile_definitions(pqxx_shared PUBLIC PQXX_SHARED)
set_target_properties(
pqxx_shared PROPERTIES
OUTPUT_NAME pqxx-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
library_target_setup(pqxx_shared)
get_target_property(pqxx_target_type pqxx TYPE)
if(pqxx_target_type STREQUAL "SHARED_LIBRARY")
target_compile_definitions(pqxx PUBLIC PQXX_SHARED)
endif()

# XXX: Experimentally disabled.
#if(NOT SKIP_PQXX_STATIC)
# # Build a static libary
# add_library(pqxx_static STATIC ${CXX_SOURCES})
# set_target_properties(pqxx_static PROPERTIES OUTPUT_NAME pqxx)
# library_target_setup(pqxx_static)
#endif()
set_target_properties(
pqxx PROPERTIES
OUTPUT_NAME pqxx-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
library_target_setup(pqxx)

# install pkg-config file
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix \${prefix})
set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(VERSION ${PROJECT_VERSION})
configure_file(${CMAKE_SOURCE_DIR}/libpqxx.pc.in ${CMAKE_BINARY_DIR}/libpqxx.pc)
install(FILES ${CMAKE_BINARY_DIR}/libpqxx.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
58 changes: 41 additions & 17 deletions src/CMakeLists.txt.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ macro(library_target_setup tgt)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
get_target_property(name ${tgt} NAME)
get_target_property(output_name ${tgt} OUTPUT_NAME)
if(NOT name STREQUAL output_name)
# Create library symlink
get_target_property(target_type ${tgt} TYPE)
if(target_type STREQUAL "SHARED_LIBRARY")
set(library_prefix ${CMAKE_SHARED_LIBRARY_PREFIX})
set(library_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX})
elseif(target_type STREQUAL "STATIC_LIBRARY")
set(library_prefix ${CMAKE_STATIC_LIBRARY_PREFIX})
set(library_suffix ${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
add_custom_target(library_symlink ALL
${CMAKE_COMMAND} -E create_symlink
${library_prefix}${output_name}${library_suffix}
${library_prefix}${name}${library_suffix}
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${library_prefix}${name}${library_suffix}
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
endif()
endmacro()

file(
Expand All @@ -33,23 +54,26 @@ file(
###MAKTEMPLATE:ENDFOREACH
)

if(NOT SKIP_PQXX_SHARED)
set(CXX_SHARED_EXTRA_SOURCE)
add_library(pqxx ${CXX_SOURCES})

# Build a shared library
add_library(pqxx_shared SHARED ${CXX_SOURCES} ${CXX_SHARED_EXTRA_SOURCE})
target_compile_definitions(pqxx_shared PUBLIC PQXX_SHARED)
set_target_properties(
pqxx_shared PROPERTIES
OUTPUT_NAME pqxx-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
library_target_setup(pqxx_shared)
get_target_property(pqxx_target_type pqxx TYPE)
if(pqxx_target_type STREQUAL "SHARED_LIBRARY")
target_compile_definitions(pqxx PUBLIC PQXX_SHARED)
endif()

# XXX: Experimentally disabled.
#if(NOT SKIP_PQXX_STATIC)
# # Build a static libary
# add_library(pqxx_static STATIC ${CXX_SOURCES})
# set_target_properties(pqxx_static PROPERTIES OUTPUT_NAME pqxx)
# library_target_setup(pqxx_static)
#endif()
set_target_properties(
pqxx PROPERTIES
OUTPUT_NAME pqxx-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
library_target_setup(pqxx)

# install pkg-config file
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix \${prefix})
set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(VERSION ${PROJECT_VERSION})
configure_file(${CMAKE_SOURCE_DIR}/libpqxx.pc.in ${CMAKE_BINARY_DIR}/libpqxx.pc)
install(FILES ${CMAKE_BINARY_DIR}/libpqxx.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ file(
)

add_executable(runner ${TEST_SOURCES})
target_link_libraries(runner PUBLIC pqxx_shared)
target_link_libraries(runner PUBLIC pqxx)
add_test(
NAME runner
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ file(
)

add_executable(runner ${TEST_SOURCES})
target_link_libraries(runner PUBLIC pqxx_shared)
target_link_libraries(runner PUBLIC pqxx)
add_test(
NAME runner
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ file(
)

add_executable(unit_runner ${UNIT_TEST_SOURCES})
target_link_libraries(unit_runner PUBLIC pqxx_shared)
target_link_libraries(unit_runner PUBLIC pqxx)
target_include_directories(unit_runner PRIVATE ${PostgreSQL_INCLUDE_DIRS})
add_test(
NAME unit_runner
Expand Down
2 changes: 1 addition & 1 deletion test/unit/CMakeLists.txt.template
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ file(
)

add_executable(unit_runner ${UNIT_TEST_SOURCES})
target_link_libraries(unit_runner PUBLIC pqxx_shared)
target_link_libraries(unit_runner PUBLIC pqxx)
target_include_directories(unit_runner PRIVATE ${PostgreSQL_INCLUDE_DIRS})
add_test(
NAME unit_runner
Expand Down

0 comments on commit 4519227

Please sign in to comment.