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 INCDIR ?= $(PREFIX)/include
49 LIBDIR ?= $(PREFIX)/lib
50 DATADIR ?= $(PREFIX)/share
52 ifeq (ar,$(notdir $(AR)))
59 ALL_LIBS := $(SLIBS) $(DYLIBS)
61 slibfiles := $(foreach x,$(SLIBS),$(a_pfx)$(x)$(a_sfx))
62 dylibfiles := $(foreach x,$(DYLIBS),$(so_pfx)$(x)$(so_sfx))
64 prgfiles := $(foreach x,$(PROGRAMS),$(x)$(exe_sfx))
65 tstfiles := $(foreach x,$(TESTS),$(x)$(exe_sfx))
67 ifeq (clang++,$(notdir $(CXX)))
73 -Wno-c++98-compat-pedantic \
75 -Wno-disabled-macro-expansion \
76 -Wno-exit-time-destructors \
78 -Wno-global-constructors \
79 -Wno-missing-field-initializers \
80 -Wno-missing-prototypes \
81 -Wno-missing-variable-declarations \
85 -Wno-sign-conversion \
86 -Wno-undefined-func-template \
87 -Wno-unused-const-variable \
88 -Wno-unused-exception-parameter \
89 -Wno-unused-parameter \
90 -Wno-zero-as-null-pointer-constant \
91 -Wno-zero-length-array \
94 warnings_cpp := $(warnings)
101 -Wno-missing-field-initializers \
103 -Wno-unused-const-variable \
104 -Wno-unused-parameter \
109 -Woverloaded-virtual \
113 ##############################################################################
117 safty_flags ?= -fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope
119 visibility_flags ?= -fvisibility=hidden
123 ##############################################################################
125 cstd_flags := -std=c11
126 cxxstd_flags := -std=c++17
130 CFLAGS += $(lto_flags) $(cstd_flags) $(warnings_c) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
131 CXXFLAGS += $(lto_flags) $(cxxstd_flags) $(warnings_cpp) $(dep_flags) $(visibility_flags) $(opt_flags) $(safty_flags)
133 LDLIBS += $(lto_flags)
134 LDFLAGS += $(safty_flags)
141 CFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
142 CXXFLAGS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --cflags $(USES))
143 LDLIBS += $(shell PKG_CONFIG_LIB=$(pkg_config_lib) pkg-config $(PKG_CONFIG_FLAGS) --libs $(USES))
146 #############################################################################
149 $(CXX) -o $@ -fPIC $(CPPFLAGS) $(CXXFLAGS) -c $<
152 $(CC) -o $@ -fPIC $(CPPFLAGS) $(CFLAGS) -c $<
154 #############################################################################
156 ifneq (,$(BUNDLE_PREFIX))
157 # Bundle for NeXTSTEP, OPENSTEP, GNUstep, and their lineal descendants
160 bundle_dir = $(BUNDLE_PREFIX)/Contents
161 bundle_bin_dir = $(bundle_dir)/MacOS
162 bundle_lib_dir = $(bundle_dir)/lib
163 bundle_dat_dir = $(bundle_dir)/share
165 bundle_all_dirs = $(bundle_bin_dir) $(bundle_lib_dir) $(bundle_dat_dir)
173 #############################################################################
177 check:: all $(tstfiles)
182 install:: $(INCLUDES) $(INCDIR)
183 cp $(INCLUDES) $(INCDIR)
186 ifneq (,$(slibfiles))
187 install:: $(slibfiles) $(LIBDIR)
188 cp $(slibfiles) $(LIBDIR)
189 ifneq (,$(bundle_lib_dir))
190 cp $(slibfiles) $(bundle_lib_dir)
194 ifneq (,$(dylibfiles))
195 install:: $(dylibfiles) $(LIBDIR)
196 cp $(dylibfiles) $(LIBDIR)
197 ifneq (,$(bundle_lib_dir))
198 cp $(dylibfiles) $(bundle_lib_dir)
203 install:: $(prgfiles) $(BINDIR)
204 cp $(prgfiles) $(BINDIR)
205 ifneq (,$(bundle_bin_dir))
206 cp $(prgfiles) $(bundle_bin_dir)
211 install:: $(DATA) $(DATADIR)
212 cp -r $(DATA) $(DATADIR)
213 ifneq (,$(bundle_dat_dir))
214 cp -r $(DATA) $(bundle_dat_dir)
219 tests: all $(tstfiles)
221 install-tests:: all $(tstfiles) $(BINDIR)
222 cp $(tstfiles) $(BINDIR)
225 $(BINDIR) $(LIBDIR) $(DATADIR):
228 #############################################################################
233 check:: $(1)$(exe_sfx)
237 valgrind $(VGFLAGS) ./$(1) ;
240 $(foreach t,$(TESTS),$(eval $(call test_cmd,$(t))))
242 #############################################################################
245 $(1)_all_stems := $$($(1)_STEMS)
246 $(1)_objs := $$(patsubst %,%$(o_sfx),$$($(1)_all_stems))
247 $(1)_deps := $$(patsubst %,%.d,$$($(1)_all_stems))
249 -include $$($(1)_deps)
251 $(1)$(exe_sfx):: $$($(1)_objs) $$($(1)_EXTRAS) $(slibfiles) $(dylibfiles)
252 $(CXX) -o $$@ $$^ $(LDFLAGS) $(LDLIBS) -L. $(foreach l,$(ALL_LIBS),-l$(l))
255 rm -f $(1)$(exe_sfx) $$($(1)_objs) $$($(1)_deps)
258 $(foreach prog,$(PROGRAMS),$(eval $(call link_cmd,$(prog))))
259 $(foreach prog,$(TESTS),$(eval $(call link_cmd,$(prog))))
261 #############################################################################
264 slib_$(1)_all_stems := $$($(1)_STEMS)
265 slib_$(1)_objs := $$(patsubst %,%$(o_sfx),$$(slib_$(1)_all_stems))
266 slib_$(1)_deps := $$(patsubst %,%.d,$$(slib_$(1)_all_stems))
268 -include $$(slib_$(1)_deps)
270 $(a_pfx)$(1)$(a_sfx): $$(slib_$(1)_objs)
274 rm -f $(a_pfx)$(1)$(a_sfx) $$(slib_$(1)_objs) $$(slib_$(1)_deps)
277 $(foreach slib,$(SLIBS),$(eval $(call slib_cmd,$(slib))))
280 dylib_$(1)_all_stems := $$($(1)_STEMS)
281 dylib_$(1)_objs := $$(patsubst %,%$(o_sfx),$$(dylib_$(1)_all_stems))
282 dylib_$(1)_deps := $$(patsubst %,%.d,$$(dylib_$(1)_all_stems))
284 -include $$(dylib_$(1)_deps)
286 $(so_pfx)$(1)$(so_sfx): $$(dylib_$(1)_objs)
287 $(CXX) -shared -o $$@ $$^ $(filter-out -static,$(LDFLAGS)) $(LOADLIBES) $(LDLIBS)
290 rm -f $(so_pfx)$(1)$(so_sfx) $$(dylib_$(1)_objs) $$(dylib_$(1)_deps)
293 $(foreach dylib,$(DYLIBS),$(eval $(call dylib_cmd,$(dylib))))
295 #############################################################################
301 @echo JAVA_HOME == $(JAVA_HOME)
302 @echo MAKEFILE_LIST == $(MAKEFILE_LIST)
303 @echo .INCLUDE_DIRS == $(.INCLUDE_DIRS)
304 @echo USES == $(USES)
305 @echo PROGRAMS == $(PROGRAMS)
306 @echo LIBS == $(LIBS)
307 @echo SLIBS == $(SLIBS)
308 @echo DYLIBS == $(DYLIBS)
309 @echo TESTS == $(TESTS)
310 @echo CFLAGS == $(CFLAGS)
311 @echo CXXFLAGS == $(CXXFLAGS)
312 @echo LDFLAGS == $(LDFLAGS)
315 @echo slibfiles == $(slibfiles)
316 @echo dylibfiles == $(dylibfiles)
317 @echo prgfiles == $(prgfiles)
318 @echo tstfiles == $(tstfiles)
323 @echo $(1)_all_stems == $$($(1)_all_stems)
324 @echo $(1)_objs == $$($(1)_objs)
325 @echo $(1)_deps == $$($(1)_deps)
328 define slib_dump_template
331 @echo slib_$(1)_all_stems == $$(slib_$(1)_all_stems)
332 @echo slib_$(1)_objs == $$(slib_$(1)_objs)
333 @echo slib_$(1)_deps == $$(slib_$(1)_deps)
336 define dylib_dump_template
339 @echo dylib_$(1)_all_stems == $$(dylib_$(1)_all_stems)
340 @echo dylib_$(1)_objs == $$(dylib_$(1)_objs)
341 @echo dylib_$(1)_deps == $$(dylib_$(1)_deps)
344 $(foreach x,$(PROGRAMS),$(eval $(call dump_template,$(x))))
345 $(foreach x,$(TESTS),$(eval $(call dump_template,$(x))))
346 $(foreach x,$(SLIBS),$(eval $(call slib_dump_template,$(x))))
347 $(foreach x,$(DYLIBS),$(eval $(call dylib_dump_template,$(x))))
349 .PHONY:: all check dump install install-tests tests all_programs all_libraries