// This file relates to internal XMOS infrastructure and should be ignored by users

@Library('xmos_jenkins_shared_library@v0.32.0')

def buildDocs(String zipFileName) {
  withVenv {
    sh 'pip install git+ssh://git@github.com/xmos/xmosdoc@v5.5.2'
    sh 'xmosdoc'
    zip zipFile: zipFileName, archive: true, dir: 'doc/_build'
  }
}

getApproval()
pipeline {
  agent none

  environment {
    AWE_CORE_VERSION = "DSPCAWE_8.D.8"
    DOC_ZIP_FILE_NAME = "an02016_docs.zip"
    TOOLS_VERSION = "15.3.0"
    XTAGCTL_VERSION = "v2.0.0"
  }

  options {
    skipDefaultCheckout()
    timestamps()
    buildDiscarder(xmosDiscardBuildSettings(onlyArtifacts = false))
  }

  stages {
    stage('Build apps and docs') {
      agent {
        label 'documentation'
      }

      stages {
        stage('Checkout') {
          steps {
            println "Stage running on ${env.NODE_NAME}"
            dir("an02016") {
              checkout scm
            }
          }
        }  // Checkout

        stage('Build') {
          steps {
            withTools(env.TOOLS_VERSION) {
              dir("an02016") {
                // This will fail as the static library archive is not present yet
                catchError(buildResult: 'SUCCESS', stageResult: 'SUCCESS') {
                  sh "cmake -G 'Unix Makefiles' -B build"
                }
              }

              dir("lib_awe") {
                withCredentials([file(credentialsId: "${AWE_CORE_VERSION}", variable: 'DSPC_AWE_LIB')]) {
                  sh "cp ${DSPC_AWE_LIB} lib_awe/lib/xs3a/libAWECore.a" // Bring AWE library in
                }
              }

              dir("an02016") {
                // Re-run cmake configuration now that the static library archive is in place
                sh "cmake build"
                sh "xmake -C build -j 8"

                stash includes: "app_an02016/bin/**/*.xe", name: "an02016_apps"

                // Build the XEs for the DFU test
                sh "cmake build -D EXTRA_BUILD_FLAGS=\"-DBCD_DEVICE=0x9998\""
                sh "xmake -C build -j 8 app_an02016_UA"
                dir("app_an02016/bin/upgrade0") {
                  sh "mv ../UA/app_an02016_UA.xe app_an02016_upgrade0.xe"
                }

                sh "cmake build -D EXTRA_BUILD_FLAGS=\"-DBCD_DEVICE=0x9999\""
                sh "xmake -C build -j 8 app_an02016_UA"
                dir("app_an02016/bin/upgrade1") {
                  sh "mv ../UA/app_an02016_UA.xe app_an02016_upgrade1.xe"
                }

                stash includes: "app_an02016/bin/upgrade*/*.xe", name: "dfu_test_apps"
              }
            }
          }
        }  // Build

        stage('Documentation') {
          steps {
            withTools(env.TOOLS_VERSION) {
              dir("an02016") {
                createVenv()
                withVenv {
                  buildDocs(DOC_ZIP_FILE_NAME)
                }
              }
            }
          }
        }  // Documentation
      }

      post {
        cleanup {
          xcoreCleanSandbox()
        }
      }
    }  // Build apps and docs

    stage('Hardware test') {
      agent {
        label 'usb_audio && macos && arm64 && xcore.ai-mcab'
      }

      stages {
        stage('Checkout') {
          steps {
            println "Stage running on ${env.NODE_NAME}"
            dir("an02016") {
              checkout scm
              unstash "an02016_apps"
              unstash "dfu_test_apps"
              sh "git submodule update --init"
            }
          }
        }  // Checkout

        stage('Test') {
          steps {
            sh "git clone -b ${env.XTAGCTL_VERSION} git@github0.xmos.com:xmos-int/xtagctl"

            dir("an02016") {
              createVenv()
              dir("tests") {
                withTools(env.TOOLS_VERSION) {
                  withVenv {
                    sh "pip install -r requirements.txt"
                    sh "pip install -e ${WORKSPACE}/xtagctl"

                    withXTAG(["usb_audio_mc_xcai_dut"]) { xtag_id ->
                      sh "pytest --xtag-id=${xtag_id[0]} --junitxml=pytest_result.xml"
                    }
                  }
                }
              }
            }
          }
        }
      }

      post {
        always {
          junit "an02016/tests/pytest_result.xml"
        }
        cleanup {
          xcoreCleanSandbox()
        }
      }
    }  // Hardware test
  }
}
