name: Pipeline Segment - Electron GN Check

on:
  workflow_call:
    inputs:
      target-platform:
        type: string
        description: 'Platform to run on, can be macos, win or linux'
        required: true
      target-archs:
        type: string
        description: 'Archs to check for, can be x64, x86, arm64 or arm space separated'
        required: true
      check-runs-on:
        type: string
        description: 'What host to run the tests on'
        required: true
      check-container:
        type: string
        description: 'JSON container information for aks runs-on'
        required: false
        default: '{"image":null}'
      gn-build-type:
        description: 'The gn build type - testing or release'
        required: true
        type: string
        default: testing

concurrency:
  group: electron-gn-check-${{ inputs.target-platform }}-${{ github.ref }}
  cancel-in-progress: true

env:
  ELECTRON_RBE_JWT: ${{ secrets.ELECTRON_RBE_JWT }}
  GCLIENT_EXTRA_ARGS: ${{ inputs.target-platform == 'macos' && '--custom-var=checkout_mac=True --custom-var=host_os=mac' || (inputs.target-platform == 'linux' && '--custom-var=checkout_arm=True --custom-var=checkout_arm64=True' || '--custom-var=checkout_win=True') }}
  ELECTRON_OUT_DIR: Default

jobs:
  gn-check:
    defaults:
      run:
        shell: bash
    runs-on: ${{ inputs.check-runs-on }}
    container: ${{ fromJSON(inputs.check-container) }}
    steps:
    - name: Checkout Electron
      uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
      with:
        path: src/electron
        fetch-depth: 0
        ref: ${{ github.event.pull_request.head.sha }}
    - name: Cleanup disk space on macOS
      if: ${{ inputs.target-platform == 'macos' }}
      shell: bash
      run: |   
        sudo mkdir -p $TMPDIR/del-target

        tmpify() {
          if [ -d "$1" ]; then
            sudo mv "$1" $TMPDIR/del-target/$(echo $1|shasum -a 256|head -n1|cut -d " " -f1)
          fi
        }   
        tmpify /Library/Developer/CoreSimulator
        tmpify ~/Library/Developer/CoreSimulator
        sudo rm -rf $TMPDIR/del-target
    - name: Check disk space after freeing up space
      if: ${{ inputs.target-platform == 'macos' }}
      run: df -h
    - name: Set Chromium Git Cookie
      uses: ./src/electron/.github/actions/set-chromium-cookie
    - name: Install Build Tools
      uses: ./src/electron/.github/actions/install-build-tools
    - name: Enable windows toolchain
      if: ${{ inputs.target-platform == 'win' }}
      run: |
        echo "ELECTRON_DEPOT_TOOLS_WIN_TOOLCHAIN=1" >> $GITHUB_ENV
    - name: Generate DEPS Hash
      run: |
        node src/electron/script/generate-deps-hash.js
        DEPSHASH=v1-src-cache-$(cat src/electron/.depshash)
        echo "DEPSHASH=$DEPSHASH" >> $GITHUB_ENV
        echo "CACHE_PATH=$DEPSHASH.tar" >> $GITHUB_ENV
    - name: Restore src cache via AZCopy
      if: ${{ inputs.target-platform == 'macos' }}
      uses: ./src/electron/.github/actions/restore-cache-azcopy
      with:
        target-platform: ${{ inputs.target-platform }}      
    - name: Restore src cache via AKS
      if: ${{ inputs.target-platform == 'linux' || inputs.target-platform == 'win' }}
      uses: ./src/electron/.github/actions/restore-cache-aks
      with:
        target-platform: ${{ inputs.target-platform }}
    - name: Run Electron Only Hooks
      run: |
        echo "solutions=[{'name':'src/electron','url':None,'deps_file':'DEPS','custom_vars':{'process_deps':False},'managed':False}]" > tmpgclient
        if [ "${{ inputs.target-platform }}" = "win" ]; then
          echo "solutions=[{'name':'src/electron','url':None,'deps_file':'DEPS','custom_vars':{'process_deps':False,'install_sysroot':False,'checkout_win':True},'managed':False}]" > tmpgclient
          echo "target_os=['win']" >> tmpgclient
        fi
        e d gclient runhooks --gclientfile=tmpgclient

        # Fix VS Toolchain
        if [ "${{ inputs.target-platform }}" = "win" ]; then
          rm -rf src/third_party/depot_tools/win_toolchain/vs_files
          e d python3 src/build/vs_toolchain.py update --force
        fi
    - name: Regenerate DEPS Hash
      run: |
        (cd src/electron && git checkout .) && node src/electron/script/generate-deps-hash.js
        echo "DEPSHASH=$(cat src/electron/.depshash)" >> $GITHUB_ENV
    - name: Add CHROMIUM_BUILDTOOLS_PATH to env
      run: echo "CHROMIUM_BUILDTOOLS_PATH=$(pwd)/src/buildtools" >> $GITHUB_ENV
    - name: Checkout Electron
      uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
      with:
        path: src/electron
        fetch-depth: 0
        ref: ${{ github.event.pull_request.head.sha }}
    - name: Install Dependencies
      uses: ./src/electron/.github/actions/install-dependencies
    - name: Default GN gen
      run: |
        cd src/electron
        git pack-refs
    - name: Run GN Check for ${{ inputs.target-archs }}
      run: |
        for target_cpu in ${{ inputs.target-archs }}
        do
          e init -f --root=$(pwd) --out=Default ${{ inputs.gn-build-type }} --import ${{ inputs.gn-build-type }} --target-cpu $target_cpu
          cd src
          export GN_EXTRA_ARGS="target_cpu=\"$target_cpu\""
          if [ "${{ inputs.target-platform }}" = "linux" ]; then
            if [ "$target_cpu" = "arm" ]; then
              export GN_EXTRA_ARGS="$GN_EXTRA_ARGS build_tflite_with_xnnpack=false"
            elif [ "$target_cpu" = "arm64" ]; then
              export GN_EXTRA_ARGS="$GN_EXTRA_ARGS fatal_linker_warnings=false enable_linux_installer=false"
            fi
          fi
          if [ "${{ inputs.target-platform }}" = "win" ]; then
            export GN_EXTRA_ARGS="$GN_EXTRA_ARGS use_v8_context_snapshot=true target_os=\"win\""
          fi

          e build --only-gen

          e d gn check out/Default //electron:electron_lib
          e d gn check out/Default //electron:electron_app
          e d gn check out/Default //electron/shell/common:mojo
          e d gn check out/Default //electron/shell/common:plugin

          # Check the hunspell filenames
          node electron/script/gen-hunspell-filenames.js --check
          node electron/script/gen-libc++-filenames.js --check
          cd ..
        done
    - name: Wait for active SSH sessions
      if: always() && !cancelled()
      shell: bash
      run: |
        while [ -f /var/.ssh-lock ]
        do
          sleep 60
        done
