Skip to content

Commit

Permalink
Fix building with mingw-w64-gcc (#389)
Browse files Browse the repository at this point in the history
* Generate pkgconfig files on Windows

pkgconfig files could be used on Windows by both MSVC and MinGW compilers

* Replace WIN32 with _WIN32

WIN32 is not defined by default on MinGW. While _WIN32 is known be always defined on Windows

reported before: https://issues.apache.org/jira/browse/LOGCXX-5

* git-ignore build directory as It's usually used by CMake users for out-of-tree building.

* Fix FindAPR.cmake on MinGW

WIN32 compile definition is required.

* Fix building with mingw-w64-gcc+winpthreads

`!(defined(_WIN32) && defined(_LIBCPP_VERSION))` was added because In some platforms winpthreads exist but It's not used by libc++.

* Silent a warning about Windows.h not being portable

* Use backslash with MSVC only
  • Loading branch information
MehdiChinoune authored Jul 19, 2024
1 parent 9b3095b commit 4f0abc2
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ src/test/cpp/Testing/
src/test/resources/org/
src/test/resources/output/
target/
build/

*.user
54 changes: 26 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,35 +170,33 @@ IF(MSVC AND BUILD_SHARED_LIBS AND LOG4CXX_INSTALL_PDB)
)
ENDIF()

if(UNIX)
# Support for pkg-config in consuming projects
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc"
)
# Support for pkg-config in consuming projects
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc"
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

if(LOG4CXX_QT_SUPPORT)
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx-qt.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}.pc"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

