name: macos with gfortran

on: [push, pull_request]

jobs:
  Build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ macos-latest ]
        include:
        - os: macos-latest
          gcc_v: 12
    env:
      FC: gfortran
      GCC_V: ${{ matrix.gcc_v }}
      TZ: UTC+04:00

    steps:
    - name: Checkout code
      uses: actions/checkout@v1

    - name: Install GFortran macOS
      run: |
          ls /usr/local/bin/gfortran-*
          ln -f -s /usr/local/bin/gfortran-${GCC_V} /usr/local/bin/gfortran
          which gfortran-${GCC_V}
          which gfortran

    - name: Install fpm
      run: |
           wget https://raw.githubusercontent.com/urbanjost/index/main/bootstrap/fpm.F90
           mkdir -p /Users/runner/bin
           gfortran fpm.F90 -o /Users/runner/bin/fpm
           ls -l

    - name: Display environment
      run: |
        echo $PATH
        pwd
        uname -a
        ulimit -a
        echo "end" > foo.f90
        gfortran -cpp -E -dM foo.f90
        gfortran --version
        fpm --version
        ulimit -a

    - name: tests MacOS (release)
      run: |
          ulimit -c unlimited -d unlimited -f unlimited -m unlimited -t unlimited -v unlimited
          exec 2>&1
          set -x
          #fpm test --profile release --verbose --flag -fno-automatic        # looks like bug where formats go on stack anyway
          #fpm test --profile debug --verbose --flag -fmax-stack-var-size=0  # Apparently infinite loop
          #fpm test --profile debug --verbose --flag -fmax-stack-var-size=2024 # loop
          #fpm test --profile debug --verbose --flag -frecursive
          #fpm test --profile debug --verbose --flag -static
          #timeout 0.5h fpm test --profile debug --verbose
          ulimit -S -t 1800
          fpm test --profile debug --verbose

    - name: tests MacOS (debug)
      run: |
          ulimit -c unlimited -d unlimited -f unlimited -m unlimited -t unlimited -v unlimited
          exec 2>&1
          set -x
          ulimit -S -t 1800
          fpm test --profile debug --verbose

    - name: cleanup MacOS
      run: ls -ltras
#       -fno-automatic
#     Treat each program unit (except those marked as RECURSIVE) as if
#     the "SAVE" statement were specified for every local variable and
#     array referenced in it. Does not affect common blocks. (Some
#     Fortran compilers provide this option under the name -static or
#     -save.)  The default, which is -fautomatic, uses the stack for
#     local variables smaller than the value given by
#     -fmax-stack-var-size.  Use the option -frecursive to use no static
#     memory.
#
#     Local variables or arrays having an explicit "SAVE" attribute are
#     silently ignored unless the -pedantic option is added.
# -fmax-stack-var-size=n
#     This option specifies the size in bytes of the largest array that
#     will be put on the stack; if the size is exceeded static memory is
#     used (except in procedures marked as RECURSIVE). Use the option
#     -frecursive to allow for recursive procedures which do not have a
#     RECURSIVE attribute or for parallel programs. Use -fno-automatic to
#     never use the stack.
#
#     This option currently only affects local arrays declared with
#     constant bounds, and may not apply to all character variables.
#     Future versions of GNU Fortran may improve this behavior.
#
#     The default value for n is 32768.
# -frecursive
#     Allow indirect recursion by forcing all local arrays to be
#     allocated on the stack. This flag cannot be used together with
#     -fmax-stack-var-size= or -fno-automatic.
