install include files
[MKUltra.git] / rules
blob6b4d5a4bf5adfb7253649b66a791251c03494385
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 INCDIR ?= $(PREFIX)/include
49 LIBDIR ?= $(PREFIX)/lib
50 DATADIR ?= $(PREFIX)/share
52 ifeq (ar,$(notdir $(AR)))
53 AR := gcc-ar
54 endif
56 SLIBS += $(LIBS)
57 DYLIBS += $(LIBS)
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)))
68 warnings := \
69  -pedantic \
70  -Weverything \
71  -Wformat=2 \
72  -Wno-c++98-compat \
73  -Wno-c++98-compat-pedantic \
74  -Wno-conversion \
75  -Wno-disabled-macro-expansion \
76  -Wno-exit-time-destructors \
77  -Wno-extra-semi \
78  -Wno-global-constructors \
79  -Wno-missing-field-initializers \
80  -Wno-missing-prototypes \
81  -Wno-missing-variable-declarations \
82  -Wno-packed \
83  -Wno-padded \
84  -Wno-shadow \
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 \
92  -Wshadow
93 warnings_c := -Wall
94 warnings_cpp := $(warnings)
95 else
96 warnings_c := \
97  -pedantic \
98  -Wall \
99  -Wextra \
100  -Wformat=2 \
101  -Wno-missing-field-initializers \
102  -Wno-padded \
103  -Wno-unused-const-variable \
104  -Wno-unused-parameter \
105  -Wshadow
106 warnings_cpp := \
107   $(warnings_c) \
108  -Wold-style-cast \
109  -Woverloaded-virtual \
110  -Wuseless-cast
111 endif
113 ##############################################################################
115 opt_flags ?= -O3
117 safty_flags ?= -fsanitize=address -fsanitize=undefined -fsanitize-address-use-after-scope
119 visibility_flags ?= -fvisibility=hidden
121 lto_flags ?= -flto
123 ##############################################################################
125 cstd_flags := -std=c11
126 cxxstd_flags := -std=c++17
128 dep_flags := -MMD
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)
136 ifeq (Linux,$(TARG))
137 LDLIBS   += -lstdc++fs
138 endif
140 ifneq (,$(USES))
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))
144 endif
146 #############################################################################
148 %.o: %.mm
149         $(CXX) -o $@ -fPIC $(CPPFLAGS) $(CXXFLAGS) -c $<
151 %.o: %.m
152         $(CC) -o $@ -fPIC $(CPPFLAGS) $(CFLAGS) -c $<
154 #############################################################################
156 ifneq (,$(BUNDLE_PREFIX))
157 # Bundle for NeXTSTEP, OPENSTEP, GNUstep, and their lineal descendants
158 # macOS and iOS.
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)
167 bundle:: all
169 $(bundle_all_dirs)::
170         mkdir -p $@
171 endif
173 #############################################################################
175 all:: $(prgfiles)
177 check:: all $(tstfiles)
179 install:: all
181 ifneq (,$(INCLUDES))
182 install:: $(INCLUDES) $(INCDIR)
183         cp $(INCLUDES) $(INCDIR)
184 endif
186 ifneq (,$(slibfiles))
187 install:: $(slibfiles) $(LIBDIR)
188         cp $(slibfiles) $(LIBDIR)
189 ifneq (,$(bundle_lib_dir))
190         cp $(slibfiles) $(bundle_lib_dir)
191 endif
192 endif
194 ifneq (,$(dylibfiles))
195 install:: $(dylibfiles) $(LIBDIR)
196         cp $(dylibfiles) $(LIBDIR)
197 ifneq (,$(bundle_lib_dir))
198         cp $(dylibfiles) $(bundle_lib_dir)
199 endif
200 endif
202 ifneq (,$(prgfiles))
203 install:: $(prgfiles) $(BINDIR)
204         cp $(prgfiles) $(BINDIR)
205 ifneq (,$(bundle_bin_dir))
206         cp $(prgfiles) $(bundle_bin_dir)
207 endif
208 endif
210 ifneq (,$(DATA))
211 install:: $(DATA) $(DATADIR)
212         cp -r $(DATA) $(DATADIR)
213 ifneq (,$(bundle_dat_dir))
214         cp -r $(DATA) $(bundle_dat_dir)
215 endif
216 endif
218 ifneq (,$(tstfiles))
219 tests: all $(tstfiles)
221 install-tests:: all $(tstfiles) $(BINDIR)
222         cp $(tstfiles) $(BINDIR)
223 endif
225 $(BINDIR) $(LIBDIR) $(DATADIR):
226         mkdir -p $@
228 #############################################################################
230 define test_cmd
231 $(1)_STEMS += $(1)
233 check:: $(1)$(exe_sfx)
234         time ./$(1) ;
236 vg:: $(1)$(exe_sfx)
237         valgrind $(VGFLAGS) ./$(1) ;
238 endef
240 $(foreach t,$(TESTS),$(eval $(call test_cmd,$(t))))
242 #############################################################################
244 define link_cmd
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))
254 clean::
255         rm -f $(1)$(exe_sfx) $$($(1)_objs) $$($(1)_deps)
256 endef
258 $(foreach prog,$(PROGRAMS),$(eval $(call link_cmd,$(prog))))
259 $(foreach prog,$(TESTS),$(eval $(call link_cmd,$(prog))))
261 #############################################################################
263 define slib_cmd
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)
271         $(AR) rsv $$@ $$^
273 clean::
274         rm -f $(a_pfx)$(1)$(a_sfx) $$(slib_$(1)_objs) $$(slib_$(1)_deps)
275 endef
277 $(foreach slib,$(SLIBS),$(eval $(call slib_cmd,$(slib))))
279 define dylib_cmd
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)
289 clean::
290         rm -f $(so_pfx)$(1)$(so_sfx) $$(dylib_$(1)_objs) $$(dylib_$(1)_deps)
291 endef
293 $(foreach dylib,$(DYLIBS),$(eval $(call dylib_cmd,$(dylib))))
295 #############################################################################
297 dump::
298 ifneq (,$(JAVA))
299         @echo "Using JAVA"
300 endif
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)
314 dump::
315         @echo slibfiles == $(slibfiles)
316         @echo dylibfiles == $(dylibfiles)
317         @echo prgfiles == $(prgfiles)
318         @echo tstfiles == $(tstfiles)
320 define dump_template
321 dump::
322         @echo
323         @echo $(1)_all_stems == $$($(1)_all_stems)
324         @echo $(1)_objs == $$($(1)_objs)
325         @echo $(1)_deps == $$($(1)_deps)
326 endef
328 define slib_dump_template
329 dump::
330         @echo
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)
334 endef
336 define dylib_dump_template
337 dump::
338         @echo
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)
342 endef
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