version: 2

templates:
  golang-template:
    &golang-template
    docker:
      - image: uroottest/test-image-amd64:v5.2.0
    working_directory: /home/circleci/go/src/github.com/u-root/u-root
    environment:
      - UROOT_SOURCE: /home/circleci/go/src/github.com/u-root/u-root
      - CGO_ENABLED: 0
      # x7 all timeouts for QEMU VM tests since they run without KVM.
      - UROOT_QEMU_TIMEOUT_X: 7

  beefy-template:
    &beefy-template
    <<: *golang-template
    resource_class: large

workflows:
  version: 2
  build_and_test:
    jobs:
      - test-tinygo
      - test-build-tamago-riscv64
      - test-build-tamago-arm
      - race
      - compile_cmds
      - check_templates
      - check_licenses

jobs:
  test-tinygo:
    <<: *beefy-template
    steps:
      - checkout
      - run:
          name: tinygo build and test commands
          command: echo sooner but not yet docker run --rm -v ~/go:/home/tinygo/go  tinygo/tinygo:0.26.0 bash -c 'cd  go/src/github.com/u-root/u-root/cmds/core/cmp && tinygo test .'
          no_output_timeout: 15m

  test-build-tamago-arm:
    docker:
      - image: uroottest/test-image-tamago:v4.5.0
    working_directory: /home/circleci/go/src/github.com/u-root/u-root
    environment:
      - UROOT_SOURCE: /home/circleci/go/src/github.com/u-root/u-root
      - CGO_ENABLED: 0
    steps:
      - checkout
      - run:
          name: Test u-root tamago build
          command: GOOS=tamago GOARCH=arm GOARM=7 go build ./pkg/dt/...
      - run:
          name: build armory-boot elf target
          command: |
            cd /home/circleci
            git clone https://github.com/usbarmory/armory-boot.git
            cd armory-boot
            go version
            BOOT=eMMC make elf

  test-build-tamago-riscv64:
    docker:
      - image: uroottest/test-image-tamago:v4.5.0
    working_directory: /home/circleci/go/src/github.com/u-root/u-root
    environment:
      - UROOT_SOURCE: /home/circleci/go/src/github.com/u-root/u-root
      - CGO_ENABLED: 0
    steps:
      - checkout
      - run:
          name: Test u-root tamago build
          command: GOOS=tamago GOARCH=riscv64 go build ./pkg/dt/...

  race:
    <<: *beefy-template
    environment:
      - UROOT_SOURCE: /home/circleci/go/src/github.com/u-root/u-root
      - CGO_ENABLED: 1
    steps:
      - checkout
      - run:
          name: Race detector
          command: go test -race -timeout=15m -p=1 -failfast ./cmds/... ./pkg/...

  compile_cmds:
    <<: *golang-template
    steps:
      - checkout
      - run:
          name: build all tools
          command: |
            cd cmds
            go install -a ./...
            cd ../tools
            go install -a ./...

  check_licenses:
    <<: *golang-template
    steps:
      - checkout
      - run:
          name: Check licenses
          command: go run tools/checklicenses/checklicenses.go -c
            tools/checklicenses/config.json

  check_templates:
    <<: *golang-template
    steps:
      - checkout
      - run:
          name: Ensure that every template builds for a variety of options.
          command: |
            go build .
            goruncmd="./u-root -stats-output-path=/tmp/stats.json"
            $goruncmd minimal
            $goruncmd core
            $goruncmd coreboot-app
            $goruncmd all
            $goruncmd world
            # Fails because of dups. Is this supposed to work?
            #$goruncmd all core
            #$goruncmd all core
            GOOS=plan9 $goruncmd -defaultsh=/bbin/rush plan9
            cat /tmp/stats.json
      - store_artifacts:
          name: Store build stats
          path: /tmp/stats.json
          destination: stats.json
