name: Pipeline Segment - Electron Build

on:
  workflow_call:
    inputs:
      environment:
        description: using the production or testing environment
        required: false
        type: string
      target-platform:
        type: string
        description: 'Platform to run on, can be macos, win or linux'
        required: true
      target-arch:
        type: string
        description: 'Arch to build for, can be x64, arm64, ia32 or arm'
        required: true
      target-variant:
        type: string
        description: 'Variant to build for, no effect on non-macOS target platforms. Can be darwin, mas or all.'
        default: all
      build-runs-on:
        type: string
        description: 'What host to run the build'
        required: true
      build-container:
        type: string
        description: 'JSON container information for aks runs-on'
        required: false
        default: '{"image":null}'
      is-release:
        description: 'Whether this build job is a release job'
        required: true
        type: boolean
        default: false
      gn-build-type:
        description: 'The gn build type - testing or release'
        required: true
        type: string
        default: testing
      generate-symbols: 
        description: 'Whether or not to generate symbols'
        required: true
        type: boolean
        default: false
      upload-to-storage: 
        description: 'Whether or not to upload build artifacts to external storage'
        required: true
        type: string
        default: '0'
      strip-binaries: 
        description: 'Strip the binaries before release (Linux only)'
        required: false
        type: boolean
        default: false
      is-asan: 
        description: 'Building the Address Sanitizer (ASan) Linux build'
        required: false
        type: boolean
        default: false


concurrency:
  group: electron-build-${{ inputs.target-platform }}-${{ inputs.target-arch }}-${{ inputs.target-variant }}-${{ inputs.is-asan }}-${{ github.ref_protected == true && github.run_id || github.ref }}
  cancel-in-progress: ${{ github.ref_protected != true }}

env:
  CHROMIUM_GIT_COOKIE: ${{ secrets.CHROMIUM_GIT_COOKIE }}
  CHROMIUM_GIT_COOKIE_WINDOWS_STRING: ${{ secrets.CHROMIUM_GIT_COOKIE_WINDOWS_STRING }}
  ELECTRON_ARTIFACTS_BLOB_STORAGE: ${{ secrets.ELECTRON_ARTIFACTS_BLOB_STORAGE }}
  ELECTRON_RBE_JWT: ${{ secrets.ELECTRON_RBE_JWT }}
  SUDOWOODO_EXCHANGE_URL: ${{ secrets.SUDOWOODO_EXCHANGE_URL }}
  SUDOWOODO_EXCHANGE_TOKEN: ${{ secrets.SUDOWOODO_EXCHANGE_TOKEN }}
  GCLIENT_EXTRA_ARGS: ${{ inputs.target-platform == 'macos' && '--custom-var=checkout_mac=True --custom-var=host_os=mac' || inputs.target-platform == 'win' && '--custom-var=checkout_win=True' || '--custom-var=checkout_arm=True --custom-var=checkout_arm64=True' }}
  ELECTRON_OUT_DIR: Default

