This file is part of MXE. See LICENSE.md for licensing information.

Contains ad hoc patches for cross building.

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jonas Kvinge <jonas@jkvinge.net>
Date: Mon, 10 Oct 2022 23:06:29 +0200
Subject: [PATCH 1/2] mingw build fixes


diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1111111..2222222 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -288,6 +288,11 @@ else()
     endif ()
 endif()
 
+if (MINGW)
+    set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -posix")
+    set(EXTRA_CXX_FLAGS "${EXTRA_CXX_FLAGS} -posix")
+endif()
+
 CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)
 CHECK_INCLUDE_FILES(intrin.h HAVE_C_INTRIN_H)
 CHECK_INCLUDE_FILE_CXX(intrin.h HAVE_CXX_INTRIN_H)
@@ -344,7 +349,7 @@ CHECK_C_SOURCE_COMPILES("void *aa_test(void *x) { return __builtin_assume_aligne
 CHECK_CXX_SOURCE_COMPILES("void *aa_test(void *x) { return __builtin_assume_aligned(x, 16);}\nint main(void) { return 0; }" HAVE_CXX_BUILTIN_ASSUME_ALIGNED)
 CHECK_C_SOURCE_COMPILES("int main(void) { __builtin_constant_p(0); }" HAVE__BUILTIN_CONSTANT_P)
 
-if (NOT WIN32)
+if (MINGW OR NOT WIN32)
 set(C_FLAGS_TO_CHECK
 # Variable length arrays are way bad, most especially at run time
 "-Wvla"
@@ -458,7 +463,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
     set(FREEBSD true)
 endif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
 
-if(NOT WIN32)
+if(MINGW OR NOT WIN32)
 if(CMAKE_C_COMPILER_ID MATCHES "Intel")
     SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -diag-error 10006 -diag-disable 68 -diag-disable 177 -diag-disable 186 -diag-disable 2304 -diag-disable 2305 -diag-disable 2338 -diag-disable 1418 -diag-disable 279 -diag-disable=remark")
 endif()
@@ -480,7 +485,7 @@ endif()
 add_subdirectory(util)
 add_subdirectory(doc/dev-reference)
 
-if (NOT WIN32)
+if (MINGW OR NOT WIN32)
 # PCRE check, we have a fixed requirement for PCRE to use Chimera
 # and hscollider
 set(PCRE_REQUIRED_MAJOR_VERSION 8)
@@ -509,7 +514,7 @@ endif()
 configure_file(${CMAKE_MODULE_PATH}/config.h.in ${PROJECT_BINARY_DIR}/config.h)
 configure_file(src/hs_version.h.in ${PROJECT_BINARY_DIR}/hs_version.h)
 
-if (NOT WIN32)
+if (MINGW OR NOT WIN32)
     # expand out library names for pkgconfig static link info
     foreach (LIB ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES})
         # this is fragile, but protects us from toolchain specific files
@@ -532,7 +537,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_C_FLAGS}")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CXX_FLAGS}")
 endif()
 
-if (WIN32)
+if (WIN32 AND NOT MINGW)
 # PCRE check, we have a fixed requirement for PCRE to use Chimera
 # and hscollider
 set(PCRE_REQUIRED_MAJOR_VERSION 8)
@@ -557,7 +562,7 @@ if (EXISTS ${CMAKE_SOURCE_DIR}/chimera/CMakeLists.txt AND BUILD_CHIMERA)
 endif()
 endif()
 
-if(NOT WIN32)
+if(MINGW OR NOT WIN32)
 set(RAGEL_C_FLAGS "-Wno-unused")
 endif()
 
diff --git a/chimera/CMakeLists.txt b/chimera/CMakeLists.txt
index 1111111..2222222 100644
--- a/chimera/CMakeLists.txt
+++ b/chimera/CMakeLists.txt
@@ -33,7 +33,7 @@ target_link_libraries(chimera hs pcre)
 
 install(TARGETS chimera DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
-if (NOT WIN32)
+if (MINGW OR NOT WIN32)
     # expand out library names for pkgconfig static link info
     foreach (LIB ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES})
         # this is fragile, but protects us from toolchain specific files
diff --git a/libhs.pc.in b/libhs.pc.in
index 1111111..2222222 100644
--- a/libhs.pc.in
+++ b/libhs.pc.in
@@ -8,4 +8,4 @@ Description: Intel(R) Hyperscan Library
 Version: @HS_VERSION@
 Libs: -L${libdir} -lhs
 Libs.private: @PRIVATE_LIBS@
-Cflags: -I${includedir}/hs
+Cflags: -I${includedir}/hs -posix
diff --git a/src/database.c b/src/database.c
index 1111111..2222222 100644
--- a/src/database.c
+++ b/src/database.c
@@ -353,7 +353,7 @@ hs_error_t dbIsValid(const hs_database_t *db) {
     return HS_SUCCESS;
 }
 
