From bfee3db861fdb6dec84b257028351815349dd548 Mon Sep 17 00:00:00 2001 From: Decaf Code Date: Fri, 23 Nov 2018 13:55:33 -0500 Subject: [PATCH] Fix MSVC compatibility issues Project now builds in Visual Studio 2017, probably earlier versions too as long as they support enough of C99. --- hook/iohook.c | 1 + hook/meson.build | 5 ++++- hooklib/meson.build | 5 ++++- hooklib/serial.c | 4 ++++ hooklib/uart.c | 4 ++++ hooklib/uart.h | 4 ++++ inject/meson.build | 5 ++++- meson.build | 24 ++++++++++++++++-------- precompiled.c | 1 + precompiled.h | 14 ++++++++++++++ 10 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 precompiled.c diff --git a/hook/iohook.c b/hook/iohook.c index 446b12a..b85e78e 100644 --- a/hook/iohook.c +++ b/hook/iohook.c @@ -1,4 +1,5 @@ #define WIN32_NO_STATUS +/* See precompiled.h for more information */ #include #undef WIN32_NO_STATUS #include diff --git a/hook/meson.build b/hook/meson.build index 3658456..4fe4040 100644 --- a/hook/meson.build +++ b/hook/meson.build @@ -1,7 +1,10 @@ hook_lib = static_library( 'hook', include_directories : inc, - c_pch : '../precompiled.h', + c_pch : [ + '../precompiled.c', + '../precompiled.h', + ], sources : [ 'args.c', 'args.h', diff --git a/hooklib/meson.build b/hooklib/meson.build index e6e62ae..a74f4cd 100644 --- a/hooklib/meson.build +++ b/hooklib/meson.build @@ -1,7 +1,10 @@ hooklib_lib = static_library( 'hooklib', include_directories : inc, - c_pch : '../precompiled.h', + c_pch : [ + '../precompiled.c', + '../precompiled.h', + ], sources : [ 'serial.c', 'serial.h', diff --git a/hooklib/serial.c b/hooklib/serial.c index d260bce..fc203a1 100644 --- a/hooklib/serial.c +++ b/hooklib/serial.c @@ -1,6 +1,10 @@ #include +#ifdef __GNUC__ #include +#else +#include +#endif #include #include diff --git a/hooklib/uart.c b/hooklib/uart.c index 59e1570..3ef21f6 100644 --- a/hooklib/uart.c +++ b/hooklib/uart.c @@ -1,6 +1,10 @@ #include +#ifdef __GNUC__ #include +#else +#include +#endif #include #include diff --git a/hooklib/uart.h b/hooklib/uart.h index 1aab138..f136086 100644 --- a/hooklib/uart.h +++ b/hooklib/uart.h @@ -2,7 +2,11 @@ #include +#ifdef __GNUC__ #include +#else +#include +#endif #include #include diff --git a/inject/meson.build b/inject/meson.build index 65608b4..cc2dbea 100644 --- a/inject/meson.build +++ b/inject/meson.build @@ -1,7 +1,10 @@ executable( 'inject', include_directories : inc, - c_pch : '../precompiled.h', + c_pch : [ + '../precompiled.c', + '../precompiled.h', + ], sources : [ 'debug.c', 'debug.h', diff --git a/meson.build b/meson.build index 1412724..cdec382 100644 --- a/meson.build +++ b/meson.build @@ -1,18 +1,26 @@ project('capnhook', 'c', version: '0.1.0') add_project_arguments( - '-Wall', '-DCOBJMACROS', - '-ffunction-sections', - '-fdata-sections', + '-DWIN32_LEAN_AND_MEAN', language: 'c', ) -add_project_link_arguments( - '-Wl,--gc-sections', - '-static-libgcc', - language: 'c', -) +# Use get_argument_syntax() instead once Meson 0.49.0 releases +if meson.get_compiler('c').get_id() != 'msvc' + add_project_arguments( + '-Wall', + '-ffunction-sections', + '-fdata-sections', + language: 'c', + ) + + add_project_link_arguments( + '-Wl,--gc-sections', + '-static-libgcc', + language: 'c', + ) +endif inc = include_directories('.') diff --git a/precompiled.c b/precompiled.c new file mode 100644 index 0000000..5f656a4 --- /dev/null +++ b/precompiled.c @@ -0,0 +1 @@ +#include "precompiled.h" diff --git a/precompiled.h b/precompiled.h index ea6d60e..3a7ba49 100644 --- a/precompiled.h +++ b/precompiled.h @@ -1,13 +1,27 @@ +/* + Making NTSTATUS available is slightly awkward. See: + https://kirkshoop.github.io/2011/09/20/ntstatus.html +*/ + /* Win32 user-mode API */ +#define WIN32_NO_STATUS #include +#undef WIN32_NO_STATUS #include #include #include /* Win32 kernel-mode definitions */ +#ifdef __GNUC__ +/* MinGW needs to include this for PHYSICAL_ADDRESS to be defined. + The MS SDK throws a bunch of duplicate symbol errors instead. */ #include +#else +#include +#endif #include #include +#include /* ANSI C */ #include