# Maintainer: Coelacanthus <coelacanthus@archlinuxcn.org>
# Markus Koch <markus@notsyncing.net>

pkgname=python-glasgow-git
epoch=1
pkgver=r2923.4c1f0c5
pkgrel=1
pkgdesc="Software for the Glasgow Interface Explorer, the Scots Army Knife for electronics"
arch=('any')
url='https://github.com/GlasgowEmbedded/glasgow.git'
license=('0BSD' 'Apache-2.0')
# https://github.com/GlasgowEmbedded/glasgow/blob/main/software/pyproject.toml
depends=(
    icestorm
    nextpnr
    python
    python-amaranth
    python-cobs
    python-fx2
    python-importlib_resources
    python-libusb1
    python-platformdirs
    python-pyvcd
    python-typing_extensions
    yosys
)
optdepends=(
    'python-aiohttp: yamaha_opx, logger'
	'python-aiohttp_remotes: yamaha_opx'
    'python-matplotlib: memory-floppy'
    'python-numpy: memory-floppy'
    'yowasp-runtime: WASM based toolchain (slower than system yosys toolchain)'
    'yowasp-yosys: WASM based toolchain (slower than system yosys toolchain)'
)
makedepends=(
    git
    python-build
    python-pdm
    python-pdm-backend
)
source=(
    "git+https://github.com/GlasgowEmbedded/glasgow.git"
    "glasgow-use-system-toolchain-by-default.patch"
)
# Only needed when build firmware
#"git+https://github.com/whitequark/libfx2.git"
b2sums=('SKIP'
        '577a43b8f51f09a83bdbd2521a3ccb9f10856f7b7c1fee3dd2c65d31deb21aa294d9fbcd111bb382b252c74b67cc0c13db2a6091b3994e9e4ddb0e1632ab8cab')
provides=(python-glasgow)
conflicts=(python-glasgow)

pkgver() {
	cd "glasgow"
	( 
        set -o pipefail
        git describe --long --abbrev=7 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g' \
            || printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short=7 HEAD)"
    )
}

prepare() {
	cd "glasgow"

    # Only needed when build firmware
    #git submodule init
    #git config submodule.vendor/libfx2.url "$srcdir/libfx2"
    #git -c protocol.file.allow=always submodule update
    
    # Change default value of $GLASGOW_TOOLCHAIN from builtin,system to system,builtin.
    # Use patch so if upstream changed the order we can found immediately.
    patch -Np1 -i ../glasgow-use-system-toolchain-by-default.patch
}

build() {
	pushd "glasgow/software"
        python -m build --wheel --no-isolation
    popd

    # Upstream do reproducible build for firmware
    #make -C vendor/libfx2/firmware/library all MODELS=medium
    #make -C firmware all
    #PYTHONPATH=vendor/libfx2/software python3 firmware/normalize.py \
    #    firmware/glasgow.ihex software/glasgow/hardware/firmware.ihex

}

package() {
	cd "glasgow/software"

	python -m installer --destdir="$pkgdir" dist/*.whl
	install -Dm 644 $srcdir/glasgow/config/70-glasgow.rules $pkgdir/etc/udev/rules.d/70-glasgow.rules
}
# vim: set sts=4 ts=4 sw=4 et:
