# This dockerfile takes ROS2 source packages from ${ROS2_WORKSPACE}/ros2_ws/src
# and builds them for the specified target platform.
# It uses qemu user-mode static emulation libraries from ${ROS2_WORKSPACE}/qemu-user-static/
# to emulate the target platform.

# Assumptions: ros2_ws/src and qemu-user-static directories are present in  ${ROS2_WORKSPACE}.

ARG ROS2_BASE_IMG
FROM ${ROS2_BASE_IMG}

ARG ROS2_WORKSPACE
ARG ROS_DISTRO
ARG TARGET_TRIPLE
ARG TARGET_ARCH

COPY qemu-user-static/ /usr/bin/
COPY ${ROS2_WORKSPACE}/src /ros2_ws/src

# Set timezone
RUN echo 'Etc/UTC' > /etc/timezone && \
    ln -sf /usr/share/zoneinfo/Etc/UTC /etc/localtime && \
    apt-get update && apt-get install -q -y tzdata && \
    rm -rf /var/lib/apt/lists/*

# Set locale
RUN apt-get update && apt-get install -y locales
RUN locale-gen en_US en_US.UTF-8 && \
    update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL C.UTF-8

# Add the ros2 apt repo
RUN apt update && apt install -y \
    curl \
    gnupg2 \
    lsb-release
RUN curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | apt-key add -
RUN sh -c 'echo "deb [arch=amd64,arm64] http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" \
    > /etc/apt/sources.list.d/ros2-latest.list'

# ROS2 dependencies
RUN apt update && apt install -y \
    build-essential \
    cmake \
    git \
    python3-pip \
    python-rosdep \
    wget

# Install some pip packages needed for testing
RUN python3 -m pip install -U \
    argcomplete \
    colcon-common-extensions \
    colcon-mixin \
    flake8 \
    flake8-blind-except \
    flake8-builtins \
    flake8-class-newline \
    flake8-comprehensions \
    flake8-deprecated \
    flake8-docstrings \
    flake8-import-order \
    flake8-quotes \
    pytest-repeat \
    pytest-rerunfailures \
    pytest \
    pytest-cov \
    pytest-runner \
    setuptools \
    vcstool

# Install Fast-RTPS dependencies
RUN apt install --no-install-recommends -y \
    libasio-dev \
    libtinyxml2-dev

# Setup ROS2 workspace
WORKDIR /ros2_ws
RUN mkdir -p /opt/ros/${ROS_DISTRO}/
RUN mkdir -p /opt/ros/${ROS_DISTRO}/share && touch /opt/ros/${ROS_DISTRO}/setup.bash

# Install rosdep dependencies
RUN rm -f /etc/ros/rosdep/sources.list.d/20-default.list    # In case of cached image.
RUN rosdep init
RUN rosdep update
RUN rosdep install --from-paths src /opt/ros/${ROS_DISTRO}/share \
    --ignore-src \
    --rosdistro ${ROS_DISTRO} -y \
    --skip-keys "console_bridge \
        fastcdr \
        fastrtps \
        libopensplice67 \
        libopensplice69 \
        rti-connext-dds-5.3.1 \
        urdfdom_headers"

RUN colcon mixin add cc_mixin \
    https://raw.githubusercontent.com/aws-ros-dev/cross_compile/cc-sysroot-creator/mixins/index.yaml && \
    colcon mixin update cc_mixin
RUN colcon build --mixin ${TARGET_ARCH}-docker

# To avoid linker issues when using a compiler with different GLIBC and GLIBCXX
# root_path/usr should be used as CMAKE_FIND_ROOT_PATH in the toolchain file
RUN mkdir -p /root_path/usr \
 && ln -s ../lib/${TARGET_TRIPLE} /root_path/lib \
 && ln -s ../../usr/lib/${TARGET_TRIPLE} /root_path/usr/lib \
 && cp -r /usr/lib/gcc/${TARGET_TRIPLE}/7/* /root_path/usr/lib

WORKDIR /
RUN apt install -y symlinks
RUN symlinks -rc .
