apache / logging-log4cxx

Apache Log4cxx is a C++ port of Apache Log4j
http://logging.apache.org/log4cxx
Apache License 2.0
278 stars 122 forks source link

Fails to build with mingw-w64-gcc #388

Closed MehdiChinoune closed 4 months ago

MehdiChinoune commented 4 months ago
[133/160] Building CXX object src/main/cpp/CMakeFiles/log4cxx.dir/threadutility.cpp.obj
FAILED: src/main/cpp/CMakeFiles/log4cxx.dir/threadutility.cpp.obj
D:\Programs\msys64_test\ucrt64\bin\g++.exe -DFMT_SHARED -DLOG4CXX -Dlog4cxx_EXPORTS -ID:/dev/MINGW-packages/mingw-w64-log4cxx/src/build
-UCRT64/src/main/include -ID:/dev/MINGW-packages/mingw-w64-log4cxx/src/apache-log4cxx-1.2.0/src/main/include -ID:/Programs/msys64_test/
ucrt64/include/apr-1 -march=nocona -msahf -mtune=generic -O2 -pipe -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong -DWIN32 -O3 -DNDEBU
G -std=c++20 -MD -MT src/main/cpp/CMakeFiles/log4cxx.dir/threadutility.cpp.obj -MF src\main\cpp\CMakeFiles\log4cxx.dir\threadutility.cp
p.obj.d -o src/main/cpp/CMakeFiles/log4cxx.dir/threadutility.cpp.obj -c D:/dev/MINGW-packages/mingw-w64-log4cxx/src/apache-log4cxx-1.2.
0/src/main/cpp/threadutility.cpp
In file included from D:/Programs/msys64_test/ucrt64/include/winbase.h:2817,
                 from D:/Programs/msys64_test/ucrt64/include/windows.h:70,
                 from D:/dev/MINGW-packages/mingw-w64-log4cxx/src/apache-log4cxx-1.2.0/src/main/cpp/threadutility.cpp:30:
D:/dev/MINGW-packages/mingw-w64-log4cxx/src/apache-log4cxx-1.2.0/src/main/cpp/threadutility.cpp: In member function 'void log4cxx::help
ers::ThreadUtility::threadStartedNameThread(log4cxx::LogString, std::thread::id, std::thread::native_handle_type)':
D:/dev/MINGW-packages/mingw-w64-log4cxx/src/apache-log4cxx-1.2.0/src/main/cpp/threadutility.cpp:164:58: error: invalid 'static_cast' fr
om type 'std::thread::native_handle_type' {aka 'long long unsigned int'} to type 'HANDLE' {aka 'void*'}
  164 |                 if(FAILED(win32Func.SetThreadDescription(static_cast<HANDLE>(nativeHandle), wthreadName.c_str())))
      |                                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
swebb2066 commented 4 months ago

@MehdiChinoune Please post the version of mingw-w64 and g++ that you are using.

MehdiChinoune commented 4 months ago

mingw-w64: master (12.0.0+r81) g++: 14.1.0

I installed them (with apr and apr-util) from MSYS2/UCRT64 environment

swebb2066 commented 4 months ago

@MehdiChinoune I was unable to reproduce the error using gcc 13.2.0

I have not managed to upgrade gcc to 14.1.0. Using the command pacman -U /c/users/steph/Downloads/mingw-w64-x86_64-gcc-14.1.0-3-any.pkg.tar.zst results in

resolving dependencies...
warning: cannot resolve "mingw-w64-x86_64-gcc-libs=14.1.0-3", a dependency of "mingw-w64-x86_64-gcc"
:: The following package cannot be upgraded due to unresolvable dependencies:
      mingw-w64-x86_64-gcc

Could you post detailed instructions on installing gcc 14.1.0?

MehdiChinoune commented 4 months ago
pacman -Syu
MehdiChinoune commented 4 months ago

I have managed to build it with mingw-w64-gcc https://github.com/msys2/MINGW-packages/commit/59ce40c8db but the patch breaks building with mingw-w64-clang. To make it work on both, It should detect whether the threading library is posix (default for g++/libstdc++ through winpthreads) or win32 (default for clang++/libc++). I don't have enough knoweldge about C++ threading so I couldn't implement such detection.

rm5248 commented 4 months ago

To make it work on both, It should detect whether the threading library is posix (default for g++/libstdc++ through winpthreads) or win32 (default for clang++/libc++).

