name: Build example project

on: [push, pull_request]

env:
  CARGO_TERM_COLOR: always

jobs:

  example-project:

    strategy:
      matrix:
        include:
          - os: ubuntu-latest
          # x86_64:
          - os: macos-13
          # arm64:
          - os: macos-latest
          - os: windows-latest
            toolchain-suffix: -gnu
          - os: windows-latest
            toolchain-suffix: -msvc

    runs-on: ${{ matrix.os }}

    steps:
      - name: Clone Git repository
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable${{ matrix.toolchain-suffix }}

      - name: Install pkgconf
        if: runner.os == 'Windows'
        uses: msys2/setup-msys2@v2
        id: msys2
        with:
          msystem: ucrt64
          install: mingw-w64-ucrt-x86_64-pkgconf

      - name: Put pkgconf on PATH
        if: runner.os == 'Windows'
        run: Add-Content $env:GITHUB_PATH "${{ steps.msys2.outputs.msys2-location }}\ucrt64\bin"

      - name: Install cargo-c applet
        run: cargo install --path .

      - name: Test example project
        working-directory: example-project
        run: cargo test --verbose

      - name: Build C API for example project
        working-directory: example-project
        run: cargo cbuild --verbose --release

      - name: Run C API tests for example project
        working-directory: example-project
        run: cargo ctest --verbose --release

      - name: Install into /usr/local
        if: runner.os != 'Windows'
        working-directory: example-project
        run: sudo -E env PATH=$PATH cargo cinstall --verbose --release --prefix=/usr/local

      - name: Install into MSYS2 root
        if: runner.os == 'Windows'
        working-directory: example-project
        run: cargo cinstall --verbose --release --prefix="${{ steps.msys2.outputs.msys2-location }}\ucrt64"

      - name: Test pkgconf
        if: runner.os == 'macOS'
        run: |
          set -x
          pkgconf --version
          test "$(pkgconf --cflags example_project)" = "-I/usr/local/include/example-project-0.1"
          test "$(pkgconf --libs example_project)" = "-L/usr/local/lib -lexample-project"

      - name: Test pkgconf
        if: runner.os == 'Linux'
        run: |
          set -x
          pkgconf --version
          ARCHDIR=`dpkg-architecture -qDEB_HOST_MULTIARCH`
          # ubuntu seems to add trailing spaces for no specific reasons.
          CFLAGS=$(pkgconf --cflags example_project)
          LIBS=$(pkgconf --libs example_project)

          test "${CFLAGS%% }" = "-I/usr/local/include/example-project-0.1"
          test "${LIBS%% }" = "-L/usr/local/lib/${ARCHDIR} -lexample-project"

      - name: Test pkgconf
        if: runner.os == 'Windows'
        shell: bash
        run: |
          set -x
          pkgconf --version
          # use --define-variable=prefix=C:/foo to test relative libdir/includedir generation
          # https://github.com/lu-zero/cargo-c/commit/76a66cd72eb4271501557eebea7060821e63b702
          test "$(pkgconf --define-variable=prefix=C:/foo --cflags example_project)" = "-IC:/foo/include/example-project-0.1"
          test "$(pkgconf --define-variable=prefix=C:/foo --libs example_project)" = "-LC:/foo/lib -lexample-project"

      - name: Update dynamic linker cache
        if: runner.os == 'Linux'
        run: sudo ldconfig

      - name: Test usage from C (using Makefile)
        if: runner.os != 'Windows'
        working-directory: example-project/usage-from-c
        run: make

      - name: Setup Meson + Ninja
        if: runner.os == 'Windows' && matrix.toolchain-suffix == '-msvc'
        run: |
          python3 -m pip install --upgrade pip setuptools wheel
          python3 -m pip install meson ninja

      - name: Setup MSVC for test
        if: runner.os == 'Windows' && matrix.toolchain-suffix == '-msvc'
        uses: ilammy/msvc-dev-cmd@v1
        with:
          arch: x86_64

      - name: Test usage from C (Meson)
        if: runner.os == 'Windows' && matrix.toolchain-suffix == '-msvc'
        working-directory: example-project/usage-from-c
        env:
          PKG_CONFIG: pkgconf
        run: |
          meson setup build
          meson compile -C build
          meson test -C build
