name: osrm-backend CI
on:
  push:
    branches:
      - master
    tags:
      - v[1-9]+.[0-9]+.[0-9]+
      - v[1-9]+.[0-9]+.[0-9]+-[0-9a-zA-Z]+
      - v[1-9]+.[0-9]+-[0-9a-zA-Z]+
  pull_request:
    branches:
      - master

env:
  CCACHE_TEMPDIR: /tmp/.ccache-temp
  CCACHE_COMPRESS: 1
  CASHER_TIME_OUT: 599 # one second less than 10m to avoid 10m timeout error: https://github.com/Project-OSRM/osrm-backend/issues/2742
  CCACHE_VERSION: 3.3.1
  CMAKE_VERSION: 3.21.2
  ENABLE_NODE_BINDINGS: "ON"

jobs:
  windows:
    needs: format-taginfo-docs
    runs-on: windows-2022
    continue-on-error: false
    env:
      BUILD_TYPE: Release
      ENABLE_APPLE_SILICON: "OFF"
    steps:
    - uses: actions/checkout@v3
    - run: pip install conan==1.51.3
    - run: conan --version
    - run: cmake --version
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - run: node --version
    - run: npm --version
    - name: Prepare environment
      shell: bash
      run: |
        PACKAGE_JSON_VERSION=$(node -e "console.log(require('./package.json').version)")
        echo PUBLISH=$([[ "${GITHUB_REF:-}" == "refs/tags/v${PACKAGE_JSON_VERSION}" ]] && echo "On" || echo "Off") >> $GITHUB_ENV
    - run: npm install --ignore-scripts
    - run: npm link --ignore-scripts
    - uses: microsoft/setup-msbuild@v1.1
    - name: Build
      run: |
        .\scripts\ci\windows-build.bat
    - name: Run node tests
      shell: bash
      run: |
        ./lib/binding/osrm-datastore.exe test/data/ch/monaco.osrm
        node test/nodejs/index.js
    - name: Build Node package
      shell: bash
      run: ./scripts/ci/node_package.sh
    - name: Publish Node package
      if: ${{ env.PUBLISH == 'On' }}
      uses: ncipollo/release-action@v1
      with:
        allowUpdates: true
        artifactErrorsFailBuild: true
        artifacts: build/stage/**/*.tar.gz
        omitBody: true
        omitBodyDuringUpdate: true
        omitName: true
        omitNameDuringUpdate: true
        replacesArtifacts: true
        token: ${{ secrets.GITHUB_TOKEN }}

  format-taginfo-docs:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 12
    - name: Enable Node.js cache
      uses: actions/cache@v2
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-
    - name: Prepare environment
      run: |
        npm ci --ignore-scripts
        sudo apt-get update -y && sudo apt-get install clang-format-10
    - name: Run checks
      run: |
        ./scripts/check_taginfo.py taginfo.json profiles/car.lua
        ./scripts/format.sh && ./scripts/error_on_dirty.sh
        node ./scripts/validate_changelog.js
        npm run docs && ./scripts/error_on_dirty.sh

  docker-image:
    needs: format-taginfo-docs
    runs-on: ubuntu-22.04
    continue-on-error: false
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3
      - name: Enable osm.pbf cache
        uses: actions/cache@v2
        with:
          path: berlin-latest.osm.pbf
          key: v1-berlin-osm-pbf
          restore-keys: |
            v1-berlin-osm-pbf
      - name: Docker build
        run: |
          docker build -t osrm-backend-local -f docker/Dockerfile .
      - name: Test Docker image
        run: |
          if [ ! -f "${PWD}/berlin-latest.osm.pbf" ]; then
            wget http://download.geofabrik.de/europe/germany/berlin-latest.osm.pbf
          fi
          TAG=osrm-backend-local
          # when `--memory-swap` value equals `--memory` it means container won't use swap
          # see https://docs.docker.com/config/containers/resource_constraints/#--memory-swap-details
          MEMORY_ARGS="--memory=1g --memory-swap=1g"
          docker run $MEMORY_ARGS -t -v "${PWD}:/data" "${TAG}" osrm-extract --dump-nbg-graph -p /opt/car.lua /data/berlin-latest.osm.pbf
          docker run $MEMORY_ARGS -t -v "${PWD}:/data" "${TAG}" osrm-components /data/berlin-latest.osrm.nbg /data/berlin-latest.geojson
          if [ ! -s "${PWD}/berlin-latest.geojson" ]
          then
            >&2 echo "No berlin-latest.geojson found"
            exit 1
          fi

          # removing `.osrm.nbg` to check that whole pipeline works without it
          rm -rf "${PWD}/berlin-latest.osrm.nbg"

          docker run $MEMORY_ARGS -t -v "${PWD}:/data" "${TAG}" osrm-partition /data/berlin-latest.osrm
          docker run $MEMORY_ARGS -t -v "${PWD}:/data" "${TAG}" osrm-customize /data/berlin-latest.osrm
          docker run $MEMORY_ARGS --name=osrm-container -t -p 5000:5000 -v "${PWD}:/data" "${TAG}" osrm-routed --algorithm mld /data/berlin-latest.osrm &
          curl --retry-delay 3 --retry 10 --retry-all-errors "http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true"
          docker stop osrm-container

  build-test-publish:
    needs: format-taginfo-docs
    strategy:
      matrix:
        include:
          - name: gcc-9-debug-cov
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Debug
            CCOMPILER: gcc-9
            CUCUMBER_TIMEOUT: 20000
            CXXCOMPILER: g++-9
            ENABLE_COVERAGE: ON

          - name: gcc-9-debug-asan-ubsan
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Debug
            CCOMPILER: gcc-9
            CUCUMBER_TIMEOUT: 20000
            CXXCOMPILER: g++-9
            ENABLE_SANITIZER: ON
            TARGET_ARCH: x86_64-asan-ubsan
            OSRM_CONNECTION_RETRIES: 10
            OSRM_CONNECTION_EXP_BACKOFF_COEF: 1.5

          - name: clang-6.0-debug
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Debug
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            CUCUMBER_TIMEOUT: 60000

          - name: clang-11.0-debug-clang-tidy
            continue-on-error: false
            node: 12
            runs-on: ubuntu-22.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Debug
            CCOMPILER: clang-14
            CXXCOMPILER: clang++-14
            CUCUMBER_TIMEOUT: 60000
            ENABLE_CLANG_TIDY: ON

          - name: conan-linux-debug-asan-ubsan
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: clang-11
            CXXCOMPILER: clang++-11
            ENABLE_CONAN: ON
            ENABLE_SANITIZER: ON

          - name: conan-linux-release
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON

          - name: gcc-11-release
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: gcc-11
            CXXCOMPILER: g++-11
            ENABLE_BENCHMARKS: ON

          - name: gcc-10-release
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: gcc-10
            CXXCOMPILER: g++-10

          - name: gcc-9-release
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: gcc-9
            CXXCOMPILER: g++-9
            CXXFLAGS: -Wno-cast-function-type

          - name: gcc-9-conan-release-i686
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: gcc-9
            CFLAGS: "-m32 -msse2 -mfpmath=sse"
            CXXCOMPILER: g++-9
            CXXFLAGS: "-m32 -msse2 -mfpmath=sse"
            TARGET_ARCH: i686
            ENABLE_CONAN: ON
            
          - name: gcc-8-release
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: gcc-8
            CXXCOMPILER: g++-8
            CXXFLAGS: -Wno-cast-function-type

          - name: gcc-7-release
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: gcc-7
            CXXCOMPILER: g++-7

          - name: conan-macos-x64-release-node-12
            build_node_package: true
            continue-on-error: false
            node: 12
            runs-on: macos-11
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON

          - name: conan-macos-x64-release-node-14
            build_node_package: true
            continue-on-error: false
            node: 14
            runs-on: macos-11
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON

          - name: conan-macos-x64-release-node-16
            build_node_package: true
            continue-on-error: false
            node: 16
            runs-on: macos-11
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON

          - name: conan-macos-arm64-release-node-16
            build_node_package: true
            continue-on-error: false
            node: 16
            runs-on: macos-11
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON
            ENABLE_APPLE_SILICON: ON

          - name: gcc-7-release-shared
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TOOLS: ON
            BUILD_TYPE: Release
            BUILD_SHARED_LIBS: ON
            CCOMPILER: gcc-7
            CXXCOMPILER: g++-7

          - name: node-12-conan-linux-release
            build_node_package: true
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TYPE: Release
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: node-12-conan-linux-debug
            build_node_package: true
            continue-on-error: false
            node: 12
            runs-on: ubuntu-20.04
            BUILD_TYPE: Debug
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: node-14-conan-linux-release
            build_node_package: true
            continue-on-error: false
            node: 14
            runs-on: ubuntu-20.04
            BUILD_TYPE: Release
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: node-14-conan-linux-debug
            build_node_package: true
            continue-on-error: false
            node: 14
            runs-on: ubuntu-20.04
            BUILD_TYPE: Debug
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON


          - name: node-16-conan-linux-release
            build_node_package: true
            continue-on-error: false
            node: 16
            runs-on: ubuntu-20.04
            BUILD_TYPE: Release
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: node-16-conan-linux-debug
            build_node_package: true
            continue-on-error: false
            node: 16
            runs-on: ubuntu-20.04
            BUILD_TYPE: Debug
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: conan-macos-x64-release-node-latest
            build_node_package: true
            continue-on-error: true
            node: latest
            runs-on: macos-11
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON

          - name: conan-macos-arm64-release-node-latest
            build_node_package: true
            continue-on-error: true
            node: latest
            runs-on: macos-11
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON
            ENABLE_APPLE_SILICON: ON

          - name: node-latest-conan-linux-release
            build_node_package: true
            continue-on-error: true
            node: latest
            runs-on: ubuntu-20.04
            BUILD_TYPE: Release
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: node-latest-conan-linux-debug
            build_node_package: true
            continue-on-error: true
            node: latest
            runs-on: ubuntu-20.04
            BUILD_TYPE: Debug
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: conan-macos-x64-release-node-lts
            build_node_package: true
            continue-on-error: true
            node: "lts/*"
            runs-on: macos-11
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON
          
          - name: conan-macos-arm64-release-node-lts
            build_node_package: true
            continue-on-error: true
            node: "lts/*"
            runs-on: macos-11
            BUILD_TYPE: Release
            CCOMPILER: clang
            CXXCOMPILER: clang++
            CUCUMBER_TIMEOUT: 60000
            ENABLE_ASSERTIONS: ON
            ENABLE_CONAN: ON
            ENABLE_APPLE_SILICON: ON

          - name: node-lts-conan-linux-release
            build_node_package: true
            continue-on-error: true
            node: "lts/*"
            runs-on: ubuntu-20.04
            BUILD_TYPE: Release
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

          - name: node-lts-conan-linux-debug
            build_node_package: true
            continue-on-error: true
            node: "lts/*"
            runs-on: ubuntu-20.04
            BUILD_TYPE: Debug
            CCOMPILER: clang-6.0
            CXXCOMPILER: clang++-6.0
            ENABLE_CONAN: ON
            NODE_PACKAGE_TESTS_ONLY: ON

    name: ${{ matrix.name}}
    continue-on-error: ${{ matrix.continue-on-error }}
    runs-on: ${{ matrix.runs-on }}
    env:
      BUILD_TOOLS: ${{ matrix.BUILD_TOOLS }}
      BUILD_TYPE: ${{ matrix.BUILD_TYPE }}
      BUILD_SHARED_LIBS: ${{ matrix.BUILD_SHARED_LIBS }}
      CCOMPILER: ${{ matrix.CCOMPILER }}
      CFLAGS: ${{ matrix.CFLAGS }}
      CUCUMBER_TIMEOUT: ${{ matrix.CUCUMBER_TIMEOUT }}
      CXXCOMPILER: ${{ matrix.CXXCOMPILER }}
      CXXFLAGS: ${{ matrix.CXXFLAGS }}
      ENABLE_ASSERTIONS: ${{ matrix.ENABLE_ASSERTIONS }}
      ENABLE_CLANG_TIDY: ${{ matrix.ENABLE_CLANG_TIDY }}
      ENABLE_COVERAGE: ${{ matrix.ENABLE_COVERAGE }}
      ENABLE_CONAN: ${{ matrix.ENABLE_CONAN }}
      ENABLE_SANITIZER: ${{ matrix.ENABLE_SANITIZER }}
      NODE_PACKAGE_TESTS_ONLY: ${{ matrix.NODE_PACKAGE_TESTS_ONLY }}
      ENABLE_APPLE_SILICON: ${{ matrix.ENABLE_APPLE_SILICON }}
      TARGET_ARCH: ${{ matrix.TARGET_ARCH }}
      OSRM_CONNECTION_RETRIES: ${{ matrix.OSRM_CONNECTION_RETRIES }}
      OSRM_CONNECTION_EXP_BACKOFF_COEF: ${{ matrix.OSRM_CONNECTION_EXP_BACKOFF_COEF }}
    steps:
    - uses: actions/checkout@v2

    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node }}
    - name: Enable Node.js cache
      uses: actions/cache@v2
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-
    - name: Enable compiler cache
      uses: actions/cache@v2
      with:
        path: ~/.ccache
        key: ccache-${{ matrix.name }}-${{ github.sha }}
        restore-keys: |
          ccache-${{ matrix.name }}-
    - name: Enable Conan cache
      uses: actions/cache@v2
      with:
        path: ~/.conan
        key: v6-conan-${{ matrix.name }}-${{ github.sha }}
        restore-keys: |
          v6-conan-${{ matrix.name }}-
    - name: Enable test cache
      uses: actions/cache@v2
      with:
        path: ${{github.workspace}}/test/cache
        key: v3-test-${{ matrix.name }}-${{ github.sha }}
        restore-keys: |
          v3-test-${{ matrix.name }}-
      
    - name: Prepare environment
      run: |
        PACKAGE_JSON_VERSION=$(node -e "console.log(require('./package.json').version)")
        echo PUBLISH=$([[ "${GITHUB_REF:-}" == "refs/tags/v${PACKAGE_JSON_VERSION}" ]] && echo "On" || echo "Off") >> $GITHUB_ENV
        echo "OSRM_INSTALL_DIR=${GITHUB_WORKSPACE}/install-osrm" >> $GITHUB_ENV
        echo "OSRM_BUILD_DIR=${GITHUB_WORKSPACE}/build-osrm" >> $GITHUB_ENV
        if [[ "$ENABLE_SANITIZER" == 'ON' ]]; then
          # We can only set this after checkout once we know the workspace directory
          echo "LSAN_OPTIONS=print_suppressions=0:suppressions=${GITHUB_WORKSPACE}/scripts/ci/leaksanitizer.conf" >> $GITHUB_ENV
          echo "UBSAN_OPTIONS=symbolize=1:halt_on_error=1:print_stacktrace=1:suppressions=${GITHUB_WORKSPACE}/scripts/ci/undefinedsanitizer.conf" >> $GITHUB_ENV
        fi

    - name: Install dev dependencies
      run: |
        python3 -m pip install conan==1.51.3

        # ccache
        sudo apt-get update -y && sudo apt-get install ccache
        ccache --version
        
        # clang
        if [[ "${CCOMPILER}" == "clang-6.0" ]]; then
          sudo apt-get update -y && sudo apt-get install clang++-6
        fi
      
        # Linux dev packages
        if [ "${TARGET_ARCH}" != "i686" ] && [ "${ENABLE_CONAN}" != "ON" ]; then
          sudo apt-get update -y
          sudo apt-get install -y libbz2-dev libxml2-dev libzip-dev liblua5.2-dev libboost-all-dev
          if [[ "${CCOMPILER}" != clang-* ]]; then
            sudo apt-get install -y ${CXXCOMPILER}
          fi
          if [[ "${ENABLE_COVERAGE}" == "ON" ]]; then
            sudo apt-get install -y lcov
          fi
        elif [[ $TARGET_ARCH == "i686" ]]; then
          source ./scripts/ci/before_install.${TARGET_ARCH}.sh
          echo "PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig:${PKG_CONFIG_PATH}" >> $GITHUB_ENV
        fi

        # TBB
        TBB_VERSION=2021.3.0
        if [[ "${RUNNER_OS}" == "Linux" ]]; then
          TBB_URL="https://github.com/oneapi-src/oneTBB/releases/download/v${TBB_VERSION}/oneapi-tbb-${TBB_VERSION}-lin.tgz"
        elif [[ "${RUNNER_OS}" == "macOS" ]]; then
          TBB_URL="https://github.com/oneapi-src/oneTBB/releases/download/v${TBB_VERSION}/oneapi-tbb-${TBB_VERSION}-mac.tgz"
        fi
        wget --tries 5 ${TBB_URL} -O onetbb.tgz
        tar zxvf onetbb.tgz
        sudo cp -a oneapi-tbb-${TBB_VERSION}/lib/. /usr/local/lib/
        sudo cp -a oneapi-tbb-${TBB_VERSION}/include/. /usr/local/include/

    - name: Prepare build
      run: |
        mkdir ${OSRM_BUILD_DIR}
        ccache --max-size=256M
        npm ci --ignore-scripts
        if [[ "${ENABLE_COVERAGE}" == "ON" ]]; then
          lcov --directory . --zerocounters # clean cached files
        fi
        echo "CC=${CCOMPILER}" >> $GITHUB_ENV
        echo "CXX=${CXXCOMPILER}" >> $GITHUB_ENV

    - name: Build and install OSRM
      run: |
        echo "Using ${JOBS} jobs"
        pushd ${OSRM_BUILD_DIR}

        # handle Apple Silicon cross compilation
        if [[ "${ENABLE_APPLE_SILICON}" == "ON" ]]; then
          ARCH=arm64
          TARGET="${ARCH}-apple-darwin"
          CFLAGS="$CFLAGS --target=$TARGET"
          CXXFLAGS="$CXXFLAGS --target=$TARGET"
          APPLE_SILICON_FLAGS=(-DCMAKE_C_COMPILER_TARGET="$TARGET" -DCMAKE_CXX_COMPILER_TARGET="$TARGET" -DCMAKE_SYSTEM_PROCESSOR="${ARCH}" -DCMAKE_SYSTEM_NAME="Darwin" -DCMAKE_C_FLAGS="$CFLAGS" -DCMAKE_CXX_FLAGS="$CXXFLAGS")
        else
          APPLE_SILICON_FLAGS=()
        fi

        cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
                 -DENABLE_CONAN=${ENABLE_CONAN:-OFF} \
                 -DENABLE_ASSERTIONS=${ENABLE_ASSERTIONS:-OFF} \
                 -DENABLE_CLANG_TIDY=${ENABLE_CLANG_TIDY:-OFF} \
                 -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS:-OFF} \
                 -DENABLE_COVERAGE=${ENABLE_COVERAGE:-OFF} \
                 -DENABLE_NODE_BINDINGS=${ENABLE_NODE_BINDINGS:-OFF} \
                 -DENABLE_SANITIZER=${ENABLE_SANITIZER:-OFF} \
                 -DBUILD_TOOLS=${BUILD_TOOLS:-OFF} \
                 -DENABLE_CCACHE=ON \
                 -DCMAKE_INSTALL_PREFIX=${OSRM_INSTALL_DIR} \
                 "${APPLE_SILICON_FLAGS[@]}"

        make --jobs=${JOBS}

        if [[ "${NODE_PACKAGE_TESTS_ONLY}" != "ON" && "${ENABLE_APPLE_SILICON}" != "ON" ]]; then
          make tests --jobs=${JOBS}
          make benchmarks --jobs=${JOBS}
          ccache -s
          sudo make install
          if [[ "${RUNNER_OS}" == "Linux" ]]; then
            echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${OSRM_INSTALL_DIR}/lib" >> $GITHUB_ENV
          fi
          echo "PKG_CONFIG_PATH=${OSRM_INSTALL_DIR}/lib/pkgconfig" >> $GITHUB_ENV
        fi
        popd
    - name: Build example
      if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY != 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
      run: |
        mkdir example/build && pushd example/build
        cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
        make --jobs=${JOBS}
        popd
    - name: Run all tests
      if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY != 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
      run: |
        make -C test/data benchmark

        # macOS SIP strips the linker path. Reset this inside the running shell
        export LD_LIBRARY_PATH=${{ env.LD_LIBRARY_PATH }}
        ./example/build/osrm-example test/data/mld/monaco.osrm

        # All tests assume to be run from the build directory
        pushd ${OSRM_BUILD_DIR}
        for i in ./unit_tests/*-tests ; do echo Running $i ; $i ; done
        if [ -z "${ENABLE_SANITIZER}" ] && [ "$TARGET_ARCH" != "i686" ]; then
          npm run nodejs-tests
        fi
        popd
        npm test
    - name: Run benchmarks
      if: ${{ matrix.ENABLE_BENCHMARKS == 'ON' }}
      run: |
        pushd ${OSRM_BUILD_DIR}
        make --jobs=${JOBS} benchmarks
        ./src/benchmarks/alias-bench 
        ./src/benchmarks/match-bench ../test/data/ch/monaco.osrm
        ./src/benchmarks/packedvector-bench
        ./src/benchmarks/rtree-bench ../test/data/monaco.osrm.ramIndex ../test/data/monaco.osrm.fileIndex ../test/data/monaco.osrm.nbg_nodes
        popd
    - name: Run Node package tests only
      if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
      run: |
        npm run nodejs-tests
    - name: Upload test logs
      uses: actions/upload-artifact@v3
      if: failure()
      with:
        name: logs
        path: test/logs/

    - name: Generate code coverage
      if: ${{ matrix.ENABLE_COVERAGE == 'ON' }}
      run: |
        lcov --directory . --capture --output-file coverage.info # capture coverage info
        lcov --remove coverage.info '/usr/*' --output-file coverage.info # filter out system
        lcov --list coverage.info #debug info
        # Uploading report to CodeCov
    - name: Upload code coverage
      if: ${{ matrix.ENABLE_COVERAGE == 'ON' }}
      uses: codecov/codecov-action@v1
      with:
        files: coverage.info
        name: codecov-osrm-backend
        fail_ci_if_error: true
        verbose: true
    - name: Check Apple Silicon binary
      if: ${{  matrix.ENABLE_APPLE_SILICON == 'ON' }}
      run: |
        ARCH=$(file ./lib/binding/node_osrm.node | awk '{printf $NF}')
        if [[ "$ARCH" != "arm64" ]]; then
          file ./lib/binding/node_osrm.node
          >&2 echo "Wrong architecture!"
          exit 1
        fi
    - name: Build Node package
      if: ${{ matrix.build_node_package }}
      run: ./scripts/ci/node_package.sh
    - name: Publish Node package
      if: ${{ matrix.build_node_package && env.PUBLISH == 'On' }}
      uses: ncipollo/release-action@v1
      with:
        allowUpdates: true
        artifactErrorsFailBuild: true
        artifacts: build/stage/**/*.tar.gz
        omitBody: true
        omitBodyDuringUpdate: true
        omitName: true
        omitNameDuringUpdate: true
        replacesArtifacts: true
        token: ${{ secrets.GITHUB_TOKEN }}

  ci-complete:
    runs-on: ubuntu-22.04
    needs: [build-test-publish, docker-image, windows]
    steps:
    - run: echo "CI complete"
