content: # syntax=docker/dockerfile:experimental
  
  # Use experimental buildkit for faster builds
  # https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md
  # Use `--progress=plain` to use plane stdout for docker build
  #
  # Example build command:
  # export DOCKER_BUILDKIT=1
  # export FROM_IMAGE="ros:humble"
  # export OVERLAY_MIXINS="release ccache lld"
  # docker build -t nav2:humble \
  #   --build-arg FROM_IMAGE \
  #   --build-arg OVERLAY_MIXINS \
  #   -f distro.Dockerfile ../
  
  ARG FROM_IMAGE=ros:humble
  ARG OVERLAY_WS=/opt/overlay_ws
  
  # multi-stage for caching
  FROM $FROM_IMAGE AS cacher
  
  # copy overlay source
  ARG OVERLAY_WS
  WORKDIR $OVERLAY_WS/src
  RUN echo "\
  repositories: \n\
    ros-planning/navigation2: \n\
      type: git \n\
      url: https://github.com/ros-planning/navigation2.git \n\
      version: ${ROS_DISTRO}-devel \n\
  " > ../overlay.repos
  RUN vcs import ./ < ../overlay.repos && \
      find ./ -name ".git" | xargs rm -rf
  # COPY ./ ./ros-planning/navigation2
  
  # copy manifests for caching
  WORKDIR /opt
  RUN mkdir -p /tmp/opt && \
      find ./ -name "package.xml" | \
        xargs cp --parents -t /tmp/opt && \
      find ./ -name "COLCON_IGNORE" | \
        xargs cp --parents -t /tmp/opt || true
  
  # multi-stage for building
  FROM $FROM_IMAGE AS builder
  ARG DEBIAN_FRONTEND=noninteractive
  
  # edit apt for caching
  RUN cp /etc/apt/apt.conf.d/docker-clean /etc/apt/ && \
      echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' \
        > /etc/apt/apt.conf.d/docker-clean
  
  # install CI dependencies
  RUN --mount=type=cache,target=/var/cache/apt \
      --mount=type=cache,target=/var/lib/apt \
      apt-get update && apt-get install -q -y \
        ccache \
        lcov \
        lld \
      && rosdep update
  
  # install overlay dependencies
  ARG OVERLAY_WS
  WORKDIR $OVERLAY_WS
  COPY --from=cacher /tmp/$OVERLAY_WS/src ./src
  RUN --mount=type=cache,target=/var/cache/apt \
      --mount=type=cache,target=/var/lib/apt \
      . /opt/ros/$ROS_DISTRO/setup.sh && \
      apt-get update && rosdep install -q -y \
        --from-paths src \
        --ignore-src \
      && rm -rf /var/lib/apt/lists/*
  
  # build overlay source
  COPY --from=cacher $OVERLAY_WS/src ./src
  ARG OVERLAY_MIXINS="release ccache lld"
  RUN --mount=type=cache,target=/root/.ccache \
      . /opt/ros/$ROS_DISTRO/setup.sh && \
      colcon build \
        --symlink-install \
        --mixin $OVERLAY_MIXINS
  
  # restore apt for docker
  RUN mv /etc/apt/docker-clean /etc/apt/apt.conf.d/ && \
      rm -rf /var/lib/apt/lists/
  
  # source overlay from entrypoint
  ENV OVERLAY_WS $OVERLAY_WS
  RUN sed --in-place \
        's|^source .*|source "$OVERLAY_WS/install/setup.bash"|' \
        /ros_entrypoint.sh
  
  # test overlay build
  ARG RUN_TESTS
  ARG FAIL_ON_TEST_FAILURE
  RUN if [ -n "$RUN_TESTS" ]; then \
          . install/setup.sh && \
          colcon test && \
          colcon test-result \
            || ([ -z "$FAIL_ON_TEST_FAILURE" ] || exit 1) \
      fi
  