#Makefile
#Řešení IJC-DU2, 22.4.2024
#Autor: Roman Nečas, FIT

# Nastavenie kompilátora a príznakov
CC=gcc
CFLAGS=-std=c11 -pedantic -Wall -Wextra -g
PIC_CFLAGS=$(CFLAGS) -fPIC

# Zdrojove subory kniznice
HTAB_SRCS=htab_init.c htab_size.c htab_bucket_count.c htab_find.c htab_lookup_add.c htab_erase.c htab_for_each.c htab_clear.c htab_free.c htab_statistics.c htab_hash_function.c

#Ciele
all: tail wordcount wordcount-dynamic libhtab.a libhtab.so

# Pravidla pre programy
tail: tail.o
	$(CC) $(CFLAGS) $< -o $@

wordcount: wordcount.o io.o libhtab.a
	$(CC) $(CFLAGS) $^ -o $@

wordcount-dynamic: wordcount.o io.o libhtab.so
	$(CC) $(CFLAGS) $^ -o $@

# Zavislosti pre objektove subory
%.o: %.c htab.h htab_struct.h
	$(CC) $(PIC_CFLAGS) -c $<

io.o: io.c
	$(CC) $(CFLAGS) -c $<

# Pravidla pre staticku a dynamicku kniznicu
libhtab.a: $(HTAB_SRCS:.c=.o)
	ar rcs $@ $^

libhtab.so: $(HTAB_SRCS:.c=.o)
	$(CC) $(CFLAGS) -shared -o $@ $^

# Pravidla pre spustenie programov
run: run-tail run-wordcount run-wordcount-dynamic

run-wordcount: wordcount
	./wordcount < wordcount.c

run-wordcount-dynamic: wordcount-dynamic
   LD_LIBRARY_PATH="." ./wordcount-dynamic < wordcount.c

run-tail: tail
	./tail < wordcount.c

# Cistenie
clean:
	rm -f *.o *.a *.so tail wordcount wordcount-dynamic

.PHONY: all clean run run-wordcount run-wordcount-dynamic run-tail