95 lines
2.4 KiB
Bash
Executable File
95 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
|
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
|
CACHE_DIR="${SCRIPT_DIR}/cache"
|
|
EXE_DIR="${SCRIPT_DIR}/exe"
|
|
BUILD_DIR="${SCRIPT_DIR}/build"
|
|
|
|
function title() {
|
|
echo "----------------------------------------"
|
|
# Center the text
|
|
local text="$1"
|
|
local line_length=40
|
|
local text_length=${#text}
|
|
local padding=$(( (line_length - text_length) / 2 ))
|
|
printf "%*s%s%*s\n" $padding "" "$text" $padding ""
|
|
echo "----------------------------------------"
|
|
}
|
|
|
|
function die() {
|
|
echo " "
|
|
title "$1"
|
|
echo " "
|
|
exit 1
|
|
}
|
|
|
|
function clean() {
|
|
echo "Cleaning build directories"
|
|
rm -rf build
|
|
rm -f cmake_install.cmake
|
|
rm -f CMakeCache.txt
|
|
rm -rf CMakeFiles
|
|
}
|
|
|
|
function clean_cache() {
|
|
echo "Cleaning cache directories"
|
|
rm -rf ${CACHE_DIR}
|
|
}
|
|
|
|
function build() {
|
|
ARCH=$1
|
|
|
|
clean
|
|
mkdir -p ${BUILD_DIR}
|
|
|
|
DOCKCROSS_SCRIPT="${CACHE_DIR}/dockcross-${ARCH}"
|
|
|
|
if [ ! -f "${DOCKCROSS_SCRIPT}" ]; then
|
|
echo "Downloading dockcross-${ARCH}"
|
|
docker run --rm "dockcross/${ARCH}-full" > "${DOCKCROSS_SCRIPT}"
|
|
chmod +x "${DOCKCROSS_SCRIPT}"
|
|
fi
|
|
|
|
echo "Building $ARCH executable"
|
|
cd "${SCRIPT_DIR}"
|
|
"${DOCKCROSS_SCRIPT}" bash -c "cd build && cmake .. && make -j$(nproc)"
|
|
|
|
if [ ! -f build/simple_object_storage ]; then
|
|
die "Failed to build $ARCH executable"
|
|
fi
|
|
|
|
echo "Copying $ARCH executable to $EXE_DIR/simple_object_storage-$ARCH"
|
|
cp build/simple_object_storage $EXE_DIR/simple_object_storage-$ARCH
|
|
}
|
|
|
|
|
|
# Exit on error
|
|
set -e
|
|
|
|
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
|
cd $SCRIPT_DIR
|
|
|
|
clean_cache
|
|
|
|
mkdir -p ${CACHE_DIR}
|
|
mkdir -p ${EXE_DIR}
|
|
|
|
title "Building linux-arm64 executable"
|
|
build linux-arm64 || die "Failed to build linux-arm64 executable"
|
|
|
|
title "Building linux-x86_64 executable"
|
|
build linux-x86_64 || die "Failed to build linux-x86_64 executable"
|
|
# use Docker architecture style
|
|
cp ${EXE_DIR}/simple_object_storage-linux-x86_64 ${EXE_DIR}/simple_object_storage-linux-amd64
|
|
|
|
echo "Compile completed successfully!"
|
|
echo "amd64 executable: ./simple_object_storage-linux-amd64"
|
|
echo "arm64 executable: ./simple_object_storage-linux-arm64"
|
|
|
|
echo "Setting up Docker BuildX"
|
|
docker buildx create --name mybuilder --use || true
|
|
|
|
echo "Building multi-platform Docker image"
|
|
docker buildx build --load -t simple-object-storage:latest --platform linux/amd64,linux/arm64 .
|
|
|
|
clean
|
|
clean_cache |