Skip to content

Commit 8a2a39a

Browse files
committed
Implement cross-platform endian.h compatibility solution
Replace broken __GNUC__ undefining approach with elegant local endian.h: - Create local ext/fastpbkdf2/endian.h that handles all platforms - macOS: Use machine/endian.h and libkern/OSByteOrder.h - Windows: Hardcode little-endian constants for x86/x64 - Linux: Use system endian.h via include_next - Simplify wrapper to just include fastpbkdf2.c cleanly - Update coverage job to run even if some test matrix jobs fail This avoids breaking Windows MinGW toolchain headers while preserving pristine vendor files and maintaining cross-platform compatibility.
1 parent 1dda948 commit 8a2a39a

File tree

5 files changed

+94
-36
lines changed

5 files changed

+94
-36
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ jobs:
7474
coverage:
7575
runs-on: ubuntu-latest
7676
needs: test
77+
if: always() # Run even if some test jobs fail
7778

7879
steps:
7980
- uses: actions/checkout@v4
@@ -83,6 +84,7 @@ jobs:
8384
with:
8485
name: coverage-report
8586
path: coverage/
87+
continue-on-error: true # In case no coverage artifact was uploaded
8688

8789
- name: Upload coverage to Qlty
8890
uses: qltysh/qlty-action/coverage@v1

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Real-world benchmarks on macOS (x86_64) show consistent performance improvements
4141

4242
### Low Security (1,000 iterations)
4343

44-
4544
| Algorithm | FastPBKDF2 | OpenSSL | Speedup | Use Case |
4645
| --------- | ---------- | ------- | --------- | ------------------------ |
4746
| SHA1 | 0.12ms | 0.21ms | **1.75x** | Basic web authentication |
@@ -173,7 +172,6 @@ The gem compiles the fastpbkdf2 C library during installation, so no external de
173172

174173
This gem is designed to compile and run across major platforms:
175174

176-
177175
### ✅ Supported Platforms
178176

179177
- **macOS**: Intel (x86_64) and Apple Silicon (arm64)
@@ -188,11 +186,9 @@ This gem is designed to compile and run across major platforms:
188186
- **SIMD Optimizations**: The underlying fastpbkdf2 C library includes architecture-specific optimizations
189187
- **Compiler Detection**: Automatically adapts compilation flags for GCC, Clang, and MSVC
190188

191-
192189
### Build Requirements by Platform
193190

