version: 2.1

setup: true
orbs:
  continuation: circleci/continuation@1

jobs:
  set-matrix:
    executor: continuation/default
    docker:
      - image: cimg/base:current
    resource_class: large
    steps:
      - checkout
      - run:
          name: Set matrix
          command: |
            MATRIX_JSON=$(python .github/workflows/ci_set_matrix.py)
            echo "MATRIX_JSON=$MATRIX_JSON"

            BUILDSYSTEM_LIST=(
              "cmake"
              "make"
            )

            TOOLCHAIN_LIST=(
              "aarch64-gcc"
              "arm-clang"
              "arm-gcc"
              "esp-idf"
              "msp430-gcc"
              "riscv-gcc"
            )

            # only build IAR if not forked PR, since IAR token is not shared
            if [ -z $CIRCLE_PR_USERNAME ]; then
              TOOLCHAIN_LIST+=("arm-iar")
            fi

            gen_build_entry() {
              local build_system="$1"
              local toolchain="$2"
              local family="$3"
              local build_args=""

              if [[ "$toolchain" == "arm-iar" || "$build_system" == "make" ]]; then
                build_args="--one-random"
              fi

              if [[ "$toolchain" == "esp-idf" ]]; then
                echo "      - build-vm:" >> .circleci/config2.yml
              else
                echo "      - build:" >> .circleci/config2.yml
              fi
              echo "          matrix:" >> .circleci/config2.yml
              echo "            alias: build-${build_system}-${toolchain}" >> .circleci/config2.yml
              echo "            parameters:" >> .circleci/config2.yml
              echo "              build-system: ['$build_system']" >> .circleci/config2.yml
              echo "              toolchain: ['$toolchain']" >> .circleci/config2.yml
              echo "              family: $family" >> .circleci/config2.yml
              echo "              resource_class: ['large']" >> .circleci/config2.yml
              echo "              build-args: ['$build_args']" >> .circleci/config2.yml
            }

            # Collect all build aliases for code-metrics requires (cmake only, exclude esp-idf)
            BUILD_ALIASES=()

            for build_system in "${BUILDSYSTEM_LIST[@]}"; do
              for toolchain in "${TOOLCHAIN_LIST[@]}"; do
                # make does not support these toolchains
                if [ "$build_system" == "make" ] && { [ "$toolchain" == "arm-clang" ] || [ "$toolchain" == "arm-iar" ] || [ "$toolchain" == "esp-idf" ]; }; then
                  continue
                fi

                FAMILY=$(echo $MATRIX_JSON | jq -r ".\"$toolchain\"")
                echo "FAMILY_${toolchain}=$FAMILY"
                gen_build_entry "$build_system" "$toolchain" "$FAMILY"

                # Only add cmake builds: excluding esp-idf or build_args="--one-random" to metrics requirements
                if [ "$build_system" == "cmake" ] && [ "$toolchain" != "esp-idf" ] && [ "$toolchain" != "arm-iar" ]; then
                  BUILD_ALIASES+=("build-${build_system}-${toolchain}")
                fi
              done
            done

            # Add code-metrics job that requires all build jobs
            echo "      - code-metrics:" >> .circleci/config2.yml
            echo "          requires:" >> .circleci/config2.yml
            for alias in "${BUILD_ALIASES[@]}"; do
              echo "            - $alias" >> .circleci/config2.yml
            done

      - continuation/continue:
          configuration_path: .circleci/config2.yml

workflows:
  set-matrix:
    jobs:
      - set-matrix
