stages:
  - build
  - build2
  - publish

debian-stable-gcc:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/debian:stable
  script:
    - autoreconf -vfi
    - ./configure --enable-max-accsets=256
    - make
    - make distcheck
  artifacts:
    when: always
    paths:
      - ./spot-*/_build/sub/tests/*/*.log
      - ./*.log
      - ./*.tar.gz

debian-unstable-gcc-coverage:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/debian
  script:
    - autoreconf -vfi
    - ./configure CXX='g++ --coverage' --enable-devel --disable-static --enable-doxygen
    - make
    - make check
    - lcov --capture --directory . --no-external --output spot.info
    - lcov --remove spot.info '*/bin/spot.cc' '*/bin/spot-x.cc' '*/spot/parse*/scan*.cc' '*/spot/parse*/parse*.cc' '*/utf8/*' '*/python/*' '*/buddy/*' '*/doc/org/tmp/*' --output spot2.info
    - lcov --summary spot2.info
    - genhtml --legend --demangle-cpp --output-directory coverage spot2.info
  artifacts:
    when: always
    paths:
      - tests/*/*.log
      - ./*.log
      - doc/spot.html/
      - doc/userdoc/
      - coverage/
      - ./*.tar.gz
      - spot2.info

debian-gcc-snapshot:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/debian
  script:
    - export PATH="/usr/lib/gcc-snapshot/bin:$PATH" LD_LIBRARY_PATH="/usr/lib/gcc-snapshot/lib:$LD_LIBRARY_PATH"
    - autoreconf -vfi
    - ./configure --with-included-ltdl CXX='g++'
    - make
    - make distcheck  DISTCHECK_CONFIGURE_FLAGS='--with-included-ltdl'
  allow_failure: true
  artifacts:
    when: always
    paths:
      - ./spot-*/_build/sub/tests/*/*.log
      - ./*.log
      - doc/spot.html/
      - doc/userdoc/
      - ./*.tar.gz

alpine-gcc:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/alpine
  script:
    - autoreconf -vfi
    - ./configure
    - make
    - make check
    - make distcheck || { chmod -R u+w ./spot-*; false; }
  artifacts:
    when: always
    paths:
      - tests/*/*.log
      - ./*.log

arch-clang:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/arch
  script:
    - autoreconf -vfi
    - ./configure --prefix ~/install_dir CC='clang -Qunused-arguments' CXX='clang++ -Qunused-arguments' --enable-devel --enable-c++20 --enable-doxygen
    - make
    - make distcheck
  artifacts:
    when: on_failure
    paths:
      - ./spot-*/_build/sub/tests/*/*.log
      - ./*.log

arch-gcc-glibcxxdebug:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/arch
  script:
    - autoreconf -vfi
    - ./configure --enable-devel --enable-c++20 --enable-glibcxx-debug
    - make
    - make distcheck DISTCHECK_CONFIGURE_FLAGS='--enable-devel --enable-c++20 --enable-glibcxx-debug'
  artifacts:
    when: on_failure
    paths:
      - ./spot-*/_build/sub/tests/*/*.log
      - ./*.log

mingw-shared:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/debian
  script:
    - autoreconf -vfi
    - ./configure CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++-posix --host i686-w64-mingw32 --disable-python
    - touch doc/org-stamp
    - make
  artifacts:
    when: on_failure
    paths:
      - tests/*/*.log
      - ./*.log

mingw-static:
  stage: build
  only:
    - branches
  except:
    - /wip/
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/debian
  script:
    - mkdir install_dir
    - autoreconf -vfi
    - ./configure CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++-posix --host i686-w64-mingw32 --disable-python --disable-shared --enable-optimizations --disable-devel LDFLAGS='-static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -Wl,-lwinpthread -Wl,--no-whole-archive' --prefix=/usr
    - touch doc/org-stamp
    - make
    - make install-exec DESTDIR=$PWD/install_dir
    - VERSION=`autoconf --trace='AC_INIT:$2'`
    - cd install_dir
    - i686-w64-mingw32-strip usr/bin/*
    - tar cfvz ../spot-${VERSION}-${CI_COMMIT_REF_SLUG}-bin.tar.gz *
  artifacts:
    when: always
    paths:
      - tests/*/*.log
      - ./*.log
      - ./*.tar.gz

debpkg-stable:
  stage: build
  only:
    - /-deb$/
    - master
    - next
    - stable
  script:
    - docker pull gitlab-registry.lrde.epita.fr/spot/buildenv/debian:stable
    - vol=spot-stable-$CI_COMMIT_SHA
    - docker volume create $vol
    - exitcode=0
    - docker run --rm=true -v $vol:/build/result gitlab-registry.lrde.epita.fr/spot/buildenv/debian:stable ./build-spot.sh $CI_COMMIT_REF_NAME -j${NBPROC-1} || exitcode=$?
    - docker run -v $vol:/build/result --name helper-$vol registry.lrde.epita.fr/spot-debuild:stable true || exitcode=$?
    - docker cp helper-$vol:/build/result _build_stable || exitcode=$?
    - docker rm helper-$vol || exitcode=$?
    - docker volume rm $vol || exitcode=$?
    - ls -l _build_stable
    - exit $exitcode
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - _build_stable/

debpkg-stable-i386:
  stage: build2
  only:
    - /-deb$/
    - master
    - next
    - stable
  tags: ["x86"]
  needs: ["debpkg-stable"]
  script:
    - docker pull gitlab-registry.lrde.epita.fr/spot/buildenv/debian-i386:stable
    - vol=spot-stable-$CI_COMMIT_SHA
    - docker volume create $vol
    - exitcode=0
    - docker create -v $vol:/build/result --name helper-$vol gitlab-registry.lrde.epita.fr/spot/buildenv/debian-i386:stable ./bin-spot.sh -j${NBPROC-1} || exitcode=$?
    - docker cp _build_stable/. helper-$vol:/build/result || exitcode=$?
    - rm -rf _build_stable
    - docker start -a helper-$vol || exitcode=$?
    - docker cp helper-$vol:/build/result _build_stable || exitcode=$?
    - docker rm helper-$vol || exitcode=$?
    - docker volume rm $vol || exitcode=$?
    - ls -l _build_stable
    - exit $exitcode
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - _build_stable/

debpkg-unstable:
  stage: build
  only:
    - /-deb$/
    - next
  script:
    - docker pull gitlab-registry.lrde.epita.fr/spot/buildenv/debian
    - vol=spot-unstable-$CI_COMMIT_SHA
    - docker volume create $vol
    - exitcode=0
    - docker run -v $vol:/build/result --name helper-$vol gitlab-registry.lrde.epita.fr/spot/buildenv/debian ./build-spot.sh $CI_COMMIT_REF_NAME -j${NBPROC-1} || exitcode=$?
    - docker cp helper-$vol:/build/result _build_unstable || exitcode=$?
    - docker rm helper-$vol || exitcode=$?
    - docker volume rm $vol || exitcode=$?
    - ls -l _build_unstable
    - exit $exitcode
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - _build_unstable/

debpkg-unstable-i386:
  stage: build2
  only:
    - /-deb$/
    - next
  tags: ["x86"]
  needs: ["debpkg-unstable"]
  script:
    - docker pull gitlab-registry.lrde.epita.fr/spot/buildenv/debian-i386
    - vol=spot-unstable-$CI_COMMIT_SHA
    - docker volume create $vol
    - exitcode=0
    - docker create -v $vol:/build/result --name helper-$vol gitlab-registry.lrde.epita.fr/spot/buildenv/debian-i386 ./bin-spot.sh -j${NBPROC-1} || exitcode=$?
    - docker cp _build_unstable/. helper-$vol:/build/result || exitcode=$?
    - rm -rf _build_unstable
    - docker start -a helper-$vol || exitcode=$?
    - docker cp helper-$vol:/build/result _build_unstable || exitcode=$?
    - docker rm helper-$vol || exitcode=$?
    - docker volume rm $vol || exitcode=$?
    - ls -l _build_unstable
    - exit $exitcode
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - _build_unstable/

rpm-pkg:
  stage: build
  only:
    - /-rpm$/
    - master
    - next
    - stable
  image: gitlab-registry.lrde.epita.fr/spot/buildenv/fedora
  script:
    - autoreconf -vfi
    - ./configure
    - make
    - make dist
    - cp spot-*.tar.gz ~/rpmbuild/SOURCES/
    - cp spot.spec ~/rpmbuild/SPECS/
    - rpmbuild -bb ~/rpmbuild/SPECS/spot.spec
    - mv ~/rpmbuild/RPMS/x86_64/*.rpm .
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - ./*.rpm

publish-rpm:
  stage: publish
  only:
    - /-rpm$/
    - next
    - stable
  tags:
    - dput
  dependencies:
    - rpm-pkg
  script:
    - case $CI_COMMIT_REF_NAME in stable) rput fedora stable *.rpm;; next) rput fedora unstable *.rpm;; esac

publish-stable:
  only:
    - stable
  tags:
    - dput
  stage: publish
  dependencies:
    - debpkg-stable-i386
  script:
    - cd _build_stable
    - ls -l
    - dput lrde *.changes
    - tgz=`ls spot-*.tar.* | head -n 1`
    - case $tgz in *[0-9].tar.*) scp $tgz doc@perso:/var/www/dload/spot/;; esac
    - curl -X POST -F ref=master -F token=$TRIGGER_SPOT_WEB -F "variables[spot_branch]=stable" https://gitlab.lrde.epita.fr/api/v4/projects/131/trigger/pipeline

publish-unstable:
  only:
    - next
  tags:
    - dput
  stage: publish
  dependencies:
    - debpkg-unstable-i386
  script:
    - cd _build_unstable
    - ls -l
    - dput lrde *.changes
    - curl -X POST -F ref=master -F token=$TRIGGER_SPOT_WEB -F "variables[spot_branch]=next" https://gitlab.lrde.epita.fr/api/v4/projects/131/trigger/pipeline
    - curl -X POST -F ref=master -F token=$TRIGGER_SANDBOX https://gitlab.lrde.epita.fr/api/v4/projects/181/trigger/pipeline

raspbian:
  stage: build
  only:
    - branches
  except:
    - /wip/
  tags:
    - armv7
  script:
    - autoreconf -vfi
    - ./configure
    - make
    - make distcheck || { chmod -R u+w ./spot-*; false; }
  artifacts:
    when: always
    paths:
      - ./spot-*/_build/sub/tests/*/*.log
      - ./*.log
      - ./*.tar.gz
