Skip to content

Commit

Permalink
Support windows mingw (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yundi339 authored Nov 20, 2024
1 parent 64a91d1 commit f272c0e
Show file tree
Hide file tree
Showing 24 changed files with 355 additions and 135 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@
*.exe
*.out
*.app

# build
_include
_lib
.vscode
build.cmake
18 changes: 10 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ if (CYGWIN)
message(FATAL_ERROR "Sorry, DO NOT support Cygwin")
endif ()

if (MINGW)
message(FATAL_ERROR "Sorry, DO NOT support MinGW")
endif ()

###Options

if (WIN32)
Expand Down Expand Up @@ -71,9 +67,12 @@ if (WIN32)
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach ()
if (MINGW)
else ()
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach ()
endif ()
endif ()
endif ()

Expand All @@ -88,7 +87,10 @@ message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}")
message("CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
message("CMAKE_CXX_FLAGS_MINSIZEREL is ${CMAKE_CXX_FLAGS_MINSIZEREL}")

if (WIN32)
if (MINGW)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fPIC -pipe -std=gnu90")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fPIC -pipe -std=c++14 -fexceptions -Wno-invalid-offsetof")
elseif (WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP /wd4200")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /wd4200 /std:c++14")
else ()
Expand Down
3 changes: 3 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ MAKE_FILE := Makefile
DEFAULT_BUILD_DIR := build.cmake
BUILD_DIR := $(shell if [ -f $(MAKE_FILE) ]; then echo "."; else echo $(DEFAULT_BUILD_DIR); fi)
CMAKE3 := $(shell if which cmake3>/dev/null ; then echo cmake3; else echo cmake; fi;)
ifeq ($(MINGW),y)
CMAKE3 += -G "MinGW Makefiles"
endif

.PHONY: $(ALL_TARGETS)

Expand Down
70 changes: 70 additions & 0 deletions README_mingw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

# Windows MingW下编译

编译方式需要采用`MSYS2`,通过自带的`pacman`来添加`OpenSSL`库等。
编译workflow前需要先安装**MSYS2和依赖库**

