Fix Windows build: add conditional headers for sys/socket.h
- Add #ifdef _WIN32 to use winsock2.h/ws2tcpip.h instead of sys/socket.h - Update build.sh to support both Linux and Windows/MSYS2 - Auto-detect platform and configure accordingly (--host=x86_64-w64-mingw32 for Windows) - All 22 tests pass on Linux
This commit is contained in:
1 parent
151bfc415c
commit
1e79bd42bb
2 files changed
+122
-5
No files matched your search
@@ -0,0 +1,112 @@
|
||||
#!/bin/bash
|
||||
# Build script for uTun (supports both Linux and Windows/MSYS2)
|
||||
# Usage: ./build.sh [options]
|
||||
# Options:
|
||||
# -h, --help Show this help
|
||||
# --clean Clean before build
|
||||
# -j[N] Build with N parallel jobs (default: 4)
|
||||
|
||||
set -e # Exit on error
|
||||
|
||||
# Detect platform
|
||||
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ -n "$MSYSTEM" ]]; then
|
||||
IS_WINDOWS=1
|
||||
echo "Detected Windows/MSYS2 environment"
|
||||
else
|
||||
IS_WINDOWS=0
|
||||
echo "Detected Linux environment"
|
||||
fi
|
||||
|
||||
# Parse arguments
|
||||
CLEAN=0
|
||||
JOBS="-j4"
|
||||
MAKE_ARGS=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
echo "Usage: ./build.sh [options]"
|
||||
echo "Options:"
|
||||
echo " -h, --help Show this help"
|
||||
echo " --clean Clean before build"
|
||||
echo " -j[N] Build with N parallel jobs (default: 4)"
|
||||
exit 0
|
||||
;;
|
||||
--clean)
|
||||
CLEAN=1
|
||||
shift
|
||||
;;
|
||||
-j*)
|
||||
JOBS="$1"
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
MAKE_ARGS="$MAKE_ARGS $1"
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check required tools
|
||||
if ! command -v gcc &> /dev/null; then
|
||||
echo "Error: gcc not found."
|
||||
if [ "$IS_WINDOWS" -eq 1 ]; then
|
||||
echo "Please install: pacman -S mingw-w64-ucrt-x86_64-toolchain"
|
||||
else
|
||||
echo "Please install build-essential or gcc"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Clean if requested
|
||||
if [ "$CLEAN" -eq 1 ]; then
|
||||
echo "Cleaning build..."
|
||||
make clean 2>/dev/null || true
|
||||
rm -f config.status config.h
|
||||
fi
|
||||
|
||||
# Check if configure script exists
|
||||
if [ ! -f ./configure ]; then
|
||||
echo "Configure script not found. Generating..."
|
||||
if ! command -v autoreconf &> /dev/null; then
|
||||
echo "Error: autoreconf not found."
|
||||
if [ "$IS_WINDOWS" -eq 1 ]; then
|
||||
echo "Install autotools: pacman -S autoconf automake libtool"
|
||||
else
|
||||
echo "Install autotools: apt-get install autoconf automake libtool"
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
autoreconf -fiv
|
||||
fi
|
||||
|
||||
# Run configure if needed
|
||||
if [ ! -f config.status ] || [ config.status -ot configure ]; then
|
||||
echo "Configuring build..."
|
||||
if [ "$IS_WINDOWS" -eq 1 ]; then
|
||||
# Windows: configure for mingw
|
||||
./configure --prefix=/usr/local --host=x86_64-w64-mingw32 2>&1 | tee configure.log
|
||||
else
|
||||
# Linux: standard configure
|
||||
./configure --prefix=/usr/local 2>&1 | tee configure.log
|
||||
fi
|
||||
else
|
||||
echo "Using existing configuration."
|
||||
fi
|
||||
|
||||
# Build
|
||||
echo "Compiling with $JOBS..."
|
||||
if [ "$IS_WINDOWS" -eq 1 ]; then
|
||||
make $JOBS $MAKE_ARGS 2>&1 | tee build_win.log
|
||||
else
|
||||
make $JOBS $MAKE_ARGS 2>&1 | tee build_linux.log
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Build completed successfully!"
|
||||
if [ "$IS_WINDOWS" -eq 1 ]; then
|
||||
echo "Binary: src/utun.exe"
|
||||
echo "Make sure wintun.dll is in the same directory as utun.exe"
|
||||
else
|
||||
echo "Binary: src/utun"
|
||||
fi
|
||||
+10
-5
@@ -3,11 +3,16 @@
|
||||
* Provides flexible control over debug output without recompilation
|
||||
*/
|
||||
|
||||
#ifndef DEBUG_CONFIG_H
|
||||
#define DEBUG_CONFIG_H
|
||||
|
||||
#include <sys/socket.h> // struct sockaddr_storage
|
||||
#include <stddef.h> // size_t
|
||||
#ifndef DEBUG_CONFIG_H
|
||||
#define DEBUG_CONFIG_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/socket.h> // struct sockaddr_storage
|
||||
#endif
|
||||
#include <stddef.h> // size_t
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
|
||||
Reference in new issue
Block a user