a few more to ignore
[MKUltra.git] / rules
blob4073175988c7f150b665314b872901582fade086
1 # -*- Mode: makefile; -*- #
3 # This file is part of MKUltra - Gene's ultimate make include files.
5 # Copyright © 2016-2017 Gene Hightower <gene@digilicious.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, version 3.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  See the file LICENSE.  If not, see
18 # <http://www.gnu.org/licenses/>.
20 SHELL := /bin/bash
22 uname := $(shell uname)
24 ifneq (,$(findstring CYGWIN_NT,$(uname)))
25   TARG := Win32
26 endif
27 ifneq (,$(findstring MINGW32_NT,$(uname)))
28   TARG := Win32
29 endif
31 TARG ?= $(uname)
33 # Java is "special" in many ways, doesn't use pkg-config: requires
34 # per-platform hacks.
36 JAVA := $(findstring Java,$(USES))
37 USES := $(filter-out Java,$(USES))
39 current_dir := $(dir $(CURDIR)/$(lastword $(MAKEFILE_LIST)))
40 include $(current_dir)defs.$(TARG)
42 ifneq (,$(JAVA))
43 JAVAC ?= $(JAVA_HOME)/bin/javac
44 endif
46 PREFIX ?= /usr/local
47 BINDIR ?= $(PREFIX)/bin
48 LIBDIR ?= $(PREFIX)/lib
49 DATADIR ?= $(PREFIX)/share
51 AR := gcc-ar
53 libfiles := $(foreach x,$(LIBS),$(a_pfx)$(x)$(a_sfx)) $(foreach x,$(LIBS),$(so_pfx)$(x)$(so_sfx))
54 prgfiles := $(foreach x,$(PROGRAMS),$(x)$(exe_sfx))
55 tstfiles := $(foreach x,$(TESTS),$(x)$(exe_sfx))
57 ifeq (clang++,$(notdir $(CXX)))
58 warnings := \
59  -Weverything \
60  -Wformat=2 \
61  -Wno-c++98-compat \
62  -Wno-c++98-compat-pedantic \
63  -Wno-conversion \
64  -Wno-disabled-macro-expansion \
65  -Wno-exit-time-destructors \
66  -Wno-extra-semi \
67  -Wno-global-constructors \
68  -Wno-missing-field-initializers \
69  -Wno-missing-prototypes \
70  -Wno-missing-variable-declarations \
71  -Wno-packed \
72  -Wno-padded \
73  -Wno-shadow \
74  -Wno-sign-conversion \
75  -Wno-undefined-func-template \
76  -Wno-unused-const-variable \
77  -Wno-unused-exception-parameter \
78  -Wno-unused-parameter \
79  -Wno-zero-as-null-pointer-constant \
80  -Wno-zero-length-array
81 warnings_c := -Wall
82 warnings_cpp := $(warnings)
83 else
84 warnings_c := \
85  -Wall \
86  -Wextra \
87  -Wformat=2 \
88  -Wno-missing-field-initializers \
89  -Wno-padded \
90  -Wno-unused-const-variable \
91  -Wno-unused-parameter
92 warnings_cpp := \
93   $(warnings_c) \
94  -Wold-style-cast \
95  -Woverloaded-virtual \
96  -Wuseless-cast
97 endif
99 opt_flags := -O3
101 safty_flags := -fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope
103 visibility_flags := -fvisibility=hidden
105 cstd_flags := -std=c11
106 cxxstd_flags := -std=c++17
108 dep_flags := -MMD
110 lto_flags := -flto
112 CFLAGS   += $(lto_flags)   $(cstd_flags)   $(warnings_c) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
113 CXXFLAGS += $(lto_flags) $(cxxstd_flags) $(warnings_cpp) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
115 LDLIBS   += $(lto_flags) -lstdc++fs
116 LDFLAGS  += $(safty_flags)
118 ifneq (,$(USES))
119 CFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
120 CXXFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
121 LDLIBS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --libs $(USES))
122 endif
124 #############################################################################
126 %.o: %.mm
127         $(CXX) -o $@ -fPIC $(CPPFLAGS) $(CXXFLAGS) -c $<
129 %.o: %.m
130         $(CC) -o $@ -fPIC $(CPPFLAGS) $(CFLAGS) -c $<
132 #############################################################################
134 ifneq (,$(BUNDLE_PREFIX))
135 # Bundle for NeXTSTEP, OPENSTEP, GNUstep, and their lineal descendants
136 # macOS and iOS.
138 bundle_dir = $(BUNDLE_PREFIX)/Contents
139 bundle_bin_dir = $(bundle_dir)/MacOS
140 bundle_lib_dir = $(bundle_dir)/lib
141 bundle_dat_dir = $(bundle_dir)/share
143 bundle_all_dirs = $(bundle_bin_dir) $(bundle_lib_dir) $(bundle_dat_dir)
145 bundle:: all
147 $(bundle_all_dirs)::
148         mkdir -p $@
149 endif
151 #############################################################################
153 all::
154 ifneq (,$(strip $(libfiles)))
155         $(MAKE) $(libfiles)
156 endif
157 ifneq (,$(strip $(prgfiles)))
158         $(MAKE) $(prgfiles)
159 endif
160         @echo done with all
162 check:: all $(tstfiles)
164 install:: all
166 ifneq (,$(libfiles))
167 install:: $(libfiles) $(LIBDIR)
168         cp $(libfiles) $(LIBDIR)
169 ifneq (,$(bundle_lib_dir))
170         cp $(libfiles) $(bundle_lib_dir)
171 endif
172 endif
174 ifneq (,$(prgfiles))
175 install:: $(prgfiles) $(BINDIR)
176         cp $(prgfiles) $(BINDIR)
177 ifneq (,$(bundle_bin_dir))
178         cp $(prgfiles) $(bundle_bin_dir)
179 endif
180 endif
182 ifneq (,$(DATA))
183 install:: $(DATA) $(DATADIR)
184         cp -r $(DATA) $(DATADIR)
185 ifneq (,$(bundle_dat_dir))
186         cp -r $(DATA) $(bundle_dat_dir)
187 endif
188 endif
190 ifneq (,$(tstfiles))
191 tests: all $(tstfiles)
193 install-tests:: all $(tstfiles) $(BINDIR)
194         cp $(tstfiles) $(BINDIR)
195 endif
197 $(BINDIR) $(LIBDIR) $(DATADIR):
198         mkdir -p $@
200 #############################################################################
202 define test_cmd
203 $(1)_STEMS += $(1)
205 check:: $(1)$(exe_sfx)
206         time ./$(1) ;
208 vg:: $(1)$(exe_sfx)
209         valgrind $(VGFLAGS) ./$(1) ;
210 endef
212 $(foreach t,$(TESTS),$(eval $(call test_cmd,$(t))))
214 #############################################################################
216 define link_cmd
217 $(1)_all_stems := $$($(1)_STEMS)
218 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
219 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
221 -include $$($(1)_deps)
224 $(1)$(exe_sfx):: $$($(1)_objs) $$($(1)_EXTRAS)
225         $(CXX) -o $$@ $$^ $(LDFLAGS) $(LDLIBS) -L. $(foreach l,$(LIBS),-l$(l))
227 clean::
228         rm -f $(1)$(exe_sfx) $$($(1)_objs) $$($(1)_deps)
229 endef
231 $(foreach prog,$(PROGRAMS),$(eval $(call link_cmd,$(prog))))
232 $(foreach prog,$(TESTS),$(eval $(call link_cmd,$(prog))))
234 #############################################################################
236 define lib_cmd
237 $(1)_all_stems := $$($(1)_STEMS)
238 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
239 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
241 -include $$($(1)_deps)
243 $(a_pfx)$(1)$(a_sfx): $$($(1)_objs)
244         $(AR) rsv $$@ $$^
246 $(so_pfx)$(1)$(so_sfx): $$($(1)_objs)
247         $(CXX) -shared -o $$@ $$^ $(filter-out -static,$(LDFLAGS)) $(LOADLIBES) $(LDLIBS)
249 clean::
250         rm -f $(so_pfx)$(1)$(so_sfx) $(a_pfx)$(1)$(a_sfx) $$($(1)_objs) $$($(1)_deps)
251 endef
253 $(foreach lib,$(LIBS),$(eval $(call lib_cmd,$(lib))))
255 #############################################################################
257 dump::
258 ifneq (,$(JAVA))
259         @echo "Using JAVA"
260 endif
261         @echo JAVA_HOME == $(JAVA_HOME)
262         @echo MAKEFILE_LIST == $(MAKEFILE_LIST)
263         @echo .INCLUDE_DIRS == $(.INCLUDE_DIRS)
264         @echo USES == $(USES)
265         @echo PROGRAMS == $(PROGRAMS)
266         @echo LIBS == $(LIBS)
267         @echo TESTS == $(TESTS)
268         @echo CFLAGS == $(CFLAGS)
269         @echo CXXFLAGS == $(CXXFLAGS)
270         @echo LDFLAGS  == $(LDFLAGS)
272 dump::
273         @echo libfiles == $(libfiles)
274         @echo prgfiles == $(prgfiles)
275         @echo tstfiles == $(tstfiles)
277 define dump_template
278 dump::
279         @echo
280         @echo $(1)_all_stems == $$($(1)_all_stems)
281         @echo $(1)_objs == $$($(1)_objs)
282         @echo $(1)_deps == $$($(1)_deps)
283 endef
285 $(foreach x,$(PROGRAMS),$(eval $(call dump_template,$(x))))
286 $(foreach x,$(LIBS),$(eval $(call dump_template,$(x))))
287 $(foreach x,$(TESTS),$(eval $(call dump_template,$(x))))
289 .PHONY:: all check dump install install-tests tests all_programs all_libraries