-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMakefunctions
103 lines (84 loc) · 5.08 KB
/
Makefunctions
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
# Functions used by the individual Build files.
#
# Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
#
# Licensed under the Universal Permissive License v 1.0 as shown at
# http://oss.oracle.com/licenses/upl.
#
# Licensed under the GNU General Public License (GPL), version 2. See the file
# COPYING in the top level of this tree.
# A variable expanding to the directory of the currently included makefile
# fragment.
current-dir = $(dir $(lastword $(MAKEFILE_LIST)))
# Functions to generate the name of an object file and source file for a
# given primary.
# Syntax: $(call *-name,primary,filename)
obj-name = $(addprefix $(objdir)/$(subst /,-,$($(1)_DIR))-,$(subst /,-,$(2:.c=.o)))
src-name = $(addprefix $($(1)_DIR),$(2))
# If 'verbose' is not set, this echoes a description and truncated target (or
# other identifier) for this rule.
# Syntax: $(call describe-target,description,optional names)
define describe-target
$(if $(filter-out $(verbose),no),,$(foreach name,$(2),printf '%s: %s\n' '$(1)' '$(name)';))
endef
# Describe an installation target. All paths on the 'names' are stripped.
# Syntax: $(call describe-install-target,directory,names)
define describe-install-target
$(call describe-target,INSTALL,$(addprefix $(1)/,$(notdir $(2))))
endef
# Rule to build a C source file.
# Syntax: $(call cc-template,primary,filename-without-dir,optional flags)
cc-dependencies = -MP -MMD -MT $(obj-name)
define cc-template
$(obj-name): $(src-name) $(foreach dep,$($(1)_SRCDEPS),$(call src-name,$(1),$(dep))) $(CONFIG_H)
$(call describe-target,CC,$(src-name))
$(CC) $(CPPFLAGS) $($(1)_CPPFLAGS) $(filter-out $($(1)_NOCFLAGS),$(CFLAGS) $($(1)_CFLAGS) $(3)) $(cc-dependencies) -c -o $(obj-name) $(src-name)
endef
# Rule to build a build library, and all its sources.
# Syntax: $(call build-lib-template,primary)
define build-lib-template
$(objdir)/build-$($(1)_TARGET).a: $(foreach source,$($(1)_SOURCES),$(call obj-name,$(1),$(source))) \
$(foreach dep,$(filter-out %.o,$($(1)_DEPS)),$(addprefix $(objdir)/,$(dep))) \
$(foreach dep,$(filter %.o,$($(1)_DEPS)),$(call obj-name,$(1),$(dep)))
$(call describe-target,BUILD-AR,$($(1)_TARGET).a)
$(AR) rc $(objdir)/build-$($(1)_TARGET).a $(foreach source,$($(1)_SOURCES),$(call obj-name,$(1),$(source)))
$(foreach post,$($(1)_POST),$(call $(post),$(objdir)/build-$($(1)_TARGET).a))
$(foreach file,$(filter-out $($(1)_EXPLICIT),$($(1)_SOURCES)),$(eval $(call cc-template,$(1),$(file),-fPIC)))
endef
# Rule to build an installable static library from a set of build libraries.
# Syntax: $(call lib-template,primary)
define lib-template
$(objdir)/$($(1)_TARGET).a: $(foreach build-lib,$($(1)_LIBSOURCES),$(objdir)/build-$($(build-lib)_TARGET).a)
$(call describe-target,AR,$($(1)_TARGET).a)
$(AR) rc $(objdir)/$($(1)_TARGET).a $(foreach build-lib,$($(1)_LIBSOURCES),$(foreach source,$($(build-lib)_SOURCES),$(call obj-name,$(build-lib),$(source))))
$(foreach post,$($(1)_POST),$(call $(post),$(objdir)/$($(1)_TARGET).a))
endef
# Rule to build an installable shared library from a set of build libraries.
# Syntax: $(call shlib-template,primary)
comma := ,
define shlib-template
$(objdir)/$($(1)_TARGET).so $(if $($(1)_VERSION),$(objdir)/$($(1)_TARGET).so.$($(1)_VERSION)) $(if $($(1)_SONAME),$(objdir)/$($(1)_SONAME)): \
$(foreach build-lib,$($(1)_LIBSOURCES),$(objdir)/build-$($(build-lib)_TARGET).a)
$(call describe-target,SHLINK,$(objdir)/$($(1)_TARGET).so)
$(CC) -o $(if $($(1)_VERSION),$(objdir)/$($(1)_TARGET).so.$($(1)_VERSION),$(objdir)/$($(1)_TARGET).so) \
-shared $(LDFLAGS) $(if $($(1)_SONAME),-Wl$(comma)-soname$(comma)$($(1)_SONAME)) \
$(if $($(1)_VERSCRIPT),-Wl$(comma)--version-script=$($(1)_VERSCRIPT)) \
-Wl,--whole-archive $(foreach primary,$(filter-out $($(1)_SECONDARY),$($(1)_LIBSOURCES)),$(objdir)/build-$($(primary)_TARGET).a) -Wl,--no-whole-archive \
$(foreach secondary,$($(1)_SECONDARY),$(objdir)/build-$($(secondary)_TARGET).a) $($(1)_LIBS)
$(if $($(1)_VERSION),ln -sf $($(1)_TARGET).so.$($(1)_VERSION) $(objdir)/$($(1)_TARGET).so)
$(if $($(1)_SONAME),ln -sf $($(1)_TARGET).so.$($(1)_VERSION) $(objdir)/$($(1)_SONAME))
$(foreach post,$($(1)_POST),$(call $(post),$(objdir)/$($(1)_TARGET).so$(if $($(1)_VERSION),.$($(1)_VERSION))))
endef
# Rule to build a binary, and all its sources.
# Syntax: $(call cmd-template,primary)
define cmd-template
$(objdir)/$($(1)_TARGET): $(foreach source,$($(1)_SOURCES),$(call obj-name,$(1),$(source))) \
$(foreach dep,$(filter-out %.o,$($(1)_DEPS)),$(addprefix $(objdir)/,$(dep))) \
$(foreach dep,$(filter %.o,$($(1)_DEPS)),$(call obj-name,$(1),$(dep)))
$(call describe-target,LINK,$($(1)_TARGET))
$(CC) $(filter-out $($(1)_NOCFLAGS),$(CFLAGS) $($(1)_CFLAGS)) $(LDFLAGS) -o $(objdir)/$($(1)_TARGET) \
$(foreach source,$($(1)_SOURCES),$(call obj-name,$(1),$(source))) \
$($(1)_LIBS)
$(foreach post,$($(1)_POST),$(call $(post),$(objdir)/$($(1)_TARGET)))
$(foreach file,$(filter-out $($(1)_EXPLICIT),$($(1)_SOURCES)),$(eval $(call cc-template,$(1),$(file))))
endef