stages:
  - prepare
  - dependencies
  - gegl

variables:
  BABL_DIR: "_babl_install"
  BABL_PREFIX: "$CI_PROJECT_DIR/$BABL_DIR"
  CCACHE_DIR: "$CI_PROJECT_DIR/.ccache"

cache:
  key: ${CI_JOB_NAME}
  paths:
  - .ccache

image: $CI_REGISTRY_IMAGE:ci-latest

build-image:
  stage: prepare
  variables:
    GIT_STRATEGY: none
  cache: {}
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - echo "FROM archlinux:base-20210214.0.15477" > Dockerfile
    - echo "RUN pacman -Sy --noconfirm archlinux-keyring && pacman -Syu --noconfirm --ignore glibc --needed git base-devel ccache meson lcms2 gobject-introspection vala pacman-contrib && pacman -Scc " >> Dockerfile

    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:ci-latest --cache=true --cache-ttl=120h

.babl-base:
  stage: dependencies
  artifacts:
    paths:
    - $BABL_DIR
  variables:
    GIT_DEPTH: "5"
    CCACHE_MAXSIZE: "32Mi"
  before_script:
    - pacman -Sy --noconfirm archlinux-keyring
    - pacman -Syu --ignore glibc --noconfirm --needed git base-devel ccache meson lcms2 gobject-introspection vala
    - git clone --depth=$GIT_DEPTH https://gitlab.gnome.org/GNOME/babl.git _babl
    - cd _babl
    - ccache -z

babl-master:
  extends: .babl-base
  variables:
    GIT_STRATEGY: none
  script:
    - meson -Dprefix="$BABL_PREFIX" _build
    - ninja -C _build
    - ninja -C _build install
    - ccache -s

#babl-min:
#  extends: .babl-base
#  script:
#    - grep babl ../meson.build | grep ">=" | grep -o '[0-9]*\.[0-9]*\.[0-9]*' | sed 's/\./_/g' > .babl_min_version
#    - if [ x$(( $(cut .babl_min_version -f 3 -d _) % 2)) = x1 ] ; then touch .use_master ; fi
#    - test -e .use_master && git fetch
#    - test -e .use_master && git checkout master
#    - test -e .use_master || git fetch --no-tags origin "refs/tags/BABL_`cat .babl_min_version`:refs/tags/MIN_VERSION"
#    - test -e .use_master || git checkout MIN_VERSION
#    - meson -Dprefix="$BABL_PREFIX" _build
#    - ninja -C _build
#    - ninja -C _build install
#    - ccache -s


.gegl-deps:
  artifacts:
    paths:
    - _build/meson-logs/*
  before_script:
    - pacman -Sy --noconfirm archlinux-keyring
    - pacman -Syu --ignore glibc --noconfirm --needed
        base-devel ccache meson
        asciidoc
        ffmpeg
        gobject-introspection
        gobject-introspection-runtime
        jasper
        json-glib
        lcms2
        libgexiv2
        libraw
        librsvg
        libspiro
        libtiff
        luajit
        openexr
        poppler-glib
        python
        python-gobject
        sdl2
        suitesparse
        vala
    - ccache -z

.gegl-build:
  stage: gegl
  extends: .gegl-deps
  variables:
    GIT_DEPTH: "15"
    CCACHE_MAXSIZE: "128Mi"
  script:
   # Add babl location to XDG_DATA_DIRS. .gir files are located there.
   # Add babl location to GI_TYPELIB_PATH. Required for python tests.
   # Add /usr/local/share:/usr/share for vapigen.
   - export XDG_DATA_DIRS="$XDG_DATA_DIRS:$BABL_PREFIX/share:/usr/local/share/:/usr/share/"
   - export GI_TYPELIB_PATH="$BABL_PREFIX/lib/girepository-1.0:$GI_TYPELIB_PATH"
   - export LD_LIBRARY_PATH="$BABL_PREFIX/lib:$LD_LIBRARY_PATH"
   - meson -Dbuildtype=$BUILD_TYPE -Dpkg_config_path=$BABL_PREFIX/lib/pkgconfig $CONFIG_OPTIONS _build
   - ninja -C _build
   - ccache -s
   - meson test -C _build --print-errorlogs

.build-default:
  extends: .gegl-build
  variables:
    BUILD_TYPE: "release"
    CONFIG_OPTIONS: ""

.build-all:
  extends: .gegl-build
  variables:
    BUILD_TYPE: "release"
    CONFIG_OPTIONS: "-Dworkshop=true -Ddocs=true -Dintrospection=true"

.build-all-debug:
  extends: .build-all
  variables:
    BUILD_TYPE: "debugoptimized"

#default-min-babl:
#  extends: .build-default
#  dependencies:
#    - babl-min
#  needs: ["babl-min"]

fullconfig-master-babl:
  extends: .build-all
  dependencies:
    - babl-master
  needs: ["babl-master"]

debug-fullconfig-master-babl:
  extends: .build-all-debug
  dependencies:
    - babl-master
  needs: ["babl-master"]
