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 \
64 -Wno-disabled-macro-expansion \
65 -Wno-exit-time-destructors \
67 -Wno-global-constructors \
68 -Wno-missing-field-initializers \
69 -Wno-missing-prototypes \
70 -Wno-missing-variable-declarations \
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
82 warnings_cpp := $(warnings)
88 -Wno-missing-field-initializers \
90 -Wno-unused-const-variable \
95 -Woverloaded-virtual \
99 ##############################################################################
103 safty_flags ?= -fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope
105 visibility_flags ?= -fvisibility=hidden
109 ##############################################################################
111 cstd_flags := -std=c11
112 cxxstd_flags := -std=c++17
116 CFLAGS += $(lto_flags) $(cstd_flags) $(warnings_c) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
117 CXXFLAGS += $(lto_flags) $(cxxstd_flags) $(warnings_cpp) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
119 LDLIBS += $(lto_flags) -lstdc++fs
120 LDFLAGS += $(safty_flags)
123 CFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
124 CXXFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
125 LDLIBS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --libs $(USES))
128 #############################################################################
131 $(CXX) -o $@ -fPIC $(CPPFLAGS) $(CXXFLAGS) -c $<
134 $(CC) -o $@ -fPIC $(CPPFLAGS) $(CFLAGS) -c $<
136 #############################################################################
138 ifneq (,$(BUNDLE_PREFIX))
139 # Bundle for NeXTSTEP, OPENSTEP, GNUstep, and their lineal descendants
142 bundle_dir = $(BUNDLE_PREFIX)/Contents
143 bundle_bin_dir = $(bundle_dir)/MacOS
144 bundle_lib_dir = $(bundle_dir)/lib
145 bundle_dat_dir = $(bundle_dir)/share
147 bundle_all_dirs = $(bundle_bin_dir) $(bundle_lib_dir) $(bundle_dat_dir)
155 #############################################################################
161 check:: all $(tstfiles)
166 install:: $(libfiles) $(LIBDIR)
167 cp $(libfiles) $(LIBDIR)
168 ifneq (,$(bundle_lib_dir))
169 cp $(libfiles) $(bundle_lib_dir)
174 install:: $(prgfiles) $(BINDIR)
175 cp $(prgfiles) $(BINDIR)
176 ifneq (,$(bundle_bin_dir))
177 cp $(prgfiles) $(bundle_bin_dir)
182 install:: $(DATA) $(DATADIR)
183 cp -r $(DATA) $(DATADIR)
184 ifneq (,$(bundle_dat_dir))
185 cp -r $(DATA) $(bundle_dat_dir)
190 tests: all $(tstfiles)
192 install-tests:: all $(tstfiles) $(BINDIR)
193 cp $(tstfiles) $(BINDIR)
196 $(BINDIR) $(LIBDIR) $(DATADIR):
199 #############################################################################
204 check:: $(1)$(exe_sfx)
208 valgrind $(VGFLAGS) ./$(1) ;
211 $(foreach t,$(TESTS),$(eval $(call test_cmd,$(t))))
213 #############################################################################
216 $(1)_all_stems := $$($(1)_STEMS)
217 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
218 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
220 -include $$($(1)_deps)
222 $(1)$(exe_sfx):: $$($(1)_objs) $$($(1)_EXTRAS)
223 $(CXX) -o $$@ $$^ $(LDFLAGS) $(LDLIBS) -L. $(foreach l,$(LIBS),-l$(l))
226 rm -f $(1)$(exe_sfx) $$($(1)_objs) $$($(1)_deps)
229 $(foreach prog,$(PROGRAMS),$(eval $(call link_cmd,$(prog))))
230 $(foreach prog,$(TESTS),$(eval $(call link_cmd,$(prog))))
232 #############################################################################
235 $(1)_all_stems := $$($(1)_STEMS)
236 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
237 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
239 -include $$($(1)_deps)
241 $(a_pfx)$(1)$(a_sfx): $$($(1)_objs)
244 $(so_pfx)$(1)$(so_sfx): $$($(1)_objs)
245 $(CXX) -shared -o $$@ $$^ $(filter-out -static,$(LDFLAGS)) $(LOADLIBES) $(LDLIBS)
248 rm -f $(so_pfx)$(1)$(so_sfx) $(a_pfx)$(1)$(a_sfx) $$($(1)_objs) $$($(1)_deps)
251 $(foreach lib,$(LIBS),$(eval $(call lib_cmd,$(lib))))
253 #############################################################################
259 @echo JAVA_HOME == $(JAVA_HOME)
260 @echo MAKEFILE_LIST == $(MAKEFILE_LIST)
261 @echo .INCLUDE_DIRS == $(.INCLUDE_DIRS)
262 @echo USES == $(USES)
263 @echo PROGRAMS == $(PROGRAMS)
264 @echo LIBS == $(LIBS)
265 @echo TESTS == $(TESTS)
266 @echo CFLAGS == $(CFLAGS)
267 @echo CXXFLAGS == $(CXXFLAGS)
268 @echo LDFLAGS == $(LDFLAGS)
271 @echo libfiles == $(libfiles)
272 @echo prgfiles == $(prgfiles)
273 @echo tstfiles == $(tstfiles)
278 @echo $(1)_all_stems == $$($(1)_all_stems)
279 @echo $(1)_objs == $$($(1)_objs)
280 @echo $(1)_deps == $$($(1)_deps)
283 $(foreach x,$(PROGRAMS),$(eval $(call dump_template,$(x))))
284 $(foreach x,$(LIBS),$(eval $(call dump_template,$(x))))
285 $(foreach x,$(TESTS),$(eval $(call dump_template,$(x))))
287 .PHONY:: all check dump install install-tests tests all_programs all_libraries