UNAME := $(shell uname -s)
CC ?= gcc
STRIP ?= strip
TARGETHELP := $(shell $(CC) --target-help 2>&1)

CFLAGS=-I. -O2
ifneq (,$(findstring sse2,$(TARGETHELP)))
CFLAGS=-I. -O3 -funroll-loops -fomit-frame-pointer -mmmx -msse -msse2 -msse3
else ifneq (,$(findstring mplt,$(TARGETHELP)))
CFLAGS=-I. -O3 -funroll-loops -fomit-frame-pointer -mplt
else ifneq (,$(findstring m4-300,$(TARGETHELP)))
CFLAGS=-I. -O2 -fPIC -funroll-loops -fomit-frame-pointer -m4-300
else
CFLAGS=-I. -O2 -funroll-loops
endif

LFLAGS=-lpthread
#LFLAGS=-L. -lpthread -ldvbcsa

CC_WARN=-W -Wall -Wshadow -Wredundant-decls
SRCS = cscrypt/des.c ffdecsa/ffdecsa.c poc.c 

Q = @
SAY = @echo
OBJS = $(SRCS:.c=.o)
DEPS = $(SRCS:.c=.d)
BIN = poc

all: poc

-include $(OBJS:.o=.d)

%.o: %.c
	$(Q)$(CC) $(CC_WARN) -c -o $@ $< $(CFLAGS)
	$(SAY) "CC	$<"
	$(Q)$(CC) $(CC_WARN) -MM $(CFLAGS) $*.c > $*.d

poc: $(OBJS)
	$(Q)$(CC) $(CC_WARN) -o $(BIN) $(OBJS) $(CFLAGS) $(LFLAGS)
	$(STRIP) $(BIN)
	
clean:
	rm -rf $(BIN) $(OBJS) $(DEPS)
	
.PHONY: poc
