name: anbox
summary: Android in a Box
description: |
  Runtime for Android applications which runs a full Android system
  in a container using Linux namespaces (user, ipc, net, mount) to
  separate the Android system fully from the host.
confinement: devmode
grade: devel
base: core20
architectures: [amd64]
adopt-info: anbox

slots:
  dbus-session-slot:
    interface: dbus
    bus: session
    name: org.anbox

plugs:
  dbus-session-plug:
    interface: dbus
    bus: session
    name: org.anbox

apps:
  anbox:
    command: bin/desktop-launch $SNAP/bin/anbox-wrapper.sh
    slots:
      - dbus-session-slot
    plugs:
      - x11
      - unity7
      - network
      - opengl
      - wayland
      - pulseaudio
      - home
      - process-control
      - desktop

  container-manager:
    command: bin/container-manager.sh start
    stop-command: bin/container-manager.sh stop
    daemon: simple
    plugs:
      - firewall-control
      - mount-observe
      - network-control
      - network-bind

  collect-bug-info:
    command: bin/collect-bug-info.sh
  shell:
    command: bin/anbox-shell.sh
  android-settings:
    command: bin/desktop-launch $SNAP/bin/app-android-settings.sh
    desktop: desktop/android-settings.desktop
    slots:
      - dbus-session-slot
    plugs:
      - dbus-session-plug
      - x11
      - unity7
      - network
      - opengl
      - wayland
      - pulseaudio
      - home
      - process-control
      - desktop

  appmgr:
    command: bin/desktop-launch $SNAP/bin/app-appmgr.sh
    desktop: desktop/appmgr.desktop
    slots:
      - dbus-session-slot
    plugs:
      - dbus-session-plug
      - x11
      - unity7
      - network
      - opengl
      - wayland
      - pulseaudio
      - home
      - process-control
      - desktop

