# Makefile
# Copyright (C) 2026 Vladimir Roncevic <elektron.ronca@gmail.com>
#
# ${project_name} is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ${project_name} 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program_name. If not, see <http://www.gnu.org/licenses/>.

include tools.mk
include sources.mk
include subdir.mk
include objects.mk
include cflags.mk
include aflags.mk
include odflags.mk
include ocflags.mk
include csflags.mk

ifneq ($$(MAKECMDGOALS),clean)
	ifneq ($$(strip $$(I_DEPS)),)
		-include $$(I_DEPS)
	endif
	ifneq ($$(strip $$(ASM_DEPS)),)
		-include $$(ASM_DEPS)
	endif
	ifneq ($$(strip $$(M_DEPS)),)
		-include $$(M_DEPS)
	endif
	ifneq ($$(strip $$(MI_DEPS)),)
		-include $$(MI_DEPS)
	endif
	ifneq ($$(strip $$(S_DEPS)),)
		-include $$(S_DEPS)
	endif
	ifneq ($$(strip $$(S_UPPER_DEPS)),)
		-include $$(S_UPPER_DEPS)
	endif
	ifneq ($$(strip $$(C_DEPS)),)
		-include $$(C_DEPS)
	endif
endif

LSS += \
	lib${project_name}.lss

SIZEDUMMY += \
	sizedummy

CLEAN_LIST += \
	$$(I_DEPS) \
	$$(OBJS) \
	$$(ASM_DEPS) \
	$$(S_DEPS) \
	$$(SIZEDUMMY) \
	$$(S_UPPER_DEPS) \
	$$(LSS) \
	$$(C_DEPS) \
	$$(ARCHIVES) \
	lib${project_name}.a

all: lib${project_name}.a secondary-outputs

lib${project_name}.a: $$(OBJS)
	@echo 'Building target: $$@'
	@echo 'Invoking: AVR Archiver'
	$$(AR) $$(AFLAGS) "lib${project_name}.a" $$(OBJS) $$(LIBS)
	@echo 'Finished building target: $$@'
	@echo ' '

lib${project_name}.lss: lib${project_name}.a
	@echo 'Invoking: AVR Create Extended Listing'
	$$(OD) $$(ODFLAGS) lib${project_name}.a >"lib${project_name}.lss"
	@echo 'Finished building: $$@'
	@echo ' '

sizedummy: lib${project_name}.a
	@echo 'Invoking: Print Size'
	$$(CS) $$(CSFLAGS) lib${project_name}.a
	@echo 'Finished building: $$@'
	@echo ' '

clean:
	@for item in $$(CLEAN_LIST);do echo "$$(RM) $$$$item";$$(RM) $$$$item;done
	@echo ' '

secondary-outputs: $$(LSS) $$(SIZEDUMMY)

.PHONY: all clean dependents

