Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,11 @@ jobs:

- name: Set library environment variables
run: |
echo "LIBRARY_PATH=$(pwd)/lib:$LIBRARY_PATH" >> $GITHUB_ENV
if [ "$RUNNER_OS" == "Linux" ]; then
if [ "$RUNNER_ARCH" == "X64" ]; then
os="linux-amd64"
elif [ "$RUNNER_ARCH" == "ARM64" ]; then
os="linux-arm64"
fi
echo "LD_LIBRARY_PATH=$(pwd)/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV
elif [ "$RUNNER_OS" == "macOS" ]; then
os="osx"
fi
echo "LIBRARY_PATH=$(pwd)/lib/dynamic/$os:$LIBRARY_PATH" >> $GITHUB_ENV
if [ "$RUNNER_OS" == "Linux" ]; then
echo "LD_LIBRARY_PATH=$(pwd)/lib/dynamic/$os:$LD_LIBRARY_PATH" >> $GITHUB_ENV
elif [ "$RUNNER_OS" == "macOS" ]; then
echo "DYLD_LIBRARY_PATH=$(pwd)/lib/dynamic/$os:$DYLD_LIBRARY_PATH" >> $GITHUB_ENV
echo "DYLD_LIBRARY_PATH=$(pwd)/lib:$DYLD_LIBRARY_PATH" >> $GITHUB_ENV
fi

- name: Build
Expand Down Expand Up @@ -81,19 +72,19 @@ jobs:

- name: Set library environment variables
run: |
echo "LIBRARY_PATH=$(pwd)/lib/dynamic/windows:$LIBRARY_PATH" >> $GITHUB_ENV
echo "LIBRARY_PATH=$(pwd)/lib:$LIBRARY_PATH" >> $GITHUB_ENV

- name: Build
run: go build -v

- name: Test
run: |
export PATH="$(pwd)/lib/dynamic/windows:$PATH"
export PATH="$(pwd)/lib:$PATH"
go test -v

- name: Run example
run: |
export PATH="$(pwd)/lib/dynamic/windows:$PATH"
export PATH="$(pwd)/lib:$PATH"
cd example
go run main.go

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ Temporary Items
example_db

