stages:
  - build
  - test
  - report

default:
  image: registry.gitlab.com/kicad/kicad-ci/source_containers/master/fedora:31

##########################################################################
# This will allow the job to run whenever code events trigger it.
##########################################################################
.only_code:
  only:
    - master
    - branches
    - merge_requests
    - pushes


##########################################################################
# Define a template for all the unit tests, since each one is run in a separate
# job to make the display nicer.
##########################################################################
.unit_test:
  stage: test
  when: on_success
  needs:
    - job: build_linux
      artifacts: true
  # We define this using a before script because GitLab cannot merge variable
  # definitions across inheritance (so the variables block from this would disappear)
  before_script:
    - export BOOST_TEST_LOGGER='JUNIT,all,test_results.${TEST}.xml:HRF,warning'
    - export CTEST_OUTPUT_ON_FAILURE=1
  script:
    - cd build/linux/qa
    - ctest -R ${TEST}
  # GitLab supports displaying the results in the GUI through JUNIT artifacts
  # (https://docs.gitlab.com/ee/ci/junit_test_reports.html)
  # so we upload the JUNIT results. Note that there is a bug with how paths
  # are processed in the junit report (https://gitlab.com/gitlab-org/gitlab/issues/23835)
  # so we can't use a glob and have to list out each hierarchy separately.
  artifacts:
    reports:
      junit:
        - build/linux/qa/*/*.xml
        - build/linux/qa/*/*/*.xml


##########################################################################
# Test the formatting in a merge request using clang-format
##########################################################################
# The variable CI_COMMIT_BEFORE_SHA is not available in normal merge requests
# so we must build the commit hash ourselves, see:
# https://gitlab.com/gitlab-org/gitlab/issues/12850
test_formatting:
  stage: test
  only:
    - merge_requests
  allow_failure: true
  before_script:
    # We must manually add the KiCad remote to ensure it is named sensibly
    - git remote add product https://gitlab.com/kicad/code/kicad.git
    - git fetch -n product
    - git fetch -n origin
    # Get the SHAs of the commits
    - "TARGET_HEAD_SHA=$(git rev-parse product/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME})"
    - "SOURCE_HEAD_SHA=$(git rev-parse origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})"
    - "MERGE_BASE_SHA=$(git merge-base ${TARGET_HEAD_SHA} ${SOURCE_HEAD_SHA})"
  script:
    - echo "Testing formatting from commit ${MERGE_BASE_SHA}"
    - ./tools/check_coding.sh --diff --ci --commit ${MERGE_BASE_SHA}



##########################################################################
# Build KiCad and save the results
##########################################################################
build_linux:
  stage: build
  extends: .only_code
  interruptible: false
  cache:
    key: "cache-linux"
    paths:
      - ccache/
  before_script:
    # CCache Config
    - mkdir -p ccache
    - export CCACHE_BASEDIR=${PWD}
    - export CCACHE_DIR=${PWD}/ccache
  script:
    - mkdir -p build/linux
    - cd build/linux
    - cmake
      -DCMAKE_C_COMPILER_LAUNCHER=ccache
      -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
      -DCMAKE_BUILD_TYPE=Debug
      -DKICAD_STDLIB_LIGHT_DEBUG=ON
      -DKICAD_SCRIPTING=ON
      -DKICAD_SCRIPTING_MODULES=ON
      -DKICAD_SCRIPTING_PYTHON3=ON
      -DKICAD_SCRIPTING_WXPYTHON=ON
      -DKICAD_SCRIPTING_WXPYTHON_PHOENIX=ON
      -DKICAD_SCRIPTING_ACTION_MENU=ON
      -DBUILD_GITHUB_PLUGIN=ON
      -DKICAD_USE_OCE=OFF
      -DKICAD_USE_OCC=ON
      -DKICAD_SPICE=ON
      ../../
    - make 2>&1 | tee compilation_log.txt
    - cd ../../
  artifacts:
    # Only save the artifacts that are needed for running the tests in the next stage
    # and the compilation log. The entire build directory is too large to save as an
    # artifact.
    expire_in: 2 hrs
    when: always
    paths:
      - build/linux/3d-viewer/
      - build/linux/pcbnew/pcbnew.py
      - build/linux/pcbnew/_pcbnew.so
      - build/linux/qa/
      - build/linux/compilation_log.txt

# Upload the compilation log in an easily downloadable form
report_build_warn:
  stage: report
  extends: .only_code
  when: always
  needs:
    - job: build_linux
      artifacts: true
  script:
    - echo "Uploading compilation log"
    - cp build/linux/compilation_log.txt compilation_log.txt
  artifacts:
    expire_in: 1 year
    expose_as: 'Build log'
    name: "build_log.txt"
    paths:
      - compilation_log.txt

# Report on the metrics of the code
report_metrics:
  stage: report
  extends: .only_code
  when: always
  needs:
    - job: build_linux
      artifacts: true
  script:
    - cat build/linux/compilation_log.txt | grep "warning:" | awk '{total+=1}END{print "number_of_warnings "total}' > metrics.txt
    - cat metrics.txt
  artifacts:
    reports:
      metrics: metrics.txt


##########################################################################
# Run the code unit tests. Each QA executable is split into a separate job so that the display
# gives the test results broken down by the test executable.
##########################################################################
qa_python:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'qa_python'

qa_common_eeschema:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'common_eeschema'

qa_common_pcbnew:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'common_pcbnew'

qa_common_gerbview:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'qa_common_gerbview'

qa_pcbnew:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'pcbnew'

qa_eeschema:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'eeschema'

qa_kimath:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'kimath'

qa_sexpr:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'sexpr'

qa_kicad2step:
  extends:
    - .unit_test
    - .only_code
  variables:
    TEST: 'kicad2step'