Skip to content

Commit

Permalink
Builds all binaries in Docker in one step.
Browse files Browse the repository at this point in the history
Add specific Gradle clean tasks and update build scripts for selective cleaning and dynamic resource allocation

 - **Gradle Configuration:**
   - Added `cleanNative*Jar` tasks in `build.gradle` to delete specific versioned native JAR files:
     - `cleanNativeMacOSArm64Jar`
     - `cleanNativeMacOSX86_64Jar`
     - `cleanNativeWindowsX86_64Jar`
     - `cleanNativeLinuxJar`
     - `cleanNativeAndroidArmJar`
     - `cleanNativeAndroidX86Jar`
     - `cleanNativeAndroidArm64Jar`
     - `cleanNativeAndroidX64Jar`

 - **Build Scripts Updates:**
   - **swig/build-*.sh:**
     - Updated `run_bjam` commands to utilize dynamic CPU core allocation:
       - `CORES=$(( $(nproc) / 2 ))`
       - `run_bjam="${BOOST_ROOT}/b2 -j${CORES} ..."`
     - Added `run_clean_native_jar` variables to specify the corresponding Gradle clean tasks, e.g., `./gradlew cleanNativeAndroidArmJar`

   - **swig/build-utils.shinc:**
     - Added `abort_if_var_unset "run_clean_native_jar" ${run_clean_native_jar}`
     - Removed global Gradle clean commands:
       - `./gradlew clean --stop`
       - `./gradlew clean --refresh-dependencies`
     - Updated `build_libraries` function to:
       - Run `./gradlew jar`
       - Execute `run_clean_native_jar` and `run_native_jar` to clean and build specific native JARs.

 - **Docker Build Script:**
   - **swig/docker_build_binaries.sh:**
     - Added shebang (`#!/bin/bash`)
     - Implemented functions to dynamically calculate 80% of host's available memory (`get_available_memory`) and number of CPU cores (`get_num_cpus`), compatible with both Linux and macOS.
     - Assigned `TOTAL_MEMORY` and `NUM_CPUS` based on calculations.
     - Updated `docker run` command to:
       - Use `--cpus=$NUM_CPUS` and `--memory=$TOTAL_MEMORY` for dynamic resource allocation.
       - Execute build sequence: `/build_desktop.sh && /apply_android_patches.sh && /build_android_all.sh`

 - **Additional Changes:**
   - Updated `swig/config/linux-x86_64-config.jam` to use `g++-9` instead of `g++-7`.

**Summary:**

 - Implemented specific Gradle clean tasks to target individual native JAR files, preventing the global clean from deleting all build artifacts.
 - Updated build scripts to utilize these specific clean tasks, ensuring only relevant JARs are cleaned and rebuilt.
 - Enhanced Docker build script to dynamically allocate 80% of host memory and appropriate CPU cores, ensuring efficient resource usage.
 - Enabled the Docker build process to execute multiple build scripts in the correct order, building desktop binaries, applying Android patches, and building all Android architectures.
 - Upgraded GCC compiler version in Linux build configuration for better compatibility and performance.

**Impact:**

 - Prevents accidental deletion of existing JARs when building new native binaries.
 - Optimizes resource allocation during Docker builds, enhancing build performance.
 - Ensures a structured and reliable multi-platform build process within Docker.
  • Loading branch information
gubatron committed Sep 26, 2024
1 parent 9085d57 commit 997382f
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 24 deletions.
45 changes: 40 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,46 @@ task nativeAndroidX64Jar(type: Zip) {
rename(".so", "-${version}.so")
}

// Clean task for nativeMacOSArm64Jar
task cleanNativeMacOSArm64Jar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-macosx-arm64-${version}.jar"
}

// Clean task for nativeMacOSX86_64Jar
task cleanNativeMacOSX86_64Jar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-macosx-x86_64-${version}.jar"
}

// Clean task for nativeWindowsX86_64Jar
task cleanNativeWindowsX86_64Jar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-windows-${version}.jar"
}

// Clean task for nativeLinuxJar
task cleanNativeLinuxJar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-linux-${version}.jar"
}