It should do this already, the error is in the #define WIN32, so I'm not sure why it's complaining(MSVC works fine).

MehdiChinoune commented 4 months ago

To make it work on both, It should detect whether the threading library is posix (default for g++/libstdc++ through winpthreads) or win32 (default for clang++/libc++).

It should do this already, the error is in the #define WIN32, so I'm not sure why it's complaining(MSVC works fine).

I don't understand?

swebb2066 commented 4 months ago

To make it work on both, It should detect whether the threading library is posix (default for g++/libstdc++ through winpthreads) or win32 (default for clang++/libc++).

The log4cxx build does detect whether the threading library is posix using the cmake try_compile command on the file:

#include <pthread.h>

int main(){
    pthread_t tid;
    pthread_setname_np(tid, "name");
}

and sets the macro to 0 or 1 in LOG4CXX_HAS_PTHREAD_SETNAME in build/src/main/include/log4cxx/private/log4cxx_private.h

When I build log4cxx using gcc 14.1.0, the macro LOG4CXX_HAS_PTHREAD_SETNAME is set to 1.

Please try compiling the above code on your system and post the result.

MehdiChinoune commented 4 months ago
swebb2066 commented 4 months ago

It detects pthread only for UNIX

mingw64 cmake seems to set UNIX to true

When I build log4cxx using /mingw64/bin/clang++.exe version 18.1.8, the macro LOG4CXX_HAS_PTHREAD_SETNAME is set to 1.

What cmake command are you using to build log4cxx?

MehdiChinoune commented 4 months ago

It detects pthread only for UNIX

mingw64 cmake seems to set UNIX to true

No, It does not

When I build log4cxx using /mingw64/bin/clang++.exe version 18.1.8, the macro LOG4CXX_HAS_PTHREAD_SETNAME is set to 1.

What cmake command are you using to build log4cxx?

I was talking about CLANG64 Environment where libc++ is the default C++ library (like clang on macOS and FreeBSD). On MINGW64 clang is using ibstdc++ by default (like clang on Linux)

MehdiChinoune commented 4 months ago

Maybe you installed cmake (/usr/bin/cmake) which is a Cygwin (MSYS) app instead on mingw-w64-x86_64-cmake which is a MinGW-w64 app.

swebb2066 commented 4 months ago

Yes, I see that UNIX is not true using mingw-w64-x86_64-cmake. Using the information in this stack overflow post

We now have to decide what we change:

  1. Test MINGW in cmake so pthread is used?
  2. Test a compiler macro in the code

What do you think?

MehdiChinoune commented 4 months ago

The issue is about detecting the C++ threading backend on MinGW. It's not about cmake on MINGW

swebb2066 commented 4 months ago

The issue is about detecting the C++ threading backend on MinGW.

It seems pthread and Win32 threading are available. We need to pick one.

MehdiChinoune commented 4 months ago

The issue is about detecting the C++ threading backend on MinGW.

It seems pthread and Win32 threading are available. We need to pick one.

No, that's not the case. libstdc++ use pthread while libc++ uses Win32. If you try to use one with the other It won't work.

swebb2066 commented 4 months ago

If you try to use one with the other It won't work. Using the following code


#include <pthread.h>
#include <iostream>

int main(){ pthread_t tid = pthread_self(); pthread_setname_np(tid, "test-name"); char result[16] = {0}; pthread_t current_thread = pthread_self(); if (pthread_getname_np(current_thread, result, sizeof(result)) < 0 || 0 == result[0]) return 1; std::cout << "result=" << result << '\n'; return 0; }


`/mingw64/bin/clang++.exe -o /tmp/test.exe test-pthread-setname.cpp`
results in
`result=test-name`
MehdiChinoune commented 4 months ago

You don't understand In MSYS2 we have many environments/variants https://www.msys2.org/docs/environments/ In MINGW64 libstdc++ is the default c++ library and It's using winpthreads as the default threading backend. On CLANG64 libc++ is the default c++ library even winpthreads is available, so finding pthread.h doesn't mean It's used by C++ as default backend.

You are testing clang++ on MINGW64.

swebb2066 commented 4 months ago

In MSYS2 we have many environments/variants

So what macros are provided to determine the default threading backend?

MehdiChinoune commented 4 months ago

In MSYS2 we have many environments/variants

So what macros are provided to determine the default threading backend?

That's what I am asking the maintainers to do.