if(LOG4CXX_QT_SUPPORT)
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "${CMAKE_INSTALL_PREFIX}")
set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(VERSION "${log4cxx_VERSION_MAJOR}.${log4cxx_VERSION_MINOR}.${log4cxx_VERSION_PATCH}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/liblog4cxx-qt.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
endif(LOG4CXX_QT_SUPPORT)
endif(UNIX)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${LOG4CXX_LIB_NAME}-qt.pc"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
endif(LOG4CXX_QT_SUPPORT)

# Support for find_package(log4cxx) in consuming CMake projects using
# target_include_directories(myApplication PRIVATE $<TARGET_PROPERTY:log4cxx,INTERFACE_INCLUDE_DIRECTORIES>)
Expand Down
6 changes: 6 additions & 0 deletions src/cmake/FindAPR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ if(APR_FOUND)
else()
set(APR_LIBRARIES ${APR_LINK_LIBRARIES})
endif()
if(WIN32)
list(APPEND APR_COMPILE_DEFINITIONS WIN32)
endif()
else()
find_program(APR_CONFIG_EXECUTABLE
apr-1-config
Expand Down Expand Up @@ -78,6 +81,9 @@ else()
find_program(APR_DLL libapr-1.dll)
endif()
endif()
if(WIN32)
list(APPEND APR_COMPILE_DEFINITIONS WIN32)
endif()
endif()

find_package_handle_standard_args(APR
Expand Down
6 changes: 3 additions & 3 deletions src/examples/cpp/com/foo/config3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <log4cxx/helpers/loglog.h>
#include <log4cxx/helpers/transcoder.h>

#ifdef WIN32
#include <Windows.h>
#ifdef _WIN32
#include <windows.h>
#elif __APPLE__
#include <mach-o/dyld.h>
#elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
Expand All @@ -48,7 +48,7 @@ auto DefaultConfigurationFileNames(std::string& altPrefix) -> std::vector<std::s
static const int bufSize = 4096;
char buf[bufSize+1] = {0}, pathSepar = '/';
uint32_t bufCount = 0;
#if defined(WIN32)
#if defined(_WIN32)
GetModuleFileName(NULL, buf, bufSize);
pathSepar = '\\';
#elif defined(__APPLE__)
Expand Down
22 changes: 11 additions & 11 deletions src/main/cpp/loggingevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ LoggingEvent::KeySet LoggingEvent::getPropertyKeySet() const

const LogString& LoggingEvent::getCurrentThreadName()
{
#if defined(_WIN32)
using ThreadIdType = DWORD;
ThreadIdType threadId = GetCurrentThreadId();
#elif LOG4CXX_HAS_PTHREAD_SELF
#if LOG4CXX_HAS_PTHREAD_SELF && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
using ThreadIdType = pthread_t;
ThreadIdType threadId = pthread_self();
#elif defined(_WIN32)
using ThreadIdType = DWORD;
ThreadIdType threadId = GetCurrentThreadId();
#else
using ThreadIdType = int;
ThreadIdType threadId = 0;
Expand All @@ -362,16 +362,16 @@ const LogString& LoggingEvent::getCurrentThreadName()
return thread_id_string;
}

#if defined(_WIN32)
char result[20];
apr_snprintf(result, sizeof(result), LOG4CXX_WIN32_THREAD_FMTSPEC, threadId);
thread_id_string = Transcoder::decode(result);
#elif LOG4CXX_HAS_PTHREAD_SELF
#if LOG4CXX_HAS_PTHREAD_SELF && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
// pthread_t encoded in HEX takes needs as many characters
// as two times the size of the type, plus an additional null byte.
char result[sizeof(pthread_t) * 3 + 10];
apr_snprintf(result, sizeof(result), LOG4CXX_APR_THREAD_FMTSPEC, (void*) &threadId);
thread_id_string = Transcoder::decode(result);
#elif defined(_WIN32)
char result[20];
apr_snprintf(result, sizeof(result), LOG4CXX_WIN32_THREAD_FMTSPEC, threadId);
thread_id_string = Transcoder::decode(result);
#else
thread_id_string = LOG4CXX_STR("0x00000000");
#endif
Expand All @@ -389,14 +389,14 @@ const LogString& LoggingEvent::getCurrentThreadUserName()
return thread_name;
}

#if LOG4CXX_HAS_PTHREAD_GETNAME
#if LOG4CXX_HAS_PTHREAD_GETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
char result[16];
pthread_t current_thread = pthread_self();
if (pthread_getname_np(current_thread, result, sizeof(result)) < 0 || 0 == result[0])
thread_name = getCurrentThreadName();
else
thread_name = Transcoder::decode(result);
#elif WIN32
#elif defined(_WIN32)
typedef HRESULT (WINAPI *TGetThreadDescription)(HANDLE, PWSTR*);
static struct initialiser
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/multiprocessrollingfileappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ bool MultiprocessRollingFileAppender::rolloverInternal(Pool& p)

if (stat == APR_SUCCESS)
{
#ifdef WIN32
#ifdef _WIN32
snprintf(szUid, MAX_FILE_LEN, "%p", uid);
#else
snprintf(szUid, MAX_FILE_LEN, "%u", (unsigned int)uid);
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/nteventlogappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

#if (defined(WIN32) || defined(_WIN32)) && !defined(_WIN32_WCE)
#include <Windows.h>
#include <windows.h>
#include <Heapapi.h>
#include <log4cxx/nt/nteventlogappender.h>
#include <log4cxx/spi/loggingevent.h>
Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/threadutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <signal.h>
#include <mutex>

#if WIN32
#ifdef _WIN32
#include <windows.h>
#include <processthreadsapi.h>
#endif
Expand Down Expand Up @@ -138,12 +138,12 @@ void ThreadUtility::threadStartedNameThread(LogString threadName,
std::thread::id /*threadId*/,
std::thread::native_handle_type nativeHandle)
{
#if LOG4CXX_HAS_PTHREAD_SETNAME
#if LOG4CXX_HAS_PTHREAD_SETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
LOG4CXX_ENCODE_CHAR(sthreadName, threadName);
if (pthread_setname_np(static_cast<pthread_t>(nativeHandle), sthreadName.c_str()) < 0) {
LOGLOG_ERROR(LOG4CXX_STR("unable to set thread name"));
}
#elif WIN32
#elif defined(_WIN32)
typedef HRESULT (WINAPI *TSetThreadDescription)(HANDLE, PCWSTR);
static struct initialiser
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/timebasedrollingpolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const std::string TimeBasedRollingPolicy::createFile(const std::string& fileName

if (stat == APR_SUCCESS)
{
#ifdef WIN32
#ifdef _WIN32
snprintf(szUid, MAX_FILE_LEN, "%p", uid);
#else
snprintf(szUid, MAX_FILE_LEN, "%u", uid);
Expand Down
9 changes: 6 additions & 3 deletions src/main/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ CHECK_SYMBOL_EXISTS(mbsrtowcs "cwchar" HAS_MBSRTOWCS)
CHECK_SYMBOL_EXISTS(wcstombs "cstdlib" HAS_WCSTOMBS)
CHECK_SYMBOL_EXISTS(fwide "cwchar" HAS_FWIDE )
CHECK_SYMBOL_EXISTS(syslog "syslog.h" HAS_SYSLOG)
if(UNIX)
if(NOT MSVC)
set(CMAKE_REQUIRED_LIBRARIES "pthread")
CHECK_SYMBOL_EXISTS(pthread_sigmask "signal.h" HAS_PTHREAD_SIGMASK)
# pthread_sigmask exists on MINGW but with no function
if(NOT MINGW)
CHECK_SYMBOL_EXISTS(pthread_sigmask "signal.h" HAS_PTHREAD_SIGMASK)
endif()
CHECK_SYMBOL_EXISTS(pthread_self "pthread.h" HAS_PTHREAD_SELF)

# Check for the (linux) pthread_setname_np.
Expand All @@ -138,7 +141,7 @@ if(UNIX)
if(${PTHREAD_GETNAME_NP_FOUND})
set(HAS_PTHREAD_GETNAME 1)
endif()
endif(UNIX)
endif()

foreach(varName
HAS_THREAD_LOCAL
Expand Down
2 changes: 1 addition & 1 deletion src/main/include/log4cxx/spi/location/locationinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <string>
#include <string.h>

#if defined(_WIN32)
#if defined(_MSC_VER)
#define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '\\'
#else
#define LOG4CXX_SHORT_FILENAME_SPLIT_CHAR '/'
Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/abts.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <vector>
#include <string>

#ifdef WIN32
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/cpp/helpers/threadutilitytestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LOGUNIT_CLASS(ThreadUtilityTest)
LOGUNIT_TEST(testNullFunctions);
LOGUNIT_TEST(testCustomFunctions);
LOGUNIT_TEST(testDefaultFunctions);
#if LOG4CXX_HAS_PTHREAD_SETNAME || defined(WIN32)
#if LOG4CXX_HAS_PTHREAD_SETNAME || defined(_WIN32)
LOGUNIT_TEST(testThreadNameLogging);
#endif
LOGUNIT_TEST_SUITE_END();
Expand Down
8 changes: 4 additions & 4 deletions src/test/cpp/pattern/patternparsertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
#define LOG4CXX_TEST 1
#include <log4cxx/private/log4cxx_private.h>
#include <thread>
#if WIN32
#include <Windows.h>
#ifdef _WIN32
#include <windows.h>
#endif

using namespace log4cxx;
Expand Down Expand Up @@ -89,12 +89,12 @@ LOGUNIT_CLASS(PatternParserTestCase)
{
LogString threadName = LOG4CXX_STR("log4cxx-thr");

#if LOG4CXX_HAS_PTHREAD_SETNAME
#if LOG4CXX_HAS_PTHREAD_SETNAME && !(defined(_WIN32) && defined(_LIBCPP_VERSION))
LOG4CXX_ENCODE_CHAR(sthreadName, threadName);
if( pthread_setname_np( pthread_self(), sthreadName.c_str() ) < 0 ){
LOGLOG_ERROR( LOG4CXX_STR("unable to set thread name") );
}
#elif WIN32
#elif defined(_WIN32)
LOG4CXX_ENCODE_WCHAR(wthreadName, threadName);
HRESULT hr = SetThreadDescription(GetCurrentThread(), wthreadName.c_str());
if(FAILED(hr)){
Expand Down

0 comments on commit 4f0abc2

Please sign in to comment.