# SPDX-License-Identifier: GPL-2.0+
# Copyright Roger Meier <r.meier@siemens.com>

# build U-Boot on Travis CI - https://travis-ci.org/

sudo: required
dist: bionic

language: c

addons:
  apt:
    sources:
    - ubuntu-toolchain-r-test
    - llvm-toolchain-bionic-7
    packages:
    - cppcheck
    - sloccount
    - sparse
    - bc
    - build-essential
    - libsdl1.2-dev
    - python
    - python-pyelftools
    - python3-virtualenv
    - python3-pip
    - swig
    - libpython-dev
    - iasl
    - grub-efi-ia32-bin
    - grub-efi-amd64-bin
    - rpm2cpio
    - wget
    - device-tree-compiler
    - lzop
    - liblz4-tool
    - lzma-alone
    - libisl15
    - clang-7
    - srecord

install:
 # Clone uboot-test-hooks
 - git clone --depth=1 git://github.com/swarren/uboot-test-hooks.git /tmp/uboot-test-hooks
 - ln -s travis-ci /tmp/uboot-test-hooks/bin/`hostname`
 - ln -s travis-ci /tmp/uboot-test-hooks/py/`hostname`
 # prepare buildman environment
 - echo -e "[toolchain]\nroot = /usr" > ~/.buildman
 - echo -e "arc = /tmp/arc_gnu_2018.09_prebuilt_uclibc_le_archs_linux_install" >> ~/.buildman
 - echo -e "\n[toolchain-alias]\nsh = sh2" >> ~/.buildman
 - echo -e "x86 = i386" >> ~/.buildman;
 - echo -e "riscv = riscv64" >> ~/.buildman;
 - cat ~/.buildman
 - grub-mkimage --prefix="" -o ~/grub_x86.efi -O i386-efi normal  echo lsefimmap lsefi lsefisystab efinet tftp minicmd
 - grub-mkimage --prefix="" -o ~/grub_x64.efi -O x86_64-efi normal  echo lsefimmap lsefi lsefisystab efinet tftp minicmd
 - mkdir ~/grub2-arm
 - ( cd ~/grub2-arm; wget -O - http://download.opensuse.org/ports/armv7hl/distribution/leap/42.2/repo/oss/suse/armv7hl/grub2-arm-efi-2.02~beta2-87.1.armv7hl.rpm | rpm2cpio | cpio -di )
 - mkdir ~/grub2-arm64
 - ( cd ~/grub2-arm64; wget -O - http://download.opensuse.org/ports/aarch64/distribution/leap/42.2/repo/oss/suse/aarch64/grub2-arm64-efi-2.02~beta2-87.1.aarch64.rpm | rpm2cpio | cpio -di )
 - wget http://mirrors.kernel.org/ubuntu/pool/main/m/mpfr4/libmpfr4_3.1.4-1_amd64.deb && sudo dpkg -i libmpfr4_3.1.4-1_amd64.deb && rm libmpfr4_3.1.4-1_amd64.deb

env:
  global:
    - PATH=/tmp/qemu-install/bin:/tmp/uboot-test-hooks/bin:/usr/bin:/bin:/usr/local/bin
    - PYTHONPATH=/tmp/uboot-test-hooks/py/travis-ci
    - BUILD_DIR=build
    - HOSTCC="cc"
    - HOSTCXX="c++"
    - QEMU_VERSION="v4.2.0"

before_script:
  # install toolchains based on TOOLCHAIN} variable
  - if [[ "${TOOLCHAIN}" == *m68k* ]]; then ./tools/buildman/buildman --fetch-arch m68k ; fi
  - if [[ "${TOOLCHAIN}" == *microblaze* ]]; then ./tools/buildman/buildman --fetch-arch microblaze ; fi
  - if [[ "${TOOLCHAIN}" == *mips* ]]; then ./tools/buildman/buildman --fetch-arch mips ; fi
  - if [[ "${TOOLCHAIN}" == *sh* ]]; then ./tools/buildman/buildman --fetch-arch sh2 ; fi
  - if [[ "${TOOLCHAIN}" == *i386* ]]; then
      ./tools/buildman/buildman --fetch-arch i386;
    fi
  - if [[ "${TOOLCHAIN}" == arc ]]; then
       wget https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/releases/download/arc-2018.09-release/arc_gnu_2018.09_prebuilt_uclibc_le_archs_linux_install.tar.gz &&
       tar -C /tmp -xf arc_gnu_2018.09_prebuilt_uclibc_le_archs_linux_install.tar.gz;
    fi
  - if [[ "${TOOLCHAIN}" == "nds32" ]]; then
       wget https://github.com/vincentzwc/prebuilt-nds32-toolchain/releases/download/20180521/nds32le-linux-glibc-v3-upstream.tar.gz &&
       tar -C /tmp -xf nds32le-linux-glibc-v3-upstream.tar.gz &&
       echo -e "\n[toolchain-prefix]\nnds32 = /tmp/nds32le-linux-glibc-v3-upstream/bin/nds32le-linux-" >> ~/.buildman;
    fi
  - if [[ "${TOOLCHAIN}" == *xtensa* ]]; then
       wget https://github.com/foss-xtensa/toolchain/releases/download/2018.02/x86_64-2018.02-${TOOLCHAIN}.tar.gz &&
       tar -C /tmp -xf x86_64-2018.02-${TOOLCHAIN}.tar.gz &&
       echo -e "\n[toolchain-prefix]\nxtensa = /tmp/2018.02/${TOOLCHAIN}/bin/${TOOLCHAIN}-" >> ~/.buildman;
    fi
  # If TOOLCHAIN is unset, we're on some flavour of ARM.
  - if [[ "${TOOLCHAIN}" == "" ]]; then
       ./tools/buildman/buildman --fetch-arch arm &&
       ./tools/buildman/buildman --fetch-arch aarch64;
    fi
  - if [[ "${TOOLCHAIN}" == "powerpc" ]]; then ./tools/buildman/buildman --fetch-arch powerpc; fi
  - if [[ "${TOOLCHAIN}" == "riscv" ]]; then
       ./tools/buildman/buildman --fetch-arch riscv64;
    fi
  - if [[ "${QEMU_TARGET}" != "" ]]; then
       git clone git://git.qemu.org/qemu.git /tmp/qemu;
       pushd /tmp/qemu;
       git submodule update --init dtc &&
       git checkout ${QEMU_VERSION} &&
       ./configure --prefix=/tmp/qemu-install --target-list=${QEMU_TARGET} &&
       make -j4 all install;
       popd;
    fi

script:
 # Comments must be outside the command strings below, or the Travis parser
 # will get confused.
 #
 # From buildman, exit code 129 means warnings only.  If we've been asked to
 # use clang only do one configuration.
 - if [[ "${BUILDMAN}" != "" ]]; then
     ret=0;
     tools/buildman/buildman -P -E ${BUILDMAN} ${OVERRIDE}|| ret=$?;
     if [[ $ret -ne 0 && $ret -ne 129 ]]; then
       tools/buildman/buildman -sdeP ${BUILDMAN};
       exit $ret;
     fi;
   fi
 # "not a_test_which_does_not_exist" is a dummy -k parameter which will
 # never prevent any test from running. That way, we can always pass
 # "-k something" even when $TEST_PY_TEST_SPEC doesnt need a custom
 # value.
 - export UBOOT_TRAVIS_BUILD_DIR=`cd .. && pwd`/.bm-work/${TEST_PY_BD};
   cp ~/grub_x86.efi $UBOOT_TRAVIS_BUILD_DIR/;
   cp ~/grub_x64.efi $UBOOT_TRAVIS_BUILD_DIR/;
   cp ~/grub2-arm/usr/lib/grub2/arm-efi/grub.efi $UBOOT_TRAVIS_BUILD_DIR/grub_arm.efi;
   cp ~/grub2-arm64/usr/lib/grub2/arm64-efi/grub.efi $UBOOT_TRAVIS_BUILD_DIR/grub_arm64.efi;
   if [[ -n "${TEST_PY_TOOLS}" ]]; then
     PYTHONPATH="${UBOOT_TRAVIS_BUILD_DIR}/scripts/dtc/pylibfdt"
     PATH="${UBOOT_TRAVIS_BUILD_DIR}/scripts/dtc:${PATH}"
     ./tools/binman/binman --toolpath ${UBOOT_TRAVIS_BUILD_DIR}/tools test &&
     ./tools/patman/patman --test &&
     ./tools/buildman/buildman -t &&
     PYTHONPATH="${UBOOT_TRAVIS_BUILD_DIR}/scripts/dtc/pylibfdt"
     PATH="${UBOOT_TRAVIS_BUILD_DIR}/scripts/dtc:${PATH}"
     ./tools/dtoc/dtoc -t;
   fi;
   if [[ "${TEST_PY_BD}" != "" ]]; then
     virtualenv -p /usr/bin/python3 /tmp/venv;
     . /tmp/venv/bin/activate;
     pip install -r test/py/requirements.txt;
     ./test/py/test.py --bd ${TEST_PY_BD} ${TEST_PY_ID}
       -k "${TEST_PY_TEST_SPEC:-not a_test_which_does_not_exist}"
       --build-dir "$UBOOT_TRAVIS_BUILD_DIR";
     ret=$?;
     if [[ $ret -ne 0 ]]; then
       exit $ret;
     fi;
   fi

matrix:
  include:
  # we need to build by vendor due to 50min time limit for builds
  # each env setting here is a dedicated build
    - name: "buildman arc"
      env:
        - BUILDMAN="arc"
          TOOLCHAIN="arc"
    - name: "buildman arm11 arm7 arm920t arm946es"
      env:
        - BUILDMAN="arm11 arm7 arm920t arm946es"
    - name: "buildman arm926ejs (non-NXP,siemens,at91,kirkwood,spear)"
      env:
        - JOB="arm926ejs"
          BUILDMAN="arm926ejs -x freescale,siemens,at91,kirkwood,spear,omap"
    - name: "buildman at91 (non arm v7)"
      env:
        - BUILDMAN="at91 -x armv7"
    - name: "buildman at91 (non arm926ejs)"
      env:
        - BUILDMAN="at91 -x arm926ejs"
    - name: "buildman boundary engicam toradex"
      env:
        - BUILDMAN="boundary engicam toradex"
    - name: "buildman ARM bcm"
      env:
        - BUILDMAN="bcm -x mips"
    - name: "buildman NXP ARM32 (catch-all)"
      env:
        - BUILDMAN="freescale -x powerpc,m68k,aarch64,ls101,ls102,ls104,ls108,ls20,lx216"
    - name: "buildman NXP LS101x"
      env:
        - BUILDMAN="freescale&ls101"
    - name: "buildman NXP LS102x"
      env:
        - BUILDMAN="freescale&ls102"
    - name: "buildman NXP LS104x"
      env:
        - BUILDMAN="freescale&ls104"
    - name: "buildman NXP LS108x"
      env:
        - BUILDMAN="freescale&ls108"
    - name: "buildman NXP LS20xx"
      env:
        - BUILDMAN="freescale&ls20"
    - name: "buildman NXP LX216x"
      env:
        - BUILDMAN="freescale&lx216"
    - name: "buildman i.MX6 tqc"
      env:
        - BUILDMAN="mx6&tqc"
    - name: "buildman i.MX6 (catch-all)"
      env:
        - BUILDMAN="mx6 -x boundary,engicam,freescale,technexion,toradex,tqc"
    - name: "buildman i.MX (non-i.MX6 catch-all)"
      env:
        - BUILDMAN="mx -x freescale,mx6,toradex,technexion"
    - name: "buildman keystone 2/3"
      env:
        - BUILDMAN="k2 k3"
    - name: "buildman samsung socfpga"
      env:
        - BUILDMAN="samsung socfpga"
    - name: "buildman spear"
      env:
        - BUILDMAN="spear"
    - name: "buildman sun4i"
      env:
        - BUILDMAN="sun4i"
    - name: "buildman sun5i"
      env:
        - BUILDMAN="sun5i"
    - name: "buildman sun6i"
      env:
        - BUILDMAN="sun6i"
    - name: "buildman sun7i"
      env:
        - BUILDMAN="sun7i"
    - name: "buildman 64bit sun8i"
      env:
        - BUILDMAN="sun8i&aarch64 -x orangepi"
    - name: "buildman 32bit sun8i"
      env:
        - BUILDMAN="sun8i&armv7 -x orangepi"
    - name: "buildman sun9i"
      env:
        - BUILDMAN="sun9i"
    - name: "buildman sun50i"
      env:
        - BUILDMAN="sun50i -x orangepi"
    - name: "buildman catch-all ARM"
      env:
        - BUILDMAN="arm -x arm11,arm7,arm9,aarch64,at91,bcm,freescale,kirkwood,mvebu,siemens,tegra,uniphier,mx,samsung,sunxi,am33xx,omap,rockchip,toradex,socfpga,k2,k3,zynq"
    - name: "buildman sandbox x86"
      env:
        - BUILDMAN="sandbox x86"
          TOOLCHAIN="i386"
    - name: "buildman technexion"
      env:
        - BUILDMAN="technexion"
    - name: "buildman kirkwood"
      env:
        - BUILDMAN="kirkwood"
    - name: "buildman mvebu"
      env:
        - BUILDMAN="mvebu"
    - name: "buildman m68k"
      env:
        - BUILDMAN="m68k"
          TOOLCHAIN="m68k"
    - name: "buildman microblaze"
      env:
        - BUILDMAN="microblaze"
          TOOLCHAIN="microblaze"
    - name: "buildman mips"
      env:
        - BUILDMAN="mips"
          TOOLCHAIN="mips"
    - name: "buildman non-Freescale PowerPC"
      env:
        - BUILDMAN="powerpc -x freescale"
          TOOLCHAIN="powerpc"
    - name: "buildman mpc85xx&freescale (excluding many)"
      env:
        - BUILDMAN="mpc85xx&freescale -x t208xrdb -x t4qds -x t102* -x p1_p2_rdb_pc -x p1010rdb -x corenet_ds -x b4860qds -x bsc91*"
          TOOLCHAIN="powerpc"
    - name: "buildman t208xrdb corenet_ds"
      env:
        - BUILDMAN="t208xrdb corenet_ds"
          TOOLCHAIN="powerpc"
    - name: "buildman Freescale PowerPC"
      env:
        - BUILDMAN="t4qds b4860qds mpc83xx&freescale mpc86xx&freescale"
          TOOLCHAIN="powerpc"
    - name: "buildman t102*"
      env:
        - BUILDMAN="t102*"
          TOOLCHAIN="powerpc"
    - name: "buildman p1_p2_rdb_pc"
      env:
        - BUILDMAN="p1_p2_rdb_pc"
          TOOLCHAIN="powerpc"
    - name: "buildman p1010rdb bsc91"
      env:
        - BUILDMAN="p1010rdb bsc91"
          TOOLCHAIN="powerpc"
    - name: "buildman siemens"
      env:
        - BUILDMAN="siemens"
    - name: "buildman tegra"
      env:
        - BUILDMAN="tegra -x toradex"
    - name: "buildman am33xx (no siemens)"
      env:
        - BUILDMAN="am33xx -x siemens"
    - name: "buildman omap"
      env:
        - BUILDMAN="omap"
    - name: "buildman orangepi"
      env:
        - BUILDMAN="orangepi"
    - name: "buildman uniphier"
      env:
        - BUILDMAN="uniphier"
    - name: "buildman catch-all AArch64"
      env:
        - BUILDMAN="aarch64 -x bcm,k3,tegra,ls1,ls2,mvebu,uniphier,sunxi,samsung,rockchip,versal,zynq"
    - name: "buildman rockchip"
      env:
        - BUILDMAN="rockchip -x orangepi"
    - name: "buildman sh"
      env:
        - BUILDMAN="sh -x arm"
          TOOLCHAIN="sh"
    - name: "buildman Zynq* (ARMv7)"
      env:
        - BUILDMAN="zynq&armv7"
    - name: "buildman ZynqMP and Versal"
      env:
        - BUILDMAN="versal|zynqmp&aarch64"
    - name: "buildman xtensa"
      env:
        - BUILDMAN="xtensa"
          TOOLCHAIN="xtensa-dc233c-elf"
    - name: "buildman riscv"
      env:
        - BUILDMAN="riscv"
          TOOLCHAIN="riscv"
    - name: "buildman nds32"
      env:
        - BUILDMAN="nds32"
          TOOLCHAIN="nds32"

    # QA jobs for code analytics
    # static code analysis with cppcheck (we can add --enable=all later)
    - name: "cppcheck"
      script:
        - cppcheck --force --quiet --inline-suppr .
    # search for TODO within source tree
    - name: "grep TODO"
      script:
        - grep -r TODO .
    # search for FIXME within source tree
    - name: "grep FIXME HACK"
      script:
        - grep -r FIXME .
    # search for HACK within source tree and ignore HACKKIT board
      script:
        - grep -r HACK . | grep -v HACKKIT
    # some statistics about the code base
    - name: "sloccount"
      script:
        - sloccount .
    # ensure all configs have MAINTAINERS entries
    - name: "Check for configs without MAINTAINERS entry"
      script:
        - if [ `./tools/genboardscfg.py -f 2>&1 | wc -l` -ne 0 ]; then exit 1; fi
    # Ensure host tools build
    - name: "Build tools-only"
      script:
        - make tools-only_config tools-only -j$(nproc)
    # Ensure env tools build
    - name: "Build envtools"
      script:
        - make tools-only_config envtools -j$(nproc)

    # test/py
    - name: "test/py sandbox"
      env:
        - TEST_PY_BD="sandbox"
          BUILDMAN="^sandbox$"
          TOOLCHAIN="i386"
    - name: "test/py sandbox with clang"
      env:
        - TEST_PY_BD="sandbox"
          BUILDMAN="^sandbox$"
          OVERRIDE="-O clang-7"
    - name: "test/py sandbox_spl"
      env:
        - TEST_PY_BD="sandbox_spl"
          TEST_PY_TEST_SPEC="test_ofplatdata"
          BUILDMAN="^sandbox$"
          TOOLCHAIN="i386"
          TEST_PY_TOOLS="yes"
    - name: "test/py sandbox_flattree"
      env:
        - TEST_PY_BD="sandbox_flattree"
          BUILDMAN="^sandbox_flattree$"
          TOOLCHAIN="i386"
    - name: "test/py evb-ast2500"
      env:
        - TEST_PY_BD="evb-ast2500"
          TEST_PY_ID="--id qemu"
          QEMU_TARGET="arm-softmmu"
          BUILDMAN="^evb-ast2500$"
    - name: "test/py vexpress_ca15_tc2"
      env:
        - TEST_PY_BD="vexpress_ca15_tc2"
          TEST_PY_ID="--id qemu"
          QEMU_TARGET="arm-softmmu"
          BUILDMAN="^vexpress_ca15_tc2$"
    - name: "test/py vexpress_ca9x4"
      env:
        - TEST_PY_BD="vexpress_ca9x4"
          TEST_PY_ID="--id qemu"
          QEMU_TARGET="arm-softmmu"
          BUILDMAN="^vexpress_ca9x4$"
    - name: "test/py integratorcp_cm926ejs"
      env:
        - TEST_PY_BD="integratorcp_cm926ejs"
          TEST_PY_TEST_SPEC="not sleep"
          TEST_PY_ID="--id qemu"
          QEMU_TARGET="arm-softmmu"
          BUILDMAN="^integratorcp_cm926ejs$"
    - name: "test/py qemu_arm"
      env:
        - TEST_PY_BD="qemu_arm"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="arm-softmmu"
          BUILDMAN="^qemu_arm$"
    - name: "test/py qemu_arm64"
      env:
        - TEST_PY_BD="qemu_arm64"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="aarch64-softmmu"
          BUILDMAN="^qemu_arm64$"
    - name: "test/py qemu_mips"
      env:
        - TEST_PY_BD="qemu_mips"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="mips-softmmu"
          BUILDMAN="^qemu_mips$"
          TOOLCHAIN="mips"
    - name: "test/py qemu_mipsel"
      env:
        - TEST_PY_BD="qemu_mipsel"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="mipsel-softmmu"
          BUILDMAN="^qemu_mipsel$"
          TOOLCHAIN="mips"
    - name: "test/py qemu_mips64"
      env:
        - TEST_PY_BD="qemu_mips64"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="mips64-softmmu"
          BUILDMAN="^qemu_mips64$"
          TOOLCHAIN="mips"
    - name: "test/py qemu_mips64el"
      env:
        - TEST_PY_BD="qemu_mips64el"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="mips64el-softmmu"
          BUILDMAN="^qemu_mips64el$"
          TOOLCHAIN="mips"
    - name: "test/py qemu-ppce500"
      env:
        - TEST_PY_BD="qemu-ppce500"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="ppc-softmmu"
          BUILDMAN="^qemu-ppce500$"
          TOOLCHAIN="powerpc"
    - name: "test/py qemu-riscv64"
      env:
        - TEST_PY_BD="qemu-riscv64"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="riscv64-softmmu"
          BUILDMAN="^qemu-riscv64$"
          TOOLCHAIN="riscv"
    - name: "test/py qemu-x86"
      env:
        - TEST_PY_BD="qemu-x86"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="i386-softmmu"
          BUILDMAN="^qemu-x86$"
          TOOLCHAIN="i386"
          BUILD_ROM="yes"
    - name: "test/py qemu-x86_64"
      env:
        - TEST_PY_BD="qemu-x86_64"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="x86_64-softmmu"
          BUILDMAN="^qemu-x86_64$"
          TOOLCHAIN="i386"
          BUILD_ROM="yes"
    - name: "test/py zynq_zc702"
      env:
        - TEST_PY_BD="zynq_zc702"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="arm-softmmu"
          TEST_PY_ID="--id qemu"
          BUILDMAN="^zynq_zc702$"
    - name: "test/py xilinx_versal_virt"
      env:
        - TEST_PY_BD="xilinx_versal_virt"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="aarch64-softmmu"
          TEST_PY_ID="--id qemu"
          BUILDMAN="^xilinx_versal_virt$"
    - name: "test/py xtfpga"
      env:
        - TEST_PY_BD="xtfpga"
          TEST_PY_TEST_SPEC="not sleep"
          QEMU_TARGET="xtensa-softmmu"
          TEST_PY_ID="--id qemu"
          BUILDMAN="^xtfpga$"
          TOOLCHAIN="xtensa-dc233c-elf"

# TODO make it perfect ;-r