# This is a basic workflow to help you get started with Actions
# This file is also read by the osx-build-wx script to fetch the
# parameters for a manual build.

name: mac-pwsafe

# Controls when the action will run. Triggers the workflow on all push requests,
# or pull request for the master branch
on:
  push:
#    branches: [ master ]
  pull_request:
    branches: [ master ]
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
permissions:
  contents: read

jobs:
  build_mac_pwsafe:
    name: Build pwsafe for macOS
    # The type of runner that the job will run on
    runs-on: macos-14

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v4

    - name: Set build parameters
      # If you need to rebuild wxWidgets for some reason, say you changed the build params, just increment FORCE_REBUILD_WX
      # Some usefull debug parameters:
      # "WX_DEBUG_FLAG=--enable-debug --enable-debug_info"
      # "XCODE_CONFIG=Debug"
      # "XCODE_CONFIG=Debug-no-yubi"
      run: |
        echo "WX_VERS=3.2.4" >> $GITHUB_ENV
        echo "WX_SRC_DIR=$HOME/wxWidgets" >> $GITHUB_ENV
        echo "WX_BUILD_DIR=$HOME/wxbuild" >> $GITHUB_ENV
        echo "WX_ARCH=arm64,x86_64" >> $GITHUB_ENV
        echo "WX_DEBUG_FLAG=--disable-debug" >> $GITHUB_ENV
        echo "MACOS_VER_MIN=10.14" >> $GITHUB_ENV
        echo "XCODE_VER=15.4" >> $GITHUB_ENV
        echo "XCODE_CONFIG=Release" >> $GITHUB_ENV
        echo "HOST_NCPU=`sysctl -n hw.ncpu`" >>$GITHUB_ENV
        echo "FORCE_REBUILD_WX=7" >> $GITHUB_ENV

    - name: Force compatible Xcode version
      run: sudo xcode-select -s /Applications/Xcode_${XCODE_VER}.app

    - name: Print Xcode version in use
      run: xcode-select -p
      
    - name: Install dependencies for creating dmg
      # gettext is already installed in the runner image
      run: brew install create-dmg googletest

    - name: Install tools for Yubikey build
      run: brew install autoconf automake libtool

    # The Yubico repos don't maintain tags, so checkout specific commits for the sake of safety and repeatability
    # These are the master HEAD commits as of 2024-02-08
    - name: Build libyubikey from upstream
      run: |
        git clone --no-checkout https://github.com/Yubico/yubico-c.git
        cd yubico-c
        git checkout e4334554857b0367085cbf845bf87dd92433e020
        autoreconf --install
        ./configure --disable-shared --disable-documentation CFLAGS="-O2 -arch arm64 -arch x86_64 -mmacosx-version-min=$MACOS_VER_MIN"
        make -j $HOST_CPU check
        sudo make install

    - name: Build ykpers from upstream
      run: |
        git clone --no-checkout https://github.com/Yubico/yubikey-personalization.git
        cd yubikey-personalization
        git checkout db0c0d641d47ee52e43af94dcee603d76186b6d3
        autoreconf --install
        ./configure --disable-shared --disable-documentation CFLAGS="-O2 -arch arm64 -arch x86_64 -mmacosx-version-min=$MACOS_VER_MIN"
        make -j $HOST_CPU check
        sudo make install

    - name: Cache wxWidgets build
      id: cache-wxWidgets-build
      uses: actions/cache@v4
      with:
        path: |
          ${{ env.WX_BUILD_DIR }}
          ${{ env.WX_SRC_DIR }}/locale
        key: wx-${{ env.WX_VERS }}-macOS-${{ env.MACOS_VER_MIN }}-Xcode-${{ env.XCODE_VER }}-build-${{ env.FORCE_REBUILD_WX }}

    - name: Checkout wxWidgets
      if: steps.cache-wxWidgets-build.outputs.cache-hit != 'true'
      run: git clone --branch v$WX_VERS --single-branch https://github.com/wxWidgets/wxWidgets.git $WX_SRC_DIR

    - name: Build wxWidgets
      if: steps.cache-wxWidgets-build.outputs.cache-hit != 'true'
      working-directory: ${{ env.WX_SRC_DIR }}
      run: |
        git -C $WX_SRC_DIR submodule update --init
        rm -rf $WX_BUILD_DIR
        mkdir -p $WX_BUILD_DIR
        cd $WX_SRC_DIR
        # use built-in versions of jpeg, tiff, png, and regex (pcre2) libs to avoid linking with those in /usr/local/opt
        ./configure --enable-universal_binary=$WX_ARCH --prefix=$WX_BUILD_DIR --disable-shared --enable-unicode --with-macosx-version-min=$MACOS_VER_MIN --with-libpng=builtin --with-libjpeg=builtin --with-libtiff=builtin --with-regex=builtin --without-liblzma $WX_DEBUG_FLAG
        make -j $HOST_NCPU
        cd locale; make allmo; cd ..
        sudo make install
        du -sh $WX_BUILD_DIR

    - name: Generate and print xcconfigs
      working-directory: ${{ github.workspace }}
      run: |
        Xcode/generate-configs -r $WX_BUILD_DIR/bin/wx-config > Xcode/pwsafe-release.xcconfig
        cat Xcode/pwsafe-release.xcconfig
    
    - name: Build pwsafe
      working-directory: ${{ github.workspace }}
      run: xcodebuild -project Xcode/pwsafe-xcode6.xcodeproj -scheme pwsafe -configuration $XCODE_CONFIG

    - name: Build and run coretest
      working-directory: src/test
      run: make -f Makefile.macos

    - name: Install language files and create dmg
      working-directory: install/macosx
      run: make

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: PasswordSafe-macOS.${{ github.sha }}
        path: PasswordSafe-macOS*.dmg
        if-no-files-found: error
