From 33f9d2b86efb4401a1549c5644e47206c948812e Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Wed, 11 Oct 2017 15:22:31 -0700 Subject: [PATCH] Added target for MSVC DLLs for use in Visual Studio --- Makefile | 17 +++++++---- include/Debug.hpp | 75 +++++++++++++++++++++++++++++++++++------------ include/libzt.h | 33 +++++++++++++-------- makelib.bat | 2 ++ res/libztdll.rc | 36 +++++++++++++++++++++++ src/dll_desc.rc | 35 ---------------------- 6 files changed, 126 insertions(+), 72 deletions(-) create mode 100644 makelib.bat create mode 100644 res/libztdll.rc delete mode 100644 src/dll_desc.rc diff --git a/Makefile b/Makefile index 27b8fb6..66d314e 100644 --- a/Makefile +++ b/Makefile @@ -140,7 +140,7 @@ else STRIP=strip endif ifeq ($(LIBZT_DEBUG),1) - CFLAGS+=-g + #CFLAGS+=-g LIBZT_DEFS+=-DLIBZT_DEBUG endif ifeq ($(NS_DEBUG),1) @@ -285,11 +285,14 @@ utilities: $(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_INCLUDES) $(STACK_DRIVER_DEFS) -dll: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS) +win_dll: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS) + # First we use mingw to build our DLL @mkdir -p $(BUILD) obj mv *.o obj - windres -i src/dll_desc.rc -o obj/dll_desc.o - $(CXX) $(CXXFLAGS) -shared -o libzt.dll obj/*.o -s -shared -Wl,--subsystem,windows $(WINDEFS) + windres -i res/libztdll.rc -o obj/libztdllres.o + $(CXX) $(CXXFLAGS) -shared -o $(BUILD)/libzt.dll obj/*.o -Wl,--output-def,$(BUILD)/libzt.def,--out-implib,$(BUILD)/libzt.a $(WINDEFS) + # Then do the following to generate the mSVC DLL from the def file (which was generated from the MinGW DLL): + # lib /machine:x64 /def:libzt.def ifeq ($(STACK_PICO),1) static_lib: picotcp picotcp_driver libzt_socket_layer utilities $(ZTO_OBJS) @@ -379,6 +382,9 @@ intercept: -shared -o $(BUILD)/intercept.so $< -ldl @./check.sh $(BUILD)/intercept.so @date +"Build script finished on %F %T" +dlltest: + $(CXX) $(CXXFLAGS) + ############################################################################## ## Misc ## @@ -392,8 +398,9 @@ standardize: clean: -rm -rf .depend - -rm -f *.o *.s .depend* *.core core + -rm -f *.o *.s *.exp *.lib .depend* *.core core -rm -rf $(BUILD)/* + -rm -rf obj/* -find . -type f \( -name '*.a' -o -name '*.o' -o -name '*.so' -o -name \ '*.o.d' -o -name '*.out' -o -name '*.log' -o -name '*.dSYM' \) -delete diff --git a/include/Debug.hpp b/include/Debug.hpp index d807cbb..941e63a 100644 --- a/include/Debug.hpp +++ b/include/Debug.hpp @@ -33,9 +33,12 @@ #ifndef LIBZT_DEBUG_HPP #define LIBZT_DEBUG_HPP +#if defined(__MINGW32__) || !defined(_WIN32) #include -#include #include +#endif + +#include #include #if defined(__linux__) @@ -107,8 +110,12 @@ extern unsigned int gettid(); // defined in libzt.cpp #if defined(__ANDROID__) #define DEBUG_STACK(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "STACK[%ld]: %17s:%5d:%20s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) +#endif +#if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_STACK(fmt, args, ...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else - #define DEBUG_STACK(fmt, args...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \ +#define DEBUG_STACK(fmt, args ...) fprintf(stderr, ZT_YEL "STACK[%ld]: %17s:%5d:%25s: " fmt \ ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif @@ -119,9 +126,13 @@ extern unsigned int gettid(); // defined in libzt.cpp #if defined(__ANDROID__) #define DEBUG_TEST(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "TEST : %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) + #endif + #if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_TEST(fmt, args, ...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else - #define DEBUG_TEST(fmt, args...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt \ - "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_TEST(fmt, args ...) fprintf(stderr, ZT_CYN "TEST [%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_TEST(fmt, args...) @@ -132,9 +143,13 @@ extern unsigned int gettid(); // defined in libzt.cpp #if defined(__ANDROID__) #define DEBUG_ERROR(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "ERROR: %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) + #endif + #if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_ERROR(fmt, args, ...) fprintf(stderr, ZT_RED "ERROR[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else - #define DEBUG_ERROR(fmt, args...) fprintf(stderr, ZT_RED \ - "ERROR[%ld]: %17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_ERROR(fmt, args ...) fprintf(stderr, ZT_RED "ERROR[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_ERROR(fmt, args...) @@ -145,9 +160,13 @@ extern unsigned int gettid(); // defined in libzt.cpp #if defined(__ANDROID__) #define DEBUG_INFO(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "INFO : %17s:%5d:%20s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) + #endif + #if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_INFO(fmt, args, ...) fprintf(stderr, ZT_WHT "INFO [%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else - #define DEBUG_INFO(fmt, args...) fprintf(stderr, \ - "INFO [%ld]: %17s:%5d:%25s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_INFO(fmt, args ...) fprintf(stderr, ZT_WHT "INFO [%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_INFO(fmt, args...) @@ -158,9 +177,13 @@ extern unsigned int gettid(); // defined in libzt.cpp #if defined(__ANDROID__) #define DEBUG_TRANS(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "TRANS: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) + #endif + #if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_TRANS(fmt, args, ...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else - #define DEBUG_TRANS(fmt, args...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt \ - "\n" ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_TRANS(fmt, args ...) fprintf(stderr, ZT_GRN "TRANS[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_TRANS(fmt, args...) @@ -171,22 +194,36 @@ extern unsigned int gettid(); // defined in libzt.cpp #if defined(__ANDROID__) #define DEBUG_EXTRA(fmt, args...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, ZT_LOG_TAG, \ "EXTRA: %17s:%5d:%25s: " fmt "\n", ZT_FILENAME, __LINE__, __FUNCTION__, ##args)) + #endif + #if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_EXTRA(fmt, args, ...) fprintf(stderr, ZT_WHT "EXTRA[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #else - #define DEBUG_EXTRA(fmt, args...) fprintf(stderr, \ - "EXTRA[%ld]: %17s:%5d:%25s: " fmt "\n", ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + #define DEBUG_EXTRA(fmt, args ...) fprintf(stderr, ZT_WHT "EXTRA[%ld]: %17s:%5d:%25s: " fmt \ + ZT_RESET, ZT_THREAD_ID, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else #define DEBUG_EXTRA(fmt, args...) #endif #else // !LIBZT_DEBUG || !__NATIVE_TEST__ - #define DEBUG_ERROR(fmt, args...) - #define DEBUG_TEST(fmt, args...) - #define DEBUG_INFO(fmt, args...) - #define DEBUG_BLANK(fmt, args...) - #define DEBUG_ATTN(fmt, args...) - #define DEBUG_TRANS(fmt, args...) - #define DEBUG_EXTRA(fmt, args...) + #if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__)) // WINDOWS + #define DEBUG_ERROR(fmt, args, ...) + #define DEBUG_TEST(fmt, args, ...) + #define DEBUG_INFO(fmt, args, ...) + #define DEBUG_BLANK(fmt, args, ...) + #define DEBUG_ATTN(fmt, args, ...) + #define DEBUG_TRANS(fmt, args, ...) + #define DEBUG_EXTRA(fmt, args, ...) + #else + #define DEBUG_ERROR(fmt, args...) + #define DEBUG_TEST(fmt, args...) + #define DEBUG_INFO(fmt, args...) + #define DEBUG_BLANK(fmt, args...) + #define DEBUG_ATTN(fmt, args...) + #define DEBUG_TRANS(fmt, args...) + #define DEBUG_EXTRA(fmt, args...) + #endif #endif #endif // _H diff --git a/include/libzt.h b/include/libzt.h index 2d0a409..34aab82 100644 --- a/include/libzt.h +++ b/include/libzt.h @@ -33,18 +33,6 @@ #ifndef LIBZT_H #define LIBZT_H -#if defined(__MING32__) || defined(__MING64__) - #ifdef ADD_EXPORTS - #define ZT_SOCKET_API __declspec(dllexport) - #else - #define ZT_SOCKET_API __declspec(dllimport) - #endif - #define ZTCALL __cdecl -#else - #define ZT_SOCKET_API - #define ZTCALL -#endif - #include #include #include @@ -56,6 +44,26 @@ #include "Debug.hpp" #include "Defs.h" +/****************************************************************************/ +/* DLL export for Windows (and other cruft) */ +/****************************************************************************/ + +#if (defined(_WIN32) || defined(_WIN64)) && !(defined(__MINGW32__) || defined(__MINGW64__)) +typedef int ssize_t; +#endif + +#if defined(__MING32__) || defined(__MING64__) +#ifdef ADD_EXPORTS +#define ZT_SOCKET_API __declspec(dllexport) +#else +#define ZT_SOCKET_API __declspec(dllimport) +#endif +#define ZTCALL __cdecl +#else +#define ZT_SOCKET_API +#define ZTCALL +#endif + /****************************************************************************/ /* ZeroTier Service Controls */ /****************************************************************************/ @@ -66,7 +74,6 @@ extern "C" { // forward declarations from ZT1Service.h ZT_SOCKET_API int ZTCALL zts_get_device_id(char *devID); - ZT_SOCKET_API void ZTCALL init_network_stack(); /** diff --git a/makelib.bat b/makelib.bat new file mode 100644 index 0000000..7fe6084 --- /dev/null +++ b/makelib.bat @@ -0,0 +1,2 @@ +REM For conversion of MinGW DLL to MSVC lib +lib /machine:x64 /def:build\mingw32_nt-6.2\libzt.def /OUT:build\mingw32_nt-6.2\libzt.lib \ No newline at end of file diff --git a/res/libztdll.rc b/res/libztdll.rc new file mode 100644 index 0000000..ac9f8e7 --- /dev/null +++ b/res/libztdll.rc @@ -0,0 +1,36 @@ +#define APSTUDIO_READONLY_SYMBOLS +#include "windows.h" +#undef APSTUDIO_READONLY_SYMBOLS + +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK +#pragma code_page(1252) + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,9,1,7 + PRODUCTVERSION 2,2,2,2 + FILEFLAGSMASK 0x3fL + FILEFLAGS 0x0L + FILEOS 0x40004L + FILETYPE 0x1L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "080904b0" + BEGIN + VALUE "CompanyName", "My Company.\0" + VALUE "FileDescription", "My File\0" + VALUE "FileVersion", "1.9.1.7\0" + VALUE "InternalName", "my-real-file\0" + VALUE "LegalCopyright", "Copyright © 2012 My Company Ltd.\0" + VALUE "OriginalFilename", "my.dll\0" + VALUE "PrivateBuild", "820\0" + VALUE "ProductName", "My Company: my dll\0" + VALUE "ProductVersion", "2.2.2.2\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x809, 1200 + END +END \ No newline at end of file diff --git a/src/dll_desc.rc b/src/dll_desc.rc deleted file mode 100644 index fb7b4ab..0000000 --- a/src/dll_desc.rc +++ /dev/null @@ -1,35 +0,0 @@ -#include - -// DLL version information. -VS_VERSION_INFO VERSIONINFO -FILEVERSION 1,1,5,0 -PRODUCTVERSION 1,1,5,0 -FILEFLAGSMASK VS_FFI_FILEFLAGSMASK -#ifdef _DEBUG - FILEFLAGS VS_FF_DEBUG | VS_FF_PRERELEASE -#else - FILEFLAGS 0 -#endif -FILEOS VOS_NT_WINDOWS32 -FILETYPE VFT_DLL -FILESUBTYPE VFT2_UNKNOWN -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "080904b0" - BEGIN - VALUE "CompanyName", "ZeroTier, Inc." - VALUE "FileDescription", "ZeroTier Socket API library." - VALUE "FileVersion", "1.1.5.0" - VALUE "InternalName", "libzt" - VALUE "LegalCopyright", "©2017 ZeroTier, Inc." - VALUE "OriginalFilename", "libzt.dll" - VALUE "ProductName", "libzt" - VALUE "ProductVersion", "1.1.5.0" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x809, 1200 - END -END \ No newline at end of file