// Clean task for nativeAndroidArmJar
task cleanNativeAndroidArmJar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-android-arm-${version}.jar"
}

// Clean task for nativeAndroidX86Jar
task cleanNativeAndroidX86Jar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-android-x86-${version}.jar"
}

// Clean task for nativeAndroidArm64Jar
task cleanNativeAndroidArm64Jar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-android-arm64-${version}.jar"
}

// Clean task for nativeAndroidX64Jar
task cleanNativeAndroidX64Jar(type: Delete) {
delete "${buildDir}/libs/jlibtorrent-android-x86_64-${version}.jar"
}

artifacts {
archives sourcesJar
archives javadocJar
Expand Down Expand Up @@ -164,11 +204,6 @@ def pomData() {
name 'Angel Leon'
email '[email protected]'
}
developer {
id 'aldenml'
name 'Alden Torres'
email '[email protected]'
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion swig/build-android-arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export NDK_VERSION=r26d
export PATH=${ANDROID_TOOLCHAIN}/bin:${PATH}
export CXX=${ANDROID_TOOLCHAIN}/bin/armv7a-linux-androideabi${android_api}-clang++
export CC=${ANDROID_TOOLCHAIN}/bin/armv7a-linux-androideabi${android_api}-clang
export run_bjam="${BOOST_ROOT}/b2 -j8 -q --debug-building --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-linux-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}eabi-v7a"
export CORES=$(( $(nproc) / 2 ))
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} -q --debug-building --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-linux-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}eabi-v7a"
export run_objcopy="${ANDROID_TOOLCHAIN}/bin/llvm-objcopy --only-keep-debug bin/release/${os_build}/${os_arch}eabi-v7a/${SHARED_LIB} bin/release/${os_build}/${os_arch}eabi-v7a/${SHARED_LIB}.debug"
export run_strip="${ANDROID_TOOLCHAIN}/bin/llvm-strip --strip-unneeded -x -g bin/release/${os_build}/${os_arch}eabi-v7a/${SHARED_LIB}"
export run_readelf="${ANDROID_TOOLCHAIN}/bin/llvm-readelf -d bin/release/${os_build}/${os_arch}eabi-v7a/${SHARED_LIB}"
export run_native_jar="./gradlew nativeAndroidArmJar"
export run_clean_native_jar="./gradlew cleanNativeAndroidArmJar"
export BOOST_ROOT=/src/boost_${BOOST_UNDERSCORE_VERSION}

if [ "${run_swig_only}" = true ]; then
Expand Down
4 changes: 3 additions & 1 deletion swig/build-android-arm64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export NDK_VERSION=r26d
export PATH=${ANDROID_TOOLCHAIN}/bin:${PATH}
export CXX=${ANDROID_TOOLCHAIN}/bin/aarch64-linux-android${android_api}-clang++
export CC=${ANDROID_TOOLCHAIN}/bin/aarch64-linux-android${android_api}-clang
export run_bjam="${BOOST_ROOT}/b2 -j8 -q --debug-building --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}-v8a"
export CORES=$(( $(nproc) / 2 ))
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} -q --debug-building --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}-v8a"
export run_objcopy="${ANDROID_TOOLCHAIN}/bin/llvm-objcopy --only-keep-debug bin/release/${os_build}/${os_arch}-v8a/${SHARED_LIB} bin/release/${os_build}/${os_arch}-v8a/${SHARED_LIB}.debug"
export run_strip="${ANDROID_TOOLCHAIN}/bin/llvm-strip --strip-unneeded -x -g bin/release/${os_build}/${os_arch}-v8a/${SHARED_LIB}"
export run_readelf="${ANDROID_TOOLCHAIN}/bin/llvm-readelf -d bin/release/${os_build}/${os_arch}-v8a/${SHARED_LIB}"
export run_native_jar="./gradlew nativeAndroidArm64Jar"
export run_clean_native_jar="./gradlew cleanNativeAndroidArm64Jar"
export BOOST_ROOT=/src/boost_${BOOST_UNDERSCORE_VERSION}

