|
| 1 | +# Windows Cross-Compilation Test using MinGW-w64 |
| 2 | +# Tests Windows API compatibility and compilation on Linux/macOS |
| 3 | +# |
| 4 | +# This simulates Windows builds by: |
| 5 | +# 1. Using MinGW-w64 cross-compiler (x86_64-w64-mingw32-gcc) |
| 6 | +# 2. Testing all Windows-specific code paths |
| 7 | +# 3. Validating Windows API usage (Winsock2, Windows.h, etc.) |
| 8 | + |
| 9 | +FROM ubuntu:22.04 |
| 10 | + |
| 11 | +ENV DEBIAN_FRONTEND=noninteractive |
| 12 | + |
| 13 | +# Install MinGW-w64 and build tools |
| 14 | +RUN apt-get update && apt-get install -y \ |
| 15 | + gcc-mingw-w64-x86-64 \ |
| 16 | + g++-mingw-w64-x86-64 \ |
| 17 | + mingw-w64-tools \ |
| 18 | + cmake \ |
| 19 | + ninja-build \ |
| 20 | + git \ |
| 21 | + curl \ |
| 22 | + wget \ |
| 23 | + zip \ |
| 24 | + unzip \ |
| 25 | + tar \ |
| 26 | + python3.11 \ |
| 27 | + python3.11-dev \ |
| 28 | + python3-pip \ |
| 29 | + pkg-config \ |
| 30 | + autoconf \ |
| 31 | + automake \ |
| 32 | + libtool \ |
| 33 | + make \ |
| 34 | + && rm -rf /var/lib/apt/lists/* |
| 35 | + |
| 36 | +# Set Python 3.11 as default |
| 37 | +RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1 && \ |
| 38 | + update-alternatives --install /usr/bin/python python /usr/bin/python3.11 1 |
| 39 | + |
| 40 | +# Upgrade pip and install build tools |
| 41 | +RUN python3 -m pip install --upgrade pip setuptools wheel cython pytest |
| 42 | + |
| 43 | +# Install vcpkg for Windows dependencies (with retry logic for network issues) |
| 44 | +ENV VCPKG_ROOT=/opt/vcpkg |
| 45 | +RUN git clone --depth 1 --single-branch https://github.com/microsoft/vcpkg.git ${VCPKG_ROOT} || \ |
| 46 | + (sleep 5 && git clone --depth 1 --single-branch https://github.com/microsoft/vcpkg.git ${VCPKG_ROOT}) || \ |
| 47 | + (sleep 10 && git clone --depth 1 --single-branch https://github.com/microsoft/vcpkg.git ${VCPKG_ROOT}) |
| 48 | +RUN cd ${VCPKG_ROOT} && ./bootstrap-vcpkg.sh |
| 49 | + |
| 50 | +# Install Windows dependencies using vcpkg with x64-mingw-static triplet |
| 51 | +RUN ${VCPKG_ROOT}/vcpkg install \ |
| 52 | + openssl:x64-mingw-static \ |
| 53 | + nghttp2:x64-mingw-static \ |
| 54 | + zlib:x64-mingw-static \ |
| 55 | + --clean-after-build |
| 56 | + |
| 57 | +# Set environment for MinGW cross-compilation |
| 58 | +ENV VCPKG_DEFAULT_TRIPLET=x64-mingw-static |
| 59 | +ENV MINGW_PREFIX=/usr/x86_64-w64-mingw32 |
| 60 | +ENV CC=x86_64-w64-mingw32-gcc |
| 61 | +ENV CXX=x86_64-w64-mingw32-g++ |
| 62 | +ENV AR=x86_64-w64-mingw32-ar |
| 63 | +ENV RANLIB=x86_64-w64-mingw32-ranlib |
| 64 | +ENV PKG_CONFIG_PATH=${VCPKG_ROOT}/installed/x64-mingw-static/lib/pkgconfig |
| 65 | + |
| 66 | +WORKDIR /workspace |
| 67 | + |
| 68 | +# Copy project files |
| 69 | +COPY . . |
| 70 | + |
| 71 | +# Create test script |
| 72 | +RUN cat > /usr/local/bin/test-windows-build << 'EOF' |
| 73 | +#!/bin/bash |
| 74 | +set -e |
| 75 | + |
| 76 | +echo "╔════════════════════════════════════════════════════════════════╗" |
| 77 | +echo "║ Windows Build Test (MinGW-w64 Cross-Compiler) ║" |
| 78 | +echo "╚════════════════════════════════════════════════════════════════╝" |
| 79 | +echo "" |
| 80 | + |
| 81 | +# Test 1: Check if Windows headers are properly guarded |
| 82 | +echo "📋 Test 1: Checking Windows-specific code guards..." |
| 83 | +if grep -r "#ifdef _WIN32" src/core/*.c | grep -q .; then |
| 84 | + echo " ✅ Found Windows-specific guards" |
| 85 | +else |
| 86 | + echo " ⚠️ No Windows guards found" |
| 87 | +fi |
| 88 | + |
| 89 | +# Test 2: Compile check for Windows-specific files |
| 90 | +echo "" |
| 91 | +echo "📋 Test 2: Testing Windows API compatibility..." |
| 92 | +cat > /tmp/test_windows.c << 'CTEST' |
| 93 | +#define _WIN32 1 |
| 94 | +#include <winsock2.h> |
| 95 | +#include <ws2tcpip.h> |
| 96 | +#include <windows.h> |
| 97 | +#include <stdio.h> |
| 98 | + |
| 99 | +int main() { |
| 100 | + WSADATA wsaData; |
| 101 | + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); |
| 106 | + if (sock == INVALID_SOCKET) { |
| 107 | + WSACleanup(); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + closesocket(sock); |
| 112 | + WSACleanup(); |
| 113 | + |
| 114 | + printf("Windows API test passed!\n"); |
| 115 | + return 0; |
| 116 | +} |
| 117 | +CTEST |
| 118 | + |
| 119 | +if x86_64-w64-mingw32-gcc /tmp/test_windows.c -o /tmp/test_windows.exe -lws2_32 2>/dev/null; then |
| 120 | + echo " ✅ Windows API headers compile successfully" |
| 121 | +else |
| 122 | + echo " ❌ Windows API compilation failed" |
| 123 | + exit 1 |
| 124 | +fi |
| 125 | + |
| 126 | +# Test 3: Syntax check of source files with Windows defines |
| 127 | +echo "" |
| 128 | +echo "📋 Test 3: Syntax checking httpmorph.c with Windows defines..." |
| 129 | +x86_64-w64-mingw32-gcc -D_WIN32 -fsyntax-only \ |
| 130 | + -I${VCPKG_ROOT}/installed/x64-mingw-static/include \ |
| 131 | + -Iinclude -Isrc/core -Isrc/tls \ |
| 132 | + src/core/httpmorph.c \ |
| 133 | + -Wno-deprecated-declarations 2>&1 | head -20 |
| 134 | + |
| 135 | +if [ ${PIPESTATUS[0]} -eq 0 ]; then |
| 136 | + echo " ✅ httpmorph.c syntax check passed" |
| 137 | +else |
| 138 | + echo " ⚠️ Syntax warnings found (check above)" |
| 139 | +fi |
| 140 | + |
| 141 | +# Test 4: Check for Windows-incompatible functions |
| 142 | +echo "" |
| 143 | +echo "📋 Test 4: Checking for Windows-incompatible code..." |
| 144 | +INCOMPATIBLE_FOUND=0 |
| 145 | + |
| 146 | +if grep -r "fork\|execve\|pipe2\|epoll\|kqueue" src/core/*.c | grep -v "ifdef" | grep -v "//"; then |
| 147 | + echo " ⚠️ Found potentially incompatible functions (check if properly guarded)" |
| 148 | + INCOMPATIBLE_FOUND=1 |
| 149 | +else |
| 150 | + echo " ✅ No unguarded incompatible functions" |
| 151 | +fi |
| 152 | + |
| 153 | +# Test 5: Verify all Windows-specific replacements |
| 154 | +echo "" |
| 155 | +echo "📋 Test 5: Verifying Windows API replacements..." |
| 156 | +MISSING=0 |
| 157 | + |
| 158 | +for file in src/core/*.c; do |
| 159 | + if grep -q "_WIN32" "$file"; then |
| 160 | + # Check for required Windows headers |
| 161 | + if ! grep -q "winsock2.h" "$file"; then |
| 162 | + echo " ⚠️ $file: Missing winsock2.h" |
| 163 | + MISSING=1 |
| 164 | + fi |
| 165 | + |
| 166 | + # Check for WSAStartup if using sockets |
| 167 | + if grep -q "socket(" "$file" && ! grep -q "WSAStartup\|Already initialized" "$file" 2>/dev/null; then |
| 168 | + echo " ℹ️ $file: Check if WSAStartup is called" |
| 169 | + fi |
| 170 | + fi |
| 171 | +done |
| 172 | + |
| 173 | +if [ $MISSING -eq 0 ]; then |
| 174 | + echo " ✅ All Windows headers present" |
| 175 | +fi |
| 176 | + |
| 177 | +# Test 6: Check for proper printf format specifiers |
| 178 | +echo "" |
| 179 | +echo "📋 Test 6: Checking printf format specifiers..." |
| 180 | +if grep -r "%lu.*uint64_t\|%ld.*uint64_t" src/core/*.c | grep -v "PRIu64\|PRId64"; then |
| 181 | + echo " ⚠️ Found potentially incorrect format specifiers for uint64_t" |
| 182 | + echo " Use PRIu64 from <inttypes.h> for portability" |
| 183 | +else |
| 184 | + echo " ✅ Printf format specifiers look correct" |
| 185 | +fi |
| 186 | + |
| 187 | +# Summary |
| 188 | +echo "" |
| 189 | +echo "╔════════════════════════════════════════════════════════════════╗" |
| 190 | +echo "║ Test Summary ║" |
| 191 | +echo "╚════════════════════════════════════════════════════════════════╝" |
| 192 | +echo "" |
| 193 | +if [ $INCOMPATIBLE_FOUND -eq 0 ] && [ $MISSING -eq 0 ]; then |
| 194 | + echo "✅ All Windows compatibility checks passed!" |
| 195 | + echo "" |
| 196 | + echo "Note: This tests API compatibility using MinGW cross-compiler." |
| 197 | + echo "For full MSVC testing, use GitHub Actions Windows runner." |
| 198 | + exit 0 |
| 199 | +else |
| 200 | + echo "⚠️ Some warnings found. Review the output above." |
| 201 | + exit 0 |
| 202 | +fi |
| 203 | +EOF |
| 204 | + |
| 205 | +RUN chmod +x /usr/local/bin/test-windows-build |
| 206 | + |
| 207 | +# Default command |
| 208 | +CMD ["test-windows-build"] |
0 commit comments