You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2023/04/16 05:38:46 UTC

[logging-log4cxx] 01/01: Include odbcappendertestcase in unit tests when LOG4CXX_ENABLE_ODBC=on

This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch package_manager_support
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git

commit d633dafa2bfcc5cec0bcf6e80945e23447a268c2
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Sun Apr 16 15:38:20 2023 +1000

    Include odbcappendertestcase in unit tests when LOG4CXX_ENABLE_ODBC=on
---
 CMakeLists.txt                   | 20 +++++++++++++++++-
 src/CMakeLists.txt               | 15 ++++----------
 src/cmake/FindODBC.cmake         | 44 ++++++++++++++++++++++++++++++++++++++++
 src/cmake/SQLAllocHandleTest.cpp | 15 ++++++++++++++
 src/main/include/CMakeLists.txt  | 23 ---------------------
 src/test/cpp/CMakeLists.txt      |  2 +-
 src/test/cpp/db/CMakeLists.txt   |  4 +---
 7 files changed, 84 insertions(+), 39 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 52e7deed..7d533701 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -67,6 +67,22 @@ elseif(TARGET expat::expat)
   set(EXPAT_LIBRARIES expat::expat)
 endif()
 
+option(LOG4CXX_ENABLE_ODBC "Support logging via ODBC" OFF)
+if(LOG4CXX_ENABLE_ODBC)
+  find_package(ODBC)
+  if(${ODBC_FOUND})
+    set(HAS_ODBC 1)
+  else()
+    set(HAS_ODBC 0)
+  endif(${ODBC_FOUND})
+
+  if(NOT ${HAS_ODBC})
+    message(SEND_ERROR "ODBC not found but requested")
+  endif()
+else()
+  set(HAS_ODBC 0)
+endif(LOG4CXX_ENABLE_ODBC)
+
 option(LOG4CXX_ENABLE_ESMTP "Support logging via libesmtp" OFF)
 option(LIBESMTP_STATIC "Link to the libesmtp static library" OFF)
 if(LOG4CXX_ENABLE_ESMTP)
@@ -102,6 +118,9 @@ set(CMAKE_CXX_EXTENSIONS OFF)
 add_subdirectory(src)
 
 ## Installing
+if(WIN32)
+  option(LOG4CXX_INSTALL_PDB "Install .pdb files (if generated)"  ON)
+endif()
 include(GNUInstallDirs)
 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/src/main/include/log4cxx
   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
@@ -207,7 +226,6 @@ endif(LOG4CXX_QT_SUPPORT)
 # Get the varaibles from the subdirectories
 #
 get_directory_property( HAS_LIBESMTP DIRECTORY src/main/include DEFINITION HAS_LIBESMTP )
-get_directory_property( HAS_ODBC DIRECTORY src/main/include DEFINITION HAS_ODBC )
 get_directory_property( HAS_SYSLOG DIRECTORY src/main/include DEFINITION HAS_SYSLOG )
 get_directory_property( THREAD_IMPL DIRECTORY src DEFINITION THREAD_IMPL )
 get_directory_property( SMART_PTR_IMPL DIRECTORY src DEFINITION SMART_PTR_IMPL )
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index cfac9457..473118c8 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -26,17 +26,10 @@ if(HAS_LIBESMTP)
   target_include_directories(log4cxx PRIVATE ${ESMTP_INCLUDE_DIR})
   target_link_libraries(log4cxx PRIVATE ${ESMTP_LIBRARIES})
 endif()
-if(WIN32)
-	# The ODBC appender is always enabled in the Windows configuration
-	target_link_libraries(log4cxx PRIVATE odbc32.lib)
-	option(LOG4CXX_INSTALL_PDB "Install .pdb files (if generated)"  ON)
-else()
-	if(${odbc_FOUND})
-		target_include_directories(log4cxx PRIVATE ${odbc_INCLUDE_DIRS})
-		target_link_directories( log4cxx PRIVATE ${odbc_LIBRARY_DIRS})
-		target_link_libraries( log4cxx PRIVATE ${odbc_LIBRARIES})
-	endif(${odbc_FOUND})
-endif()
+if(HAS_ODBC)
+    target_include_directories(log4cxx PRIVATE ${ODBC_INCLUDE_DIR})
+    target_link_libraries( log4cxx PRIVATE ${ODBC_LIBRARIES})
+endif(HAS_ODBC)
 
 if(BUILD_TESTING)
    add_subdirectory(test)