if [ "${run_swig_only}" = true ]; then
Expand Down
4 changes: 3 additions & 1 deletion swig/build-android-x86.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export NDK_VERSION=r26d
export PATH=${ANDROID_TOOLCHAIN}/bin:${PATH};
export CXX=${ANDROID_TOOLCHAIN}/bin/i686-linux-android${android_api}-clang++
export CC=${ANDROID_TOOLCHAIN}/bin/i686-linux-android${android_api}-clang
export run_bjam="${BOOST_ROOT}/b2 -j8 -q --debug-building --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export CORES=$(( $(nproc) / 2 ))
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} -q --debug-building --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export run_objcopy="${ANDROID_TOOLCHAIN}/bin/llvm-objcopy --only-keep-debug bin/release/${os_build}/${os_arch}/${SHARED_LIB} bin/release/${os_build}/${os_arch}/${SHARED_LIB}.debug"
export run_strip="${ANDROID_TOOLCHAIN}/bin/llvm-strip --strip-unneeded -x -g bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_readelf="${ANDROID_TOOLCHAIN}/bin/llvm-readelf -d bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_native_jar="./gradlew nativeAndroidX86Jar"
export run_clean_native_jar="./gradlew cleanNativeAndroidX86Jar"
export BOOST_ROOT=/src/boost_${BOOST_UNDERSCORE_VERSION}

if [ "${run_swig_only}" = true ]; then
Expand Down
4 changes: 3 additions & 1 deletion swig/build-android-x86_64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export NDK_VERSION=r26d
export PATH=${ANDROID_TOOLCHAIN}/bin:${PATH};
export CXX=${ANDROID_TOOLCHAIN}/bin/x86_64-linux${android_api}-android-clang++
export CC=${ANDROID_TOOLCHAIN}/bin/x86_64-linux-android${android_api}-clang
export run_bjam="${BOOST_ROOT}/b2 -j8 --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export CORES=$(nproc)
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=clang-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export run_objcopy="${ANDROID_TOOLCHAIN}/bin/llvm-objcopy --only-keep-debug bin/release/${os_build}/${os_arch}/${SHARED_LIB} bin/release/${os_build}/${os_arch}/${SHARED_LIB}.debug"
export run_strip="${ANDROID_TOOLCHAIN}/bin/llvm-strip --strip-unneeded -x -g bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_readelf="${ANDROID_TOOLCHAIN}/bin/llvm-readelf -d bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_native_jar="./gradlew nativeAndroidX64Jar"
export run_clean_native_jar="./gradlew cleanNativeAndroidX86Jar"
export BOOST_ROOT=/src/boost_${BOOST_UNDERSCORE_VERSION}

if [ "${run_swig_only}" = true ]; then
Expand Down
4 changes: 3 additions & 1 deletion swig/build-linux-x86_64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export os_build=linux
export SHARED_LIB=lib${LIBRARY_NAME}.so
export CXX=g++
export CC=gcc-7
export CORES=$(( $(nproc) / 2 ))
export run_openssl_configure="./Configure linux-x86_64 ${OPENSSL_NO_OPTS} -fPIC --prefix=${OPENSSL_ROOT}"
export run_readelf="readelf -d bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_bjam="${BOOST_ROOT}/b2 -j2 --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=gcc-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=gcc-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export run_objcopy="objcopy --only-keep-debug bin/release/${os_build}/${os_arch}/${SHARED_LIB} bin/release/${os_build}/${os_arch}/${SHARED_LIB}.debug"
export run_strip="strip --strip-unneeded -x bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_readelf="readelf -d bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_native_jar="./gradlew nativeLinuxJar"
export run_clean_native_jar="./gradlew cleanNativeLinuxJar"
press_any_to_continue
prepare_libtorrent
export BOOST_ROOT=/src/boost_${BOOST_UNDERSCORE_VERSION}
Expand Down
4 changes: 3 additions & 1 deletion swig/build-macos-arm64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export SHARED_LIB=lib${LIBRARY_NAME}.dylib
export RELEASE_SHARED_LIB=lib${LIBRARY_NAME}.${os_arch}.dylib
export CXX=g++
export CC=gcc
export CORES=$(nproc)
export run_openssl_configure="./Configure darwin64-${os_arch}-cc ${OPENSSL_NO_OPTS} --prefix=${OPENSSL_ROOT}"
export run_readelf="otool -L bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_bjam="${BOOST_ROOT}/b2 -j16 -d2 --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=darwin-${os_arch} target-os=darwin location=bin/release/${os_build}/${os_arch}"
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} -d2 --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=darwin-${os_arch} target-os=darwin location=bin/release/${os_build}/${os_arch}"
export run_objcopy="echo dummy run_objcopy for ${os_build} ${os_arch}"
export run_strip="strip -S -x bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_native_jar="./gradlew nativeMacOSArm64Jar"
export run_clean_native_jar="./gradlew cleanNativeMacOSArm64Jar"
create_folder_if_it_doesnt_exist ${SRC}
prompt_msg "$0:About to prepare BOOST ${BOOST_VERSION}"