-#if defined(_WIN32)
+#if NATIVE_WIN32
 #define SNPRINTF_COMPAT _snprintf
 #else
 #define SNPRINTF_COMPAT snprintf
diff --git a/src/nfa/nfa_internal.h b/src/nfa/nfa_internal.h
index 1111111..2222222 100644
--- a/src/nfa/nfa_internal.h
+++ b/src/nfa/nfa_internal.h
@@ -234,7 +234,7 @@ int isMultiTopType(u8 t) {
 
 /** Macros used in place of unimplemented NFA API functions for a given
  * engine. */
-#if !defined(_WIN32)
+#if !NATIVE_WIN32
 
 /* Use for functions that return an integer. */
 #define NFA_API_NO_IMPL(...)                                                   \
diff --git a/src/ue2common.h b/src/ue2common.h
index 1111111..2222222 100644
--- a/src/ue2common.h
+++ b/src/ue2common.h
@@ -46,7 +46,7 @@
 #include <stdint.h>
 
 /* ick */
-#if defined(_WIN32)
+#if NATIVE_WIN32
 #define ALIGN_ATTR(x) __declspec(align(x))
 #else
 #define ALIGN_ATTR(x) __attribute__((aligned((x))))
@@ -83,7 +83,7 @@ typedef u32 ReportID;
 
 /* Shorthand for attribute to mark a function as part of our public API.
  * Functions without this attribute will be hidden. */
-#if !defined(_WIN32)
+#if !NATIVE_WIN32
 #define HS_PUBLIC_API     __attribute__((visibility("default")))
 #else
 // TODO: dllexport defines for windows
@@ -93,14 +93,14 @@ typedef u32 ReportID;
 #define ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
 
 /** \brief Shorthand for the attribute to shut gcc about unused parameters */
-#if !defined(_WIN32)
+#if !NATIVE_WIN32
 #define UNUSED __attribute__ ((unused))
 #else
 #define UNUSED
 #endif
 
 /* really_inline forces inlining always */
-#if !defined(_WIN32)
+#if !NATIVE_WIN32
 #if defined(HS_OPTIMIZE)
 #define really_inline inline __attribute__ ((always_inline, unused))
 #else
@@ -130,7 +130,7 @@ typedef u32 ReportID;
 
 
 // We use C99-style "restrict".
-#ifdef _WIN32
+#if NATIVE_WIN32
 #ifdef __cplusplus
 #define restrict
 #else
@@ -186,7 +186,7 @@ typedef u32 ReportID;
 #define LIMIT_TO_AT_MOST(a, b) (*(a) = MIN(*(a),(b)))
 #define ENSURE_AT_LEAST(a, b) (*(a) = MAX(*(a),(b)))
 
-#ifndef _WIN32
+#if !NATIVE_WIN32
 #ifndef likely
   #define likely(x)     __builtin_expect(!!(x), 1)
 #endif
diff --git a/src/util/bitutils.h b/src/util/bitutils.h
index 1111111..2222222 100644
--- a/src/util/bitutils.h
+++ b/src/util/bitutils.h
@@ -46,7 +46,7 @@
 static really_inline
 u32 clz32(u32 x) {
     assert(x); // behaviour not defined for x == 0
-#if defined(_WIN32)
+#if NATIVE_WIN32
     unsigned long r;
     _BitScanReverse(&r, x);
     return 31 - r;
@@ -58,11 +58,11 @@ u32 clz32(u32 x) {
 static really_inline
 u32 clz64(u64a x) {
     assert(x); // behaviour not defined for x == 0
-#if defined(_WIN64)
+#if NATIVE_WIN32 && defined(_WIN64)
     unsigned long r;
     _BitScanReverse64(&r, x);
     return 63 - r;
-#elif defined(_WIN32)
+#elif NATIVE_WIN32
     unsigned long x1 = (u32)x;
     unsigned long x2 = (u32)(x >> 32);
     unsigned long r;
@@ -81,7 +81,7 @@ u32 clz64(u64a x) {
 static really_inline
 u32 ctz32(u32 x) {
     assert(x); // behaviour not defined for x == 0
-#if defined(_WIN32)
+#if NATIVE_WIN32
     unsigned long r;
     _BitScanForward(&r, x);
     return r;
@@ -93,11 +93,11 @@ u32 ctz32(u32 x) {
 static really_inline
 u32 ctz64(u64a x) {
     assert(x); // behaviour not defined for x == 0
-#if defined(_WIN64)
+#if NATIVE_WIN32 && defined(_WIN64)
     unsigned long r;
     _BitScanForward64(&r, x);
     return r;
-#elif defined(_WIN32)
+#elif NATIVE_WIN32
     unsigned long r;
     if (_BitScanForward(&r, (u32)x)) {
         return (u32)r;
diff --git a/src/util/cpuid_inline.h b/src/util/cpuid_inline.h
index 1111111..2222222 100644
--- a/src/util/cpuid_inline.h
+++ b/src/util/cpuid_inline.h
@@ -32,7 +32,7 @@
 #include "ue2common.h"
 #include "cpuid_flags.h"
 
-#if !defined(_WIN32) && !defined(CPUID_H_)
+#if !NATIVE_WIN32 && !defined(CPUID_H_)
 #include <cpuid.h>
 /* system header doesn't have a header guard */
 #define CPUID_H_
@@ -46,7 +46,7 @@ extern "C"
 static inline
 void cpuid(unsigned int op, unsigned int leaf, unsigned int *eax,
            unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
-#ifndef _WIN32
+#if !NATIVE_WIN32
     __cpuid_count(op, leaf, *eax, *ebx, *ecx, *edx);
 #else
     int a[4];
@@ -95,7 +95,7 @@ void cpuid(unsigned int op, unsigned int leaf, unsigned int *eax,
 
 static inline
 u64a xgetbv(u32 op) {
-#if defined(_WIN32) || defined(__INTEL_COMPILER)
+#if NATIVE_WIN32 || defined(__INTEL_COMPILER)
     return _xgetbv(op);
 #else
     u32 a, d;
diff --git a/src/util/make_unique.h b/src/util/make_unique.h
index 1111111..2222222 100644
--- a/src/util/make_unique.h
+++ b/src/util/make_unique.h
@@ -29,7 +29,7 @@
 #ifndef UTIL_MAKE_UNIQUE_H
 #define UTIL_MAKE_UNIQUE_H
 
-#if (defined(_WIN32) || defined(_WIN64)) && (_MSC_VER > 1700)
+#if NATIVE_WIN32 && (_MSC_VER > 1700)
 // VC++ 2013 onwards has make_unique in the STL
 #define USE_STD
 #include <memory>
diff --git a/src/util/simd_utils.h b/src/util/simd_utils.h
index 1111111..2222222 100644
--- a/src/util/simd_utils.h
+++ b/src/util/simd_utils.h
@@ -33,7 +33,7 @@
 #ifndef SIMD_UTILS
 #define SIMD_UTILS
 
-#if !defined(_WIN32) && !defined(__SSSE3__)
+#if !NATIVE_WIN32 && !defined(__SSSE3__)
 #error SSSE3 instructions must be enabled
 #endif
 
diff --git a/src/util/unaligned.h b/src/util/unaligned.h
index 1111111..2222222 100644
--- a/src/util/unaligned.h
+++ b/src/util/unaligned.h
@@ -35,7 +35,7 @@
 
 #include "ue2common.h"
 
-#if !defined(_WIN32)
+#if !NATIVE_WIN32
 #define PACKED__MAY_ALIAS __attribute__((packed, may_alias))
 #else
 #define PACKED__MAY_ALIAS
@@ -89,7 +89,7 @@ void unaligned_store_u64a(void *ptr, u64a val) {
     struct unaligned *uptr = (struct unaligned *)ptr;
     uptr->u = val;
 }
-#if defined(_WIN32)
+#if NATIVE_WIN32
 #pragma pack(pop)
 #endif // win32
 
diff --git a/tools/hsbench/CMakeLists.txt b/tools/hsbench/CMakeLists.txt
index 1111111..2222222 100644
--- a/tools/hsbench/CMakeLists.txt
+++ b/tools/hsbench/CMakeLists.txt
@@ -58,7 +58,7 @@ if (BUILD_CHIMERA)
     )
     add_executable(hsbench ${hsbench_SOURCES})
     include_directories(${PCRE_INCLUDE_DIRS})
-    if(NOT WIN32)
+    if(MINGW OR NOT WIN32)
         target_link_libraries(hsbench hs chimera ${PCRE_LDFLAGS} databaseutil
             expressionutil ${SQLITE3_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT})
     else()
diff --git a/tools/hscheck/CMakeLists.txt b/tools/hscheck/CMakeLists.txt
index 1111111..2222222 100644
--- a/tools/hscheck/CMakeLists.txt
+++ b/tools/hscheck/CMakeLists.txt
@@ -10,7 +10,7 @@ if (BUILD_CHIMERA)
     include_directories(${PCRE_INCLUDE_DIRS})
     add_definitions(-DHS_HYBRID)
     add_executable(hscheck ${hscheck_SOURCES})
-    if(NOT WIN32)
+    if(MINGW OR NOT WIN32)
         target_link_libraries(hscheck hs chimera ${PCRE_LDFLAGS} expressionutil pthread)
     else()
         target_link_libraries(hscheck hs chimera pcre expressionutil)
@@ -21,7 +21,7 @@ else()
     else()
         add_executable(hscheck ${hscheck_SOURCES})
     endif()
-    if(NOT WIN32)
+    if(MINGW OR NOT WIN32)
         target_link_libraries(hscheck hs expressionutil pthread)
     else()
         target_link_libraries(hscheck hs expressionutil)
diff --git a/tools/hscollider/CMakeLists.txt b/tools/hscollider/CMakeLists.txt
index 1111111..2222222 100644
--- a/tools/hscollider/CMakeLists.txt
+++ b/tools/hscollider/CMakeLists.txt
@@ -65,7 +65,7 @@ set_source_files_properties(${hscollider_SOURCES} PROPERTIES
 add_executable(hscollider ${hscollider_SOURCES})
 add_dependencies(hscollider ragel_ColliderCorporaParser)
 
-if(NOT WIN32)
+if(MINGW OR NOT WIN32)
     if (BUILD_CHIMERA)
         target_link_libraries(hscollider hs chimera ${PCRE_LDFLAGS} databaseutil
             expressionutil corpusomatic crosscompileutil pthread
diff --git a/unit/CMakeLists.txt b/unit/CMakeLists.txt
index 1111111..2222222 100644
--- a/unit/CMakeLists.txt
+++ b/unit/CMakeLists.txt
@@ -38,7 +38,7 @@ endif()
 
 add_definitions(-DGTEST_HAS_PTHREAD=0 -DSRCDIR=${PROJECT_SOURCE_DIR})
 
-if (WIN32)
+if (WIN32 AND NOT MINGW)
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4309 /wd4018")
 endif()
 
diff --git a/unit/hyperscan/test_util.h b/unit/hyperscan/test_util.h
index 1111111..2222222 100644
--- a/unit/hyperscan/test_util.h
+++ b/unit/hyperscan/test_util.h
@@ -37,7 +37,7 @@
 #include <vector>
 
 #ifndef UNUSED
-#if defined(_WIN32) || defined(_WIN64)
+#if NATIVE_WIN32
 #define UNUSED
 #else
 #define UNUSED __attribute__ ((unused))
diff --git a/util/expressions.cpp b/util/expressions.cpp
index 1111111..2222222 100644
--- a/util/expressions.cpp
+++ b/util/expressions.cpp
@@ -40,7 +40,7 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#if !defined(_WIN32)
+#if !NATIVE_WIN32
 #include <dirent.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -98,7 +98,7 @@ void processLine(string &line, unsigned lineNum,
     }
 }
 
-#if defined(_WIN32)
+#if NATIVE_WIN32
 #define stat _stat
 #define S_ISDIR(st_m) (_S_IFDIR & (st_m))
 #define S_ISREG(st_m) (_S_IFREG & (st_m))
@@ -143,7 +143,7 @@ bool isIgnorable(const std::string &f) {
     return false;
 }
 
-#ifndef _WIN32
+#if !NATIVE_WIN32
 void loadExpressions(const string &inPath, ExpressionMap &exprMap) {
     // Is our input path a file or a directory?
     struct stat st;

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Boris Nagaev <bnagaev@gmail.com>
Date: Tue, 31 Dec 2024 16:41:39 +0000
Subject: [PATCH 2/2] fix ragel generated files if host is arm64

Ragel uses host's signness of "char" type, so it generates .cpp files assuming char
is unsigned if the host is arm64. This resulted in "error: narrowing conversion"
errors when the generated file is compiled.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1111111..2222222 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74,6 +74,7 @@ include (${CMAKE_MODULE_PATH}/boost.cmake)
 # -- make this work? set(python_ADDITIONAL_VERSIONS 2.7 2.6)
 find_package(PythonInterp)
 find_program(RAGEL ragel)
+find_program(SED sed HINTS ENV SED)
 
 if(PYTHONINTERP_FOUND)
     set(PYTHON ${PYTHON_EXECUTABLE})
diff --git a/cmake/ragel.cmake b/cmake/ragel.cmake
index 1111111..2222222 100644
--- a/cmake/ragel.cmake
+++ b/cmake/ragel.cmake
@@ -8,6 +8,7 @@ function(ragelmaker src_rl)
         OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${src_dir}/${src_file}.cpp
         COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${src_dir}
         COMMAND ${RAGEL} ${CMAKE_CURRENT_SOURCE_DIR}/${src_rl} -o ${rl_out}
+        COMMAND ${SED} -i '/_offsets/s/const char _/const unsigned char _/' ${rl_out}
         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src_rl}
         )
     add_custom_target(ragel_${src_file} DEPENDS ${rl_out})
