Skip to content

Support mingw-w64 (from msys2) on Windows. #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

sighingnow
Copy link
Contributor

@sighingnow sighingnow commented Jan 23, 2021

Supporting for MinGW on Windows is a long-standling feature request for the cpprestsdk project. This PR makes the codebase compilable with mingw-w64 from msys2, include main library, test cases and examples.

At the same time, this PR has minimized the changeset for the convenience of code review.

Resolves #202, #1361 and #1541.

@sighingnow
Copy link
Contributor Author

/cc @barcharcraz.

Copy link
Member

@barcharcraz barcharcraz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a quick look at this, and I'm a bit concerned about how many new preprocessor branches are added. I'm also concerned that you are conditionally avoiding calling functions that should be available on mingw-w64, as they are in the UCRT and the legacy CRT.

Comment on lines +19 to +20
# on mingw there are tons of warnings.
set(WERROR OFF CACHE BOOL "Treat Warnings as Errors.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep WERROR on, and either address the warnings or tell people to turn it off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressing the warnings requires a lot changes in the codebase. Could I print a CMake warning if users are working with MinGW and WERROR is ON?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah a warning is fine, I just don't like setting werror based on platform

Comment on lines +31 to +36
SET(DEFAULT_BUILD_TYPE "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change the build type like this, just print a warning during configuration if the build isn't likely to work.

#
# see also: https://stackoverflow.com/questions/14125007/gcc-string-table-overflow-error-during-compilation
if(MINGW AND CMAKE_BUILD_TYPE STREQUAL "Debug")
message(FATAL_ERROR "mingw-w64 cannot build the debug version of this library")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a forward compat issue, just warn.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fixes that by using a warning.

#ifdef _WIN32
#if defined(_WIN32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

driveby change

@@ -69,7 +69,7 @@
#endif // __clang__
#endif // _WIN32

#ifdef _NO_ASYNCRTIMP
#if defined(_NO_ASYNCRTIMP) || defined(__MINGW32__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just force _NO_ASYNCRTIMP on mingw, don't special case the feature here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, actually this is a visibility macro, so I'm not sure this is even right, even on mingw if you're building a shared library I think you need __declspec(dllexport) (attribute((visibility)) may work as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember it correctly mingw-w64 (gcc) exports all symbol by default, just like what happens to the shared library on Linux.

But I would restore the dllexport behaviour on mingw-w64 by good.

Comment on lines +375 to +377
#if defined(__MINGW32__)
// link error on mingw-w64: undefined reference to `__imp__wcstod_l'
return wcstod(str, nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_wcstod_l should be available in mingw

#else // LINUX or APPLE
#endif // _WIN32

#if !defined(_WIN32) || defined(__MINGW32__) // LINUX, APPLE or MinGW
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably either be based on GCC or on detecting libstdc++

Comment on lines +177 to +179
bool _open_fsb_str(_filestream_callback* callback, const wchar_t* filename, std::ios_base::openmode mode, int prot) {
return _open_fsb_str(callback, utility::conversions::to_utf8string(filename).c_str(), mode, prot);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why these wide functions are required for mingw but somehow not for msvc with /UNICODE

Comment on lines +508 to +515
#if !defined(__MINGW32__)
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
#endif // !defined(__MINGW32__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just do _MSC_VER here, or perhaps _UCRT

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will fix.


#if defined(__MINGW32__)
const auto numChars = strnlen(tempBuffer, tempSize);
#else
const auto numChars = strnlen_s(tempBuffer, tempSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strnlen_s should be available in mingw (but not msys)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will double check that, but I remember here is an compile error.

@sighingnow
Copy link
Contributor Author

I took a quick look at this, and I'm a bit concerned about how many new preprocessor branches are added. I'm also concerned that you are conditionally avoiding calling functions that should be available on mingw-w64, as they are in the UCRT and the legacy CRT.

@barcharcraz Thanks for reviewing this PR! I will revise that as soon as my Windows laptop back.

@ysangkok
Copy link

ysangkok commented Apr 29, 2021

Got this error during "make -j":

[ 98%] Linking CXX shared library ../../../../Binaries/libhttplistener_test.dll
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/httplistener_test.dir/objects.a(building_response_tests.cpp.obj):building_response_tests.cpp:(.rdata$.refptr._ZN3web4http12status_codes2OKE[.refptr._ZN3web4http12status_codes2OKE]+0x0):
undefined reference to `web::http::status_codes::OK'

@Lord-Kamina
Copy link

I guess this PR is dead?

@sighingnow
Copy link
Contributor Author

I guess this PR is dead?

Not dead yet. Will address the review comments in this Sep.

@sighingnow
Copy link
Contributor Author

Closing. Will reopen when I come back to this project.

@sighingnow sighingnow closed this Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to build cpprestsdk with MingW on windows?
4 participants