Skip to content

Commit

Permalink
Adding gen.sh and additional binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Jan 18, 2024
1 parent 058ab24 commit ead81ac
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,38 @@ built Go binary, or in the Windows system path.

Notes for building the `libresvg` artifacts:

### Using gen.sh

Build Darwin images:

```sh
# get cross
$ git clone https://github.com/cross-rs/cross.git
$ cd cross && git submodule update --init --remote

# grab sdk
$ cd cross/docker/cross-toolchains/docker
$ export SDK='https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz'
$ curl -O -J -L "$SDK"

# build containers
$ cd cross
$ cargo build-docker-image x86_64-apple-darwin-cross --build-arg "MACOS_SDK_FILE='$(basename $SDK)'"
$ cargo build-docker-image aarch64-apple-darwin-cross --build-arg "MACOS_SDK_FILE='$(basename $SDK)'"

# add rust toolchains
$ rustup target add x86_64-apple-darwin
$ rustup target add aarch64-apple-darwin
```

Then use:

```sh
$ ./gen.sh
```

### Manually

```sh
$ mkdir -p libresvg/$(go env GOOS)_$(go env GOARCH)
$ git clone https://github.com/RazrFalcon/resvg.git && cd resvg/crates/c-api
Expand All @@ -99,6 +131,8 @@ $ cd resvg/crates/c-api
$ RUSTFLAGS="--print=native-static-libs" cargo build --release
```

Use

## TODO

- Rewrite as pure Go
Expand Down
74 changes: 74 additions & 0 deletions gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash

# github repo
REPO=RazrFalcon/resvg

SRC=$(realpath $(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd))

set -e

WORKDIR=$HOME/src/vega

mkdir -p $WORKDIR

git_latest_tag() {
git -C "$1" describe --abbrev=0 --tags
}

git_checkout_reset() {
local dir="$WORKDIR/$1" name="$1" repo="$2"
if [ ! -d "$dir" ]; then
(set -x;
git clone "$repo" "$dir"
)
fi
(set -x;
git -C "$dir" fetch origin
)
local ver=$(git_latest_tag "$dir")
echo "$name $ver"
echo "$ver" > "$SRC/version.txt"
(set -x;
git -C "$dir" reset --hard
git -C "$dir" clean -f -x -d -e node_modules
git -C "$dir" checkout "$ver" &> /dev/null
)
}

git_checkout_reset resvg "https://github.com/${REPO}.git"

declare -A TARGETS=(
[darwin_arm64]=aarch64-apple-darwin
# [darwin_amd64]=x86_64-apple-darwin
# [linux_amd64]=x86_64-unknown-linux-gnu
# [linux_arm64]=aarch64-unknown-linux-gnu
# [linux_arm]=armv7-unknown-linux-gnueabihf
# [windows_amd64]=x86_64-pc-windows-gnu
)

for f in $(find $WORKDIR/resvg -type f -name Cargo.toml); do
cat > $(dirname "$f")/Cross.toml << __END__
[target.x86_64-apple-darwin]
image = "ghcr.io/cross-rs/x86_64-apple-darwin-cross:local"
[target.aarch64-apple-darwin]
image = "ghcr.io/cross-rs/aarch64-apple-darwin-cross:local"
__END__
done

pushd $WORKDIR/resvg/crates/c-api &> /dev/null
for TARGET in "${!TARGETS[@]}"; do
DEST=$SRC/libresvg/$TARGET
mkdir -p $DEST
RUST_TARGET="${TARGETS[$TARGET]}"
(set -x;
export "CARGO_TARGET_$(sed -e 's/-/_/g' <<< "$RUST_TARGET"|tr [:lower:] [:upper:])_RUSTFLAGS"="--print=native-static-libs"
cross build \
--verbose \
--release \
--target $RUST_TARGET
cp $WORKDIR/resvg/target/$RUST_TARGET/release/libresvg.a $DEST/libresvg.a
chmod -x $DEST/libresvg.a
)
done
popd &> /dev/null
Binary file modified libresvg/darwin_amd64/libresvg.a
Binary file not shown.
Binary file modified libresvg/darwin_arm64/libresvg.a
Binary file not shown.
Binary file modified libresvg/linux_amd64/libresvg.a
Binary file not shown.
Binary file added libresvg/linux_arm/libresvg.a
Binary file not shown.
Binary file added libresvg/linux_arm64/libresvg.a
Binary file not shown.
Binary file modified libresvg/windows_amd64/libresvg.a
Binary file not shown.
16 changes: 16 additions & 0 deletions resvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ package resvg
#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/libresvg/windows_amd64 -lresvg -lm -lkernel32 -ladvapi32 -lbcrypt -lntdll -luserenv -lws2_32
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "resvg.h"
char* version() {
char* s = malloc(sizeof(RESVG_VERSION));
strncpy(s, RESVG_VERSION, sizeof(RESVG_VERSION));
return s;
}
resvg_render_tree* parse(_GoBytes_ data, resvg_options* opts) {
// parse
resvg_render_tree* tree;
Expand Down Expand Up @@ -47,6 +55,14 @@ func Render(data []byte, opts ...Option) (*image.RGBA, error) {
return New(opts...).Render(data)
}

// Version returns the resvg version.
func Version() string {
v := C.version()
ver := C.GoString(v)
C.free(unsafe.Pointer(v))
return ver
}

// Resvg wraps the [resvg c-api].
//
// [resvg c-api]: https://github.com/RazrFalcon/resvg
Expand Down
18 changes: 18 additions & 0 deletions resvg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resvg

import (
"bytes"
_ "embed"
"encoding/base64"
"image/png"
"io/fs"
Expand All @@ -11,6 +12,14 @@ import (
"testing"
)

func TestVersion(t *testing.T) {
ver := Version()
if v, exp := cleanString(ver), cleanString(string(versionTxt)); v != exp {
t.Fatalf("expected %s, got: %s", exp, v)
}
t.Logf("resvg: %s", ver)
}

func TestRender(t *testing.T) {
var files []string
err := filepath.Walk("testdata", func(name string, info fs.FileInfo, err error) error {
Expand Down Expand Up @@ -74,3 +83,12 @@ func testRender(t *testing.T, name string) {
t.Errorf("expected %s and %s to be equal!", orig, out)
}
}

func cleanString(s string) string {
return strings.TrimPrefix(strings.TrimSpace(s), "v")
}

// versionTxt is the embedded resvg version.
//
//go:embed version.txt
var versionTxt []byte
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.37.0

0 comments on commit ead81ac

Please sign in to comment.