Expand Down
6 changes: 4 additions & 2 deletions swig/build-macos-x86_64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export SHARED_LIB=lib${LIBRARY_NAME}.dylib
export RELEASE_SHARED_LIB=lib${LIBRARY_NAME}.${os_arch}.dylib
export CXX=g++
export CC=gcc
export CORES=$(( $(nproc) / 2 ))
export run_openssl_configure="./Configure darwin64-${os_arch}-cc ${OPENSSL_NO_OPTS} --prefix=${OPENSSL_ROOT}"
export run_readelf="otool -L bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_bjam="${BOOST_ROOT}/b2 -j10 --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=darwin-${os_arch} target-os=darwin location=bin/release/${os_build}/${os_arch}"
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=darwin-${os_arch} target-os=darwin location=bin/release/${os_build}/${os_arch}"
export run_objcopy="echo dummy run_objcopy for ${os_build} ${os_arch}"
export run_strip="strip -S -x bin/release/${os_build}/${os_arch}/${SHARED_LIB}"
export run_native_jar="./gradlew nativeMacOSX86_64Jar"
export run_clean_native_jar="./gradlew cleanNativeMacOSX86_64Jar"
create_folder_if_it_doesnt_exist ${SRC}
prompt_msg "$0:About to prepare BOOST ${BOOST_VERSION}"

Expand All @@ -53,4 +55,4 @@ if [ "${run_build_only}" = true ]; then
fi

