Browse Source

Remove windows.h include from hl.h (#804)

* Use hl_detect_debugger in hl.h for MSVC builds

This is already used on other platforms. It is also implemented on
windows using `IsDebuggerPresent` in src/std/error.c

* Move windows.h from hl.h to hlsystem.h

`windows.h` is a big include that adds a lot of declarations and macros,
which may conflict with user code when `hl.h` is included in hlc code.

The last function that `hl.h` was using from this header was
`IsDebuggerPresent`, which is no longer called directly, so we can just
move use of this header out from `hl.h` and into a private `hlsystem.h`
header.

* Use WIN32_LEAN_AND_MEAN for windows.h in hlc_main

This reduces the impact of the windows.h header on hlc code

* Remove windows _WINSOCKAPI_ hack

Now that hl.h no longer includes windows.h, this hack to prevent it
from including winsock.h is no longer necessary.

* Fix undefined strlen in src/std/date.c
tobil4sk 3 weeks ago
parent
commit
2c7f6c819c

+ 1 - 0
libs/directx/directx.cpp

@@ -1,5 +1,6 @@
 #define HL_NAME(n) directx_##n
 #include <hl.h>
+#undef _GUID
 
 #ifdef HL_WIN_DESKTOP
 #include <dxgi.h>

+ 1 - 0
libs/directx/dx12.cpp

@@ -1,5 +1,6 @@
 #define HL_NAME(n) dx12_##n
 #include <hl.h>
+#undef _GUID
 
 #ifdef HL_WIN_DESKTOP
 #include <dxgi.h>

+ 2 - 0
libs/directx/gamecontroller.c

@@ -1,5 +1,7 @@
 #define HL_NAME(n) directx_##n
 #include <hl.h>
+#include "hlsystem.h"
+
 #include <xinput.h>
 #include <InitGuid.h>
 #define DIRECTINPUT_VERSION 0x0800

+ 1 - 0
libs/directx/window.c

@@ -1,5 +1,6 @@
 #define HL_NAME(n) directx_##n
 #include <hl.h>
+#include "hlsystem.h"
 
 #define MAX_EVENTS 1024
 

+ 1 - 0
libs/sdl/gl.c

@@ -1,5 +1,6 @@
 #define HL_NAME(n) sdl_##n
 #include <hl.h>
+#include "hlsystem.h"
 
 #if defined(HL_IOS) || defined (HL_TVOS)
 #	include <SDL.h>

+ 2 - 0
libs/sdl/sdl.c

@@ -1,6 +1,8 @@
 #define HL_NAME(n) sdl_##n
 
 #include <hl.h>
+#include "hlsystem.h"
+
 #include <locale.h>
 
 #if defined(_WIN32) || defined(__ANDROID__) || defined(HL_IOS) || defined(HL_TVOS)

+ 1 - 1
libs/ssl/ssl.c

@@ -1,8 +1,8 @@
 #define HL_NAME(n) ssl_##n
 
-#define _WINSOCKAPI_
 #include <hl.h>
 #ifdef HL_WIN
+#undef _GUID
 #include <winsock2.h>
 #include <wincrypt.h>
 #else

+ 1 - 0
src/gc.c

@@ -21,6 +21,7 @@
  */
 #include "hl.h"
 #ifdef HL_WIN
+#	undef _GUID
 #	include <windows.h>
 #else
 #	include <sys/types.h>

+ 1 - 8
src/hl.h

@@ -231,13 +231,6 @@ typedef unsigned long long uint64;
 // -------------- UNICODE -----------------------------------
 
 #if defined(HL_WIN) && !defined(HL_LLVM)
-#if (defined(HL_WIN_DESKTOP) && !defined(HL_MINGW)) || defined(HL_XBS)
-#	include <Windows.h>
-#elif defined(HL_WIN_DESKTOP) && defined(HL_MINGW)
-#	include<windows.h>
-#else
-#	include <xdk.h>
-#endif
 #	include <wchar.h>
 typedef wchar_t	uchar;
 #	define USTR(str)	L##str
@@ -283,7 +276,7 @@ HL_API void uprintf( const uchar *fmt, const uchar *str );
 C_FUNCTION_END
 
 #if defined(HL_VCC)
-#	define hl_debug_break()	if( IsDebuggerPresent() ) __debugbreak()
+#	define hl_debug_break()	if( hl_detect_debugger() ) __debugbreak()
 #elif defined(HL_PS) && defined(_DEBUG)
 #	define hl_debug_break()	__debugbreak()
 #elif defined(HL_NX)

+ 3 - 12
src/hlc_main.c

@@ -26,19 +26,10 @@
 #endif
 
 #ifdef HL_WIN_DESKTOP
-# ifndef CONST
-#	define CONST
-# endif
-# ifndef IN
-#	define IN
-# endif
-# ifndef OUT
-#	define OUT
-# endif
-# ifndef OPTIONAL
-#	define OPTIONAL
-# endif
 #	pragma warning(disable:4091)
+# undef _GUID
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
 #if !defined(HL_MINGW)
 #	include <DbgHelp.h>
 #else

+ 16 - 0
src/hlsystem.h

@@ -0,0 +1,16 @@
+#ifndef HLSYSTEM_H
+#define HLSYSTEM_H
+
+#include <hl.h>
+
+/* System specific headers required internally */
+#ifdef HL_WIN
+#	undef _GUID
+#	if defined(HL_WIN_DESKTOP) || defined(HL_XBS)
+#		include <windows.h>
+#	else
+#		include <xdk.h>
+#	endif
+#endif
+
+#endif

+ 1 - 0
src/jit.c

@@ -24,6 +24,7 @@
 #endif
 #include <math.h>
 #include <hlmodule.h>
+#include "hlsystem.h"
 
 #ifdef __arm__
 #	error "JIT does not support ARM processors, only x86 and x86-64 are supported, please use HashLink/C native compilation instead"

+ 1 - 0
src/main.c

@@ -21,6 +21,7 @@
  */
 #include <hl.h>
 #include <hlmodule.h>
+#include "hlsystem.h"
 
 #ifdef HL_WIN
 #	include <locale.h>

+ 1 - 0
src/module.c

@@ -23,6 +23,7 @@
 #include <hlmodule.h>
 
 #ifdef HL_WIN
+#	undef _GUID
 #	include <windows.h>
 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
 #	define dlopen(l,p)		(void*)( (l) ? LoadLibraryA(l) : (HMODULE)&__ImageBase)

+ 1 - 0
src/profile.c

@@ -21,6 +21,7 @@
  */
 #include <hl.h>
 #include <hlmodule.h>
+#include "hlsystem.h"
 
 #ifdef HL_LINUX
 #include <semaphore.h>

+ 1 - 0
src/std/date.c

@@ -21,6 +21,7 @@
  */
 #include <hl.h>
 
+#include <string.h>
 #ifdef HL_CONSOLE
 #	include <posix/posix.h>
 #else

+ 1 - 0
src/std/debug.c

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include <hl.h>
+#include "hlsystem.h"
 #if defined(HL_LINUX) && (defined(__i386__) || defined(__x86_64__))
 #	include <sys/ptrace.h>
 #	include <sys/wait.h>

+ 2 - 0
src/std/error.c

@@ -20,6 +20,8 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include <hl.h>
+#include "hlsystem.h"
+
 #include <stdarg.h>
 #include <string.h>
 

+ 2 - 3
src/std/file.c

@@ -24,17 +24,16 @@
 #endif
 
 #include <hl.h>
+#include "hlsystem.h"
+
 #include <stdio.h>
 #ifdef HL_CONSOLE
 #	include <posix/posix.h>
 #endif
 #ifdef HL_WIN
 #ifdef HL_WIN_DESKTOP
-#	include <windows.h>
 #	include <io.h>
 #	include <fcntl.h>
-#elif defined(HL_XBO)
-#	include<xdk.h>
 #endif
 #	define fopen(name,mode) _wfopen(name,mode)
 #	define HL_UFOPEN

+ 1 - 0
src/std/fun.c

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include <hl.h>
+#include "hlsystem.h"
 
 HL_PRIM int hl_closure_stack_capture = 0;
 

+ 1 - 0
src/std/process.c

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include <hl.h>
+#include "hlsystem.h"
 
 #if defined(HL_CONSOLE)
 #	include <posix/posix.h>

+ 1 - 0
src/std/random.c

@@ -23,6 +23,7 @@
 #include <time.h>
 #include <string.h>
 #if defined(HL_WIN_DESKTOP)
+#	undef _GUID
 #	include <windows.h>
 #	include <process.h>
 #elif defined(HL_CONSOLE)

+ 1 - 1
src/std/socket.c

@@ -25,8 +25,8 @@
 #pragma warning(disable:4548)
 
 #	include <string.h>
-#	define _WINSOCKAPI_
 #	include <hl.h>
+#	undef _GUID
 #	include <winsock2.h>
 #	define FDSIZE(n)	(sizeof(void*) + (n) * sizeof(SOCKET))
 #	define SHUT_WR		SD_SEND

+ 1 - 0
src/std/sys.c

@@ -34,6 +34,7 @@
 #include <sys/stat.h>
 
 #if defined(HL_WIN)
+#	undef _GUID
 #	include <windows.h>
 #	include <direct.h>
 #	include <conio.h>

+ 1 - 0
src/std/thread.c

@@ -21,6 +21,7 @@
  */
 
 #include <hl.h>
+#include "hlsystem.h"
 
 typedef struct _hl_semaphore hl_semaphore;
 typedef struct _hl_condition hl_condition;

+ 1 - 0
src/std/types.c

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include <hl.h>
+#include "hlsystem.h"
 
 HL_PRIM hl_type hlt_array = { HARRAY };
 HL_PRIM hl_type hlt_bytes = { HBYTES };