parts:
  android:
    plugin: dump
    # This needs to be any directory but not the root one as if we use
    # it we get superious permissions errors with the copied .git tree
    source: data
    build-packages:
      - wget
    override-build: |
      ARCH=$(uname -m)
      LOCAL_IMAGE="$SNAPCRAFT_PART_SRC/android-images/android-$ARCH.img"

      if [ -f "$LOCAL_IMAGE" ]; then

        echo "Using local image $LOCAL_IMAGE"
        cp "$LOCAL_IMAGE" "$SNAPCRAFT_PART_INSTALL"/android.img

      else
        echo "android image 'data/android-images/android-$ARCH.img'" \
             "not found, downloading one\n\n"

        IMAGE_PATH=
        IMAGE_NAME=

        case "$ARCH" in
          x86_64)
            IMAGE_PATH="2018/07/19"
            IMAGE_NAME="android_amd64.img"
            IMAGE_HASH="6b04cd33d157814deaf92dccf8a23da4dc00b05ca6ce982a03830381896a8cca"
            ;;
          *)
            echo "ERROR: Anbox upstream doesn't provide an image for $ARCH"
            exit 1
            ;;
        esac

        # FIXME: downloading with a source: field doesn't work as snapcraft
        # expects the downloaded file to be an archive it can extract.
        echo "Downloading image..."
        wget https://build.anbox.io/android-images/$IMAGE_PATH/$IMAGE_NAME

        echo "$IMAGE_HASH $IMAGE_NAME" > image-hash
        sha256sum -c image-hash || exit 1

        mv $IMAGE_NAME $SNAPCRAFT_PART_INSTALL/android.img

      fi
    prime:
      - android.img

  desktop-glib-only:
    source: https://github.com/ubuntu/snapcraft-desktop-helpers.git
    source-subdir: glib-only
    plugin: make
    build-packages:
      - libglib2.0-dev
    stage-packages:
      - libglib2.0-bin

  apparmor:
    plugin: nil
    stage-packages:
      - apparmor

  zip:
    plugin: nil
    stage-packages:
      - zip
    prime:
      - usr/bin/zip

  lxc:
    source: https://github.com/lxc/lxc
    source-type: git
    source-tag: lxc-3.0.1
    build-packages:
      - libapparmor-dev
      - libcap-dev
      - libgnutls28-dev
      - libseccomp-dev
      - pkg-config
    plugin: autotools
    autotools-configure-parameters:
      - --disable-selinux
      - --disable-python
      - --disable-lua
      - --disable-tests
      - --disable-examples
      - --disable-doc
      - --disable-api-docs
      - --disable-bash
      - --disable-cgmanager
      - --enable-apparmor
      - --enable-seccomp
      - --enable-capabilities
      - --with-rootfs-path=/var/snap/anbox/common/lxc/
      - --libexecdir=/snap/anbox/current/libexec/
      - --prefix=/usr/
    override-build: |
      set -ex
      git config user.email "buildbot@anbox.io"
      git config user.name "Anbox Buildbot"
      git remote add anbox https://github.com/anbox/lxc
      git fetch anbox
      # apparmor: don't require a transition for Anbox child profiles
      git cherry-pick 2f81fb7c91560b32e506bb874f8cd63e37985906
      set +ex
      snapcraftctl build
    organize:
      snap/anbox/current/libexec: libexec
    prime:
      - usr/lib/liblxc.so.1
      - usr/lib/liblxc.so.1.4.0
      - libexec/lxc/lxc-monitord
      - usr/bin/lxc-start
      - usr/bin/lxc-stop
      - usr/bin/lxc-info
      - usr/bin/lxc-attach
      - usr/bin/lxc-ls
      - usr/bin/lxc-top

  swiftshader:
    plugin: cmake
    source: https://swiftshader.googlesource.com/SwiftShader
    source-type: git
    source-commit: b6e8c3f0f4830887d69ba765a922ac3c40e81dd9
    organize:
      libEGL.so: lib/anbox/swiftshader/libEGL.so
      libGLES_CM.so: lib/anbox/swiftshader/libGLES_CM.so
      libGLESv2.so: lib/anbox/swiftshader/libGLESv2.so

  anbox-scripts:
    plugin: dump
    source: scripts
    organize:
      snap-wrapper.sh: bin/anbox-wrapper.sh
      container-manager.sh: bin/container-manager.sh
      anbox-bridge.sh: bin/anbox-bridge.sh
      collect-bug-info.sh: bin/collect-bug-info.sh
      app-android-settings.sh: bin/app-android-settings.sh
      app-appmgr.sh: bin/app-appmgr.sh
      anbox-shell.sh: bin/anbox-shell.sh
    prime:
      - bin/anbox-bridge.sh
      - bin/anbox-shell.sh
      - bin/anbox-wrapper.sh
      - bin/container-manager.sh
      - bin/collect-bug-info.sh
      - bin/app-*.sh

  anbox-data:
    plugin: dump
    source: data
    prime:
      - glvnd
      - desktop
      - apparmor
      - seccomp

  anbox:
    plugin: cmake
    after:
      - lxc
      - desktop-glib-only
    source: .
    source-type: git
    cmake-parameters:
      # FIXME: Anbox currently has some paths with hard coded prefixes. Once
      # that is fixed we can avoid using a prefix here.
      - -DCMAKE_INSTALL_PREFIX:PATH=/usr
      - -DANBOX_VERSION=$SNAPCRAFT_PROJECT_VERSION
      - -DBINDERFS_PATH=/var/snap/anbox/common/binderfs
      # FIXME: Once we have everything in place for full snap confinement we
      # can securely enable this.
      # - -DSNAP_CONFINEMENT=ON
    build-packages:
      - build-essential
      - cmake
      - cmake-data
      - cmake-extras
      - dbus
      - debhelper
      - google-mock
      - libboost-dev
      - libboost-filesystem-dev
      - libboost-iostreams-dev
      - libboost-log-dev
      - libboost-program-options-dev
      - libboost-system-dev
      - libboost-test-dev
      - libboost-thread-dev
      - libcap-dev
      - libdbus-1-dev
      - libegl1-mesa-dev
      - libexpat1-dev
      - libgles2-mesa-dev
      - libglm-dev
      - libgmock-dev
      - libgtest-dev
      - libproperties-cpp-dev
      - libprotobuf-dev
      - libsdl2-dev
      - libsdl2-image-dev
      - libsystemd-dev
      - libx11-xcb-dev
      - pkg-config
      - protobuf-compiler
      - python3
    stage-packages:
      - libboost-filesystem1.71.0
      - libboost-iostreams1.71.0
      - libboost-log1.71.0
      - libboost-program-options1.71.0
      - libboost-system1.71.0
      - libboost-thread1.71.0
      - libdb5.3
      - libegl1-mesa
      - libgl1-mesa-glx
      - libgles2-mesa
      - libprotobuf-lite17
      - libsdl2-2.0-0
      - libsdl2-gfx-1.0-0
      - libsdl2-image-2.0-0
      - libsystemd0
    prime:
      - usr/bin/anbox
      - usr/share/anbox
      - usr/lib/*-linux-*/
    override-build: |
      rev=$(cd "$SNAPCRAFT_PART_SRC_WORK" ; git rev-parse --short HEAD)
      snapcraftctl set-version 4+gitr"$rev"
      # HACK: for some reason, snapcraft puts a copy of the source code
      # in the build directory. Let's remove it so it doesn't conflict.
      rm -rf *
      snapcraftctl build
