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/08/02 00:04:32 UTC
[logging-log4cxx] 01/01: Allow CFString code testing on non-Apple systems
This is an automated email from the ASF dual-hosted git repository.
swebb2066 pushed a commit to branch mock_apple
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit d3d5d490049afef56b4981186fbc07aa228c019b
Author: Stephen Webb <sw...@gmail.com>
AuthorDate: Wed Aug 2 10:04:09 2023 +1000
Allow CFString code testing on non-Apple systems
---
src/main/CMakeLists.txt | 8 ++++
src/main/cpp/CMakeLists.txt | 4 --
src/main/mock-apple/CFString.cpp | 66 +++++++++++++++++++++++++++
src/main/{ => mock-apple}/CMakeLists.txt | 22 +++------
src/main/mock-apple/CoreFoundation/CFString.h | 24 ++++++++++
src/test/cpp/CMakeLists.txt | 4 ++
6 files changed, 109 insertions(+), 19 deletions(-)
diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt
index 3c3fc641..0fa79048 100644
--- a/src/main/CMakeLists.txt
+++ b/src/main/CMakeLists.txt
@@ -26,6 +26,14 @@ target_include_directories(log4cxx PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
+if(LOG4CXX_CFSTRING)
+ if(APPLE)
+ target_link_libraries(log4cxx PUBLIC "-framework CoreFoundation")
+ else()
+ add_subdirectory(mock-apple)
+ endif()
+endif()
+
if(LOG4CXX_QT_SUPPORT)
target_include_directories(log4cxx-qt PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
diff --git a/src/main/cpp/CMakeLists.txt b/src/main/cpp/CMakeLists.txt
index be187a7b..16419ffe 100644
--- a/src/main/cpp/CMakeLists.txt
+++ b/src/main/cpp/CMakeLists.txt
@@ -216,10 +216,6 @@ if(${ENABLE_FMT_LAYOUT})
target_link_libraries(log4cxx PUBLIC fmt::fmt)
endif()
-if (LOG4CXX_CFSTRING)
- target_link_libraries(log4cxx PRIVATE "-framework CoreFoundation")
-endif()
-
if(LOG4CXX_ABI_CHECK)
message("Getting dependencies for ABI compatability check...")
# Get the latest version of abi-dumper and abi-compliance-checker
diff --git a/src/main/mock-apple/CFString.cpp b/src/main/mock-apple/CFString.cpp
new file mode 100644
index 00000000..0a172ac2
--- /dev/null
+++ b/src/main/mock-apple/CFString.cpp
@@ -0,0 +1,66 @@
+/* Mocked CFString implementation
+*/
+#include "CoreFoundation/CFString.h"
+#include <apr_pools.h>
+#include <exception>
+
+namespace {
+int throw_out_of_mem(int status)
+{
+ throw std::bad_alloc();
+ return status;
+}
+apr_pool_t* getStringPool()
+{
+ static struct cfstring_pool
+ {
+ apr_pool_t* ptr = 0;
+ cfstring_pool()
+ {
+ apr_pool_create_core_ex(&ptr, throw_out_of_mem, NULL);
+ }
+ ~cfstring_pool()
+ {
+ apr_pool_destroy(ptr);
+ }
+ } pool;
+ return pool.ptr;
+}
+} // namespace
+
+extern "C" {
+
+CFRange CFRangeMake(CFIndex loc, CFIndex len) {
+ CFRange result;
+ result.location = loc;
+ result.length = len;
+ return result;
+}
+
+CFIndex CFStringGetLength(CFStringRef theString) {
+ UniChar* data = (UniChar*)theString;
+ CFIndex result = 0;
+ while (data[result])
+ ++result;
+ return result;
+}
+void CFStringGetCharacters(CFStringRef theString, CFRange range, UniChar *buffer) {
+ UniChar* data = (UniChar*)theString;
+ CFIndex index = 0;
+ while (index < range.length) {
+ *buffer = data[range.location + index];
+ ++index;
+ ++buffer;
+ }
+}
+CFStringRef CFStringCreateWithCharacters(CFAllocatorRef alloc, const UniChar *chars, CFIndex numChars) {
+ return (CFStringRef)apr_palloc(getStringPool(), (numChars + 1) * sizeof(UniChar));
+}
+CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding) {
+ UniChar* result = (UniChar*)apr_palloc(getStringPool(), (strlen(cStr) + 1) * sizeof(UniChar));
+ for (UniChar *p = result; *p++ = *cStr++;)
+ ;
+ return (CFStringRef)result;
+}
+
+} // extern "C"
diff --git a/src/main/CMakeLists.txt b/src/main/mock-apple/CMakeLists.txt
similarity index 62%
copy from src/main/CMakeLists.txt
copy to src/main/mock-apple/CMakeLists.txt
index 3c3fc641..2c94fde7 100644
--- a/src/main/CMakeLists.txt
+++ b/src/main/mock-apple/CMakeLists.txt
@@ -14,20 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-add_subdirectory(include)
-add_subdirectory(cpp)
-add_subdirectory(cpp-qt)
-add_subdirectory(resources)
-# setup include directories
-include(GNUInstallDirs)
-target_include_directories(log4cxx PUBLIC
- $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
-)
+# Build the MockCoreFoundation library
+add_library(MockCoreFoundation STATIC CFString.cpp)
+target_compile_definitions(MockCoreFoundation PRIVATE ${APR_COMPILE_DEFINITIONS})
+target_include_directories(MockCoreFoundation INTERFACE PRIVATE ${APR_INCLUDE_DIR})
-if(LOG4CXX_QT_SUPPORT)
- target_include_directories(log4cxx-qt PUBLIC
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
- )
-endif(LOG4CXX_QT_SUPPORT)
+# Add to log4cxx as a dependency
+target_link_libraries(log4cxx PRIVATE MockCoreFoundation)
+target_include_directories(log4cxx INTERFACE PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
diff --git a/src/main/mock-apple/CoreFoundation/CFString.h b/src/main/mock-apple/CoreFoundation/CFString.h
new file mode 100644
index 00000000..23cd54e3
--- /dev/null
+++ b/src/main/mock-apple/CoreFoundation/CFString.h
@@ -0,0 +1,24 @@
+/* Mocked CFString.h
+*/
+#include <stdint.h>
+#if !defined(__COREFOUNDATION_CFSTRING__)
+#define __COREFOUNDATION_CFSTRING__ 1
+extern "C" {
+typedef unsigned short UniChar;
+typedef long CFIndex;
+typedef struct __CFRange {
+ CFIndex location;
+ CFIndex length;
+} CFRange;
+typedef const struct __CFString* CFStringRef;
+typedef const struct __CFAllocator* CFAllocatorRef;
+typedef uint32_t CFStringEncoding;
+CFRange CFRangeMake(CFIndex loc, CFIndex len);
+CFIndex CFStringGetLength(CFStringRef theString);
+void CFStringGetCharacters(CFStringRef theString, CFRange range, UniChar *buffer);
+CFStringRef CFStringCreateWithCharacters(CFAllocatorRef alloc, const UniChar *chars, CFIndex numChars);
+CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
+#define kCFAllocatorDefault 0
+#define CFSTR(cStr) CFStringCreateWithCString(kCFAllocatorDefault, cStr, 0)
+}
+#endif /* ! __COREFOUNDATION_CFSTRING__ */
diff --git a/src/test/cpp/CMakeLists.txt b/src/test/cpp/CMakeLists.txt
index 3b66d1c5..af71b8ba 100644
--- a/src/test/cpp/CMakeLists.txt
+++ b/src/test/cpp/CMakeLists.txt
@@ -109,8 +109,12 @@ if(LOG4CXX_CFSTRING)
endif()
foreach(testName IN LISTS ALL_LOG4CXX_TESTS)
if (${testName} IN_LIST CFSTR_TESTS)
+ if(APPLE)
target_compile_options(${testName} PRIVATE "-fconstant-cfstrings")
target_link_libraries(${testName} PRIVATE "-framework CoreFoundation")
+ else()
+ target_link_libraries(${testName} PRIVATE MockCoreFoundation)
+ endif()
endif()
target_compile_definitions(${testName} PRIVATE ${TEST_COMPILE_DEFINITIONS} ${LOG4CXX_COMPILE_DEFINITIONS} ${APR_COMPILE_DEFINITIONS} ${APR_UTIL_COMPILE_DEFINITIONS} )
target_include_directories(${testName} PRIVATE ${CMAKE_CURRENT_LIST_DIR} $<TARGET_PROPERTY:log4cxx,INCLUDE_DIRECTORIES>)