OSDN Git Service

Updated README file.
[slunkcrypt/SlunkCrypt.git] / Makefile
1 # ---------------------------------------------------------------------------
2 # Options
3 # ---------------------------------------------------------------------------
4
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 THREAD ?= 1
14
15 $(info Options: DEBUG=$(DEBUG), NALYZE=$(NALYZE), ASAN=$(ASAN), STATIC=$(STATIC), FLTO=$(FLTO), FPGO=$(FPGO), STRIP=$(STRIP), CPU=$(CPU), THREAD=$(THREAD))
16
17 # ---------------------------------------------------------------------------
18 # Directories
19 # ---------------------------------------------------------------------------
20
21 SUBDIR_APP := frontend
22 SUBDIR_LIB := libslunkcrypt
23
24 # ---------------------------------------------------------------------------
25 # Flags
26 # ---------------------------------------------------------------------------
27
28 CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall -pedantic
29
30 ifneq (,$(firstword $(filter 32 64,$(CPU))))
31   CFLAGS += -m$(firstword $(CPU))
32 endif
33 ifneq (,$(firstword $(TARGET)))
34   CFLAGS += --target=$(firstword $(TARGET))
35   LDFLGS += --target=$(firstword $(TARGET))
36 endif
37
38 ifneq (,$(firstword $(MARCH)))
39   CFLAGS += -march=$(firstword $(MARCH))
40 endif
41 ifneq (,$(firstword $(MTUNE)))
42   CFLAGS += -mtune=$(firstword $(MTUNE))
43 endif
44
45 ifneq ($(ASAN),0)
46   CONFIG := _a
47   CFLAGS += -O1 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
48 else
49   ifneq ($(DEBUG),0)
50     CONFIG := _g
51     CFLAGS += -Og -g
52   else
53     CFLAGS += -O3 -DNDEBUG
54     ifneq ($(FLTO),0)
55       CFLAGS += -flto
56     endif
57     ifneq ($(FPGO),0)
58       CFLAGS += -fprofile-$(firstword $(FPGO))
59     endif
60   endif
61 endif
62 ifneq ($(NALYZE),0)
63   CFLAGS += -fanalyzer
64 endif
65
66 MACHINE := $(strip $(shell $(CC) -dumpmachine))
67 ifeq (,$(MACHINE))
68   $(error Failed to determine target machine, please check CC is working!)
69 else
70   $(info Target machine type: $(MACHINE))
71 endif
72
73 ifneq (,$(firstword $(filter %mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE))))
74   EXE_SUFFIX := .exe
75 endif
76
77 ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))))
78   LDFLGS += -mconsole -municode
79 endif
80
81 ifeq ($(STATIC),1)
82   LDFLGS += -static
83 endif
84
85 ifneq ($(STRIP),0)
86   LDFLGS += -s
87 endif
88
89 ifeq ($(THREAD),1)
90   LDFLGS += -lpthread
91 else
92   CFLAGS += -DSLUNKBUILD_NOTHREADS
93 endif
94
95 ifneq (,$(firstword $(filter %-pc-haiku %-unknown-haiku,$(MACHINE))))
96   LDFLGS += -lbsd
97 endif
98
99 # ---------------------------------------------------------------------------
100 # File names
101 # ---------------------------------------------------------------------------
102
103 OUTNAME_APP := slunkcrypt$(CONFIG)$(EXE_SUFFIX)
104 OUTNAME_LIB := libslunkcrypt$(CONFIG)-1.a
105
106 OUTPATH_APP := $(SUBDIR_APP)/bin/$(OUTNAME_APP)
107 OUTPATH_LIB := $(SUBDIR_LIB)/lib/$(OUTNAME_LIB)
108
109 SOURCES_APP := $(wildcard $(SUBDIR_APP)/src/*.c)
110 OBJECTS_APP := $(patsubst $(SUBDIR_APP)/src/%.c,$(SUBDIR_APP)/obj/%$(CONFIG).o,$(SOURCES_APP))
111
112 SOURCES_LIB := $(wildcard $(SUBDIR_LIB)/src/*.c)
113 OBJECTS_LIB := $(patsubst $(SUBDIR_LIB)/src/%.c,$(SUBDIR_LIB)/obj/%$(CONFIG).o,$(SOURCES_LIB))
114
115 ifneq ($(filter %-mingw32 %-windows-gnu %-cygwin %-cygnus,$(MACHINE)),)
116   RCFILES_APP := $(wildcard $(SUBDIR_APP)/res/*.rc)
117   OBJECTS_APP += $(patsubst $(SUBDIR_APP)/res/%.rc,$(SUBDIR_APP)/obj/%.rsrc.o,$(RCFILES_APP))
118 endif
119
120 # ---------------------------------------------------------------------------
121 # Targets
122 # ---------------------------------------------------------------------------
123
124 .PHONY: all clean
125
126 all: $(OUTPATH_APP)
127
128 $(OUTPATH_APP): $(OBJECTS_APP) $(OUTPATH_LIB)
129         @mkdir -p $(@D)
130         $(CC) $(CFLAGS) $^ -o $@ $(LDFLGS)
131
132 $(OUTPATH_LIB): $(OBJECTS_LIB)
133         @mkdir -p $(@D)
134         $(AR) rcs $@ $^
135
136 $(SUBDIR_APP)/obj/%$(CONFIG).o: $(SUBDIR_APP)/src/%.c
137         @mkdir -p $(@D)
138         $(CC) $(CFLAGS) -c $< -o $@
139
140 $(SUBDIR_APP)/obj/%.rsrc.o: $(SUBDIR_APP)/res/%.rc
141         @mkdir -p $(@D)
142         windres -o $@ $<
143
144 $(SUBDIR_LIB)/obj/%$(CONFIG).o: $(SUBDIR_LIB)/src/%.c
145         @mkdir -p $(@D)
146         $(CC) $(CFLAGS) -c $< -o $@
147
148 clean:
149         $(RM) $(SUBDIR_APP)/obj/*.o $(SUBDIR_APP)/obj/*.gcda $(SUBDIR_APP)/lib/*.a $(SUBDIR_APP)/bin/*$(EXE_SUFFIX)
150         $(RM) $(SUBDIR_LIB)/obj/*.o $(SUBDIR_LIB)/obj/*.gcda $(SUBDIR_LIB)/lib/*.a $(SUBDIR_LIB)/bin/*$(EXE_SUFFIX)