BUILD     = build
BUILD_DBG = build_debug
INSTALL_BIN = /opt/homebrew/bin
INSTALL_LIB = /opt/homebrew/lib
INSTALL_INC = /opt/homebrew/include
CELL_SHOP   = $(HOME)/.cell

all: $(BUILD)/build.ninja
	meson compile -C $(BUILD)
	cp $(BUILD)/libcell_runtime.dylib .
	cp $(BUILD)/cell .

$(BUILD)/build.ninja:
	meson setup $(BUILD) -Dbuildtype=release

debug: $(BUILD_DBG)/build.ninja
	meson compile -C $(BUILD_DBG)
	cp $(BUILD_DBG)/libcell_runtime.dylib .
	cp $(BUILD_DBG)/cell .

$(BUILD_DBG)/build.ninja:
	meson setup $(BUILD_DBG) -Dbuildtype=debug -Db_sanitize=address

install: all $(CELL_SHOP)
	cp cell $(INSTALL_BIN)/cell
	cp libcell_runtime.dylib $(INSTALL_LIB)/
	cp source/cell.h source/quickjs.h source/wota.h $(INSTALL_INC)/
	rm -rf $(CELL_SHOP)/packages/core
	ln -s $(CURDIR) $(CELL_SHOP)/packages/core
	@echo "Installed cell to $(INSTALL_BIN) and $(INSTALL_LIB)"

install_debug: debug $(CELL_SHOP)
	cp cell $(INSTALL_BIN)/cell
	cp libcell_runtime.dylib $(INSTALL_LIB)/
	cp source/cell.h source/quickjs.h source/wota.h $(INSTALL_INC)/
	rm -rf $(CELL_SHOP)/packages/core
	ln -s $(CURDIR) $(CELL_SHOP)/packages/core
	@echo "Installed cell (debug+asan) to $(INSTALL_BIN) and $(INSTALL_LIB)"

$(CELL_SHOP):
	mkdir -p $(CELL_SHOP)/packages $(CELL_SHOP)/cache $(CELL_SHOP)/build

clean:
	rm -rf $(BUILD) $(BUILD_DBG)
	rm -f cell libcell_runtime.dylib

.PHONY: all install debug install_debug clean
