Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.

Add CMake package configuration (feature #243) #245

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,15 @@ if(NANODBC_STATIC OR WIN32)
message(STATUS "nanodbc build: Enable nanodbc target - STATIC")
else()
add_library(nanodbc SHARED src/nanodbc.cpp src/nanodbc.h)
target_link_libraries(nanodbc ${Boost_LIBRARIES} ${ODBC_LIBRARIES})
message(STATUS "nanodbc build: Enable nanodbc target - SHARED")
endif()

target_link_libraries(nanodbc ${Boost_LIBRARIES} ${ODBC_LIBRARIES})

target_include_directories(nanodbc INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>) # <prefix>/include

if(UNIX)
set_target_properties(nanodbc PROPERTIES
COMPILE_FLAGS "${ODBC_CFLAGS}"
Expand All @@ -222,17 +227,30 @@ endif()
## install targets
########################################
if(NANODBC_INSTALL)
set(NANODBC_CONFIG nanodbc-config)
# 'make install' to the correct location
if(NANODBC_STATIC)
install(TARGETS nanodbc
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
EXPORT ${NANODBC_CONFIG} # associate installed target files with export
INCLUDES DESTINATION include
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
else()
install(TARGETS nanodbc
RUNTIME DESTINATION bin
EXPORT ${NANODBC_CONFIG} # associate installed target files with export
INCLUDES DESTINATION include
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin) # for Windows
endif()
# Install public include headers
install(FILES src/nanodbc.h DESTINATION include)
# Make project importable from the install directory
## Generate and install *-config.cmake exporting targets from install tree.
install(EXPORT ${NANODBC_CONFIG} DESTINATION cmake)
# Make project importable from the build directory
## Generate file *-config.cmake exporting targets from build tree.
export(TARGETS nanodbc FILE ${NANODBC_CONFIG}.cmake)
endif()
message(STATUS "nanodbc build: Enable install target - ${NANODBC_INSTALL}")

Expand Down
9 changes: 8 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,16 @@ test_script:
after_test:
- ps: |
if ($env:GENERATOR -Match "NMake") {
'Write-Host "Running install:" -ForegroundColor Magenta'
Write-Host "Running install:" -ForegroundColor Magenta
cmake --build . --target install
}
- ps: |
if ($env:GENERATOR -Match "NMake") {
Write-Host "Building examples/client based on nanodbc package configuration:" -ForegroundColor Magenta
cd $env:APPVEYOR_BUILD_FOLDER\examples\client
cmake.exe -G $env:GENERATOR -DCMAKE_BUILD_TYPE=Release .
cmake --build . --config Release
}

# If you need to debug AppVeyor session (https://www.appveyor.com/docs/how-to/rdp-to-build-worker), then:
# 1. Uncomment the on_finish section below:
Expand Down
12 changes: 12 additions & 0 deletions examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.0.0)
project(nanodbc-client CXX)

if (NOT TARGET nanodbc)
find_package(nanodbc CONFIG REQUIRED)
endif()

message(STATUS "nanodbc package found - ${nanodbc_FOUND}")
message(STATUS "nanodbc package directory - ${nanodbc_DIR}")

add_executable(nanodbc_client nanodbc_client.cpp)
target_link_libraries(nanodbc_client nanodbc)
23 changes: 23 additions & 0 deletions examples/client/nanodbc_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <cstdlib>
#include <exception>
#include <iostream>
#include <nanodbc.h>

int main(int argc, char* argv[]) try
{
nanodbc::connection conn;
try
{
std::cout << conn.driver_name() << std::endl;
}
catch (nanodbc::database_error const& e)
{
std::cout << "Connection not open - OK" << std::endl;
}
return EXIT_SUCCESS;
}
catch (std::exception const& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}