Skip to content

Commit

Permalink
fix win32 dll export/import
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Sep 27, 2021
1 parent d86cccb commit dec277f
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.xmake
build
lib
config.h
2 changes: 1 addition & 1 deletion include/co/config.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define CO_DLL 0
#define USING_CO_DLL 1
2 changes: 1 addition & 1 deletion include/co/config.h.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define CO_DLL ${CO_DLL}
#define USING_CO_DLL ${USING_CO_DLL}
18 changes: 13 additions & 5 deletions include/co/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
8 changes: 4 additions & 4 deletions include/co/flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; \
}()
Expand All @@ -63,5 +63,5 @@ __coapi void add_flag(
return value; \
}()

DEC_string(help);
DEC_string(config);
__codec DEC_string(help);
__codec DEC_string(config);
4 changes: 2 additions & 2 deletions include/co/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 12 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/co/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include <vector>
#include <map>

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)

Expand Down
2 changes: 1 addition & 1 deletion src/co/sock_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ void exit() {
} // sock
} // co

#ifdef CO_DLL
#ifdef BUILDING_CO_DLL
extern "C" {

BOOL WINAPI DllMain(HINSTANCE, DWORD reason, LPVOID) {
Expand Down
8 changes: 5 additions & 3 deletions src/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
8 changes: 1 addition & 7 deletions unitest/main.cc
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 0 additions & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit dec277f

Please sign in to comment.