## 安装MSYS2
通过[MSYS2官网](https://www.msys2.org/)下载msys2对应的安装包既可。
* 64位选x86_64
* 32位选i686

## 换源
打开`MSYS2``etc\pacman.d`目录,在对应的目录添加相应源`mirrorlist.mingw32``mirrorlist.mingw64``mirrorlist.msys`,镜像源请自行通过搜索引擎进行搜索。

## 安装依赖库
打开`MSYS2`终端,弹窗显示shell窗口。
更新依赖包
```powershell
pacman -Syuu
```

依次安装(自动安装openssl)
```powershell
pacman -S mingw-w64-x86_64-cmake mingw-w64-x86_64-extra-cmake-modules
pacman -S mingw-w64-x86_64-make
pacman -S mingw-w64-x86_64-gdb
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-x86_64-gcc
pacman -S make
pacman -S cmake
#(可选)
pacman -S mingw-w64-x86_64-gtest
```

# 编译
代码主要调整:
* 兼容原版Windows和MingW,编译时只需要添加`MINGW=y`
* `MingW`编译,开启了编译`libworkflow.so`
* `MingW`编译,开启了返回异常。关闭请更改`-fexceptions` -> `-fno-exceptions`
* 新增基线`tutorial-00-helloworld.cc`,方便检查
* 支持`gtest`
* 修复编译检查报错

## 编译release
```
make -j MINGW=y
cd tutorial/
make -j MINGW=y
cd test/
make -j check MINGW=y
```

## 编译debug
```
make -j MINGW=y DEBUG=y
cd tutorial/
make -j MINGW=y DEBUG=y
cd test/
make -j check MINGW=y DEBUG=y
```

## 清理
```powershell
make clean
```
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ install(

if (APPLE)
set(LIBSO ${LIB_DIR}/libworkflow.a)
elseif (NOT WIN32)
elseif (MINGW)
set(LIBSO ${LIB_DIR}/libworkflow.so)
add_custom_target(
SCRIPT_SHARED_LIB ALL
Expand Down
36 changes: 24 additions & 12 deletions src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ set(SRC

add_library(${PROJECT_NAME} OBJECT ${SRC})
if (WIN32)
target_compile_definitions(
${PROJECT_NAME} PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
if (MINGW)
target_compile_definitions(
${PROJECT_NAME} PRIVATE
)
else ()
target_compile_definitions(
${PROJECT_NAME} PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
endif ()
endif ()

if (KAFKA STREQUAL "y")
Expand All @@ -22,11 +28,17 @@ if (KAFKA STREQUAL "y")
)
add_library("client_kafka" OBJECT ${SRC})
if (WIN32)
target_compile_definitions(
"client_kafka" PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
if (MINGW)
target_compile_definitions(
"client_kafka" PRIVATE
)
else ()
target_compile_definitions(
"client_kafka" PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
endif ()
endif ()
endif ()
40 changes: 26 additions & 14 deletions src/factory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,39 @@ set(SRC

add_library(${PROJECT_NAME} OBJECT ${SRC})
if (WIN32)
target_compile_definitions(
${PROJECT_NAME} PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
if (MINGW)
target_compile_definitions(
${PROJECT_NAME} PRIVATE
)
else ()
target_compile_definitions(
${PROJECT_NAME} PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
endif ()
endif ()

if (KAFKA STREQUAL "y")
set(SRC
KafkaTaskImpl.cc
)
add_library("factory_kafka" OBJECT ${SRC})
if(WIN32)
target_compile_definitions(
"factory_kafka" PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
if (WIN32)
if (MINGW)
target_compile_definitions(
"factory_kafka" PRIVATE
)
else ()
target_compile_definitions(
"factory_kafka" PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
endif ()
else()
set_property(SOURCE KafkaTaskImpl.cc APPEND PROPERTY COMPILE_OPTIONS "-fno-rtti")
endif()
endif ()
endif ()
19 changes: 12 additions & 7 deletions src/kernel_win/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ set(SRC

add_library(${PROJECT_NAME} OBJECT ${SRC})
if (WIN32)
target_compile_definitions(
${PROJECT_NAME} PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
if (MINGW)
target_compile_definitions(
${PROJECT_NAME} PRIVATE
)
else ()
target_compile_definitions(
${PROJECT_NAME} PRIVATE
strdup=_strdup
strcasecmp=_stricmp
strncasecmp=_strnicmp
)
endif ()
endif ()

30 changes: 16 additions & 14 deletions src/kernel_win/Communicator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
Expand Down Expand Up @@ -464,7 +465,7 @@ void Communicator::handle_event_result(struct poller_result *res)
void Communicator::handle_sleep_result(struct poller_result *res)
{
SleepSession *session = (SleepSession *)res->data.context;
int cs_state;
int cs_state = CS_STATE_SUCCESS;

switch (res->state)
{
Expand Down Expand Up @@ -886,7 +887,7 @@ void Communicator::handle_incoming_request(struct poller_result *res)
CommTarget *target = entry->target;
CommSession *session = NULL;
int timeout;
int cs_state;
int cs_state = CS_STATE_SUCCESS;
int ret;

if (ctx->msgsize == 0)
Expand Down Expand Up @@ -1010,7 +1011,7 @@ void Communicator::handle_incoming_reply(struct poller_result *res)
CommTarget *target = entry->target;
CommSession *session = entry->session;
int timeout;
int cs_state;
int cs_state = CS_STATE_SUCCESS;
int ret;

switch (res->state)
Expand Down Expand Up @@ -1109,7 +1110,7 @@ void Communicator::handle_incoming_idle(struct poller_result *res)
CommConnEntry *entry = (CommConnEntry *)ctx->entry;
CommTarget *target = entry->target;
CommSession *session = NULL;
int cs_state;
int cs_state = CS_STATE_SUCCESS;

target->mutex.lock();
if (entry->state == CONN_STATE_IDLE)
Expand Down Expand Up @@ -1162,7 +1163,7 @@ void Communicator::handle_reply_result(struct poller_result *res)
CommService *service = entry->service;
CommSession *session = entry->session;
CommTarget *target = entry->target;
int cs_state;
int cs_state = CS_STATE_SUCCESS;
int timeout;
ULONG nleft = res->iobytes;
WSABUF *buffer = ctx->buffers;
Expand Down Expand Up @@ -1272,7 +1273,7 @@ void Communicator::handle_request_result(struct poller_result *res)
WriteContext *ctx = (WriteContext *)res->data.context;
CommConnEntry *entry = (CommConnEntry *)ctx->entry;
CommSession *session = entry->session;
int cs_state;
int cs_state = CS_STATE_SUCCESS;
int timeout;
ULONG nleft = res->iobytes;
WSABUF *buffer = ctx->buffers;
Expand Down Expand Up @@ -1456,12 +1457,13 @@ void Communicator::handle_read_result(struct poller_result *res)
CommConnEntry *entry = (CommConnEntry *)ctx->entry;
bool is_ssl = false;
int ret;
DWORD iobytes;
std::string buf;

if (entry->ssl && res->state == PR_ST_SUCCESS && res->iobytes > 0)
{
ret = BIO_write(entry->bio_recv, ctx->buffer.buf, res->iobytes);
if (ret == res->iobytes)
iobytes = BIO_write(entry->bio_recv, ctx->buffer.buf, res->iobytes);
if (iobytes == res->iobytes)
is_ssl = true;
else
{
Expand Down Expand Up @@ -1562,7 +1564,7 @@ void Communicator::handle_connect_result(struct poller_result *res)
delete ctx;
CommTarget *target = entry->target;
CommSession *session = entry->session;
int cs_state;
int cs_state = CS_STATE_SUCCESS;

switch (res->state)
{
Expand Down Expand Up @@ -1955,19 +1957,19 @@ void __thrdpool_schedule(const struct thrdpool_task *, void *,

int Communicator::increase_handler_thread()
{
void *buf = new char(4 * sizeof (void *));
char *buf = new char[4 * sizeof (void *)];

if (buf)
{
if (thrdpool_increase(this->thrdpool) >= 0)
{
struct thrdpool_task task = {Communicator::handler_thread_routine, this};

__thrdpool_schedule(&task, buf, this->thrdpool);
__thrdpool_schedule(&task, (void*)buf, this->thrdpool);
return 0;
}

free(buf);
delete []buf;
}

return -1;
Expand Down
1 change: 0 additions & 1 deletion src/kernel_win/SubTask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void SubTask::subtask_done()
{
SubTask *cur = this;
ParallelTask *parent;
SubTask **entry;

while (1)
{
Expand Down
Loading

0 comments on commit f272c0e

Please sign in to comment.