Skip to content

Commit f37154e

Browse files
committed
move libsais wrappers out of vendor folder so they don't get wiped out
1 parent 8e32514 commit f37154e

5 files changed

Lines changed: 49 additions & 8 deletions

File tree

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ bin/*
1313

1414
# Vendored dependencies
1515
vendors/
16-
!vendors/libsais/
17-
vendors/libsais/*
18-
!vendors/libsais/zig_wrapper.c
19-
!vendors/libsais/zig_wrapper.h
2016

2117
# OS files
2218
.DS_Store

build.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub fn build(b: *std.Build) void {
6767
// This is for the cImport to import the .h files
6868
bsdiff.addIncludePath(b.path("zstd/lib"));
6969
bsdiff.addIncludePath(b.path("vendors/libsais"));
70+
bsdiff.addIncludePath(b.path("src/libsais-wrapper"));
7071

7172
b.installArtifact(bsdiff);
7273

@@ -90,6 +91,7 @@ pub fn build(b: *std.Build) void {
9091

9192
tests.addIncludePath(b.path("zstd/lib"));
9293
tests.addIncludePath(b.path("vendors/libsais"));
94+
tests.addIncludePath(b.path("src/libsais-wrapper"));
9395
tests.linkLibrary(libzstd);
9496
tests.addObjectFile(b.path("vendors/libsais/libsais.a"));
9597

scripts/setup.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import { execSync } from 'child_process';
11-
import { existsSync, mkdirSync, rmSync, unlinkSync, renameSync } from 'fs';
11+
import { existsSync, mkdirSync, rmSync, unlinkSync, renameSync, writeFileSync } from 'fs';
1212
import { join } from 'path';
1313

1414
const ZIG_VERSION = '0.13.0';
@@ -57,10 +57,24 @@ async function vendorLibsais() {
5757
// Compile libsais (Zig's C backend has issues with libsais)
5858
console.log('Compiling libsais...');
5959

60-
// Use clang/gcc on all platforms for consistency
61-
// This produces .a libraries that work with Zig's linker
60+
const compiler = platform === 'win32' ? 'gcc' : 'clang';
61+
const wrapperDir = join(process.cwd(), 'src', 'libsais-wrapper');
62+
63+
// Compile libsais source files
64+
execSync(
65+
`cd ${libsaisDir} && ${compiler} -c -O3 -std=c99 libsais.c libsais64.c`,
66+
{ stdio: 'inherit' }
67+
);
68+
69+
// Compile wrapper with include path to libsais headers
70+
execSync(
71+
`${compiler} -c -O3 -std=c99 -I ${libsaisDir} ${wrapperDir}/zig_wrapper.c -o ${libsaisDir}/zig_wrapper.o`,
72+
{ stdio: 'inherit' }
73+
);
74+
75+
// Create static library
6276
execSync(
63-
`cd ${libsaisDir} && ${platform === 'win32' ? 'gcc' : 'clang'} -c -O3 -std=c99 libsais.c libsais64.c zig_wrapper.c && ar rcs libsais.a libsais.o libsais64.o zig_wrapper.o`,
77+
`cd ${libsaisDir} && ar rcs libsais.a libsais.o libsais64.o zig_wrapper.o`,
6478
{ stdio: 'inherit' }
6579
);
6680

src/libsais-wrapper/zig_wrapper.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Simple C wrapper for libsais64 to ensure clean ABI for Zig
2+
#include "libsais64.h"
3+
#include <stddef.h>
4+
#include <stdint.h>
5+
6+
// Wrapper function with explicit types and no macros
7+
int64_t zig_libsais64_wrapper(
8+
const uint8_t * T,
9+
int64_t * SA,
10+
int64_t n
11+
) {
12+
// Call libsais64 with default parameters
13+
// fs=0 means no extra space, freq=NULL means no frequency table
14+
return libsais64(T, SA, n, 0, NULL);
15+
}

src/libsais-wrapper/zig_wrapper.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Simple C wrapper for libsais64 to ensure clean ABI for Zig
2+
#ifndef ZIG_WRAPPER_H
3+
#define ZIG_WRAPPER_H
4+
5+
#include <stdint.h>
6+
7+
// Simple wrapper with no macros or complex types
8+
int64_t zig_libsais64_wrapper(
9+
const uint8_t * T,
10+
int64_t * SA,
11+
int64_t n
12+
);
13+
14+
#endif

0 commit comments

Comments
 (0)