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
51 ifeq (ar,$(notdir $(AR)))
58 ALL_LIBS := $(SLIBS) $(DYLIBS)
60 slibfiles := $(foreach x,$(SLIBS),$(a_pfx)$(x)$(a_sfx))
61 dylibfiles := $(foreach x,$(DYLIBS),$(so_pfx)$(x)$(so_sfx))
63 prgfiles := $(foreach x,$(PROGRAMS),$(x)$(exe_sfx))
64 tstfiles := $(foreach x,$(TESTS),$(x)$(exe_sfx))
66 ifeq (clang++,$(notdir $(CXX)))
72 -Wno-c++98-compat-pedantic \
74 -Wno-disabled-macro-expansion \
75 -Wno-exit-time-destructors \
77 -Wno-global-constructors \
78 -Wno-missing-field-initializers \
79 -Wno-missing-prototypes \
80 -Wno-missing-variable-declarations \
84 -Wno-sign-conversion \
85 -Wno-undefined-func-template \
86 -Wno-unused-const-variable \
87 -Wno-unused-exception-parameter \
88 -Wno-unused-parameter \
89 -Wno-zero-as-null-pointer-constant \
90 -Wno-zero-length-array \
93 warnings_cpp := $(warnings)
100 -Wno-missing-field-initializers \
102 -Wno-unused-const-variable \
103 -Wno-unused-parameter \
108 -Woverloaded-virtual \
112 ##############################################################################
116 safty_flags ?= -fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope
118 visibility_flags ?= -fvisibility=hidden
122 ##############################################################################
124 cstd_flags := -std=c11
125 cxxstd_flags := -std=c++17
129 CFLAGS += $(lto_flags) $(cstd_flags) $(warnings_c) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
130 CXXFLAGS += $(lto_flags) $(cxxstd_flags) $(warnings_cpp) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
132 LDLIBS += $(lto_flags)
133 LDFLAGS += $(safty_flags)
140 CFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
141 CXXFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
142 LDLIBS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --libs $(USES))
145 #############################################################################
148 $(CXX) -o $@ -fPIC $(CPPFLAGS) $(CXXFLAGS) -c $<
151 $(CC) -o $@ -fPIC $(CPPFLAGS) $(CFLAGS) -c $<
153 #############################################################################
155 ifneq (,$(BUNDLE_PREFIX))
156 # Bundle for NeXTSTEP, OPENSTEP, GNUstep, and their lineal descendants
159 bundle_dir = $(BUNDLE_PREFIX)/Contents
160 bundle_bin_dir = $(bundle_dir)/MacOS
161 bundle_lib_dir = $(bundle_dir)/lib
162 bundle_dat_dir = $(bundle_dir)/share
164 bundle_all_dirs = $(bundle_bin_dir) $(bundle_lib_dir) $(bundle_dat_dir)
172 #############################################################################
176 check:: all $(tstfiles)
180 ifneq (,$(slibfiles))
181 install:: $(slibfiles) $(LIBDIR)
182 cp $(slibfiles) $(LIBDIR)
183 ifneq (,$(bundle_lib_dir))
184 cp $(slibfiles) $(bundle_lib_dir)
188 ifneq (,$(dylibfiles))
189 install:: $(dylibfiles) $(LIBDIR)
190 cp $(dylibfiles) $(LIBDIR)
191 ifneq (,$(bundle_lib_dir))
192 cp $(dylibfiles) $(bundle_lib_dir)
197 install:: $(prgfiles) $(BINDIR)
198 cp $(prgfiles) $(BINDIR)
199 ifneq (,$(bundle_bin_dir))
200 cp $(prgfiles) $(bundle_bin_dir)
205 install:: $(DATA) $(DATADIR)
206 cp -r $(DATA) $(DATADIR)
207 ifneq (,$(bundle_dat_dir))
208 cp -r $(DATA) $(bundle_dat_dir)
213 tests: all $(tstfiles)
215 install-tests:: all $(tstfiles) $(BINDIR)
216 cp $(tstfiles) $(BINDIR)
219 $(BINDIR) $(LIBDIR) $(DATADIR):
222 #############################################################################
227 check:: $(1)$(exe_sfx)
231 valgrind $(VGFLAGS) ./$(1) ;
234 $(foreach t,$(TESTS),$(eval $(call test_cmd,$(t))))
236 #############################################################################
239 $(1)_all_stems := $$($(1)_STEMS)
240 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
241 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
243 -include $$($(1)_deps)
245 $(1)$(exe_sfx):: $$($(1)_objs) $$($(1)_EXTRAS) $(slibfiles) $(dylibfiles)
246 $(CXX) -o $$@ $$^ $(LDFLAGS) $(LDLIBS) -L. $(foreach l,$(ALL_LIBS),-l$(l))
249 rm -f $(1)$(exe_sfx) $$($(1)_objs) $$($(1)_deps)
252 $(foreach prog,$(PROGRAMS),$(eval $(call link_cmd,$(prog))))
253 $(foreach prog,$(TESTS),$(eval $(call link_cmd,$(prog))))
255 #############################################################################
258 slib_$(1)_all_stems := $$($(1)_STEMS)
259 slib_$(1)_objs := $$(patsubst %,%$(o_sfx),$$(slib_$(1)_all_stems))
260 slib_$(1)_deps := $$(patsubst %,%.d,$$(slib_$(1)_all_stems))
262 -include $$(slib_$(1)_deps)
264 $(a_pfx)$(1)$(a_sfx): $$(slib_$(1)_objs)
268 rm -f $(a_pfx)$(1)$(a_sfx) $$(slib_$(1)_objs) $$(slib_$(1)_deps)
271 $(foreach slib,$(SLIBS),$(eval $(call slib_cmd,$(slib))))
274 dylib_$(1)_all_stems := $$($(1)_STEMS)
275 dylib_$(1)_objs := $$(patsubst %,%$(o_sfx),$$(dylib_$(1)_all_stems))
276 dylib_$(1)_deps := $$(patsubst %,%.d,$$(dylib_$(1)_all_stems))
278 -include $$(dylib_$(1)_deps)
280 $(so_pfx)$(1)$(so_sfx): $$(dylib_$(1)_objs)
281 $(CXX) -shared -o $$@ $$^ $(filter-out -static,$(LDFLAGS)) $(LOADLIBES) $(LDLIBS)
284 rm -f $(so_pfx)$(1)$(so_sfx) $$(dylib_$(1)_objs) $$(dylib_$(1)_deps)
287 $(foreach dylib,$(DYLIBS),$(eval $(call dylib_cmd,$(dylib))))
289 #############################################################################
295 @echo JAVA_HOME == $(JAVA_HOME)
296 @echo MAKEFILE_LIST == $(MAKEFILE_LIST)
297 @echo .INCLUDE_DIRS == $(.INCLUDE_DIRS)
298 @echo USES == $(USES)
299 @echo PROGRAMS == $(PROGRAMS)
300 @echo LIBS == $(LIBS)
301 @echo SLIBS == $(SLIBS)
302 @echo DYLIBS == $(DYLIBS)
303 @echo TESTS == $(TESTS)
304 @echo CFLAGS == $(CFLAGS)
305 @echo CXXFLAGS == $(CXXFLAGS)
306 @echo LDFLAGS == $(LDFLAGS)
309 @echo slibfiles == $(slibfiles)
310 @echo dylibfiles == $(dylibfiles)
311 @echo prgfiles == $(prgfiles)
312 @echo tstfiles == $(tstfiles)
317 @echo $(1)_all_stems == $$($(1)_all_stems)
318 @echo $(1)_objs == $$($(1)_objs)
319 @echo $(1)_deps == $$($(1)_deps)
322 define slib_dump_template
325 @echo slib_$(1)_all_stems == $$(slib_$(1)_all_stems)
326 @echo slib_$(1)_objs == $$(slib_$(1)_objs)
327 @echo slib_$(1)_deps == $$(slib_$(1)_deps)
330 define dylib_dump_template
333 @echo dylib_$(1)_all_stems == $$(dylib_$(1)_all_stems)
334 @echo dylib_$(1)_objs == $$(dylib_$(1)_objs)
335 @echo dylib_$(1)_deps == $$(dylib_$(1)_deps)
338 $(foreach x,$(PROGRAMS),$(eval $(call dump_template,$(x))))
339 $(foreach x,$(TESTS),$(eval $(call dump_template,$(x))))
340 $(foreach x,$(SLIBS),$(eval $(call slib_dump_template,$(x))))
341 $(foreach x,$(DYLIBS),$(eval $(call dylib_dump_template,$(x))))
343 .PHONY:: all check dump install install-tests tests all_programs all_libraries