#
# Makefile
# 
# Copyright 2024 keXa <nikitospikalov2008@gmail.com>
# Copyright 2025 keXa <nikitospikalov2008@gmail.com>, Egor00f
# 
# This program 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 2 of the License, or
# (at your option) any later version.
# 
# This program 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; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
 
CC = kos32-gcc
LD = kos32-ld
AR = kos32-ar

HOME_DIR = $(shell echo ~)
TOOLCHAIN_DIR = $(abspath /home/autobuild/tools/win32/)
CONTRIB_DIR = $(abspath $(HOME_DIR)/kolibrios/contrib)
SDK_DIR = $(CONTRIB_DIR)/sdk
C_LAYER = $(CONTRIB_DIR)/C_Layer

MICROTAR = lib/microtar/src
CCSV = lib/ccsv

CFLAGS = -fno-ident -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -DNDEBUG -c
LDFLAGS = -static -S -nostdlib -T app.lds --subsystem native -L $(TOOLCHAIN_DIR)/lib -L $(TOOLCHAIN_DIR)/mingw32/lib -L $(SDK_DIR)/lib

INCLUDES = -I $(SDK_DIR)/sources/newlib/libc/include -I include -I $(SDK_DIR)/sources/newlib/libc/include -I $(MICROTAR) -I $(CCSV)/include
LIBPATH = -L $(SDK_DIR)/lib -L $(TOOLCHAIN_DIR)/mingw32/lib

MICROTAR_SOURCES = $(MICROTAR)/microtar.c
CCSV_SOURCE = $(CCSV)/src/ccsv.c

SOURCES := $(MICROTAR_SOURCES) $(CCSV_SOURCE) \
		src/kospm_tar.c src/kospm_list.c src/kospm_csv.c \
		src/kospm_pkgmeta.c src/kospm_pkgfile.c src/kospm_pkglist.c \
		src/kospm_pkgbundle.c src/kospm_pkgdb.c src/kospm.c

OBJECTS := $(SOURCES:.c=.o)

default: libkospm.a

libkospm.a: $(OBJECTS) Makefile
	$(AR) -crs libkospm.a $(OBJECTS)

%.o : %.c Makefile $(SOURCES)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $<

clean:
	rm -f $(OBJECTS) libkospm.a
