Skip to content

Commit

Permalink
Add support for building with MinGW
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schulte committed Apr 23, 2020
1 parent f71fc50 commit 58f298e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cmake/AwsCFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function(aws_set_common_properties target)
if (LEGACY_COMPILER_SUPPORT)
list(APPEND AWS_C_FLAGS -Wno-strict-aliasing)
endif()

if(CMAKE_C_IMPLICIT_LINK_LIBRARIES MATCHES "mingw32")
list(APPEND AWS_C_FLAGS -D__USE_MINGW_ANSI_STDIO=1 -Wno-unused-local-typedefs)
endif()
endif()

check_include_file(stdint.h HAS_STDINT)
Expand Down
14 changes: 7 additions & 7 deletions include/aws/common/byte_order.inl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#include <aws/common/byte_order.h>
#include <aws/common/common.h>

#ifdef _MSC_VER
#ifdef _WIN32
# include <stdlib.h>
#else
# include <netinet/in.h>
#endif /* _MSC_VER */
#endif /* _WIN32 */

AWS_EXTERN_C_BEGIN

Expand All @@ -49,7 +49,7 @@ AWS_STATIC_IMPL uint64_t aws_hton64(uint64_t x) {
uint64_t v;
__asm__("bswap %q0" : "=r"(v) : "0"(x));
return v;
#elif defined(_MSC_VER)
#elif defined(_WIN32)
return _byteswap_uint64(x);
#else
uint32_t low = (uint32_t)x;
Expand All @@ -69,7 +69,7 @@ AWS_STATIC_IMPL uint64_t aws_ntoh64(uint64_t x) {
* Convert 32 bit integer from host to network byte order.
*/
AWS_STATIC_IMPL uint32_t aws_hton32(uint32_t x) {
#ifdef _MSC_VER
#ifdef _WIN32
return aws_is_big_endian() ? x : _byteswap_ulong(x);
#else
return htonl(x);
Expand Down Expand Up @@ -126,7 +126,7 @@ AWS_STATIC_IMPL double aws_htonf64(double x) {
* Convert 32 bit integer from network to host byte order.
*/
AWS_STATIC_IMPL uint32_t aws_ntoh32(uint32_t x) {
#ifdef _MSC_VER
#ifdef _WIN32
return aws_is_big_endian() ? x : _byteswap_ulong(x);
#else
return ntohl(x);
Expand All @@ -151,7 +151,7 @@ AWS_STATIC_IMPL double aws_ntohf64(double x) {
* Convert 16 bit integer from host to network byte order.
*/
AWS_STATIC_IMPL uint16_t aws_hton16(uint16_t x) {
#ifdef _MSC_VER
#ifdef _WIN32
return aws_is_big_endian() ? x : _byteswap_ushort(x);
#else
return htons(x);
Expand All @@ -162,7 +162,7 @@ AWS_STATIC_IMPL uint16_t aws_hton16(uint16_t x) {
* Convert 16 bit integer from network to host byte order.
*/
AWS_STATIC_IMPL uint16_t aws_ntoh16(uint16_t x) {
#ifdef _MSC_VER
#ifdef _WIN32
return aws_is_big_endian() ? x : _byteswap_ushort(x);
#else
return ntohs(x);
Expand Down
4 changes: 3 additions & 1 deletion include/aws/common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ struct aws_logger_vtable {
aws_log_subject_t subject,
const char *format,
...)
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#if defined(__MINGW32__)
__attribute__((format(gnu_printf, 4, 5)))
#elif defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
__attribute__((format(printf, 4, 5)))
#endif /* non-ms compilers: TODO - find out what versions format support was added in */
;
Expand Down
3 changes: 3 additions & 0 deletions include/aws/testing/aws_test_harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ struct aws_test_harness {
};

#if defined(_WIN32)
# ifdef __MINGW32__
# include <winsock2.h>
# endif
# include <windows.h>
static LONG WINAPI s_test_print_stack_trace(struct _EXCEPTION_POINTERS *exception_pointers) {
# if !defined(AWS_HEADER_CHECKER)
Expand Down
4 changes: 4 additions & 0 deletions source/windows/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ int aws_get_environment_value(
const struct aws_string *variable_name,
struct aws_string **value_out) {

#ifndef __MINGW32__
#pragma warning(push)
#pragma warning(disable : 4996)
#endif

const char *value = getenv(aws_string_c_str(variable_name));

#ifndef __MINGW32__
#pragma warning(pop)
#endif

if (value == NULL) {
*value_out = NULL;
Expand Down
8 changes: 5 additions & 3 deletions source/windows/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ void aws_debug_break(void) {
}

/* If I meet the engineer that wrote the dbghelp.h file for the windows 8.1 SDK we're gonna have words! */
#ifndef __MINGW32__
#pragma warning(disable : 4091)
#endif
#include <dbghelp.h>

struct win_symbol_data {
Expand Down Expand Up @@ -124,7 +126,7 @@ static void s_init_dbghelp_impl(void *user_data) {
return;
}

static bool s_init_dbghelp() {
static bool s_init_dbghelp(void) {
if (AWS_LIKELY(s_SymInitialize)) {
return true;
}
Expand Down Expand Up @@ -188,7 +190,7 @@ char **aws_backtrace_symbols(void *const *stack, size_t num_frames) {
/* no luck, record the address and last error */
DWORD last_error = GetLastError();
int len = snprintf(
sym_buf, AWS_ARRAY_SIZE(sym_buf), "at 0x%p: Failed to lookup symbol: error %u", stack[i], last_error);
sym_buf, AWS_ARRAY_SIZE(sym_buf), "at 0x%p: Failed to lookup symbol: error %lu", stack[i], last_error);
if (len > 0) {
struct aws_byte_cursor sym_cur = aws_byte_cursor_from_array(sym_buf, len);
aws_byte_buf_append_dynamic(&symbols, &sym_cur);
Expand All @@ -209,7 +211,7 @@ char **aws_backtrace_addr2line(void *const *frames, size_t stack_depth) {
void aws_backtrace_print(FILE *fp, void *call_site_data) {
struct _EXCEPTION_POINTERS *exception_pointers = call_site_data;
if (exception_pointers) {
fprintf(fp, "** Exception 0x%x occured **\n", exception_pointers->ExceptionRecord->ExceptionCode);
fprintf(fp, "** Exception 0x%lx occured **\n", exception_pointers->ExceptionRecord->ExceptionCode);
}

if (!s_init_dbghelp()) {
Expand Down
4 changes: 3 additions & 1 deletion tests/atomics_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

#ifdef _WIN32
# include <malloc.h>
# define alloca _alloca
# ifndef __MINGW32__
# define alloca _alloca
# endif
#elif defined(__FreeBSD__) || defined(__NetBSD__)
# include <stdlib.h>
#else
Expand Down

0 comments on commit 58f298e

Please sign in to comment.