Skip to content

Commit 803511b

Browse files
discord9paomian
authored andcommitted
build: add cross compile docker (GreptimeTeam#1156)
* build: add cross compile docker * build: added compile python to github action * fix: correct path * fix: Python Compile * fix: run mulitple cmds * fix: both cross compile docker file&github action * refactor: compile-python.sh * chore: put wget install together * fix: CR advices * chore: add `-F pyo3_backend`
1 parent 89dcc88 commit 803511b

3 files changed

Lines changed: 105 additions & 1 deletion

File tree

.github/workflows/release.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ jobs:
9696
if: contains(matrix.arch, 'linux') && endsWith(matrix.arch, '-gnu')
9797
run: |
9898
sudo apt-get -y update
99-
sudo apt-get -y install libssl-dev pkg-config g++-aarch64-linux-gnu gcc-aarch64-linux-gnu
99+
sudo apt-get -y install libssl-dev pkg-config g++-aarch64-linux-gnu gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu wget
100+
101+
- name: Compile Python 3.10.10 from source for Aarch64
102+
if: contains(matrix.arch, 'aarch64-unknown-linux-gnu')
103+
run: |
104+
sudo chmod +x ./docker/aarch64/compile-python.sh
105+
sudo ./docker/aarch64/compile-python.sh
106+
export PYO3_CROSS_LIB_DIR=${PWD}/python310-aarch64/lib
107+
echo $PYO3_CROSS_LIB_DIR
100108
101109
- name: Install rust toolchain
102110
uses: dtolnay/rust-toolchain@master

docker/aarch64/Dockerfile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
FROM ubuntu:22.04 as builder
2+
3+
ENV LANG en_US.utf8
4+
WORKDIR /greptimedb
5+
6+
# Install dependencies.
7+
RUN apt-get update && apt-get install -y \
8+
libssl-dev \
9+
protobuf-compiler \
10+
curl \
11+
build-essential \
12+
pkg-config \
13+
wget
14+
15+
# Install Rust.
16+
SHELL ["/bin/bash", "-c"]
17+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path --default-toolchain none -y
18+
ENV PATH /root/.cargo/bin/:$PATH
19+
20+
# Install cross platform toolchain
21+
RUN apt-get -y update && \
22+
apt-get -y install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu && \
23+
apt-get install binutils-aarch64-linux-gnu
24+
25+
COPY . .
26+
# This three env var is set in script, so I set it manually in dockerfile.
27+
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
28+
ENV LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib/
29+
ENV PY_INSTALL_PATH=${PWD}/python_arm64_build
30+
RUN chmod +x ./docker/aarch64/compile-python.sh && \
31+
./docker/aarch64/compile-python.sh
32+
# Install rustup target for cross compiling.
33+
RUN rustup target add aarch64-unknown-linux-gnu
34+
# Set the environment variable for cross compiling and compile it
35+
# Build the project in release mode. Set Net fetch with git cli to true to avoid git error.
36+
RUN export PYO3_CROSS_LIB_DIR=$PY_INSTALL_PATH/lib && \
37+
alias python=python3 && \
38+
CARGO_NET_GIT_FETCH_WITH_CLI=1 && \
39+
cargo build --target aarch64-unknown-linux-gnu --release -F pyo3_backend
40+
41+
# Exporting the binary to the clean image
42+
FROM ubuntu:22.04 as base
43+
44+
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install ca-certificates
45+
46+
WORKDIR /greptime
47+
COPY --from=builder /greptimedb/target/aarch64-unknown-linux-gnu/release/greptime /greptime/bin/
48+
ENV PATH /greptime/bin/:$PATH
49+
50+
ENTRYPOINT ["greptime"]

docker/aarch64/compile-python.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# this script will download Python source code, compile it, and install it to /usr/local/lib
2+
# then use this python to compile cross-compiled python for aarch64
3+
4+
wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz
5+
tar -xvf Python-3.10.10.tgz
6+
cd Python-3.10.10
7+
# explain Python compile options here a bit:s
8+
# --enable-shared: enable building a shared Python library (default is no) but we do need it for calling from rust
9+
# CC, CXX, AR, LD, RANLIB: set the compiler, archiver, linker, and ranlib programs to use
10+
# build: the machine you are building on, host: the machine you will run the compiled program on
11+
# --with-system-ffi: build _ctypes module using an installed ffi library, see Doc/library/ctypes.rst, not used in here TODO: could remove
12+
# ac_cv_pthread_is_default=no ac_cv_pthread=yes ac_cv_cxx_thread=yes:
13+
# allow cross-compiled python to have -pthread set for CXX, see https://github.com/python/cpython/pull/22525
14+
# ac_cv_have_long_long_format=yes: target platform supports long long type
15+
# disable-ipv6: disable ipv6 support, we don't need it in here
16+
# ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no: disable pty support, we don't need it in here
17+
18+
# Build local python first, then build cross-compiled python.
19+
./configure \
20+
--enable-shared \
21+
ac_cv_pthread_is_default=no ac_cv_pthread=yes ac_cv_cxx_thread=yes \
22+
ac_cv_have_long_long_format=yes \
23+
--disable-ipv6 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no && \
24+
make
25+
make install
26+
cd ..
27+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
28+
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib/
29+
export PY_INSTALL_PATH=${PWD}/python_arm64_build
30+
cd Python-3.10.10 && \
31+
make clean && \
32+
make distclean && \
33+
alias python=python3 && \
34+
./configure --build=x86_64-linux-gnu --host=aarch64-linux-gnu \
35+
--prefix=$PY_INSTALL_PATH --enable-optimizations \
36+
CC=aarch64-linux-gnu-gcc \
37+
CXX=aarch64-linux-gnu-g++ \
38+
AR=aarch64-linux-gnu-ar \
39+
LD=aarch64-linux-gnu-ld \
40+
RANLIB=aarch64-linux-gnu-ranlib \
41+
--enable-shared \
42+
ac_cv_pthread_is_default=no ac_cv_pthread=yes ac_cv_cxx_thread=yes \
43+
ac_cv_have_long_long_format=yes \
44+
--disable-ipv6 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no && \
45+
make && make altinstall && \
46+
cd ..

0 commit comments

Comments
 (0)