From f2aaa65ea31ce7f1a03cc82b096a2684ffe6f963 Mon Sep 17 00:00:00 2001 From: Diego Sogari Date: Wed, 25 May 2016 23:49:39 -0300 Subject: [PATCH 1/3] fixes compilation on Windows using MinGW --- CMakeLists.txt | 13 ++++++++++--- examples/table_schema.cpp | 2 ++ src/nanodbc.cpp | 4 +++- test/base_test_fixture.h | 4 +++- test/mssql_test.cpp | 4 ++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bd1f81009..5fff81a62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(nanodbc) option(NANODBC_USE_UNICODE "build with unicode support turned on" OFF) option(NANODBC_USE_BOOST_CONVERT "build using Boost.Locale for string convert" OFF) option(NANODBC_HANDLE_NODATA_BUG "enable special handling for SQL_NO_DATA (required for vertica)" OFF) -option(NANODBC_DISABLE_ASYNC "disable async features entierly (may help resolve issues in VS2015 builds)" OFF) +option(NANODBC_DISABLE_ASYNC "disable async features entirely (may help resolve issues in VS2015 builds)" OFF) option(NANODBC_STATIC "build static instead of shared library" OFF) option(NANODBC_EXAMPLES "build examples" ON) option(NANODBC_TEST "build tests" ON) @@ -186,6 +186,8 @@ if(UNIX) set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${ODBC_LINK_FLAGS}") elseif(MSVC) set(ODBC_LIBRARIES odbc32.lib odbccp32.lib Ws2_32.lib) +elseif(MINGW) + set(ODBC_LIBRARIES odbc32 odbccp32) endif() ######################################## @@ -209,9 +211,14 @@ endif() if(NANODBC_INSTALL) install(FILES src/nanodbc.h DESTINATION include) if(NANODBC_STATIC) - install(TARGETS nanodbc ARCHIVE DESTINATION lib) + install(TARGETS nanodbc + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib) else() - install(TARGETS nanodbc LIBRARY DESTINATION lib) + install(TARGETS nanodbc + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) endif() message(STATUS "Target install: Turned on") else() diff --git a/examples/table_schema.cpp b/examples/table_schema.cpp index 92f079e34..d411d6481 100644 --- a/examples/table_schema.cpp +++ b/examples/table_schema.cpp @@ -11,7 +11,9 @@ #include #ifdef _WIN32 +#ifndef __MINGW32__ #define NOMINMAX +#endif #include // SQLLEN, SQLULEN, SQLHWND #endif #include diff --git a/src/nanodbc.cpp b/src/nanodbc.cpp index 741fe1f58..7dae11c5e 100755 --- a/src/nanodbc.cpp +++ b/src/nanodbc.cpp @@ -44,7 +44,9 @@ #ifdef _WIN32 // needs to be included above sql.h for windows - #define NOMINMAX + #ifndef __MINGW32__ + #define NOMINMAX + #endif #include #endif diff --git a/test/base_test_fixture.h b/test/base_test_fixture.h index 743dff2fe..b111bc3bc 100644 --- a/test/base_test_fixture.h +++ b/test/base_test_fixture.h @@ -25,7 +25,9 @@ #ifdef _WIN32 // needs to be included above sql.h for windows -#define NOMINMAX +#ifndef __MINGW32__ + #define NOMINMAX +#endif #include #endif #include diff --git a/test/mssql_test.cpp b/test/mssql_test.cpp index a9d4abe12..8fdd3add7 100644 --- a/test/mssql_test.cpp +++ b/test/mssql_test.cpp @@ -211,7 +211,7 @@ TEST_CASE_METHOD(mssql_fixture, "while_next_iteration_test", "[mssql][looping]") while_next_iteration_test(); } -#ifdef WIN32 +#if defined(WIN32) && !defined(NANODBC_DISABLE_ASYNC) TEST_CASE_METHOD(mssql_fixture, "async_test", "[mssql][async]") { HANDLE event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); @@ -236,4 +236,4 @@ TEST_CASE_METHOD(mssql_fixture, "async_test", "[mssql][async]") REQUIRE(row.get(0) >= 0); } -#endif //WIN32 +#endif //defined(WIN32) && !defined(NANODBC_DISABLE_ASYNC) From 57b7d5cf7010eeef9e83cc38cb6173278888569c Mon Sep 17 00:00:00 2001 From: Diego Sogari Date: Wed, 25 May 2016 23:57:59 -0300 Subject: [PATCH 2/3] correct indentation (tabs instead of spaces, as per the original) --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fff81a62..c67dea339 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,13 +212,13 @@ if(NANODBC_INSTALL) install(FILES src/nanodbc.h DESTINATION include) if(NANODBC_STATIC) install(TARGETS nanodbc - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib) + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib) else() - install(TARGETS nanodbc - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) + install(TARGETS nanodbc + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) endif() message(STATUS "Target install: Turned on") else() From c4fe9f10069bf6bcd76dbb197e491d0ccbdc1b3d Mon Sep 17 00:00:00 2001 From: Diego Sogari Date: Thu, 26 May 2016 10:44:26 -0300 Subject: [PATCH 3/3] alter CMake scripts to build all combinations of ANSI/Unicode in static/shared library versions --- CMakeLists.txt | 132 ++++++++++++++++++++++++++++------------ examples/CMakeLists.txt | 35 ++++++----- test/CMakeLists.txt | 36 +++++------ 3 files changed, 131 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c67dea339..78f783b2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,9 @@ -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.0.2) project(nanodbc) -option(NANODBC_USE_UNICODE "build with unicode support turned on" OFF) option(NANODBC_USE_BOOST_CONVERT "build using Boost.Locale for string convert" OFF) option(NANODBC_HANDLE_NODATA_BUG "enable special handling for SQL_NO_DATA (required for vertica)" OFF) option(NANODBC_DISABLE_ASYNC "disable async features entirely (may help resolve issues in VS2015 builds)" OFF) -option(NANODBC_STATIC "build static instead of shared library" OFF) option(NANODBC_EXAMPLES "build examples" ON) option(NANODBC_TEST "build tests" ON) option(NANODBC_INSTALL "generate install target" ON) @@ -107,9 +105,6 @@ if(UNIX) set(CMAKE_FLAGS "${CMAKE_FLAGS} ${ODBC_CFLAGS}") execute_process(COMMAND ${ODBC_CONFIG} --libs OUTPUT_VARIABLE ODBC_LINK_FLAGS OUTPUT_STRIP_TRAILING_WHITESPACE) - if(NANODBC_USE_UNICODE) - add_definitions(-DNANODBC_USE_IODBC_WIDE_STRINGS) - endif() endif() endif() @@ -131,17 +126,6 @@ else() add_definitions(-DNANODBC_ODBC_VERSION=${NANODBC_ODBC_VERSION}) endif() -if(NANODBC_USE_UNICODE) - message(STATUS "Unicode support: Turned on") - add_definitions(-DNANODBC_USE_UNICODE) - if(MSVC) - # Sets "Use Unicode Character Set" property in Visual Studio projects - add_definitions(-DUNICODE -D_UNICODE) - endif() -else() - message(STATUS "Unicode support: Turned off") -endif() - if(NANODBC_USE_BOOST_CONVERT) message(STATUS "Boost string convert: Turned on") add_definitions(-DNANODBC_USE_BOOST_CONVERT) @@ -191,38 +175,108 @@ elseif(MINGW) endif() ######################################## -## nanodbc library target +## nanodbc static library target - ANSI ######################################## -if(NANODBC_STATIC) - add_library(nanodbc STATIC src/nanodbc.cpp src/nanodbc.h) - message(STATUS "Build target: STATIC") -else() - add_library(nanodbc SHARED src/nanodbc.cpp src/nanodbc.h) - target_link_libraries(nanodbc ${Boost_LIBRARIES} ${ODBC_LIBRARIES}) - message(STATUS "Build target: SHARED") +add_library(nanodbca_static STATIC src/nanodbc.cpp src/nanodbc.h) +set_target_properties(nanodbca_static PROPERTIES OUTPUT_NAME nanodbca) +target_link_libraries(nanodbca_static PUBLIC ${Boost_LIBRARIES} ${ODBC_LIBRARIES}) + +if(UNIX) + set_target_properties(nanodbca_static PROPERTIES + COMPILE_FLAGS "${ODBC_CFLAGS}" + LIBRARY_OUTPUT_DIRECTORY "lib") endif() +if(NANODBC_INSTALL) + install(TARGETS nanodbca_static + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) +endif() + +######################################## +## nanodbc shared library target - ANSI +######################################## +add_library(nanodbca_shared SHARED src/nanodbc.cpp src/nanodbc.h) +set_target_properties(nanodbca_shared PROPERTIES OUTPUT_NAME nanodbca) +target_link_libraries(nanodbca_shared PUBLIC ${Boost_LIBRARIES} ${ODBC_LIBRARIES}) + if(UNIX) - set_target_properties(nanodbc PROPERTIES + set_target_properties(nanodbca_shared PROPERTIES COMPILE_FLAGS "${ODBC_CFLAGS}" LIBRARY_OUTPUT_DIRECTORY "lib") endif() +if(NANODBC_INSTALL) + install(TARGETS nanodbca_shared + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) +endif() + +######################################## +## nanodbc static library target - Unicode +######################################## +add_library(nanodbcu_static STATIC src/nanodbc.cpp src/nanodbc.h) +set_target_properties(nanodbcu_static PROPERTIES OUTPUT_NAME nanodbcu) +target_link_libraries(nanodbcu_static PUBLIC ${Boost_LIBRARIES} ${ODBC_LIBRARIES}) +target_compile_definitions(nanodbcu_static PUBLIC -DNANODBC_USE_UNICODE) + +if(MSVC OR MINGW) + # Enables "Use Unicode Character Set" in Visual Studio or MinGW + target_compile_definitions(nanodbcu_static PUBLIC -DUNICODE -D_UNICODE) +endif() + +if(ODBC_CONFIG) + target_compile_definitions(nanodbcu_static PUBLIC -DNANODBC_USE_IODBC_WIDE_STRINGS) +endif() + +if(UNIX) + set_target_properties(nanodbcu_static PROPERTIES + COMPILE_FLAGS "${ODBC_CFLAGS}" + LIBRARY_OUTPUT_DIRECTORY "lib") +endif() + +if(NANODBC_INSTALL) + install(TARGETS nanodbcu_static + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) +endif() + +######################################## +## nanodbc shared library target - Unicode +######################################## +add_library(nanodbcu_shared SHARED src/nanodbc.cpp src/nanodbc.h) +set_target_properties(nanodbcu_shared PROPERTIES OUTPUT_NAME nanodbcu) +target_link_libraries(nanodbcu_shared PUBLIC ${Boost_LIBRARIES} ${ODBC_LIBRARIES}) +target_compile_definitions(nanodbcu_shared PUBLIC -DNANODBC_USE_UNICODE) + +if(MSVC OR MINGW) + # Enables "Use Unicode Character Set" in Visual Studio or MinGW + target_compile_definitions(nanodbcu_shared PUBLIC -DUNICODE -D_UNICODE) +endif() + +if(ODBC_CONFIG) + target_compile_definitions(nanodbcu_shared PUBLIC -DNANODBC_USE_IODBC_WIDE_STRINGS) +endif() + +if(UNIX) + set_target_properties(nanodbcu_shared PROPERTIES + COMPILE_FLAGS "${ODBC_CFLAGS}" + LIBRARY_OUTPUT_DIRECTORY "lib") +endif() + +if(NANODBC_INSTALL) + install(TARGETS nanodbcu_shared + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) +endif() + +######################################## +## headers +######################################## if(NANODBC_INSTALL) install(FILES src/nanodbc.h DESTINATION include) - if(NANODBC_STATIC) - install(TARGETS nanodbc - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib) - else() - install(TARGETS nanodbc - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) - endif() - message(STATUS "Target install: Turned on") -else() - message(STATUS "Target install: Turned off") endif() ######################################## diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e02af7f9f..4f6ae8f3b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -4,24 +4,27 @@ set(examples northwind usage rowset_iteration table_schema) include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src ${ODBC_INCLUDE_DIR}) link_directories(${CMAKE_BINARY_DIR}/lib) -foreach(example ${examples}) - add_executable(example_${example} ${example}.cpp example_unicode_utils.h) - if (NANODBC_STATIC) - target_link_libraries(example_${example} nanodbc ${ODBC_LIBRARIES}) - else() - target_link_libraries(example_${example} nanodbc "${ODBC_LINK_FLAGS}") - endif() - add_dependencies(examples example_${example}) +foreach(config "ansi" "unicode") + foreach(example ${examples}) + add_executable(example_${example}_${config} ${example}.cpp example_unicode_utils.h) + if (${config} STREQUAL "ansi") + target_link_libraries(example_${example}_${config} nanodbca_shared) + else() + target_link_libraries(example_${example}_${config} nanodbcu_shared) + endif() + add_dependencies(examples example_${example}_${config}) + endforeach() endforeach() set(example_empty ${CMAKE_CURRENT_SOURCE_DIR}/empty.cpp) if(NOT EXISTS ${example_empty}) # Behavior is well-defined only for full paths. - configure_file(${example_empty}.in ${example_empty} COPYONLY) + configure_file(${example_empty}.in ${example_empty} COPYONLY) endif() -add_executable(example_empty ${example_empty} example_unicode_utils.h) -if (NANODBC_STATIC) - target_link_libraries(example_empty nanodbc ${ODBC_LIBRARIES}) -else() - target_link_libraries(example_empty nanodbc "${ODBC_LINK_FLAGS}") -endif() -add_dependencies(examples example_empty) + +add_executable(example_empty_ansi ${example_empty} example_unicode_utils.h) +target_link_libraries(example_empty_ansi nanodbca_shared) +add_dependencies(examples example_empty_ansi) + +add_executable(example_empty_unicode ${example_empty} example_unicode_utils.h) +target_link_libraries(example_empty_unicode nanodbcu_shared) +add_dependencies(examples example_empty_unicode) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 276ff966e..8671be6d1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8.7) +cmake_minimum_required(VERSION 3.0.2) include(ExternalProject) if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX) @@ -25,20 +25,22 @@ link_directories(${CMAKE_BINARY_DIR}/lib) file(GLOB headers *.h *.hpp) add_custom_target(tests DEPENDS tests catch) set(test_list mssql mysql odbc postgresql sqlite) -foreach(test_item ${test_list}) - set(test_name ${test_item}_tests) - add_executable(${test_name} main.cpp ${test_item}_test.cpp ${headers}) - add_dependencies(${test_name} catch) - if (NANODBC_STATIC) - target_link_libraries(${test_name} nanodbc ${ODBC_LIBRARIES}) - else() - target_link_libraries(${test_name} nanodbc "${ODBC_LINK_FLAGS}") - endif() - add_test(NAME ${test_name} COMMAND ${test_name}) - add_custom_target(${test_item}_test - COMMAND ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --output-on-failure -R ${test_name}) - add_custom_target(${test_item}_check - COMMAND ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --output-on-failure -R ${test_name} - DEPENDS ${test_name}) - add_dependencies(tests ${test_name}) +foreach(config "ansi" "unicode") + foreach(test_item ${test_list}) + set(test_name ${test_item}_${config}_tests) + add_executable(${test_name} main.cpp ${test_item}_test.cpp ${headers}) + add_dependencies(${test_name} catch) + if (${config} STREQUAL "ansi") + target_link_libraries(${test_name} nanodbca_shared) + else() + target_link_libraries(${test_name} nanodbcu_shared) + endif() + add_test(NAME ${test_name} COMMAND ${test_name}) + add_custom_target(${test_item}_${config}_test + COMMAND ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --output-on-failure -R ${test_name}) + add_custom_target(${test_item}_${config}_check + COMMAND ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --output-on-failure -R ${test_name} + DEPENDS ${test_name}) + add_dependencies(tests ${test_name}) + endforeach() endforeach()