# MIT License
#
# Copyright (c) 2020-2025 Daniel J. Mazure
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# sim/Makefile
# Usage: make -C sim bootstrap | update-deps | clean

.PHONY: bootstrap deps pip i2c xpm update-deps clean help

PKG_DIR := cocotb/packages
XPM_DIR := xpm_vhdl

I2C_URL  := https://github.com/etlsystems/cocotbext-i2c.git
# UART: Using native SDK driver (tb/drivers/uart_source.py) - no external dep needed
XPM_URL  := https://github.com/fransschreuder/xpm_vhdl.git

help:
	@echo "Targets:"
	@echo "  bootstrap     Clone deps if missing and pip install editable packages"
	@echo "  deps          Only clone deps if missing"
	@echo "  pip           Only pip-install editable packages if not already importable"
	@echo "  update-deps   git pull --ff-only for deps (no auto-rebase)"
	@echo "  clean         Remove cloned deps (does not touch your repo)"

bootstrap: deps pip
	@echo "[bootstrap] Done."

# ---------- Git clones (idempotent) ----------
deps: i2c xpm

i2c: $(PKG_DIR)/cocotbext-i2c/.git
$(PKG_DIR)/cocotbext-i2c/.git:
	@mkdir -p $(PKG_DIR)
	git clone $(I2C_URL) $(PKG_DIR)/cocotbext-i2c

xpm: $(XPM_DIR)/.git
$(XPM_DIR)/.git:
	@mkdir -p $(dir $(XPM_DIR))
	git clone $(XPM_URL) $(XPM_DIR)

# ---------- Pip installs (idempotent-ish) ----------
pip: deps
	python3 pip_bootstrap.py

# ---------- Update / Clean ----------
update-deps:
	@test -d $(PKG_DIR)/cocotbext-i2c && git -C $(PKG_DIR)/cocotbext-i2c pull --ff-only || true
	@test -d $(XPM_DIR)               && git -C $(XPM_DIR)               pull --ff-only || true

clean:
	rm -rf $(PKG_DIR)/cocotbext-i2c $(XPM_DIR)
