# Copyright (C) 2025,2026 Junhao Ding
# This file is part of ELFCOST Compiler.
#
# ELFCOST Compiler is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ELFCOST Compiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with ELFCOST Compiler.  If not, see <https://www.gnu.org/licenses/>.

CC = gcc
CFLAGS = -I src/ -w -Wno-error
RELEASE_FLAGS = -O2
DEBUG_FLAGS = -g -O0
TARGET = ecc
DEBUG_TARGET = ecc-dbg

SRC_FILES = main.c cli/cli.c codegen/codegen.c common/utils.c lexer/lexer.c module/modules.c parser/parser.c

all:
	$(CC) $(SRC_FILES) $(CFLAGS) $(RELEASE_FLAGS) -o $(TARGET)
	@echo "Release version compiled: ./$(TARGET)"

debug:
	$(CC) $(SRC_FILES) $(CFLAGS) $(DEBUG_FLAGS) -o $(DEBUG_TARGET)
	@echo "Debug version compiled: ./$(DEBUG_TARGET)"

clean:
	rm -rf $(TARGET) $(DEBUG_TARGET) ecc-mvp *.bin
	@echo "Cleaned all artifacts"

package: all
	mkdir -p ecc-mvp/examples
	cp $(TARGET) ecc-mvp/
	printf "use x86_real;\nreg.ax = 0x1234;\nreg.bx = 0x5678;\n" > ecc-mvp/examples/test.elfc
	printf "# ECC MVP\nELFCOST compiler supporting basic register operations.\n\nUsage:\n./ecc debug -el examples/test.elfc -ma output.bin\n" > ecc-mvp/README.txt
	@echo "Package created in ./ecc-mvp"