From dec277ffe0c2fc132a7b1bd6acc42a3cc019d43a Mon Sep 17 00:00:00 2001 From: idealvin Date: Mon, 27 Sep 2021 23:19:51 +0800 Subject: [PATCH] fix win32 dll export/import --- .gitignore | 1 + include/co/config.h | 2 +- include/co/config.h.in | 2 +- include/co/def.h | 18 +++++++++++++----- include/co/flag.h | 8 ++++---- include/co/log.h | 4 ++-- src/CMakeLists.txt | 22 ++++++++++++---------- src/co/scheduler.h | 6 +++--- src/co/sock_win.cc | 2 +- src/xmake.lua | 8 +++++--- unitest/main.cc | 8 +------- xmake.lua | 1 - 12 files changed, 44 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index fff2e247b..99e2b7ff4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .xmake build lib +config.h diff --git a/include/co/config.h b/include/co/config.h index e5caab135..a83887985 100644 --- a/include/co/config.h +++ b/include/co/config.h @@ -1 +1 @@ -#define CO_DLL 0 +#define USING_CO_DLL 1 diff --git a/include/co/config.h.in b/include/co/config.h.in index e39fef7d8..194275147 100644 --- a/include/co/config.h.in +++ b/include/co/config.h.in @@ -1 +1 @@ -#define CO_DLL ${CO_DLL} \ No newline at end of file +#define USING_CO_DLL ${USING_CO_DLL} \ No newline at end of file diff --git a/include/co/def.h b/include/co/def.h index 4eaf00319..aae8a029b 100644 --- a/include/co/def.h +++ b/include/co/def.h @@ -37,9 +37,9 @@ typedef uint64_t uint64; #define MIN_INT32 ((int32) ~MAX_INT32) #define MIN_INT64 ((int64) ~MAX_INT64) -#define DISALLOW_COPY_AND_ASSIGN(ClassName) \ - ClassName(const ClassName&) = delete; \ - void operator=(const ClassName&) = delete +#define DISALLOW_COPY_AND_ASSIGN(T) \ + T(const T&) = delete; \ + void operator=(const T&) = delete #if (defined(__GNUC__) && __GNUC__ >= 3) || defined(__clang__) #define unlikely(x) __builtin_expect(!!(x), 0) @@ -48,14 +48,22 @@ typedef uint64_t uint64; #endif +#ifdef _MSC_VER #include "config.h" +#endif -#if defined(_MSC_VER) && CO_DLL > 0 - #ifdef CO_BUILDING_DLL +#if defined(_MSC_VER) && USING_CO_DLL > 0 + #ifdef BUILDING_CO_DLL + #define __codef __declspec(dllexport) + #define __codec __declspec(dllexport) #define __coapi __declspec(dllexport) #else + #define __codef + #define __codec __declspec(dllimport) #define __coapi __declspec(dllimport) #endif #else + #define __codef + #define __codec #define __coapi #endif diff --git a/include/co/flag.h b/include/co/flag.h index 5c569aefb..7a5178a66 100644 --- a/include/co/flag.h +++ b/include/co/flag.h @@ -30,7 +30,7 @@ __coapi void add_flag( } // namespace xx } // namespace flag -#define _CO_DEC_FLAG(type, name) __coapi extern type FLG_##name +#define _CO_DEC_FLAG(type, name) extern type FLG_##name // Declare a flag. // DEC_string(s); -> extern fastring FLG_s; @@ -43,7 +43,7 @@ __coapi void add_flag( #define DEC_string(name) extern fastring FLG_##name #define _CO_DEF_FLAG(type, id, name, value, help) \ - __coapi type FLG_##name = []() { \ + __codef type FLG_##name = []() { \ ::flag::xx::add_flag(id, #name, #value, help, __FILE__, __LINE__, &FLG_##name); \ return value; \ }() @@ -63,5 +63,5 @@ __coapi void add_flag( return value; \ }() -DEC_string(help); -DEC_string(config); +__codec DEC_string(help); +__codec DEC_string(config); diff --git a/include/co/log.h b/include/co/log.h index 9c0563718..47d76663f 100644 --- a/include/co/log.h +++ b/include/co/log.h @@ -4,8 +4,8 @@ #include "fastream.h" #include "atomic.h" -DEC_bool(cout); -DEC_int32(min_log_level); +__codec DEC_bool(cout); +__codec DEC_int32(min_log_level); namespace ___ { namespace log { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9935dde91..e0155ed05 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,7 +10,7 @@ endif() if(WIN32) set(ASM_FILES co/context/context_x64.asm) if(MSVC) - if(${CMAKE_SIZEOF_VOID_P} EQUAL 4) + if(CMAKE_SIZEOF_VOID_P EQUAL 4) set(ASM_FILES co/context/context_x86.asm) endif() set_property(SOURCE ${ASM_FILES} PROPERTY LANGUAGE ASM_MASM) @@ -67,16 +67,18 @@ endif() if(WIN32) - if(BUILD_SHARED_LIBS) - target_compile_definitions(co PRIVATE CO_BUILDING_DLL) - set(CO_DLL 1) - else() - set(CO_DLL 0) + if(MSVC) + if(BUILD_SHARED_LIBS) + target_compile_definitions(co PRIVATE BUILDING_CO_DLL) + set(USING_CO_DLL 1) + else() + set(USING_CO_DLL 0) + endif() + configure_file( + "${PROJECT_SOURCE_DIR}/include/co/config.h.in" + "${PROJECT_SOURCE_DIR}/include/co/config.h" + ) endif() - configure_file( - "../include/co/config.h.in" - "${PROJECT_SOURCE_DIR}/include/co/config.h" - ) target_compile_definitions(co PUBLIC WIN32_LEAN_AND_MEAN) target_compile_definitions(co PRIVATE _WINSOCK_DEPRECATED_NO_WARNINGS) if(NOT MSVC) diff --git a/src/co/scheduler.h b/src/co/scheduler.h index 3169cdeb2..0709eb9bd 100644 --- a/src/co/scheduler.h +++ b/src/co/scheduler.h @@ -26,9 +26,9 @@ #include #include -DEC_uint32(co_sched_num); -DEC_uint32(co_stack_size); -DEC_bool(co_debug_log); +__codec DEC_uint32(co_sched_num); +__codec DEC_uint32(co_stack_size); +__codec DEC_bool(co_debug_log); #define CO_DBG_LOG DLOG_IF(FLG_co_debug_log) diff --git a/src/co/sock_win.cc b/src/co/sock_win.cc index f0c08e933..968c86e6a 100644 --- a/src/co/sock_win.cc +++ b/src/co/sock_win.cc @@ -414,7 +414,7 @@ void exit() { } // sock } // co -#ifdef CO_DLL +#ifdef BUILDING_CO_DLL extern "C" { BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID) { diff --git a/src/xmake.lua b/src/xmake.lua index 35a70d624..c86e54cf6 100644 --- a/src/xmake.lua +++ b/src/xmake.lua @@ -24,12 +24,13 @@ target("libco") add_files("co/detours/disasm.cpp") if is_plat("windows") then if is_kind("shared") then - add_defines("CO_BUILDING_DLL") - set_configvar("CO_DLL", 1) + add_defines("BUILDING_CO_DLL") + set_configvar("USING_CO_DLL", 1) else - set_configvar("CO_DLL", 0) + set_configvar("USING_CO_DLL", 0) end add_configfiles("../include/co/config.h.in", {filename = "../include/co/config.h"}) + if is_arch("x64") then add_files("co/context/context_x64.asm") else @@ -38,6 +39,7 @@ target("libco") else add_defines("__MINGW_USE_VC2005_COMPAT=1") -- use 64bit time_t add_files("co/context/context.S") + add_syslinks("ws2_32") end else add_cxflags("-Wno-strict-aliasing") diff --git a/unitest/main.cc b/unitest/main.cc index 8a386ff24..7d03a6ebf 100644 --- a/unitest/main.cc +++ b/unitest/main.cc @@ -1,18 +1,12 @@ #include "co/unitest.h" #include "co/co.h" -DEC_uint32(co_sched_num); DEC_bool(noco); int main(int argc, char** argv) { flag::init(argc, argv); - if (!FLG_noco) { - if (FLG_co_sched_num > 8) FLG_co_sched_num = 8; - co::init(); - } - + if (!FLG_noco) co::init(); unitest::run_all_tests(); - if (!FLG_noco) co::exit(); return 0; } diff --git a/xmake.lua b/xmake.lua index 78b983dc2..c754d3fd5 100644 --- a/xmake.lua +++ b/xmake.lua @@ -25,7 +25,6 @@ if is_plat("windows") then elseif is_plat("mingw") then add_ldflags("-static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lwinpthread -Wl,-Bdynamic", {force = true}) set_optimize("faster") - add_syslinks("ws2_32") else set_optimize("faster") -- faster: -O2 fastest: -O3 none: -O0 --add_cxflags("-Wno-narrowing", "-Wno-sign-compare", "-Wno-class-memaccess", "-Wno-strict-aliasing")