-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantimake.mk
executable file
·1493 lines (1193 loc) · 40.8 KB
/
antimake.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/make -f
#
# antimake.mk - automake syntax with GNU Make
#
# Copyright (c) 2011 Marko Kreen
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Goals:
# - Clean user Makefiles, by using automake syntax
# - Clean output during build
# - Optional ties with `autoconf` and `libtool`
# - Automatic dependency tracking
# - Avoid separate build step for Makefiles
# - No extra tools needed except GNU Make
# Usage without autoconf:
# - copy antimake.mk into source dir, then: include antimake.mk
# - copy/link antimake.mk into PATH, then: include $(shell antimake.mk)
#
# Usage with autoconf:
# - Copy to antimake.mk.in at top dir, then process with autoconf
# to antimake.mk and include that one in Makefiles.
#
# - Have config.mak.in that also includes antimake.mk.
# Suggestion: the separate file should include antimake.mk
# using $(abs_top_srcdir) to support separate build dir.
#
# - Include config and antimake.mk separately in user Makefiles
##
## Startup hacks
##
# detect GNU make version, confuse others
$(eval GNUMAKE380=1)
GNUMAKE381=$(or ,$(GNUMAKE380))
define GNUMAKE382 =
$(GNUMAKE381)
endef
# give error of too old
ifeq ($(GNUMAKE381),)
$(error GNU Make 3.81+ required)
endif
# extra targets if this file is executed directly
ifeq ($(words $(MAKEFILE_LIST)), 1)
.PHONY: show-location show-config
# default: print location. For "include $(shell antimake.mk)"-style usage.
show-location:
@echo $(MAKEFILE_LIST)
# show autoconfigurable variables
show-config:
@grep '@[^ ]*@$$' $(MAKEFILE_LIST)
endif
##
## Allow this file to be processed through autoconf
##
#
# to extract autoconfigurable values:
# $ grep '@[^ ]*@$' antimake.mk > config.mk.in
# $ antimake.mk show-config > config.mk.in
#
ifneq ($(filter-out @%,@PACKAGE_NAME@),)
PACKAGE_NAME = @PACKAGE_NAME@
PACKAGE_TARNAME = @PACKAGE_TARNAME@
PACKAGE_VERSION = @PACKAGE_VERSION@
PACKAGE_STRING = @PACKAGE_STRING@
PACKAGE_URL = @PACKAGE_URL@
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
PORTNAME = @PORTNAME@
EXEEXT = @EXEEXT@
HAVE_CC_DEPFLAG = @HAVE_CC_DEPFLAG@
# C language
CC = @CC@
CPP = @CPP@
CPPFLAGS = @CPPFLAGS@
CFLAGS = @CFLAGS@
DEFS = @DEFS@
WFLAGS = @WFLAGS@
# linking
LD = @LD@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
# static and shared libs
AR = @AR@
ARFLAGS = @ARFLAGS@
RANLIB = @RANLIB@
LIBTOOL = @LIBTOOL@
# other tools
SHELL = @SHELL@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_DATA = @INSTALL_DATA@
MKDIR_P = @MKDIR_P@
SED = @SED@
AWK = @AWK@
GREP = @GREP@
EGREP = @EGREP@
STRIP = @STRIP@
# install locations
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
includedir = @includedir@
sbindir = @sbindir@
libexecdir = @libexecdir@
datarootdir = @datarootdir@
datadir = @datadir@
sysconfdir = @sysconfdir@
docdir = @docdir@
mandir = @mandir@
libdir = @libdir@
localedir = @localedir@
pkgdatadir = @pkgdatadir@
pkgconfigdir = @pkgconfigdir@
aclocaldir = @aclocaldir@
# autoconf values for top dir
abs_top_srcdir ?= @abs_top_srcdir@
abs_top_builddir ?= @abs_top_builddir@
nosub_top_srcdir ?= @top_srcdir@
nosub_top_builddir ?= @top_builddir@
endif # end of @xx@ values
##
## In case of missing autoconf values, provide sane defaults
##
PACKAGE_NAME ?= package
PACKAGE_TARNAME ?= $(PACKAGE_NAME)
PACKAGE_VERSION ?= 0.0
PACKAGE_STRING ?= $(PACKAGE_NAME) $(PACKAGE_VERSION)
PACKAGE_URL ?=
PACKAGE_BUGREPORT ?=
PORTNAME ?= unix
EXEEXT ?=
HAVE_CC_DEPFLAG ?= yes
# C language
CC ?= cc
CPP ?= cpp
CPPFLAGS ?=
CFLAGS ?= -O -g
DEFS ?=
# warning flags are keps separately to allow easy override
WFLAGS ?= -Wall
# add them to main flags now
CFLAGS += $(WFLAGS)
# linking
LD ?= ld
LDFLAGS ?=
LIBS ?=
# static and shared libs
LIBTOOL ?= libtool
AR ?= ar
ARFLAGS ?= rcs
ifeq ($(ARFLAGS),rv)
ARFLAGS = rcs
endif
RANLIB ?= ranlib
# other tools
SHELL ?= /bin/sh
INSTALL ?= install
INSTALL_PROGRAM ?= $(INSTALL)
INSTALL_SCRIPT ?= $(INSTALL)
INSTALL_DATA ?= $(INSTALL)
MKDIR_P ?= mkdir -p
SED ?= sed
AWK ?= awk
GREP ?= grep
EGREP ?= grep -E
STRIP ?= strip
# install locations
prefix ?= /usr/local
exec_prefix ?= ${prefix}
bindir ?= ${exec_prefix}/bin
includedir ?= ${prefix}/include
sbindir ?= ${exec_prefix}/sbin
libexecdir ?= ${exec_prefix}/libexec
datarootdir ?= ${prefix}/share
datadir ?= ${datarootdir}
sysconfdir ?= ${prefix}/etc
docdir ?= ${datarootdir}/doc/${PACKAGE_TARNAME}
mandir ?= ${datarootdir}/man
libdir ?= ${exec_prefix}/lib
localedir ?= ${datarootdir}/locale
pkgdatadir ?= ${datarootdir}/${PACKAGE_TARNAME}
pkgconfigdir ?= ${libdir}/pkgconfig
aclocaldir ?= ${datarootdir}/aclocal
# autoconf values for top dir
abs_top_srcdir ?= $(CURDIR)
abs_top_builddir ?= $(CURDIR)
# make sure nosub vals are not empty
ifeq ($(nosub_top_builddir),)
nosub_top_builddir = .
endif
ifeq ($(nosub_top_srcdir),)
nosub_top_srcdir = .
endif
##
## Variables for user makefiles
##
# current subdirectory location from top dir (foo/bar)
SUBLOC ?= .
# subdirectories in current directory
SUBDIRS ?=
# extra files for clean targets
CLEANFILES ?=
DISTCLEANFILES ?=
MAINTAINERCLEANFILES ?=
# Additional flags for Makefile use, to avoid need
# to touch flags coming from autoconf/cmdline
AM_DEFS ?=
AM_CPPFLAGS ?=
AM_CFLAGS ?=
AM_LDFLAGS ?=
AM_LIBTOOLFLAGS ?=
AM_MAKEFLAGS ?=
AM_LIBS ?=
# libusual sources, for embedded usage
USUAL_DIR ?= .
# V=1 -> verbose build
V ?= 0
# turn on function tracing
AM_TRACE ?=
# default formats for 'dist'
AM_DIST_DEFAULT ?= gzip
##
## Non-user-serviceable area
##
# Hacking:
#
# - Uppercase names are simple (late) variables, lowercase names - targets,
# mixedcase - functions that need to be $(call)-ed.
#
# - Minimal amount of shell should be used here.
#
# - Minimal amount of := and $(eval)
#
# - It's useful to indent the expressions for easier understanding.
# Later the indendation needs to be removed, as whitespace is significant for Make.
# Several functions must not add any extra whitespace.
#
# GNU Make features in new versions:
#
# 3.80 - 2002-10-03: base version. $(eval) $(value) $(MAKEFILE_LIST) $(.VARIABLES) $(call fixes)
# 3.81 - 2006-04-01: $(or), $(and), $(lastword), $(abspath), $(realpath), $(info), $(flavor)
# 3.82 - 2010-07-28: private, undefine, define var :=
#
# This file should use only features from 3.80
##
## command helpers
##
CCLD ?= $(CC)
COMPILE ?= $(CC) $(AM_DEFS) $(DEFS) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
LINK ?= $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
AM_AR ?= $(AR) $(ARFLAGS)
LIBTOOLCMD ?= $(LIBTOOL) $(LIBTOOLQ) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS)
RM = rm -f
##
## Internals
##
# varables that can be set per-target with target_VAR
# they appear as AM_foo. [Not supported: COMPILE]
AM_TARGET_VARIABLES += CFLAGS CPPFLAGS LDFLAGS LIBTOOLFLAGS DEFS LIBS
# list of language (rather compiler) names
AM_LANGUAGES += C
AM_BIG_PRIMARIES += LIBRARIES LTLIBRARIES PROGRAMS
AM_SMALL_PRIMARIES += HEADERS SCRIPTS DATA MANS
# list of destinations per primary
AM_DESTINATIONS += bin lib libexec sbin \
data doc include locale man sysconf \
pkgdata pkgconfig aclocal \
noinst EXTRA
# primaries where 'dist' is default
AM_DIST_PRIMARIES += HEADERS
AM_PRIMARIES = $(AM_BIG_PRIMARIES) $(AM_SMALL_PRIMARIES)
# distclean does rm -rf on that
OBJDIR = .objs
# extension for objects
OBJEXT = .o
# extension for static libraries
LIBEXT = .a
# files that need to be converted to objects
AM_SRCEXTS = $(foreach lang,$(AM_LANGUAGES),$(AM_LANG_$(lang)_SRCEXTS))
# target types - big/small: with/without objects
# list of flags, 'noinst' is taken as dest, 'base' is always default
AM_FLAGS = base nobase dist nodist
## configure non-defult target params
AM_PROGRAMS_InstFunc = ProgInstall
AM_LTLIBRARIES_InstFunc = LTLibInstall
AM_LTLIBRARIES_OBJEXT = .lo
AM_SCRIPTS_InstFunc = ScriptInstall
AM_MANS_InstFunc = ManInstall
# files to distribute
am_DISTFILES :=
am_FINAL_DISTFILES = $(sort $(am_DISTFILES))
AM_DIST_BASE = $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)
AM_ALL_TARGETS =
##
## Make dependencies work
##
HAVE_CC_DEPFLAG ?= yes
ifeq ($(HAVE_CC_DEPFLAG),yes)
OBJDEPS = -MD -MP -MT $@ -MF [email protected]
endif
##
## Quiet by default, 'make V=1' shows commands
##
# 1-dir
MkDir = $(MKDIR_P) $(1)
# 1-fmt, 2-args
Printf = printf $(1) $(2)
CTX ?=
ifeq ($(V), 0)
E = @$(call Printf,"%-4s %-8s %s\n","$(CTX)")
Q = @
LIBTOOLQ = --silent
MAKEFLAGS += --no-print-directory
else
E = @true
Q =
LIBTOOLQ = --silent
endif
##
## libtool activation
##
# libtool activates when detects %.lo / %.la pattern
LTCOMPILE = $(if $(filter %.lo,$@),$(LIBTOOLCMD) --mode=compile)
LTLINK = $(if $(filter %.la %.lo,$^),$(LIBTOOLCMD) --mode=link)
LTCLEAN = $(LIBTOOLCMD) --mode=clean
##
## Default setup for C
##
AM_LANG_C_SRCEXTS = .c
define AM_LANG_C_COMPILE
$(E) "CC" $<
$(Q) $(LTCOMPILE) $(COMPILE) $(OBJDEPS) -c -o $@ $<
endef
define AM_LANG_C_LINK
$(E) "CCLD" $@
$(Q) $(LTLINK) $(LINK) $^ $(AM_LIBS) $(LIBS) $(AM_LT_RPATH)
endef
##
## Various other shortcuts
##
define ar_lib
$(E) "AR" $@
$(Q) $(AM_AR) $@ $^
$(E) "RANLIB" $@
$(Q) $(RANLIB) $@
endef
# 1 - dir
define ProgInstall
$(E) "INSTALL" "$< $(1)"
$(Q) $(call MkDir,$(1))
$(Q) $(INSTALL_PROGRAM) $< $(1)
endef
# 1 - dir
define ScriptInstall
$(E) "INSTALL" "$< $(1)"
$(Q) $(call MkDir,$(1))
$(Q) $(INSTALL_SCRIPT) $< $(1)
endef
# 1 - dir
define DataInstall
$(E) "INSTALL" "$< $(1)"
$(Q) $(call MkDir,$(1))
$(Q) $(INSTALL_DATA) $< $(1)
endef
# 1 - dir, add manX subdir
ManInstall = $(call DataInstall,$(1)/man$(call LastWord,$(subst ., ,$<)))
# 1 - dir
define LTLibInstall
$(E) "INSTALL" "$< $(1)"
$(Q) $(call MkDir,$(1))
$(Q) $(LIBTOOLCMD) --mode=install $(INSTALL) $< $(1)
endef
##
## Create .srcext -> .obj mapping for a language
##
# 1-tgt, 2-name, 3-srcext
define LangObjTarget
$(trace3)
$$(OBJDIR)/$(1)/%$(OBJEXT) $$(OBJDIR)/$(1)/%.lo: %$(3)
@$$(call MkDir,$$(dir $$@))
$$(AM_LANG_$(2)_COMPILE)
endef
# 1=tgt, 2=name
define LangSetup
$(trace2)
$(foreach ext,$(AM_LANG_$(2)_SRCEXTS),$(call LangObjTarget,$(1),$(2),$(ext))$(NewLine))
endef
##
## Utility functions
##
# for function debugging, put them at the start of body
ifdef AM_TRACE
trace1=$(warning $0('$1'))
trace2=$(warning $0('$1','$2'))
trace3=$(warning $0('$1','$2','$3'))
trace4=$(warning $0('$1','$2','$3','$4'))
trace5=$(warning $0('$1','$2','$3','$4','$5'))
trace6=$(warning $0('$1','$2','$3','$4','$5','$6'))
trace7=$(warning $0('$1','$2','$3','$4','$5','$6','$7'))
trace8=$(warning $0('$1','$2','$3','$4','$5','$6','$7','$8'))
trace9=$(warning $0('$1','$2','$3','$4','$5','$6','$7','$8','$9'))
endif
# for use inside $(eval)
IFDEF = ifdef
IFEQ = ifeq
IFNEQ = ifneq
ELSE = else
ENDIF = endif
# returns 'true' if $1==$2
Eq = $(if $(1)$(2),$(if $(findstring $(1),$(2)),$(if $(findstring $(2),$(1)),true)),true)
Not = $(if $(1),,true)
Neq = $(call Not,$(call Eq,$(1),$(2)))
# replace [-./] with '_'
CleanName = $(subst /,_,$(subst -,_,$(subst .,_,$(1))))
# return last word from word list
LastWord = $(if $(1),$(word $(words $(1)),$(1)))
Empty =
Space = $(Empty) $(Empty)
# twice to unconfuse syntax hiliters
SQuote = '
SQuote = '
define NewLine
endef
# quote str for shell
ShellQuote = '$(subst $(SQuote),'\$(SQuote)',$(1))'
# replace extensions
# 1-src ext list
# 2-target ext
# 3-source list
ReplaceExts = $(foreach ext,$(1),$(patsubst %$(ext),%$(2),$(filter %$(ext),$(3))))
# objs with objdir from source list (1-cleantgt, 2-src list)
SourceObjs = $(trace1)$(call SourceObjsExt,$(1),$(OBJEXT),$(2))
# objs with objdir from source list
# 1-cleantgt, 2-objext, 3-srcs list
SourceObjsExt = $(addprefix $(call JoinPath,$(OBJDIR),$(1))/, $(call ReplaceExts,$(AM_SRCEXTS),$(2),$(3)))
# dependency files from object files, must match OBJDEPS
DepFiles = $(wildcard $(addsuffix .d,$(1)))
# per-target var override, 1=target, 2=varname
# if foo_VAR exists, expand to:
# build_foo install_foo clean_foo: AM_VAR = $(foo_VAR)
# 1-tgt, 2-var, 3-final
TgtVar2 = $(3): AM_$(2) = $$($(1)_$(2))$(NewLine)
TgtVar = $(if $($(1)_$(2)),$(call TgtVar2,$(1),$(2),$(3)))
# loop TgtVar over AM_TARGET_VARIABLES, 1=target, 2-final
VarOverride = $(foreach var,$(AM_TARGET_VARIABLES),$(call TgtVar,$(1),$(var),$(2)))
# check if actual target (.h, .exe) is nodist based on primary and flags
# 1-prim 2-flags
TargetNoDist = $(strip $(if $(filter nodist,$(2)), \
true, \
$(if $(filter dist,$(2)), \
, \
$(filter-out $(AM_DIST_PRIMARIES),$(1)))))
# return sources that match language
# 1-lang
# 2-sources
LangFiles = $(filter $(addprefix %,$(AM_LANG_$(1)_SRCEXTS)),$(2))
# return list of langs that match sources.
# 1-sources
LangList = $(strip $(foreach lang,$(AM_LANGUAGES),$(if $(call LangFiles,$(lang),$(1)),$(lang))))
# 1-sources
LinkLangList = $(foreach lang,$(call LangList,$(1)),$(if $(AM_LANG_$(lang)_LINK),$(lang)))
# pick linker variable based on sources, fallback to C
# 1-sources
DetectLinkVar = AM_LANG_$(call LastWord,C $(call LinkLangList,$(1)))_LINK
# convert 'foo/bar' -> '../..'
UpDirStep1 = $(subst /, ,$(1))
UpDirStep2 = $(foreach dir,$(call UpDirStep1,$(1)),../)
UpDirStep3 = $(subst / ,/,$(call UpDirStep2,$(1)))
UpDirStep4 = $(patsubst %/,%,$(call UpDirStep3,$(1)))
UpDir = $(if $(filter-out .,$(1)),$(call UpDirStep4,$(1)),.)
#
# AntiMake requires that joining clean names must result in clean names.
#
# Thus:
# JoinPath(.,foo) -> foo
# JoinPath(foo,/abs) -> /abs
# JoinPath(a/b,../c) -> a/c
# JoinPath(a,../../b/c) -> ../b/c
#
# 1-path, 2-last name : foo => . | /foo => / | foo/bar => foo
CutLastName = $(if $(filter $(2),$(1)),.,$(if $(filter /$(2),$(1)),/,$(patsubst %/$(2),%,$(1))))
# 1-path component, remove last elem :
CutLast = $(call CutLastName,$(1),$(lastword $(subst /, ,$(1))))
# 1/2 : actual place where / is put
JoinPathFinal = $(if $(filter /,$(1)),$(1)$(2),$(1)/$(2))
# 1/2 : second starts with ../, remove it and last component of $(1)
JoinPath5 = $(call JoinPath,$(call CutLast,$(1)),$(patsubst ../%,%,$(2)))
# 1/2: check if first ends with ..
JoinPath4 = $(if $(filter .. %/..,$(1)),$(call JoinPathFinal,$(1),$(2)),$(call JoinPath5,$(1),$(2)))
# 1/2 : check if second starts with ..; otherwise join
JoinPath3 = $(if $(filter ../%,$(2)),$(call JoinPath4,$(1),$(2)),$(call JoinPathFinal,$(1),$(2)))
# 1/2 : skips component if '.'
JoinPath2 = $(if $(filter-out .,$(1)),$(if $(filter-out .,$(2)),$(call JoinPath3,$(1),$(2)),$(1)),$(2))
# 1/2 : check if b is absolute, otherwise fix minor problems
JoinPath = $(trace2)$(if $(filter /%,$(2)),$(2),$(call JoinPath2,$(if $(filter /,$(1)),$(1),$(patsubst %/,%,$(1))),$(patsubst ./%,%,$(2))))
##
## Parse target list variables
##
## pick out components from name, call function
# 1-varname, 2-words, 3-func, 4-func arg
# func args: 1-var, 2-prim, 3-dest, 4-flags, 5-arg
ParseName = $(call $(3),$(1),$(filter $(AM_PRIMARIES),$(2)),$(filter $(AM_DESTINATIONS),$(2)),$(filter $(AM_FLAGS),$(2)),$(4))
ForEachList = $(foreach var,$(2),$(call ParseName,$(var),$(subst _, ,$(var)),$(1),$(3)))
## try reconstruct name, if fails, its a random variable
# 1-var, 2-prim,3-dest,4-flags
CheckName = $(if $(call Eq,$(subst _, ,$(1)),$(strip $(4) $(call LastWord,$(3)) $(call LastWord,$(2)))),$(1))
## also check if variable is filled
# 1-var, 2-prim,3-dest,4-flags
CheckNameFull = $(if $(call CheckName,$(1),$(2),$(3),$(4)),$(if $($(1)),$(1)))
##
## Loop over targets in list variables
##
## call function on parsed target
# 1-var, 2-prim, 3-dest, 4-flags, 5-func
# func args: 1-cleantgt, 2-tgt, 3-prim, 4-dest, 5-flags
ForEachTarget2 = $(foreach tgt,$($(1)),$(call $(5),$(call CleanName,$(tgt)),$(tgt),$(2),$(3),$(4)))
## ForEachTarget: call function on all targets in lists
# 1-func, 2- var list
# func args: 1-cleantgt, 2-tgt, 3-prim, 4-dest, 5-flags
ForEachTarget = $(call ForEachList,ForEachTarget2,$(2),$(1))
## EMBED_SUBDIRS relocations
## add subdir to files
# 1-subdir, 2-file list
RelocFiles = $(foreach f,$(2),$(if $(filter -%,$(f)),$(f),$(call JoinPath,$(1),$(f))))
# 1-dir, 2-pfx, 3-full
RelocOneFlag2 = $(2)$(call JoinPath,$(1),$(patsubst $(2)%,%,$(3)))
# 1-dir, 2-flag
RelocOneFlag = $(if $(filter -L%,$(2)), \
$(call RelocOneFlag2,$(1),-L,$(2)), \
$(if $(filter -I%,$(2)), \
$(call RelocOneFlag2,$(1),-I,$(2)), \
$(2)))
## Relocate relative files, relative -I/-L, ignore -*
# 1-dir, 2- flaglist
RelocFlags = $(strip $(if $(filter-out .,$(1)), \
$(foreach flg,$(2),$(call RelocOneFlag,$(1),$(flg))), \
$(2)))
## Separate build dir relocation
## non-local source dir: -Isrc/include -> -Isrc/include -I$(srcdir)/src/include
# 1-srcdir, 2-flag list
FixIncludes = $(strip $(if $(filter-out .,$(1)), \
$(foreach flg,$(2),$(call FixIncludes2,$(1),$(flg))), \
$(2)))
# 1-dir, 2-flg
FixIncludes2 = $(if $(filter -I%,$(2)), \
$(call FixIncludes3,$(1),$(patsubst -I%,%,$(2))), \
$(2))
# 1-dir, 2-orig dir
FixIncludes3 = -I$(2) -I$(call JoinPath,$(srcdir),$(2))
##
## Makefile fragments
##
### fill values
# abs_top_srcdir, abs_top_builddir
# nosub_top_builddir, nosub_top_srcdir
# 1 - subdir
define SetDirs
abs_builddir := $$(call JoinPath,$$(abs_top_builddir),$(1))
abs_srcdir := $$(call JoinPath,$$(abs_top_srcdir),$(1))
top_builddir := $$(call UpDir,$(1))
top_srcdir := $$(call JoinPath,$$(top_builddir),$$(nosub_top_srcdir))
builddir := .
$(IFEQ) ($$(nosub_top_srcdir),$$(nosub_top_builddir))
srcdir := .
$(ELSE)
srcdir := $$(call JoinPath,$$(top_srcdir),$(1))
$(ENDIF)
endef
##
## Embedded subdirs
##
# func args: 1-cleantgt, 2-tgt, 3-prim, 4-dest, 5-flags
define RelocBigTarget
$(trace5)
# move vars:
$(foreach var,$(AM_TARGET_VARIABLES),$(NewLine)$$(am_PFX)_$(1)_$(var) := $$($(1)_$(var)))
# move and relocate
EXTRA_$$(am_PFX)_$(1)_SOURCES := $$(call RelocFiles,$$(am_DIR),$$(EXTRA_$(1)_SOURCES))
$$(am_PFX)_$(1)_SOURCES := $$(call RelocFiles,$$(am_DIR),$$($(1)_SOURCES))
$$(am_PFX)_$(1)_DEPENDENCIES := $$(call RelocFiles,$$(am_DIR),$$($(1)_DEPENDENCIES))
$$(am_PFX)_$(1)_LDADD := $$(call RelocFiles,$$(am_DIR),$$($(1)_LDADD))
$$(am_PFX)_$(1)_LIBADD := $$(call RelocFiles,$$(am_DIR),$$($(1)_LIBADD))
$$(am_PFX)_$(1)_CFLAGS := $$(call RelocFlags,$$(am_DIR),$$($(1)_CFLAGS))
$$(am_PFX)_$(1)_CPPFLAGS := $$(call RelocFlags,$$(am_DIR),$$($(1)_CPPFLAGS))
$$(am_PFX)_$(1)_LDFLAGS := $$(call RelocFlags,$$(am_DIR),$$($(1)_LDFLAGS))
# clean vars
$(1)_SOURCES =
$(1)_LDADD =
$(1)_LIBADD =
$(foreach var,$(AM_TARGET_VARIABLES),$(NewLine)$(1)_$(var) = )
endef
## pick actual func
# func args: 1-cleantgt, 2-tgt, 3-prim, 4-dest, 5-flags
define RelocTarget
$(trace5)
$(if $(filter $(AM_BIG_PRIMARIES),$(3)),$(call RelocBigTarget,$(1),$(2),$(3),$(4),$(5)))
endef
## relocate target list
# func args: 1-var, 2-prim, 3-dest, 4-flags, 5-arg
define RelocTList
$(trace5)
# detect top and subdir target conflict - it's easier to detect
# and error out than to work around the rare case
$(IFNEQ) (,$$(filter $(2),$$(AM_BIG_PRIMARIES)))
$(IFEQ) (.,$$(am_DIR))
am_TOP_NAMES += $$(foreach tgt,$$($(1)),$$(call CleanName,$$(tgt)))
$(ELSE)
$(IFNEQ) (,$$(filter $$(am_TOP_NAMES),$$(foreach tgt,$$($(1)),$$(call CleanName,$$(tgt)))))
$$(error $$(NewLine)$$(NewLine)\
*** Target names used in top Makefile cannot be re-used in embedded Makefiles. $$(NewLine)\
*** The target variables (eg. <tgt>_SOURCES) conflict is not handled yet)
$(ENDIF)
$(ENDIF)
$(ENDIF)
# move value under real_%
$(IFEQ) ($(real_$(1)),)
real_$(1) :=
$(ENDIF)
real_$(1) += $$(call RelocFiles,$$(am_DIR),$$($(1)))
$(1) =
# remember in proper list
$(IFEQ) ($(3),EXTRA)
am_EXTRA_TARGETLISTS += real_$(1)
$(ELSE)
am_TARGETLISTS += real_$(1)
$(ENDIF)
endef
## process included values
# 1-dir, 2-pfx, 3-tlist
define EmbedProcess
$(trace3)
$(IFNEQ) ($$(filter $(1),$$(am_EMBED_DONE)),)
$$(error Double entry in EMBED_SUBDIRS: $(1))
$(ENDIF)
# init local vars
am_DIR := $(1)
am_LOC := $$(call JoinPath,$$(SUBLOC),$(1))
am_PFX := $(2)
am_EMBED_DONE += $(1)
# reloc & save vars
am_DISTFILES += $$(call RelocFiles,$$(am_DIR),$$(EXTRA_DIST))
am_CLEANFILES += $$(call RelocFiles,$$(am_DIR),$$(CLEANFILES))
am_DISTCLEANFILES += $$(call RelocFiles,$$(am_DIR),$$(DISTCLEANFILES))
am_MAINTAINERCLEANFILES += $$(call RelocFiles,$$(am_DIR),$$(MAINTAINERCLEANFILES))
am_EMBED_TODO += $$(call RelocFiles,$$(am_DIR),$$(EMBED_SUBDIRS))
am_SUBDIRS += $$(call RelocFiles,$$(am_DIR),$$(SUBDIRS))
am_DIST_SUBDIRS += $$(call RelocFiles,$$(am_DIR),$$(DIST_SUBDIRS))
# clean vars for new dir
EXTRA_DIST =
CLEANFILES =
DISTCLEANFILES =
MAINTAINERCLEANFILES =
EMBED_SUBDIRS =
SUBDIRS =
DIST_SUBDIRS =
$(call SetDirs,$(call JoinPath,$(SUBLOC),$(1)))
$(call ForEachTarget,RelocTarget,$(3))
$(call ForEachList,RelocTList,$(3))
endef
## read Makefile.am, process it
# 1 - dir
DoEmbed = $(trace1)$(strip \
$(if $(wildcard $(am_srcdir)/$(1)/Makefile.am), \
$(eval include $(am_srcdir)/$(1)/Makefile.am $(NewLine)) \
$(eval $(call EmbedProcess,$(1),$(call CleanName,$(1)),$(AM_NONEXTRA_TLISTS) $(AM_EXTRA_TLISTS))), \
$(error $(SUBLOC)/Makefile failure: $(call JoinPath,$(SUBLOC),$(1)/Makefile.am) not found.)))
##
## Fragments that build targets
##
# Note that variable initialization order is important here
# as some of them will be used immediately.
##
## Install target object
##
# 1=cleantgt,2=rawtgt,3=prim,4=dest,5=flags
define InstallTarget
$(trace5)
$(1)_DEST := $$(if $$($(4)dir),$$($(4)dir),$$(error '$(4)dir' empty))$(if $(filter nobase,$(5)),/$(dir $(2)))
$(1)_InstFunc := $$(if $$(AM_$(3)_InstFunc),$$(AM_$(3)_InstFunc),DataInstall)
# actual installation
.PHONY: install_$(1)
install: install_$(1)
install_$(1): $(2)
$$(call $$($(1)_InstFunc),$$(DESTDIR)$$($(1)_DEST))
# hack to pass -rpath to LTLIBRARIES on build time (1)
$(2): AM_DEST = $$($(1)_DEST)
endef
# hack to pass -rpath to LTLIBRARIES on build time (2)
%.la: AM_LT_RPATH = $(if $(AM_DEST),-rpath $(AM_DEST))
##
## Rules for big target
##
# 1-varname, 2-ifset, 3-ifnotset
IfSet = $(if $(filter-out undefined,$(flavor $(1))),$(2),$(3))
# 1-clean, 2-raw, 3-prim
PROGRAMS_Final = $(if $($(1)_EXT),$(2)$($(1)_EXT),$(2)$(EXEEXT))
# 1-clean, 2-raw, 3-prim
LIBRARIES_Final = $(if $($(1)_EXT),$(2)$($(1)_EXT),$(patsubst %.a,%$(LIBEXT),$(2)))
# calculate target file name
# 1-clean, 2-raw, 3-prim
FinalTargetFile = $(call IfSet,$(3)_Final,$(call $(3)_Final,$(1),$(2),$(3)),$(2)$($(1)_EXT))
# 1-objs
FixObjs = $(patsubst %.a,%$(LIBEXT),$(1))
# 1=cleantgt,2=rawtgt,3=prim,4=dest,5=flags
define BigTargetBuild
$(trace5)
AM_ALL_TARGETS += $(1)
$(1)_ALLSRCS := $$($(1)_SOURCES) $$(EXTRA_$(1)_SOURCES) $$(nodist_$(1)_SOURCES) $$(nodist_EXTRA_$(1)_SOURCES)
# calculate OBJS from SOURCES
$(1)_OBJEXT := $$(if $$(AM_$(3)_OBJEXT),$$(AM_$(3)_OBJEXT),$$(OBJEXT))
$(1)_OBJS := $$(call SourceObjsExt,$(1),$$($(1)_OBJEXT), \
$$($(1)_SOURCES) $$(nodist_$(1)_SOURCES))
$(1)_OBJS_CLEAN := $$($(1)_OBJS)
# include additional objects, move flags to _LIBS
$(IFEQ) ($(3),PROGRAMS)
$(1)_OBJS += $$(filter-out -%,$$($(1)_LDADD))
$(1)_LIBS += $$(filter -%,$$($(1)_LDADD))
$(ELSE)
$(1)_OBJS += $$(filter-out -%,$$($(1)_LIBADD))
$(1)_LIBS += $$(filter -%,$$($(1)_LIBADD))
$(ENDIF)
# autodetect linker, unless given
$(IFEQ) ($($(1)_LINK),)
$(1)_LINKVAR := $$(call DetectLinkVar,$$($(1)_ALLSRCS))
$(ELSE)
$(1)_LINKVAR := $(1)_LINK
$(ENDIF)
# calculate target file name
$(1)_FINAL = $(call FinalTargetFile,$(1),$(2),$(3))
# hook libtool into LTLIBRARIES cleanup
$(IFEQ) ($(3),LTLIBRARIES)
$(1)_RM = $$(LTCLEAN) $$(RM)
$(ELSE)
$(1)_RM = $$(RM)
$(ENDIF)
# fix includes in case of separate build dir
$(1)_CPPFLAGS := $$(call FixIncludes,$$(srcdir),$$($(1)_CPPFLAGS))
$(1)_CFLAGS := $$(call FixIncludes,$$(srcdir),$$($(1)_CFLAGS))
# load dependencies
-include .dummy. $$(call DepFiles, $$($(1)_OBJS))
# actual build, clean & install targets
.PHONY: build_$(1) clean_$(1)
# allow target-specific variables
$$(eval $$(call VarOverride,$(1),$(call FinalTargetFile,$(1),$(2),$(3))))
# build and clean by default, unless flagged EXTRA
$(IFNEQ) ($(4),EXTRA)
all: build_$(1)
$(ENDIF)
clean: clean_$(1)
# _DEPENDENCIES and nodist_SOURCES must exist before build starts.
$$(call FixObjs,$$($(1)_OBJS)): $$($(1)_DEPENDENCIES) $$(nodist_$(1)_SOURCES)
build_$(1): $$($(1)_FINAL)
$$($(1)_FINAL): $$(call FixObjs,$$($(1)_OBJS))
@$$(call MkDir,$$(dir $$@))
$$($(if $(filter LIBRARIES,$(3)),ar_lib,$$($(1)_LINKVAR)))
clean_$(1):
$$(E) "CLEAN" "$$($(1)_FINAL)"
$$(Q) $$($(1)_RM) -- $$($(1)_OBJS_CLEAN) $(if $(call TargetNoDist,$(3),$(5)),$$($(1)_FINAL))
DISTCLEANFILES += $$(nodist_$(1)_SOURCES) $$(nodist_EXTRA_$(1)_SOURCES)
$(foreach lang,$(AM_LANGUAGES),$(call LangSetup,$(1),$(lang)))
endef
# 1=cleantgt,2=rawtgt,3=prim,4=dest,5=flags
define BigTargetDist
am_DISTFILES += $$(filter-out $$(nodist_EXTRA_$(1)_SOURCES) $$(nodist_$(1)_SOURCES),$$($(1)_SOURCES) \
$$(EXTRA_$(1)_SOURCES)) $(if $(call TargetNoDist,$(3),$(5)),,$$($(1)_FINAL))
endef
# 1=cleantgt,2=rawtgt,3=prim,4=dest,5=flags
define MakeBigTarget
$(trace5)
# build if first time
$(IFEQ) ($(filter $(1),$(AM_ALL_TARGETS)),)
$(call BigTargetBuild,$(1),$(2),$(3),$(4),$(5))
$(call BigTargetDist,$(1),$(2),$(3),$(4),$(5))
$(ELSE)
# allow only EXTRA be double
$(IFNEQ) ($(4),EXTRA)
$$(error Target '$2' described listed several times)
$(ENDIF)
$(ENDIF)
# call InstallTarget, for dest != (EXTRA, noinst)
$(IFEQ) ($(filter EXTRA noinst,$(4)),)
$(call InstallTarget,$(1),$$($(1)_FINAL),$(3),$(4),$(5))
$(ENDIF)