jobs:
  build:
    defaults:
      run:
        shell: bash
    runs-on: ${{ inputs.build-runs-on }}
    container: ${{ fromJSON(inputs.build-container) }}
    environment: ${{ inputs.environment }}
    env:
      TARGET_ARCH: ${{ inputs.target-arch }}
    steps:
    - name: Create src dir
      run: |
        mkdir src
    - name: Checkout Electron
      uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
      with:
        path: src/electron
        fetch-depth: 0
        ref: ${{ github.event.pull_request.head.sha }}
    - name: Free up space (macOS)
      if: ${{ inputs.target-platform == 'macos' }}
      uses: ./src/electron/.github/actions/free-space-macos
    - name: Check disk space after freeing up space
      if: ${{ inputs.target-platform == 'macos' }}
      run: df -h
    - name: Setup Node.js/npm
      if: ${{ inputs.target-platform == 'macos' }}
      uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6
      with:
        node-version: 20.11.x
        cache: yarn
        cache-dependency-path: src/electron/yarn.lock
    - name: Install Dependencies
      uses: ./src/electron/.github/actions/install-dependencies
    - name: Install AZCopy
      if: ${{ inputs.target-platform == 'macos' }}
      run: brew install azcopy
    - name: Set GN_EXTRA_ARGS for Linux
      if: ${{ inputs.target-platform == 'linux' }}
      run: |
        if [ "${{ inputs.target-arch  }}" = "arm" ]; then
          if [ "${{ inputs.is-release  }}" = true ]; then
            GN_EXTRA_ARGS='target_cpu="arm" build_tflite_with_xnnpack=false symbol_level=1'
          else
            GN_EXTRA_ARGS='target_cpu="arm" build_tflite_with_xnnpack=false'
          fi
        elif [ "${{ inputs.target-arch }}" = "arm64" ]; then
          GN_EXTRA_ARGS='target_cpu="arm64" fatal_linker_warnings=false enable_linux_installer=false'
        elif [ "${{ inputs.is-asan }}" = true ]; then
          GN_EXTRA_ARGS='is_asan=true'
        fi
        echo "GN_EXTRA_ARGS=$GN_EXTRA_ARGS" >> $GITHUB_ENV
    - name: Set Chromium Git Cookie
      uses: ./src/electron/.github/actions/set-chromium-cookie
    - name: Get Depot Tools
      timeout-minutes: 5
      run: |
        git clone --filter=tree:0 https://chromium.googlesource.com/chromium/tools/depot_tools.git

        SEDOPTION="-i"
        if [ "`uname`" = "Darwin" ]; then
          SEDOPTION="-i ''"
        fi

        # remove ninjalog_uploader_wrapper.py from autoninja since we don't use it and it causes problems
        sed $SEDOPTION '/ninjalog_uploader_wrapper.py/d' ./depot_tools/autoninja

        # Ensure depot_tools does not update.
        test -d depot_tools && cd depot_tools
        touch .disable_auto_update
    - name: Add Depot Tools to PATH
      run: echo "$(pwd)/depot_tools" >> $GITHUB_PATH
    - 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
    - name: Restore src cache via AKS
      if: ${{ inputs.target-platform == 'linux' }}
      uses: ./src/electron/.github/actions/restore-cache-aks
    - name: Checkout src via gclient sync
      if: ${{ inputs.target-platform == 'win' }}
      uses: ./src/electron/.github/actions/checkout
      with:
        use-cache: 'false'
    - name: Checkout Electron
      uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
      with:
        path: src/electron
        fetch-depth: 0
        ref: ${{ github.event.pull_request.head.sha }}
    - name: Install Build Tools
      uses: ./src/electron/.github/actions/install-build-tools
    - name: Init Build Tools
      run: |
        e init -f --root=$(pwd) --out=Default ${{ inputs.gn-build-type }} --import ${{ inputs.gn-build-type }} --target-cpu ${{ inputs.target-arch }}
    - name: Run Electron Only Hooks
      run: |
        e d gclient runhooks --spec="solutions=[{'name':'src/electron','url':None,'deps_file':'DEPS','custom_vars':{'process_deps':False},'managed':False}]"
    - 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: Fix Sync (macOS)
      if: ${{ inputs.target-platform == 'macos' }}
      uses: ./src/electron/.github/actions/fix-sync-macos
    - name: Setup Number of Ninja Processes
      run: |
        echo "NUMBER_OF_NINJA_PROCESSES=${{ inputs.target-platform != 'macos' && '300' || '200' }}" >> $GITHUB_ENV
    - name: Free up space (macOS)
      if: ${{ inputs.target-platform == 'macos' }}
      uses: ./src/electron/.github/actions/free-space-macos
    - name: Build Electron
      if: ${{ inputs.target-platform != 'macos' || (inputs.target-variant == 'all' || inputs.target-variant == 'darwin') }}
      uses: ./src/electron/.github/actions/build-electron
      with:
        target-arch: ${{ inputs.target-arch }}
        target-platform: ${{ inputs.target-platform }}
        artifact-platform: ${{ inputs.target-platform == 'macos' && 'darwin' || inputs.target-platform }}
        is-release: '${{ inputs.is-release }}'
        generate-symbols: '${{ inputs.generate-symbols }}'
        strip-binaries: '${{ inputs.strip-binaries }}'
        upload-to-storage: '${{ inputs.upload-to-storage }}'
        is-asan: '${{ inputs.is-asan }}'
    - name: Set GN_EXTRA_ARGS for MAS Build
      if: ${{ inputs.target-platform == 'macos' && (inputs.target-variant == 'all' || inputs.target-variant == 'mas') }}
      run: |
        echo "MAS_BUILD=true" >> $GITHUB_ENV
        GN_EXTRA_ARGS='is_mas_build=true'
        echo "GN_EXTRA_ARGS=$GN_EXTRA_ARGS" >> $GITHUB_ENV
    - name: Build Electron (MAS)
      if: ${{ inputs.target-platform == 'macos' && (inputs.target-variant == 'all' || inputs.target-variant == 'mas') }}
      uses: ./src/electron/.github/actions/build-electron
      with:
        target-arch: ${{ inputs.target-arch }}
        target-platform: ${{ inputs.target-platform }}
        artifact-platform: 'mas'
        is-release: '${{ inputs.is-release }}'
        generate-symbols: '${{ inputs.generate-symbols }}'
        upload-to-storage: '${{ inputs.upload-to-storage }}'
        step-suffix: '(mas)'