diff --git a/src/cmake/FindODBC.cmake b/src/cmake/FindODBC.cmake
new file mode 100644
index 00000000..ed10269a
--- /dev/null
+++ b/src/cmake/FindODBC.cmake
@@ -0,0 +1,44 @@
+# Locate odbcinst include paths and libraries
+include(FindPackageHandleStandardArgs)
+
+# This module defines
+# ODBC_INCLUDE_DIR, where to find odbc.h, etc.
+# ODBC_LIBRARIES, the libraries to link against to use odbc.
+# ODBC_FOUND, set to 'yes' if found
+if(WIN32)
+  set(ODBC_LIBRARIES odbc32.lib user32.lib)
+  try_compile(ODBC_FOUND "${CMAKE_BINARY_DIR}/find-odbc"
+    "${CMAKE_CURRENT_LIST_DIR}/SQLAllocHandleTest.cpp"
+    LINK_LIBRARIES ${ODBC_LIBRARIES})
+else()
+  option(ODBC_STATIC "Link to the odbc static library" OFF)
+  if(NOT ODBC_STATIC) # 'pkg-config --static odbc' does not provide libodbc.a file path
+    find_package(PkgConfig)
+    pkg_check_modules(odbc odbc)
+    #message("odbc_FOUND=${odbc_FOUND}")
+  endif()
+
+  if(odbc_FOUND)
+    find_path(ODBC_INCLUDE_DIR
+              NAMES odbcinst.h
+              HINTS ${ODBC_INCLUDE_DIRS}
+              PATH_SUFFIXES odbc)
+    if (ODBC_STATIC OR NOT BUILD_SHARED_LIBS)
+      set(ODBC_LIBRARIES ${odbc_STATIC_LINK_LIBRARIES})
+    else()
+      set(ODBC_LIBRARIES ${odbc_LINK_LIBRARIES})
+    endif()
+  else()
+    find_path(ODBC_INCLUDE_DIR odbcinst.h)
+    if (ODBC_STATIC OR NOT BUILD_SHARED_LIBS)
+      find_library(ODBC_LIBRARIES NAMES libodbc.a)
+    else()
+      find_library(ODBC_LIBRARIES NAMES odbc)
+    endif()
+  endif()
+
+  #message("ODBC_INCLUDE_DIR=${ODBC_INCLUDE_DIR}")
+  #message("ODBC_LIBRARIES=${ODBC_LIBRARIES}")
+  find_package_handle_standard_args(ODBC
+      ODBC_LIBRARIES)
+endif()
diff --git a/src/cmake/SQLAllocHandleTest.cpp b/src/cmake/SQLAllocHandleTest.cpp
new file mode 100644
index 00000000..ba2a74ab
--- /dev/null
+++ b/src/cmake/SQLAllocHandleTest.cpp
@@ -0,0 +1,15 @@
+// SQLAllocHandleTest.cpp
+// compile with: odbc32.lib user32.lib
+#include <windows.h>
+#include <sqlext.h>
+
+int main() {
+   SQLHENV henv;
+   SQLRETURN retcode;
+
+   retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
+   if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
+       SQLFreeHandle(SQL_HANDLE_ENV, henv);
+   }
+   return 1;
+}
\ No newline at end of file
diff --git a/src/main/include/CMakeLists.txt b/src/main/include/CMakeLists.txt
index b0c524ff..675de038 100644
--- a/src/main/include/CMakeLists.txt
+++ b/src/main/include/CMakeLists.txt
@@ -102,32 +102,9 @@ endif()
 #
 include(CheckFunctionExists)
 include(CheckCXXSymbolExists)
-include(CheckIncludeFiles)
 include(CheckIncludeFileCXX)
 include(CheckLibraryExists)
 
-option(LOG4CXX_ENABLE_ODBC "Support logging via ODBC" OFF)
-if(LOG4CXX_ENABLE_ODBC)
-    if(WIN32)
-            CHECK_INCLUDE_FILES(sqlext.h HAS_ODBC)
-    else()
-            include(FindPkgConfig)
-
-            pkg_check_modules( odbc odbc )
-            if(${odbc_FOUND})
-                    set(HAS_ODBC 1)
-            else()
-                    set(HAS_ODBC 0)
-            endif(${odbc_FOUND})
-    endif(WIN32)
-
-    if(NOT ${HAS_ODBC})
-        message(SEND_ERROR "ODBC not found but requested")
-    endif()
-else()
-    set(HAS_ODBC 0)
-endif(LOG4CXX_ENABLE_ODBC)
-
 CHECK_INCLUDE_FILE_CXX(locale HAS_STD_LOCALE)
 CHECK_FUNCTION_EXISTS(mbsrtowcs HAS_MBSRTOWCS)
 CHECK_FUNCTION_EXISTS(wcstombs HAS_WCSTOMBS)
diff --git a/src/test/cpp/CMakeLists.txt b/src/test/cpp/CMakeLists.txt
index 35f1cf2d..70a51a1d 100644
--- a/src/test/cpp/CMakeLists.txt
+++ b/src/test/cpp/CMakeLists.txt
@@ -74,7 +74,7 @@ target_sources(rollingfileappendertestcase PRIVATE fileappendertestcase.cpp)
 # Tests defined in subdirectories
 add_subdirectory(helpers)
 add_subdirectory(customlogger)
-if(LOG4CXX_HAS_ODBC OR WIN32)
+if(HAS_ODBC)
     add_subdirectory(db)
 endif()
 add_subdirectory(defaultinit)
diff --git a/src/test/cpp/db/CMakeLists.txt b/src/test/cpp/db/CMakeLists.txt
index ccf4d9fd..b898d5aa 100644
--- a/src/test/cpp/db/CMakeLists.txt
+++ b/src/test/cpp/db/CMakeLists.txt
@@ -1,4 +1,2 @@
 add_executable(odbcappendertestcase odbcappendertestcase.cpp)
-target_compile_definitions(odbcappendertestcase PRIVATE ${LOG4CXX_COMPILE_DEFINITIONS} ${APR_COMPILE_DEFINITIONS} ${APR_UTIL_COMPILE_DEFINITIONS} )
-target_include_directories(odbcappendertestcase PRIVATE ${CMAKE_CURRENT_LIST_DIR} $<TARGET_PROPERTY:log4cxx,INCLUDE_DIRECTORIES>)
-target_link_libraries(odbcappendertestcase log4cxx testingFramework testingUtilities ${APR_LIBRARIES} ${APR_SYSTEM_LIBS})
+set(ALL_LOG4CXX_TESTS ${ALL_LOG4CXX_TESTS} odbcappendertestcase PARENT_SCOPE)