Skip to content

Commit

Permalink
[cmake] fix make file on mingw on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Jul 16, 2023
1 parent 2e749b9 commit 7efee34
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 52 deletions.
1 change: 1 addition & 0 deletions build/cmake_env.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ELSEIF (("${CMAKE_SYSTEM_NAME}" MATCHES "MINGW32") OR ("${CMAKE_SYSTEM_NAME}" M
SET(OS_WINDOWS TRUE)
SET(ENV_MINGW TRUE)
MESSAGE(STATUS " Target OS: WINDOWS with MINGW32/64")
MESSAGE(STATUS " NOTES: ENV_MINGW will be the major macro to check")
ELSE ()
MESSAGE(STATUS " Target OS: unknown ${CMAKE_SYSTEM_NAME}")
EXIT ()
Expand Down
21 changes: 16 additions & 5 deletions gear-lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ SET(MP4_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libmp4/)
SET(DICT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libdict/)
SET(DEBUG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libdebug/)
SET(STREX_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libstrex/)
SET(HASH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libhash/)
SET(DARRAY_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libdarray/)
IF (NOT DEFINED ENV_MINGW)
SET(HASH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libhash/)
SET(THREAD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libthread/)
SET(SOCK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libsock/)
ENDIF ()
SET(GEVENT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libgevent/)
SET(MEDIA_IO_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libmedia-io/)
SET(QUEUE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libqueue/)
SET(LOG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/liblog/)
SET(SOCK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libsock/)
SET(FILE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libfile/)
SET(AVCAP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libavcap/)
SET(TIME_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libtime/)
Expand All @@ -44,17 +46,26 @@ ADD_SUBDIRECTORY(libposix)
ADD_SUBDIRECTORY(libstrex)
ADD_SUBDIRECTORY(libbitmap)
ADD_SUBDIRECTORY(libdict)
ADD_SUBDIRECTORY(libdebug)
ADD_SUBDIRECTORY(libfile)
ADD_SUBDIRECTORY(libhash)


IF (NOT DEFINED ENV_MINGW)
ADD_SUBDIRECTORY(libdarray)
ADD_SUBDIRECTORY(libthread)
ADD_SUBDIRECTORY(libsock)
ADD_SUBDIRECTORY(libgevent)
ENDIF ()

IF (NOT DEFINED OS_WINDOWS)
ADD_SUBDIRECTORY(libqueue)
ADD_SUBDIRECTORY(libthread)
ADD_SUBDIRECTORY(libgevent)
ADD_SUBDIRECTORY(libfile)
ADD_SUBDIRECTORY(libdebug)
ADD_SUBDIRECTORY(libhash)
ADD_SUBDIRECTORY(libdarray)
ADD_SUBDIRECTORY(libqueue)
ADD_SUBDIRECTORY(libtime)
ADD_SUBDIRECTORY(libsock)
ADD_SUBDIRECTORY(liblog)
ADD_SUBDIRECTORY(libmedia-io)
ADD_SUBDIRECTORY(libavcap)
Expand Down
2 changes: 1 addition & 1 deletion gear-lib/libgevent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LIST(APPEND SOURCE_FILES libgevent.c)
IF (DEFINED OS_LINUX)
LIST(APPEND SOURCE_FILES epoll.c libgevent.c poll.c select.c)
ELSEIF (DEFINED OS_WINDOWS)
LIST(APPEND SOURCE_FILES wepoll.c iocp.c)
LIST(APPEND SOURCE_FILES wepoll.c)
ENDIF ()

ADD_LIBRARY(gevent ${SOURCE_FILES})
66 changes: 33 additions & 33 deletions gear-lib/libgevent/wepoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@
#endif

#include <stdint.h>
#include <assert.h>

#include <stdlib.h>

#define WEPOLL_INTERNAL static
#define WEPOLL_INTERNAL_EXTERN static

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonportable-system-include-path"
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#elif defined(_MSC_VER)
#pragma warning(push, 1)
#endif

#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN

#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>

#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif


enum EPOLL_EVENTS {
EPOLLIN = (int) (1U << 0),
Expand Down Expand Up @@ -70,7 +101,7 @@ enum EPOLL_EVENTS {
#define EPOLL_CTL_DEL 3

typedef void* HANDLE;
typedef uintptr_t SOCKET;
//typedef uintptr_t SOCKET;

typedef union epoll_data {
void* ptr;
Expand Down Expand Up @@ -109,37 +140,6 @@ WEPOLL_EXPORT int epoll_wait(HANDLE ephnd,
} /* extern "C" */
#endif

#include <assert.h>

#include <stdlib.h>

#define WEPOLL_INTERNAL static
#define WEPOLL_INTERNAL_EXTERN static

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonportable-system-include-path"
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#elif defined(_MSC_VER)
#pragma warning(push, 1)
#endif

#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN

#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>

#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif

WEPOLL_INTERNAL int nt_global_init(void);

typedef LONG NTSTATUS;
Expand Down Expand Up @@ -482,7 +482,7 @@ WEPOLL_INTERNAL ts_tree_node_t* port_state_to_handle_tree_node(
*/

typedef struct reflock {
volatile long state; /* 32-bit Interlocked APIs operate on `long` values. */
volatile int state; /* 32-bit Interlocked APIs operate on `long` values. */
} reflock_t;

WEPOLL_INTERNAL int reflock_global_init(void);
Expand Down
8 changes: 4 additions & 4 deletions gear-lib/libhash/libhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static struct hash_item *hash_lookup(struct hash *h, const char *key, uint32_t *
i = *hash & (h->bucket-1);
list = &((struct hlist_head *)h->opaque_list)[i];

#if defined (OS_LINUX) || defined (OS_RTOS)
#if defined (OS_LINUX) || defined (OS_RTOS) || defined (ENV_MINGW)
hlist_for_each_entry_safe(hi, next, list, item) {
#elif defined (OS_WINDOWS)
hlist_for_each_entry_safe(hi, struct hash_item, next, struct hlist_node, list, item) {
Expand Down Expand Up @@ -188,7 +188,7 @@ void hash_destroy(struct hash *h)
}
list = h->opaque_list;
for (i = 0; i < h->bucket; i++) {
#if defined (OS_LINUX) || defined (OS_RTOS)
#if defined (OS_LINUX) || defined (OS_RTOS) || defined (ENV_MINGW)
hlist_for_each_entry_safe(hi, next, &list[i], item) {
#elif defined (OS_WINDOWS)
hlist_for_each_entry_safe(hi, struct hash_item, next, struct hlist_node, &list[i], item) {
Expand Down Expand Up @@ -311,7 +311,7 @@ int hash_get_all_cnt(struct hash *h)

for (i = 0; i < h->bucket - 1; i++) {
list = &((struct hlist_head *)h->opaque_list)[i];
#if defined (OS_LINUX) || defined (OS_RTOS)
#if defined (OS_LINUX) || defined (OS_RTOS) || defined (ENV_MINGW)
hlist_for_each_entry_safe(hi, next, list, item) {
#elif defined (OS_WINDOWS)
hlist_for_each_entry_safe(hi, struct hash_item, next, struct hlist_node, list, item) {
Expand All @@ -332,7 +332,7 @@ void hash_dump_all(struct hash *h, int *num, char **key, void **val)

for (i = 0; i < h->bucket - 1; i++) {
list = &((struct hlist_head *)h->opaque_list)[i];
#if defined (OS_LINUX) || defined (OS_RTOS)
#if defined (OS_LINUX) || defined (OS_RTOS) || defined (ENV_MINGW)
hlist_for_each_entry_safe(hi, next, list, item) {
#elif defined (OS_WINDOWS)
hlist_for_each_entry_safe(hi, struct hash_item, next, struct hlist_node, list, item) {
Expand Down
2 changes: 1 addition & 1 deletion gear-lib/libposix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ INCLUDE_DIRECTORIES(.)

LIST(APPEND SOURCE_FILES libposix.c)

IF (DEFINED OS_LINUX)
IF (DEFINED OS_LINUX OR DEFINED ENV_MINGW)
LIST(APPEND SOURCE_FILES libposix4nix.c)
ELSEIF (DEFINED OS_WINDOWS)

Expand Down
9 changes: 4 additions & 5 deletions gear-lib/libposix/libposix.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ extern "C" {

/******************************************************************************
* OS_WINDOWS
* support MSVC, msys2, mingw32/64 on Windows
* support MSVC on Windows
******************************************************************************/
#if defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER) || defined (ENV_MINGW)
#if defined (__WIN32__) || defined (WIN32) || defined (_MSC_VER)
#define OS_WINDOWS
#define GEAR_API __declspec(dllexport)
#include "libposix4win.h"


/******************************************************************************
* OS_LINUX
* support UNIX on Linux
* support UNIX on Linux, and mingw on Windows
******************************************************************************/
#elif defined (__linux__) /* || defined (__CYGWIN__) */
#elif defined (__linux__) || defined (__CYGWIN__) || defined (ENV_MINGW)
#define OS_LINUX
#define GEAR_API __attribute__((visibility("default")))
#include "libposix4nix.h"
Expand Down
7 changes: 4 additions & 3 deletions gear-lib/libposix/libposix4win.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#ifndef LIBPOSIX4WIN_H
#define LIBPOSIX4WIN_H

#ifndef ENV_MINGW
#include "msvclibx.h"
#ifdef ENV_MINGW
#error the msvclibx should not be include in mingw env
#endif

#include "msvclibx.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Expand All @@ -35,7 +37,6 @@
#include <ws2tcpip.h>
#include <tlhelp32.h>
#include <windows.h>
#ifndef ENV_MINGW
#include "pthreads4w/pthread.h"
#include "pthreads4w/semaphore.h"
#endif
Expand Down
5 changes: 5 additions & 0 deletions gear-lib/libsock/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ PROJECT(gear-lib)
INCLUDE_DIRECTORIES(. ${POSIX_INCLUDE_DIR} ${GEVENT_INCLUDE_DIR} ${DARRAY_INCLUDE_DIR} ${THREAD_INCLUDE_DIR})
AUX_SOURCE_DIRECTORY(. SOURCE_FILES)

IF (DEFINED OS_LINUX)
set(CMAKE_C_FLAGS "-DOS_LINUX")
ELSEIF (DEFINED OS_WINDOWS)
set(CMAKE_C_FLAGS "-DOS_WINDOWS")
ENDIF ()
ADD_LIBRARY(sock ${SOURCE_FILES})

0 comments on commit 7efee34

Please sign in to comment.