Skip to content

Commit

Permalink
fix cross compilation: libc::c_char is platform dependent
Browse files Browse the repository at this point in the history
  • Loading branch information
Daulet Zhanguzin committed Jan 7, 2024
1 parent 4fd5a66 commit 6e58579
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ release-darwin-%:
release-linux-%:
docker buildx build --platform linux/$* -f release/Dockerfile . -t tokenizers.linux-$*
mkdir -p artifacts/linux-$*
docker run -v $(PWD)/artifacts/linux-$*:/mnt --entrypoint cp tokenizers.linux-$* /go/pkg/mod/github.com/daulet/tokenizers@v0.6.0/libtokenizers.a /mnt/libtokenizers.a
docker run -v $(PWD)/artifacts/linux-$*:/mnt --entrypoint cp tokenizers.linux-$* /workspace/tokenizers/libtokenizers.a /mnt/libtokenizers.a
cd artifacts/linux-$* && \
tar -czf libtokenizers.linux-$*.tar.gz libtokenizers.a
mkdir -p artifacts/all
Expand Down
13 changes: 8 additions & 5 deletions release/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ RUN cargo build --release
FROM golang:1.21 as builder-go
ARG TARGETPLATFORM
WORKDIR /workspace
COPY ./release .
COPY ./test/data ./test/data
RUN go mod download
COPY ./release/go.mod .
COPY ./release/main.go .
# can't rely on tokenizers module because latest release doesn't include recent changes
COPY tokenizer.go ./tokenizers/
COPY tokenizers.h ./tokenizers/
COPY --from=builder-rust \
/workspace/target/release/libtokenizers.a \
/go/pkg/mod/github.com/daulet/tokenizers@v0.6.0/libtokenizers.a
RUN go run main.go
./tokenizers/
COPY ./test/data ./test/data
RUN go run .
6 changes: 2 additions & 4 deletions release/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/daulet/tokenizers/example
module github.com/daulet/tokenizers/release

go 1.21

require github.com/daulet/tokenizers v0.6.0
go 1.21.5
10 changes: 0 additions & 10 deletions release/go.sum

This file was deleted.

2 changes: 1 addition & 1 deletion release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"github.com/daulet/tokenizers"
"github.com/daulet/tokenizers/release/tokenizers"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub extern "C" fn encode(ptr: *mut libc::c_void, message: *const libc::c_char, o
std::mem::forget(vec_type_ids);
}

let mut tokens: *mut *mut i8 = ptr::null_mut();
let mut tokens: *mut *mut libc::c_char = ptr::null_mut();
if options.return_tokens {
let mut vec_tokens = encoding.get_tokens()
.to_vec().into_iter()
Expand Down Expand Up @@ -180,7 +180,7 @@ pub extern "C" fn free_buffer(buf: Buffer) {
unsafe {
let strings = Vec::from_raw_parts(buf.tokens, buf.len, buf.len);
for s in strings {
drop(std::ffi::CString::from_raw(s));
drop(std::ffi::CString::from_raw(s.cast::<libc::c_char>()));
}
}
}
Expand Down

0 comments on commit 6e58579

Please sign in to comment.