# Libraries and Headers
lib/dynamic
lib/

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ If you prefer not to clone the go-ladybug repo, you can download the libraries (

1. Add a `go:generate` directive to your `main.go` or `tools.go` to download the libraries into a local folder (e.g. `lib-ladybug`) in order to automatically download the libraries at build time:
```go
//go:generate sh -c "curl -sL https://raw.githubusercontent.com/LadybugDB/go-ladybug/refs/heads/master/download_lbug.sh | bash -s -- -out lib-ladybug"
//go:generate sh -c "curl -fsSL https://raw.githubusercontent.com/LadybugDB/ladybug/refs/heads/main/scripts/download-liblbug.sh | LBUG_TARGET_DIR=$(pwd)/lib-ladybug bash"
//go:generate sh -c "[ \"$(uname)\" = Darwin ] && ln -sf liblbug.dylib lib-ladybug/liblbug.0.dylib || ln -sf liblbug.so lib-ladybug/liblbug.so.0"
```

2. Run generation:
Expand Down Expand Up @@ -122,7 +123,7 @@ For Cgo to properly work on Windows, MSYS2 with `UCRT64` environment is required
```
4. Add the path to `lbug_shared.dll` to your `PATH` environment variable. You can do this by running the following command in the MSYS2 terminal:
```bash
export PATH="$(pwd)/lib/dynamic/windows:$PATH"
export PATH="$(pwd)/lib/windows:$PATH"
```
This is required to run the test cases and examples. If you are deploying your application, you can also copy the `lbug_shared.dll` file to the same directory as your executable or to a directory that is already in the `PATH`.

Expand Down
8 changes: 4 additions & 4 deletions cgo_bundled.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package lbug
//go:generate sh download_lbug.sh

/*
#cgo darwin LDFLAGS: -lc++ -L${SRCDIR}/lib/dynamic/osx -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/osx
#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/dynamic/linux-amd64 -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/linux-amd64
#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/dynamic/linux-arm64 -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/linux-arm64
#cgo windows LDFLAGS: -L${SRCDIR}/lib/dynamic/windows -llbug_shared
#cgo CFLAGS: -I${SRCDIR}/lib
#cgo darwin LDFLAGS: -lc++ -L${SRCDIR}/lib -llbug -Wl,-rpath,${SRCDIR}/lib
#cgo linux LDFLAGS: -L${SRCDIR}/lib -llbug -Wl,-rpath,${SRCDIR}/lib
#cgo windows LDFLAGS: -L${SRCDIR}/lib -llbug_shared
#include "lbug.h"
*/
import "C"
219 changes: 46 additions & 173 deletions download_lbug.sh
Original file line number Diff line number Diff line change
@@ -1,178 +1,51 @@
#!/bin/bash

set -e

# Helper function to download a file
download_file() {
local url=$1
local output=$2

if command -v curl >/dev/null 2>&1; then
curl -L -o "$output" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -O "$output" "$url"
else
echo "ERROR: Neither curl nor wget is available"
exit 1
fi
}

# Helper function to extract an archive
extract_archive() {
local archive=$1

case "$archive" in
*.tar.gz)
tar -xzf "$archive"
;;
*)
unzip -q "$archive"
;;
esac
}

# Function to download and extract a specific library
download_library() {
local asset=$1
local target_dir=$2
local lib_pattern=$3
local os_type=$4
local copy_header=$5 # Optional: if set, also copy lbug.h
local header_dest=$6 # Optional: where to copy lbug.h

echo "Downloading asset: $asset"

# Create temp directory
local temp_dir=$(mktemp -d)
cd "$temp_dir"

# Download the asset
local download_url="https://github.com/LadybugDB/ladybug/releases/latest/download/$asset"
echo " Downloading from: $download_url"
download_file "$download_url" "$asset"

# Extract the asset
extract_archive "$asset"
#!/bin/sh
# Wrapper around download-liblbug.sh that places the library into lib/ where
# cgo_bundled.go expects it, then creates the versioned symlink that the
# runtime dynamic linker needs (the dylib/so embed a versioned install name).
#
# download-liblbug.sh is kept as a verbatim copy of the upstream script at:
# https://raw.githubusercontent.com/LadybugDB/ladybug/refs/heads/main/scripts/download-liblbug.sh
# To update it: curl -fsSL <url above> -o download-liblbug.sh

# pipefail is a bashism; use -eu which POSIX sh supports.
# The script contains no pipelines, so pipefail is not needed here.
set -eu

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
LIB_DIR="$SCRIPT_DIR/lib"
UPSTREAM_SCRIPT="$SCRIPT_DIR/download-liblbug.sh"
UPSTREAM_URL="https://raw.githubusercontent.com/LadybugDB/ladybug/refs/heads/main/scripts/download-liblbug.sh"

# Fetch the upstream helper script if it is not already present.
if [ ! -f "$UPSTREAM_SCRIPT" ]; then
echo "Fetching $UPSTREAM_URL ..."
curl -fsSL "$UPSTREAM_URL" -o "$UPSTREAM_SCRIPT"
chmod +x "$UPSTREAM_SCRIPT"
fi

# Copy header file if requested
if [ -n "$copy_header" ]; then
local header_file=$(find . -name "lbug.h" | head -1)
if [ -n "$header_file" ]; then
mkdir -p "$OLDPWD/$header_dest"
cp "$header_file" "$OLDPWD/$header_dest/"
echo "Copied lbug.h to $header_dest"
else
echo "WARNING: lbug.h not found in the extracted files"
fi
# The upstream script defaults TARGET_DIR to SCRIPT_DIR/../lib because it assumes
# it lives in a scripts/ subdir. Override to put things in lib/ at the project root.
LBUG_TARGET_DIR="$LIB_DIR" bash "$UPSTREAM_SCRIPT"

# The dylib/so embed a versioned install name (e.g. @rpath/liblbug.0.dylib,
# liblbug.so.0) but the archive only contains the unversioned file. Create a
# symlink so the runtime dynamic linker can resolve the name it expects.
OS="$(uname -s)"
case "$OS" in
Darwin)
if [ ! -e "$LIB_DIR/liblbug.0.dylib" ]; then
ln -s liblbug.dylib "$LIB_DIR/liblbug.0.dylib"
echo "Created symlink liblbug.0.dylib -> liblbug.dylib"
fi

# Find and copy library file
local lib_file=$(find . -name "$lib_pattern" | head -1)
if [ -n "$lib_file" ]; then
mkdir -p "$OLDPWD/$target_dir"
cp "$lib_file" "$OLDPWD/$target_dir/"
echo "Copied $lib_pattern to $target_dir"

# For Windows, also look for .lib if it exists
if [ "$os_type" = "windows" ]; then
local lib_import=$(find . -name "lbug_shared.lib" -o -name "lbug.lib" | head -1)
if [ -n "$lib_import" ]; then
cp "$lib_import" "$OLDPWD/$target_dir/"
echo "Copied $(basename "$lib_import") to $target_dir"
fi
fi
else
echo "ERROR: Library file ($lib_pattern) not found in the extracted files"
cd "$OLDPWD"
rm -rf "$temp_dir"
exit 1
;;
Linux)
if [ ! -e "$LIB_DIR/liblbug.so.0" ]; then
ln -s liblbug.so "$LIB_DIR/liblbug.so.0"
echo "Created symlink liblbug.so.0 -> liblbug.so"
fi

# Cleanup
cd "$OLDPWD"
rm -rf "$temp_dir"
}

# Parse arguments
out_dir=""
while [[ $# -gt 0 ]]; do
case $1 in
-out)
out_dir="$2"
shift 2
;;
*)
shift
;;
esac
done

# Detect OS and Architecture
os=$(uname -s)
arch=$(uname -m)

case $os in
Linux) os="linux" ;;
Darwin) os="osx" ;;
MINGW*|CYGWIN*) os="windows" ;;
*) echo "ERROR: Unsupported OS: $os"; exit 1 ;;
esac

case $arch in
x86_64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) echo "ERROR: Unsupported architecture: $arch"; exit 1 ;;
esac

# Construct target directory based on cgo_shared.go expectations or use provided out_dir
if [ -n "$out_dir" ]; then
target_dir="$out_dir"
else
# Map architecture for path construction
path_arch="$arch"
[ "$arch" = "x86_64" ] && path_arch="amd64"
[ "$arch" = "aarch64" ] && path_arch="arm64"

# Build platform-specific path
case "$os" in
linux) platform="linux-${path_arch}" ;;
osx) platform="osx" ;;
windows) platform="windows" ;;
esac
target_dir="lib/dynamic/$platform"
fi

echo "Detected OS: $os, Architecture: $arch"
echo "Target Directory: $target_dir"

# Determine asset name and library pattern
case "$os" in
osx)
asset="liblbug-osx-universal.tar.gz"
lib_pattern="liblbug.dylib"
;;
windows)
if [ "$arch" != "x86_64" ]; then
echo "ERROR: Windows only supports x86_64 architecture"
exit 1
fi
asset="liblbug-windows-x86_64.zip"
lib_pattern="lbug_shared.dll"
;;
linux)
asset="liblbug-linux-${arch}.tar.gz"
lib_pattern="liblbug.so"
;;
;;
esac

# Download the platform-specific library
# Only extract header if downloading to go-ladybug source (no -out flag)
if [ -n "$out_dir" ]; then
# External directory - don't copy header
download_library "$asset" "$target_dir" "$lib_pattern" "$os"
else
# go-ladybug source tree - copy header to project root
download_library "$asset" "$target_dir" "$lib_pattern" "$os" "yes" "."
fi

echo "Done!"
# Copy the header to the project root so it is available without a -I flag.
cp "$LIB_DIR/lbug.h" "$SCRIPT_DIR/lbug.h"
echo "Copied lbug.h to $SCRIPT_DIR"
7 changes: 5 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (

// To run this example with Option 2 (local library download), use:
//
// 1. Download libraries:
// 1. Download libraries into this directory:
// go generate ./...
//
// 2. Build/Run with the system_ladybug tag:
// go run -tags system_ladybug main.go
//
// With Option 1 (go.work), no tag is needed - just: go run main.go

//go:generate sh -c "curl -sL https://raw.githubusercontent.com/LadybugDB/go-ladybug/refs/heads/master/download_lbug.sh | bash -s -- -out lib-ladybug"
//go:generate sh -c "curl -fsSL https://raw.githubusercontent.com/LadybugDB/ladybug/refs/heads/main/scripts/download-liblbug.sh | LBUG_TARGET_DIR=$(pwd)/lib-ladybug bash"
//go:generate sh -c "[ \"$(uname)\" = Darwin ] && ln -sf liblbug.dylib lib-ladybug/liblbug.0.dylib || ln -sf liblbug.so lib-ladybug/liblbug.so.0"

/*
#cgo darwin LDFLAGS: -L${SRCDIR}/lib-ladybug -Wl,-rpath,${SRCDIR}/lib-ladybug
Expand Down
Loading
Loading