./run-swig.sh
build_libraries
build_libraries
5 changes: 3 additions & 2 deletions swig/build-utils.shinc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ build_libraries() {
abort_if_var_unset "run_objcopy" ${run_objcopy}
abort_if_var_unset "run_strip" ${run_strip}
abort_if_var_unset "run_native_jar" ${run_native_jar}
abort_if_var_unset "run_clean_native_jar" ${run_clean_native_jar}
set -x
cd ${SWIG}
export PATH=${PATH}:${BOOST_ROOT}/tools/build/src/engine
Expand Down Expand Up @@ -254,9 +255,9 @@ build_libraries() {
mv swig/bin/release/${os_build}/${os_arch}/${SHARED_LIB} swig/bin/release/${os_build}/${os_arch}/${RELEASE_SHARED_LIB}
cp swig/bin/release/${os_build}/${os_arch}/${RELEASE_SHARED_LIB} .
fi
./gradlew clean --stop
./gradlew clean --refresh-dependencies

./gradlew jar
$run_clean_native_jar
$run_native_jar
}

Expand Down
4 changes: 3 additions & 1 deletion swig/build-windows-x86_64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export os_build=windows
export SHARED_LIB=lib${LIBRARY_NAME}.dll
export CXX=x86_64-w64-mingw32-g++-posix
export CC=x86_64-w64-mingw32-gcc-posix
export run_bjam="${BOOST_ROOT}/b2 -j2 --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=gcc-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export CORES=$(( $(nproc) / 2 ))
export run_bjam="${BOOST_ROOT}/b2 -j${CORES} --user-config=config/${os_build}-${os_arch}-config.jam variant=release toolset=gcc-${os_arch} target-os=${os_build} location=bin/release/${os_build}/${os_arch}"
export run_objcopy="echo dummy run_objcopy for ${os_build} ${os_arch}"
export run_strip="x86_64-w64-mingw32-strip --strip-unneeded -x bin/release/${os_build}/${os_arch}/libjlibtorrent.dll";
export run_readelf="eval objdump -p bin/release/${os_build}/${os_arch}/jlibtorrent.dll | grep DLL"
export run_native_jar="./gradlew nativeWindowsX86_64Jar"
export run_clean_native_jar="./gradlew cleanNativeWindowsX86_64Jar"

if [ "${run_prep}" = true ]; then
prepare_libtorrent
Expand Down
2 changes: 1 addition & 1 deletion swig/config/linux-x86_64-config.jam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os ;

using gcc : x86_64 : g++-7 :
using gcc : x86_64 : g++-9 :
<cxxflags>-std=c++17
<cxxflags>-fPIC
<cxxflags>-fno-strict-aliasing
Expand Down
72 changes: 65 additions & 7 deletions swig/docker_build_binaries.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
#!/bin/bash
# One Step Build (It will be cached if nothing changed)
./docker_build_image.sh

# Function to get 80% of free memory in GB
get_available_memory() {
local free_mem
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux
free_mem_kb=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
free_mem=$(echo "$free_mem_kb / 1024 / 1024" | bc -l) # Convert to GB
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
# Get page size
page_size=$(sysctl -n hw.pagesize)
# Get number of free pages
free_pages=$(vm_stat | awk '/Pages free/ {print $3}' | sed 's/\.//')
# Get number of inactive pages (considered as available)
inactive_pages=$(vm_stat | awk '/Pages inactive/ {print $3}' | sed 's/\.//')
# Calculate free memory in bytes
free_mem_bytes=$(( (free_pages + inactive_pages) * page_size ))
# Convert to GB
free_mem=$(echo "$free_mem_bytes / 1024 / 1024 / 1024" | bc -l)
else
echo "Unsupported OS"
exit 1
fi

# Calculate 80% of free memory
mem_limit=$(echo "$free_mem * 0.8" | bc -l)

# Round down to the nearest whole number to avoid over-allocation
mem_limit_int=$(echo "$mem_limit" | awk '{printf("%d\n",$1)}')

# Ensure at least 1 GB is allocated
if [[ "$mem_limit_int" -lt 1 ]]; then
mem_limit_int=1
fi

echo "${mem_limit_int}gb"
}

# Function to get the number of CPU cores
get_num_cpus() {
local num_cpus
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
num_cpus=$(nproc)
elif [[ "$OSTYPE" == "darwin"* ]]; then
num_cpus=$(sysctl -n hw.ncpu)
else
echo "Unsupported OS"
exit 1
fi
echo "$num_cpus"
}

TOTAL_MEMORY=$(get_available_memory)
NUM_CPUS=$(get_num_cpus)

echo "Total memory assigned to Docker: $TOTAL_MEMORY"
echo "Number of CPUs assigned to Docker: $NUM_CPUS"

# Mounts this repo's folder as a volume in the container's /frostwire-jlibtorrent create_folder_if_it_doesnt_exist
# Then executes the build scripts for android
docker \
run \
--cpus=8 \
--memory=32gb \
-v "$PWD/../../frostwire-jlibtorrent:/frostwire-jlibtorrent" \
-it jlibtorrent-android \
/bin/bash /build.sh #for some reason it won't run the script
docker run \
--cpus=$NUM_CPUS \
--memory=$TOTAL_MEMORY \
-v "$PWD/../../frostwire-jlibtorrent:/frostwire-jlibtorrent" \
-it jlibtorrent-android \
/bin/bash -c "/build_desktop.sh && /apply_android_patches.sh && /build_android_all.sh"

0 comments on commit 997382f

Please sign in to comment.