Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update build system for multiplatform #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 27 additions & 17 deletions scripts/build_image_layers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ function usage() {
}

# Initialize arguments
DOCKER_BUILDKIT=1
DOCKER_BUILDKIT=0
IGNORE_COMPOSITE_KEYS=0
ADDITIONAL_BUILD_ARGS=()
ADDITIONAL_DOCKER_ARGS=()
DOCKER_SEARCH_DIRS=(${DOCKER_DIR})
SKIP_REGISTRY_CHECK=0
BASE_DOCKER_REGISTRY_NAMES=("nvcr.io/isaac/ros")
USER="azhangvo"

# Read and parse config file if exists
#
Expand Down Expand Up @@ -165,7 +166,7 @@ function cleanup {
}
trap cleanup EXIT

PLATFORM="$(uname -m)"
# PLATFORM="$(uname -m)"

# Resolve Dockerfiles by matching target image ids to available files
TARGET_IMAGE_IDS=(${TARGET_IMAGE_STR//./ })
Expand Down Expand Up @@ -283,22 +284,22 @@ fi
BUILD_ARGS+=("--build-arg" "USERNAME="admin"")
BUILD_ARGS+=("--build-arg" "USER_UID=`id -u`")
BUILD_ARGS+=("--build-arg" "USER_GID=`id -g`")
BUILD_ARGS+=("--build-arg" "PLATFORM=$PLATFORM")
# BUILD_ARGS+=("--build-arg" "PLATFORM=$PLATFORM")

for BUILD_ARG in ${ADDITIONAL_BUILD_ARGS[@]}
do
BUILD_ARGS+=("--build-arg" "${BUILD_ARG}")
done

# Check if GPU is installed
if [[ $PLATFORM == "x86_64" ]]; then
GPU_ATTACHED=(`nvidia-smi -a | grep "Attached GPUs" || true`)
if [ -z $GPU_ATTACHED ]; then
print_warning "No GPU detected! Not setting build args for HAS_GPU"
else
BUILD_ARGS+=("--build-arg" "HAS_GPU="true"")
fi
fi
# if [[ $PLATFORM == "x86_64" ]]; then
# GPU_ATTACHED=(`nvidia-smi -a | grep "Attached GPUs" || true`)
# if [ -z $GPU_ATTACHED ]; then
# print_warning "No GPU detected! Not setting build args for HAS_GPU"
# else
# BUILD_ARGS+=("--build-arg" "HAS_GPU="true"")
# fi
# fi

if [ ${#DOCKERFILES[@]} -gt 0 ]; then
print_info "Resolved the following ${#DOCKERFILES[@]} Dockerfiles for target image: ${TARGET_IMAGE_STR}"
Expand All @@ -316,7 +317,7 @@ for (( i=${#DOCKERFILES[@]}-1 ; i>=0 ; i-- )); do
DOCKERFILE=${DOCKERFILES[i]}
DOCKERFILE_CONTEXT_DIR=${DOCKERFILE_CONTEXT_DIRS[i]}
IMAGE_NAME=${DOCKERFILE#*"/Dockerfile."}
IMAGE_NAME="${IMAGE_NAME//./-}-image"
IMAGE_NAME="${USER}/${IMAGE_NAME//./-}"

# Build the base images in layers first
BASE_IMAGE_ARG=
Expand All @@ -329,7 +330,7 @@ for (( i=${#DOCKERFILES[@]}-1 ; i>=0 ; i-- )); do
if [ $i -lt $(( ${#DOCKERFILES[@]} - 1 )) ]; then
BASE_DOCKERFILE=${DOCKERFILES[i+1]}
BASE_IMAGE_NAME=${BASE_DOCKERFILE#*"/Dockerfile."}
BASE_IMAGE_NAME="${BASE_IMAGE_NAME//./-}-image"
BASE_IMAGE_NAME="${USER}/${BASE_IMAGE_NAME//./-}"

BASE_IMAGE_ARG="--build-arg BASE_IMAGE="${BASE_IMAGE_NAME}""
fi
Expand All @@ -346,12 +347,21 @@ for (( i=${#DOCKERFILES[@]}-1 ; i>=0 ; i-- )); do

print_warning "Building ${DOCKERFILE} as image: ${IMAGE_NAME} with base: ${BASE_IMAGE_NAME}"

DOCKER_BUILDKIT=${DOCKER_BUILDKIT} docker build -f ${DOCKERFILE} \
--network host \
print_info "DOCKER_BUILDKIT=${DOCKER_BUILDKIT} docker buildx build -f ${DOCKERFILE} \
-t ${IMAGE_NAME} \
${BASE_IMAGE_ARG} \
\"${BUILD_ARGS[@]}\" \
\"${ADDITIONAL_DOCKER_ARGS[@]}\" \
$@ \
${DOCKER_CONTEXT_ARG}"

DOCKER_BUILDKIT=${DOCKER_BUILDKIT} docker buildx build -f ${DOCKERFILE} \
-t ${IMAGE_NAME} \
${BASE_IMAGE_ARG} \
"${BUILD_ARGS[@]}" \
"${ADDITIONAL_DOCKER_ARGS[@]}" \
$@ \
${DOCKER_CONTEXT_ARG}
done
${DOCKER_CONTEXT_ARG} \
--push

done
8 changes: 2 additions & 6 deletions scripts/build_raw_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ IMAGE_KEY="ros2_humble.realsense_source.tools"

print_info "Building $IMAGE_KEY base as image: $BASE_NAME"
print_info "Running $ROOT/build_image_layers.sh -i \"$IMAGE_KEY\" --image_name \"$BASE_NAME\""
$ROOT/build_image_layers.sh -i "$IMAGE_KEY" --image_name "$IMAGE_NAME" -r
$ROOT/build_image_layers.sh -i "$IMAGE_KEY" --image_name "$IMAGE_NAME" -r --docker_arg "--platform=linux/amd64,linux/arm64"

# Check result
if [ $? -ne 0 ]; then
Expand All @@ -23,8 +23,4 @@ if [ $? -ne 0 ]; then
fi
fi

# Check image is available
if [[ -z $(docker image ls --quiet $IMAGE_NAME) ]]; then
print_error "No built image found for $IMAGE_NAME, aborting."
exit 1
fi
print_info "Finished building $IMAGE_KEY base as image: $BASE_NAME"