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/>.
22 uname := $(shell uname)
24 ifneq (,$(findstring CYGWIN_NT,$(uname)))
27 ifneq (,$(findstring MINGW32_NT,$(uname)))
33 # Java is "special" in many ways, doesn't use pkg-config: requires
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)
43 JAVAC ?= $(JAVA_HOME)/bin/javac
47 BINDIR ?= $(PREFIX)/bin
48 LIBDIR ?= $(PREFIX)/lib
49 DATADIR ?= $(PREFIX)/share
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)))
62 -Wno-c++98-compat-pedantic \
63 -Wno-disabled-macro-expansion \
64 -Wno-exit-time-destructors \
66 -Wno-global-constructors \
67 -Wno-missing-field-initializers \
68 -Wno-missing-prototypes \
72 -Wno-sign-conversion \
73 -Wno-undefined-func-template \
74 -Wno-unused-const-variable \
75 -Wno-unused-exception-parameter \
76 -Wno-unused-parameter \
77 -Wno-zero-length-array
83 -Wno-missing-field-initializers \
85 -Wno-unused-const-variable \
91 -Woverloaded-virtual \
97 safty_flags := -fstack-check -fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope
99 CFLAGS += $(warnings_c) -MMD -fvisibility=hidden $(opt_flags) $(safty_flags)
100 CXXFLAGS += -std=c++17 $(warnings_cpp) -MMD -fvisibility=hidden $(opt_flags) $(safty_flags)
102 # hack for libraries that must be first in the list
103 ldlibs_first := -lasan -lubsan
111 CFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
112 CXXFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
113 LDLIBS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --libs $(USES))
116 #############################################################################
119 $(CXX) -o $@ -fPIC $(CPPFLAGS) $(CXXFLAGS) -c $<
122 $(CC) -o $@ -fPIC $(CPPFLAGS) $(CFLAGS) -c $<
124 #############################################################################
126 ifneq (,$(BUNDLE_PREFIX))
127 # Bundle for NeXTSTEP, OPENSTEP, GNUstep, and their lineal descendants
130 bundle_dir = $(BUNDLE_PREFIX)/Contents
131 bundle_bin_dir = $(bundle_dir)/MacOS
132 bundle_lib_dir = $(bundle_dir)/lib
133 bundle_dat_dir = $(bundle_dir)/share
135 bundle_all_dirs = $(bundle_bin_dir) $(bundle_lib_dir) $(bundle_dat_dir)
143 #############################################################################
146 ifneq (,$(strip $(libfiles)))
149 ifneq (,$(strip $(prgfiles)))
154 check:: all $(tstfiles)
159 install:: $(libfiles) $(LIBDIR)
160 cp $(libfiles) $(LIBDIR)
161 ifneq (,$(bundle_lib_dir))
162 cp $(libfiles) $(bundle_lib_dir)
167 install:: $(prgfiles) $(BINDIR)
168 cp $(prgfiles) $(BINDIR)
169 ifneq (,$(bundle_bin_dir))
170 cp $(prgfiles) $(bundle_bin_dir)
175 install:: $(DATA) $(DATADIR)
176 cp -r $(DATA) $(DATADIR)
177 ifneq (,$(bundle_dat_dir))
178 cp -r $(DATA) $(bundle_dat_dir)
183 tests: all $(tstfiles)
185 install-tests:: all $(tstfiles) $(BINDIR)
186 cp $(tstfiles) $(BINDIR)
189 $(BINDIR) $(LIBDIR) $(DATADIR):
192 #############################################################################
197 check:: $(1)$(exe_sfx)
201 valgrind $(VGFLAGS) ./$(1) ;
204 $(foreach t,$(TESTS),$(eval $(call test_cmd,$(t))))
206 #############################################################################
209 $(1)_all_stems := $$($(1)_STEMS)
210 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
211 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
213 -include $$($(1)_deps)
216 $(1)$(exe_sfx):: $$($(1)_objs) $$($(1)_EXTRAS)
217 $(CXX) -o $$@ $$^ $(LDFLAGS) $(ldlibs_first) $(LDLIBS) -L. $(foreach l,$(LIBS),-l$(l))
220 rm -f $(1)$(exe_sfx) $$($(1)_objs) $$($(1)_deps)
223 $(foreach prog,$(PROGRAMS),$(eval $(call link_cmd,$(prog))))
224 $(foreach prog,$(TESTS),$(eval $(call link_cmd,$(prog))))
226 #############################################################################
229 $(1)_all_stems := $$($(1)_STEMS)
230 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
231 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
233 -include $$($(1)_deps)
235 $(a_pfx)$(1)$(a_sfx): $$($(1)_objs)
238 $(so_pfx)$(1)$(so_sfx): $$($(1)_objs)
239 $(CXX) -shared -o $$@ $$^ $(filter-out -static,$(LDFLAGS)) $(LOADLIBES) $(LDLIBS)
242 rm -f $(so_pfx)$(1)$(so_sfx) $(a_pfx)$(1)$(a_sfx) $$($(1)_objs) $$($(1)_deps)
245 $(foreach lib,$(LIBS),$(eval $(call lib_cmd,$(lib))))
247 #############################################################################
253 @echo JAVA_HOME == $(JAVA_HOME)
254 @echo MAKEFILE_LIST == $(MAKEFILE_LIST)
255 @echo .INCLUDE_DIRS == $(.INCLUDE_DIRS)
256 @echo USES == $(USES)
257 @echo PROGRAMS == $(PROGRAMS)
258 @echo LIBS == $(LIBS)
259 @echo TESTS == $(TESTS)
260 @echo CFLAGS == $(CFLAGS)
261 @echo CXXFLAGS == $(CXXFLAGS)
262 @echo LDFLAGS == $(LDFLAGS)
265 @echo libfiles == $(libfiles)
266 @echo prgfiles == $(prgfiles)
267 @echo tstfiles == $(tstfiles)
272 @echo $(1)_all_stems == $$($(1)_all_stems)
273 @echo $(1)_objs == $$($(1)_objs)
274 @echo $(1)_deps == $$($(1)_deps)
277 $(foreach x,$(PROGRAMS),$(eval $(call dump_template,$(x))))
278 $(foreach x,$(LIBS),$(eval $(call dump_template,$(x))))
279 $(foreach x,$(TESTS),$(eval $(call dump_template,$(x))))
281 .PHONY:: all check dump install install-tests tests all_programs all_libraries