From 05e7cbccb5b602d1c9ee0889589a5b5c26cf755f Mon Sep 17 00:00:00 2001 From: Mike Kershaw / Dragorn Date: Fri, 28 Oct 2022 15:55:51 -0400 Subject: [PATCH] mingw32: Enable cross-building from Linux to Windows via mingw32-64. Fix include of Windows.h on case-sensitive systems, and add -DMINGW32 targets --- CMakeLists.txt | 54 ++++++++++++++++++++------ libwifiuserspace/CMakeLists.txt | 23 ++++++++++- libwifiuserspace/kernel/kernel.h | 2 + libwifiuserspace/rt2800usb/rt2800lib.c | 2 +- libwifiuserspace/rt2800usb/rt2800usb.c | 2 +- libwifiuserspace/rt2800usb/rt2x00usb.c | 2 +- libwifiuserspace/userspace/userspace.c | 7 +++- wifi_capture.c | 2 +- wifi_coconut.c | 2 +- wifi_coconut/wifi_coconut.h | 2 +- 10 files changed, 77 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a9650e5..a7b9178 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,6 @@ # Based on the hackrf cmakefiles, in turn based on the libftdi cmake -cmake_minimum_required(VERSION 2.8) -project(userspace-wifi C) -set(PACKAGE userspace-wifi) -include(${PROJECT_SOURCE_DIR}/cmake/set_release.cmake) -add_definitions(-DRELEASE="${RELEASE}") -set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) +cmake_minimum_required(VERSION 3.0) if (MSVC) include_directories(getopt) @@ -13,6 +8,18 @@ if (MSVC) if (FIND_DEBUG) add_definitions(/D FIND_DEBUG) endif() +elseif (MINGW32) + set(CMAKE_SYSTEM_NAME Windows) + set(TOOLCHAIN_PREFIX x86_64-w64-mingw32) + set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) + set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) + set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran) + set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) + set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") else() add_definitions(-Wall) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") @@ -21,6 +28,12 @@ else() endif() endif() +project(userspace-wifi C) +set(PACKAGE userspace-wifi) +include(${PROJECT_SOURCE_DIR}/cmake/set_release.cmake) +add_definitions(-DRELEASE="${RELEASE}") +set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules) + set(prefix ${CMAKE_INSTALL_PREFIX}) if(MSVC) @@ -33,6 +46,23 @@ if(MSVC) link_directories(${LIBUSB_1_LIBRARY_DIR}) set(INT_LIBUSB_LIBS ${LIBUSB_1_LIBRARIES}) + set (firmwaredir .) +elseif(MINGW32) + # find_package(PThread REQUIRED) + # include_directories(${PTHREAD_INCLUDE_DIR}) + # set(INT_THREAD_LIBS ${PTHREADS_LIBRARY}) + + #find_package(LibUSB-1.0 REQUIRED) + #include_directories(${LIBUSB_1_INCLUDE_DIR}) + #link_directories(${LIBUSB_1_LIBRARY_DIR}) + #set(INT_LIBUSB_LIBS ${LIBUSB_1_LIBRARIES}) + + find_package(Threads REQUIRED) + set(INT_THREAD_LIBS ${THREADS_LIBRARY}) + + include_directories("${VCPKG_HOME}/installed/x64-mingw-static/include/libusb-1.0") + set(INT_LIBUSB_LIBS "${VCPKG_HOME}/installed/x64-mingw-static/lib/libusb-1.0.a") + set (firmwaredir .) else() find_package(Threads REQUIRED) @@ -52,7 +82,6 @@ add_subdirectory(libwifiuserspace) include_directories(${PROJECT_SOURCE_DIR}/libwifiuserspace) SET(TOOLS - wifi_coconut_capture wifi_coconut ) @@ -62,8 +91,13 @@ if (MSVC) ) endif() +if (MINGW32) + add_library(libpthread STATIC IMPORTED) +else() + LIST(APPEND TOOLS_LINK_LIBS ${INT_THREAD_LIBS}) +endif() + LIST(APPEND TOOLS_LINK_LIBS wifiuserspace-static) -LIST(APPEND TOOLS_LINK_LIBS ${INT_THREAD_LIBS}) LIST(APPEND TOOLS_LINK_LIBS ${INT_LIBUSB_LIBS}) LIST(APPEND TOOLS_LINK_LIBS ${CMAKE_THREAD_LIBS_INIT}) @@ -75,10 +109,6 @@ if (MSVC) add_custom_target(copydlls) endif() -add_executable(wifi_coconut_capture - wifi_capture.c -) - add_executable(wifi_coconut wifi_coconut.c wifi_coconut/wifi_coconut.c diff --git a/libwifiuserspace/CMakeLists.txt b/libwifiuserspace/CMakeLists.txt index 0be1cde..7db03cd 100644 --- a/libwifiuserspace/CMakeLists.txt +++ b/libwifiuserspace/CMakeLists.txt @@ -1,6 +1,5 @@ # Based on the hackrf cmakefiles, in turn based on the libftdi cmake -cmake_minimum_required(VERSION 2.8) project(libwifiuserspace C) set(PACKAGE libwifiuserspace) include(${PROJECT_SOURCE_DIR}/../cmake/set_release.cmake) @@ -17,6 +16,28 @@ if(MSVC) link_directories(${LIBUSB_1_LIBRARY_DIR}) set(INT_LIBUSB_LIBS ${LIBUSB_1_LIBRARIES}) + set(sharedir "./") + set(INSTALL_LIB_DIR "./") + set(INSTALL_DEFAULT_BINDIR "./") +elseif (MINGW32) + #find_package(PThread REQUIRED) + #include_directories(${PTHREAD_INCLUDE_DIR}) + #set(INT_THREAD_LIBS ${PTHREADS_LIBRARY}) + + #find_package(LibUSB-1.0 REQUIRED) + #include_directories(${LIBUSB_1_INCLUDE_DIR}) + #link_directories(${LIBUSB_1_LIBRARY_DIR}) + #set(INT_LIBUSB_LIBS ${LIBUSB_1_LIBRARIES}) + + find_package(Threads REQUIRED) + set(INT_THREAD_LIBS ${THREADS_LIBRARY}) + + #find_package(USB1 REQUIRED) + #include_directories(${LIBUSB_INCLUDE_DIR}) + #set(INT_LIBUSB_LIBS ${LIBUSB_LIBRARIES}) + include_directories("${VCPKG_HOME}/installed/x64-mingw-static/include/libusb-1.0") + set(INT_LIBUSB_LIBS "${VCPKG_HOME}/installed/x64-mingw-static/lib/libusb-1.0.a") + set(sharedir "./") set(INSTALL_LIB_DIR "./") set(INSTALL_DEFAULT_BINDIR "./") diff --git a/libwifiuserspace/kernel/kernel.h b/libwifiuserspace/kernel/kernel.h index 9d8f06b..3898f92 100644 --- a/libwifiuserspace/kernel/kernel.h +++ b/libwifiuserspace/kernel/kernel.h @@ -113,7 +113,9 @@ * @x: first value * @y: second value */ +#ifndef max #define max(x, y) __cmp(x, y, >) +#endif /* * This looks more complex than it should be. But we need to diff --git a/libwifiuserspace/rt2800usb/rt2800lib.c b/libwifiuserspace/rt2800usb/rt2800lib.c index 218c860..efc922b 100644 --- a/libwifiuserspace/rt2800usb/rt2800lib.c +++ b/libwifiuserspace/rt2800usb/rt2800lib.c @@ -14,7 +14,7 @@ #ifndef _WIN32 #include #else -#include "Windows.h" +#include "windows.h" #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #define sleep(x) Sleep(x*1000) #endif diff --git a/libwifiuserspace/rt2800usb/rt2800usb.c b/libwifiuserspace/rt2800usb/rt2800usb.c index 286f0b8..9e16baf 100644 --- a/libwifiuserspace/rt2800usb/rt2800usb.c +++ b/libwifiuserspace/rt2800usb/rt2800usb.c @@ -19,7 +19,7 @@ #ifndef _WIN32 #include #else -#include +#include #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #endif diff --git a/libwifiuserspace/rt2800usb/rt2x00usb.c b/libwifiuserspace/rt2800usb/rt2x00usb.c index cf63bc1..dcd22bb 100644 --- a/libwifiuserspace/rt2800usb/rt2x00usb.c +++ b/libwifiuserspace/rt2800usb/rt2x00usb.c @@ -15,7 +15,7 @@ #ifndef _WIN32 #include #else -#include +#include #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #endif diff --git a/libwifiuserspace/userspace/userspace.c b/libwifiuserspace/userspace/userspace.c index ffe73b8..3c8a352 100644 --- a/libwifiuserspace/userspace/userspace.c +++ b/libwifiuserspace/userspace/userspace.c @@ -17,7 +17,7 @@ #include #include #else -#include +#include #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #define sleep(x) Sleep(x * 1000) #endif @@ -29,7 +29,7 @@ #include "userspace.h" #include "rt2800usb/rt2800usb.h" -#ifdef _WIN32 +#if defined(_WIN32) && !defined(__MINGW32__) /* * Kluge a windows time into a user time */ @@ -55,6 +55,9 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp) { ((a)->tv_usec CMP (b)->tv_usec) : \ ((a)->tv_sec CMP (b)->tv_sec)) +#endif + +#ifdef _WIN32 #define timeradd(a, b, result) \ do { \ (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ diff --git a/wifi_capture.c b/wifi_capture.c index ed21405..c9a30d2 100644 --- a/wifi_capture.c +++ b/wifi_capture.c @@ -16,7 +16,7 @@ #include #include #else -#include +#include #define sleep(x) Sleep(x*1000) #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #endif diff --git a/wifi_coconut.c b/wifi_coconut.c index ae4623e..798b861 100644 --- a/wifi_coconut.c +++ b/wifi_coconut.c @@ -17,7 +17,7 @@ #include #include #else -#include +#include #define sleep(x) Sleep(x*1000) #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #endif diff --git a/wifi_coconut/wifi_coconut.h b/wifi_coconut/wifi_coconut.h index 57ac17d..ce595fb 100644 --- a/wifi_coconut/wifi_coconut.h +++ b/wifi_coconut/wifi_coconut.h @@ -19,7 +19,7 @@ #include #include #else -#include +#include #define sleep(x) Sleep(x*1000) #define usleep(x) Sleep((x) < 1000 ? 1 : (x) / 1000) #endif