Small code clean-up + improved the Makefile + detect GNU/Hurd operating system.
[slunkcrypt.git] / Makefile
blob115f71c1898ed07fe30df7a7d2651696277224e0
1 # ---------------------------------------------------------------------------
2 # Options
3 # ---------------------------------------------------------------------------
5 DEBUG ?= 0
6 NALYZE ?= 0
7 ASAN ?= 0
8 STATIC ?= 0
9 FLTO ?= 0
10 FPGO ?= 0
11 STRIP ?= 0
12 CPU ?= 0
13 MARCH ?= 0
14 MTUNE ?= 0
15 THREAD ?= 1
17 # ---------------------------------------------------------------------------
18 # Directories
19 # ---------------------------------------------------------------------------
21 SUBDIR_APP := frontend
22 SUBDIR_LIB := libslunkcrypt
24 # ---------------------------------------------------------------------------
25 # Flags
26 # ---------------------------------------------------------------------------
28 CONFIG =
29 LDFLGS = -lpthread
30 CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall
32 ifneq ($(CPU),0)
33 CFLAGS += -m$(firstword $(CPU))
34 endif
35 ifneq ($(TARGET),)
36 CFLAGS += --target=$(firstword $(TARGET))
37 LDFLGS += --target=$(firstword $(TARGET))
38 endif
40 ifneq ($(MARCH),0)
41 CFLAGS += -march=$(firstword $(MARCH))
42 endif
43 ifneq ($(MTUNE),0)
44 CFLAGS += -mtune=$(firstword $(MTUNE))
45 endif
47 ifneq ($(ASAN),0)
48 CONFIG := _a
49 CFLAGS += -O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
50 else ifneq ($(DEBUG),0)
51 CONFIG := _g
52 CFLAGS += -Og -g
53 else
54 CFLAGS += -O3 -DNDEBUG
55 ifneq ($(FLTO),0)
56 CFLAGS += -flto
57 endif
58 ifneq ($(FPGO),0)
59 CFLAGS += -fprofile-$(firstword $(FPGO))
60 endif
61 endif
62 ifneq ($(NALYZE),0)
63 CFLAGS += -fanalyzer
64 endif
66 MACHINE := $(shell $(CC) -dumpmachine || echo unknown)
68 ifeq ($(MACHINE),$(filter %mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE)))
69 SUFFIX := .exe
70 else
71 SUFFIX :=
72 endif
74 ifeq ($(THREAD),0)
75 CFLAGS += -DSLUNKBUILD_NOTHREADS
76 endif
78 ifneq ($(STRIP),0)
79 LDFLGS += -s
80 endif
82 ifeq ($(STATIC),1)
83 LDFLGS += -static
84 endif
86 ifeq ($(MACHINE),$(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE)))
87 LDFLGS += -mconsole -municode
88 endif
90 # ---------------------------------------------------------------------------
91 # File names
92 # ---------------------------------------------------------------------------
94 OUTNAME_APP := slunkcrypt$(CONFIG)$(SUFFIX)
95 OUTNAME_LIB := libslunkcrypt$(CONFIG)-1.a
97 OUTPATH_APP := $(SUBDIR_APP)/bin/$(OUTNAME_APP)
98 OUTPATH_LIB := $(SUBDIR_LIB)/lib/$(OUTNAME_LIB)
100 SOURCES_APP := $(wildcard $(SUBDIR_APP)/src/*.c)
101 OBJECTS_APP := $(patsubst $(SUBDIR_APP)/src/%.c,$(SUBDIR_APP)/obj/%$(CONFIG).o,$(SOURCES_APP))
103 SOURCES_LIB := $(wildcard $(SUBDIR_LIB)/src/*.c)
104 OBJECTS_LIB := $(patsubst $(SUBDIR_LIB)/src/%.c,$(SUBDIR_LIB)/obj/%$(CONFIG).o,$(SOURCES_LIB))
106 ifneq ($(filter %-mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE)),)
107 RCFILES_APP := $(wildcard $(SUBDIR_APP)/res/*.rc)
108 OBJECTS_APP += $(patsubst $(SUBDIR_APP)/res/%.rc,$(SUBDIR_APP)/obj/%.rsrc.o,$(RCFILES_APP))
109 endif
111 # ---------------------------------------------------------------------------
112 # Targets
113 # ---------------------------------------------------------------------------
115 .PHONY: all clean
117 all: $(OUTPATH_APP)
119 $(OUTPATH_APP): $(OBJECTS_APP) $(OUTPATH_LIB)
120 @mkdir -p $(@D)
121 $(CC) $(CFLAGS) $^ -o $@ $(LDFLGS)
123 $(OUTPATH_LIB): $(OBJECTS_LIB)
124 @mkdir -p $(@D)
125 $(AR) rcs $@ $^
127 $(SUBDIR_APP)/obj/%$(CONFIG).o: $(SUBDIR_APP)/src/%.c
128 @mkdir -p $(@D)
129 $(CC) $(CFLAGS) -c $< -o $@
131 $(SUBDIR_APP)/obj/%.rsrc.o: $(SUBDIR_APP)/res/%.rc
132 @mkdir -p $(@D)
133 windres -o $@ $<
135 $(SUBDIR_LIB)/obj/%$(CONFIG).o: $(SUBDIR_LIB)/src/%.c
136 @mkdir -p $(@D)
137 $(CC) $(CFLAGS) -c $< -o $@
139 clean:
140 $(RM) $(SUBDIR_APP)/obj/*.o $(SUBDIR_APP)/obj/*.gcda $(SUBDIR_APP)/lib/*.a $(SUBDIR_APP)/bin/*$(SUFFIX)
141 $(RM) $(SUBDIR_LIB)/obj/*.o $(SUBDIR_LIB)/obj/*.gcda $(SUBDIR_LIB)/lib/*.a $(SUBDIR_LIB)/bin/*$(SUFFIX)