Skip to content

Commit fac0fa3

Browse files
authored
Merge pull request #5 from JakkuSakura/fix-deps
Refactor update_libchdb.sh for system-wide install and improve README
2 parents bfe8020 + 85db3a8 commit fac0fa3

File tree

5 files changed

+114
-19
lines changed

5 files changed

+114
-19
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Cargo.lock
33
*.tar.gz
44
*.so
5-
*.h
5+
chdb.h
66
var
77
udf
88
*.parquet

README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,42 @@
33
[![Rust](https://github.com/chdb-io/chdb-rust/actions/workflows/rust.yml/badge.svg)](https://github.com/chdb-io/chdb-rust/actions/workflows/rust.yml)
44

55
# chdb-rust <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Rust_programming_language_black_logo.svg/1024px-Rust_programming_language_black_logo.svg.png" height=20 />
6+
67
Experimental [chDB](https://github.com/chdb-io/chdb) FFI bindings for Rust
78

8-
### Status
9+
## Status
910

1011
- Experimental, unstable, subject to changes
11-
- Requires [`libchdb`](https://github.com/chdb-io/chdb) on the system
12+
- Requires [`libchdb`](https://github.com/chdb-io/chdb) on the system. You can install the compatible version from
13+
`install_libchdb.sh`
14+
15+
## Usage
16+
17+
### Install libchdb
18+
19+
You can install it system-wide
20+
21+
```bash
22+
./update_libchdb.sh --global
23+
```
24+
25+
or use it in a local directory
26+
27+
```bash
28+
./update_libchdb.sh --local
29+
```
30+
31+
### Build
1232

13-
#### Build binding
1433
```bash
15-
./update_libchdb.sh
1634
RUST_BACKTRACE=full cargo build --verbose
35+
1736
```
1837

1938
### Run tests
39+
2040
`cargo test`
2141

2242
### Examples
43+
2344
See `tests` directory.

build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
let bindings = bindgen::Builder::default()
1717
// The input header we would like to generate
1818
// bindings for.
19-
.header("chdb.h")
19+
.header("wrapper.h")
2020
// Tell cargo to invalidate the built crate whenever any of the
2121
// included header files changed.
2222
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))

update_libchdb.sh

+86-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1-
21
#!/bin/bash
2+
33
set -e
4-
cd $(dirname "${BASH_SOURCE[0]}")
4+
5+
# Check for necessary tools
6+
command -v curl >/dev/null 2>&1 || { echo >&2 "curl is required but it's not installed. Aborting."; exit 1; }
7+
command -v tar >/dev/null 2>&1 || { echo >&2 "tar is required but it's not installed. Aborting."; exit 1; }
8+
9+
# Function to download and extract the file
10+
download_and_extract() {
11+
local url=$1
12+
local file="libchdb.tar.gz"
13+
14+
echo "Attempting to download $PLATFORM from $url"
15+
16+
# Download the file with a retry logic
17+
if curl -L -o "$file" "$url"; then
18+
echo "Download successful."
19+
20+
# Optional: Verify download integrity here, if checksums are provided
21+
22+
# Untar the file
23+
if tar -xzf "$file"; then
24+
echo "Extraction successful."
25+
return 0
26+
fi
27+
fi
28+
return 1
29+
}
530

631
# Get the newest release version
7-
# LATEST_RELEASE=$(curl --silent "https://api.github.com/repos/chdb-io/chdb/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
832
LATEST_RELEASE=v2.1.1
933

10-
# Download the correct version based on the platform
34+
# Select the correct package based on OS and architecture
1135
case "$(uname -s)" in
1236
Linux)
1337
if [[ $(uname -m) == "aarch64" ]]; then
@@ -29,18 +53,67 @@ case "$(uname -s)" in
2953
;;
3054
esac
3155

56+
# Main download URL
3257
DOWNLOAD_URL="https://github.com/chdb-io/chdb/releases/download/$LATEST_RELEASE/$PLATFORM"
58+
FALLBACK_URL="https://github.com/chdb-io/chdb/releases/latest/download/$PLATFORM"
3359

34-
echo "Downloading $PLATFORM from $DOWNLOAD_URL"
60+
# Try the main download URL first
61+
if ! download_and_extract "$DOWNLOAD_URL"; then
62+
echo "Retrying with fallback URL..."
63+
if ! download_and_extract "$FALLBACK_URL"; then
64+
echo "Both primary and fallback downloads failed. Aborting."
65+
exit 1
66+
fi
67+
fi
68+
69+
chmod +x libchdb.so
3570

36-
# Download the file
37-
curl -L -o libchdb.tar.gz $DOWNLOAD_URL
71+
if [[ "$1" == "--global" ]]; then
72+
# If current uid is not 0, check if sudo is available and request the user to input the password
73+
if [[ $EUID -ne 0 ]]; then
74+
command -v sudo >/dev/null 2>&1 || { echo >&2 "This script requires sudo privileges but sudo is not installed. Aborting."; exit 1; }
75+
echo "Installation requires administrative access. You will be prompted for your password."
76+
fi
3877

39-
# Untar the file
40-
tar -xzf libchdb.tar.gz
78+
# Define color messages if terminal supports them
79+
if [[ -t 1 ]]; then
80+
RED='\033[0;31m'
81+
GREEN='\033[0;32m'
82+
NC='\033[0m' # No Color
83+
REDECHO() { echo -e "${RED}$@${NC}"; }
84+
GREENECHO() { echo -e "${GREEN}$@${NC}"; }
85+
ENDECHO() { echo -ne "${NC}"; }
86+
else
87+
REDECHO() { echo "$@"; }
88+
GREENECHO() { echo "$@"; }
89+
ENDECHO() { :; }
90+
fi
4191

42-
# Set execute permission for libchdb.so
43-
chmod +x libchdb.so
92+
# Use sudo if not running as root
93+
SUDO=''
94+
if [[ $EUID -ne 0 ]]; then
95+
SUDO='sudo'
96+
GREENECHO "\nYou will be asked for your sudo password to install:"
97+
echo " libchdb.so to /usr/local/lib/"
98+
echo " chdb.h to /usr/local/include/"
99+
fi
100+
101+
# Make sure the library and header directory exists
102+
${SUDO} mkdir -p /usr/local/lib /usr/local/include || true
103+
104+
# Install the library and header file
105+
${SUDO} /bin/cp libchdb.so /usr/local/lib/
106+
${SUDO} /bin/cp chdb.h /usr/local/include/
107+
108+
# Set execute permission for libchdb.so
109+
${SUDO} chmod +x /usr/local/lib/libchdb.so
110+
111+
# Update library cache (Linux specific)
112+
if [[ "$(uname -s)" == "Linux" ]]; then
113+
${SUDO} ldconfig
114+
fi
44115

45-
# Clean up
46-
rm -f libchdb.tar.gz
116+
GREENECHO "Installation completed successfully." ; ENDECHO
117+
GREENECHO "If any error occurred, please report it to:" ; ENDECHO
118+
GREENECHO " https://github.com/chdb-io/chdb/issues/new/choose" ; ENDECHO
119+
fi

wrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "chdb.h"

0 commit comments

Comments
 (0)