194191
#### macOS
195-
196192
```bash
197193

198194
# Xcode Command Line Tools (includes clang and OpenSSL)

ext/fastpbkdf2/endian.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* endian.h - Cross-platform endian compatibility header
3+
*
4+
* This file provides endian.h compatibility for platforms that don't have it
5+
* (like Windows and macOS). It gets included by fastpbkdf2.c when __GNUC__ is defined.
6+
*/
7+
8+
#ifndef FASTPBKDF2_ENDIAN_H
9+
#define FASTPBKDF2_ENDIAN_H
10+
11+
#ifdef __APPLE__
12+
/* macOS approach: use system headers to define endian macros */
13+
#include <machine/endian.h>
14+
#include <libkern/OSByteOrder.h>
15+
16+
/* Define the Linux-style endian macros that fastpbkdf2.c expects */
17+
#ifndef __BYTE_ORDER
18+
#define __BYTE_ORDER BYTE_ORDER
19+
#endif
20+
#ifndef __LITTLE_ENDIAN
21+
#define __LITTLE_ENDIAN LITTLE_ENDIAN
22+
#endif
23+
#ifndef __BIG_ENDIAN
24+
#define __BIG_ENDIAN BIG_ENDIAN
25+
#endif
26+
27+
#elif defined(_WIN32)
28+
/* Windows doesn't have endian.h, but it's always little-endian on x86/x64 */
29+
#ifndef __BYTE_ORDER
30+
#define __BYTE_ORDER 1234
31+
#endif
32+
#ifndef __LITTLE_ENDIAN
33+
#define __LITTLE_ENDIAN 1234
34+
#endif
35+
#ifndef __BIG_ENDIAN
36+
#define __BIG_ENDIAN 4321
37+
#endif
38+
39+
#else
40+
/* Linux and other Unix systems - include the real system endian.h */
41+
#if __has_include(<endian.h>)
42+
#include_next <endian.h>
43+
#elif __has_include(<sys/endian.h>)
44+
#include <sys/endian.h>
45+
#else
46+
/* Fallback definitions for systems without endian headers */
47+
#ifndef __BYTE_ORDER
48+
#ifdef __BYTE_ORDER__
49+
#define __BYTE_ORDER __BYTE_ORDER__
50+
#else
51+
#define __BYTE_ORDER 1234 /* Assume little-endian */
52+
#endif
53+
#endif
54+
#ifndef __LITTLE_ENDIAN
55+
#ifdef __ORDER_LITTLE_ENDIAN__
56+
#define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
57+
#else
58+
#define __LITTLE_ENDIAN 1234
59+
#endif
60+
#endif
61+
#ifndef __BIG_ENDIAN
62+
#ifdef __ORDER_BIG_ENDIAN__
63+
#define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
64+
#else
65+
#define __BIG_ENDIAN 4321
66+
#endif
67+
#endif
68+
#endif
69+
#endif
70+
71+
#endif /* FASTPBKDF2_ENDIAN_H */

ext/fastpbkdf2/fastpbkdf2_wrapper.c

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,12 @@
55
* without modifying the upstream source files.
66
*/
77

8-
/* Set up platform-specific environment before including fastpbkdf2.c */
9-
#ifdef __APPLE__
10-
/* On macOS, we need to provide the endian.h functionality */
11-
#include <machine/endian.h>
12-
#include <libkern/OSByteOrder.h>
13-
14-
/* Define the Linux-style endian macros that fastpbkdf2.c expects */
15-
#ifndef __BYTE_ORDER
16-
#define __BYTE_ORDER BYTE_ORDER
17-
#endif
18-
#ifndef __LITTLE_ENDIAN
19-
#define __LITTLE_ENDIAN LITTLE_ENDIAN
20-
#endif
21-
#ifndef __BIG_ENDIAN
22-
#define __BIG_ENDIAN BIG_ENDIAN
23-
#endif
24-
25-
/* Redefine __GNUC__ temporarily to prevent endian.h include in fastpbkdf2.c */
26-
#ifdef __GNUC__
27-
#define __FASTPBKDF2_SAVED_GNUC__ __GNUC__
28-
#undef __GNUC__
29-
#endif
30-
#endif
8+
/*
9+
* fastpbkdf2_wrapper.c - Cross-platform wrapper for fastpbkdf2.c
10+
*
11+
* This wrapper includes fastpbkdf2.c with cross-platform endian compatibility.
12+
* The local endian.h file handles platform differences automatically.
13+
*/
3114

3215
/* Now include the actual fastpbkdf2.c implementation */
3316
#include "../../vendor/fastpbkdf2/fastpbkdf2.c"
34-
35-
#ifdef __APPLE__
36-
/* Restore __GNUC__ if it was defined */
37-
#ifdef __FASTPBKDF2_SAVED_GNUC__
38-
#define __GNUC__ __FASTPBKDF2_SAVED_GNUC__
39-
#undef __FASTPBKDF2_SAVED_GNUC__
40-
#endif
41-
#endif

ext/fastpbkdf2/fastpbkdf2_wrapper.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#ifndef FASTPBKDF2_WRAPPER_H
99
#define FASTPBKDF2_WRAPPER_H
1010

11-
/* macOS/Apple platform compatibility fixes */
11+
/* Platform-specific compatibility fixes */
1212
#ifdef __APPLE__
1313
/* On macOS, we have __GNUC__ but don't want to use the GCC-specific
1414
* endian.h and __BYTE_ORDER checks. Let's define the endian macros
@@ -28,6 +28,20 @@
2828
#endif
2929
#endif
3030

31+
/* Windows platform compatibility fixes */
32+
#ifdef _WIN32
33+
/* Windows doesn't have endian.h, but it's always little-endian on x86/x64 */
34+
#ifndef __BYTE_ORDER
35+
#define __BYTE_ORDER 1234
36+
#endif
37+
#ifndef __LITTLE_ENDIAN
38+
#define __LITTLE_ENDIAN 1234
39+
#endif
40+
#ifndef __BIG_ENDIAN
41+
#define __BIG_ENDIAN 4321
42+
#endif
43+
#endif
44+
3145
/* Include the actual fastpbkdf2 header */
3246
#include "../../vendor/fastpbkdf2/fastpbkdf2.h"
3347

0 commit comments

Comments
 (0)