summaryrefslogtreecommitdiffstats
path: root/tools/build/common.dir/exe.rules.mk
blob: 3d1a02d0d2f662167bc2a841c94585b9a372828a (plain)
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
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: tools/build/common.dir/exe.rules.mk $
#
# IBM CONFIDENTIAL
#
# EKB Project
#
# COPYRIGHT 2015,2017
# [+] International Business Machines Corp.
#
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.
#
# IBM_PROLOG_END_TAG

# Makefile that defines how we build executables.
#
# Macros:
#     BUILD_EXE - Macro to automatically generate all the recipes for
#                 building an executable.
#     ADD_EXE_OBJ - Macro to add additional objects to an executable.
#     ADD_EXE_SRCDIR - Macro to add additional source dirs to an executable.
#     ADD_EXE_INCDIR - Macro to add additional include dirs to an executable.
# Special variables:
#     $(EXE)_*FLAGS - Extra compile / linker flags for a specific executable.
#     $(EXE)_DEPLIBS - Local modules that this executable depends on.
#     $(EXE)_EXTRALIBS - External shared libs that this executable depends on.
#     $(EXE)_PATH - Optional alternate location to place executable objects.
#     $(EXE)_TARGET - Optional mechanism to select between the 'host' or
#                     'target' (default) cross-compiler set.
# Input:
#     Optional input to delay the running of BUILD_GENERATED macro to a
#     later phase.
#
# BUILD_EXE
#     This macro will automatically generate all the recipes for building an
#     executable.  Requires that the following variables are defined
#     prior to calling this macro:
#         * EXE=name - Give a name to the executable.
#         * OBJS=list of objects - Give a list of .o files required.
#
#     As a result, this macro will define how to build $(MODULE).exe as well
#     as all necessary dependencies.
BUILD_EXE = $(eval $(call __BUILD_EXE, $1))

# Order of operations:
#   * Define default exe object PATH.
#   * Add all OBJ to exe objects.
#   * Add directory of the defining .mk file to the source path for this exe.
#   * Set up LOCAL*FLAGS variables for the exe.
#   * Define dependencies for the exe:
#       - lib$(EXE).a
#       - All $(EXE)_DEPLIB modules.
#       - All $(EXE)_EXTRALIBS shared libraries
#       - Call environment's __CALL_LD macro to link exe.
#   * Define dependencies for the lib$(EXE).a archive.
#       - All lib$(EXE)_OBJS.
#       - Call environment's __CALL_AR macro to create archive.
#   * Add exe and archive to 'clean' target list.
#   * Create helper target for 'make $(EXE)'.
#   * Erase EXE and OBJS variables.
#
# Input:
#     Optional input to delay the running of BUILD_GENERATED macro to a
#     later phase.

define __BUILD_EXE
$(EXE)_PATH ?= $(OBJPATH)/$(EXE)
$(EXE)_LIBPATH ?= $(LIBPATH)
$(call __ADD_EXE_OBJ,$(EXE),$(OBJS))
$(call __ADD_EXE_SRCDIR,$(EXE),$(dir $(lastword $(MAKEFILE_LIST))))

$(EXEPATH)/$(EXE).exe: LOCALCOMMONFLAGS += $$($(EXE)_COMMONFLAGS)
$(EXEPATH)/$(EXE).exe: LOCALCFLAGS += $$($(EXE)_CFLAGS)
$(EXEPATH)/$(EXE).exe: LOCALCXXFLAGS += $$($(EXE)_CXXFLAGS)
$(EXEPATH)/$(EXE).exe: LOCALLDFLAGS += $$($(EXE)_LDFLAGS)
$(EXEPATH)/$(EXE).exe: $$($(EXE)_PATH)/lib$(EXE).a \
	$$(foreach lib,$$($(EXE)_DEPLIBS),$$($(EXE)_LIBPATH)/lib$$(lib).a) \
	$$($(EXE)_EXTRALIBS) $$($(EXE)_DEPS) | $$($(EXE)_DEPSORDER)
$(call __CALL_LD,$$(or $$($(EXE)_HOST),HOST))

$$($(EXE)_PATH)/lib$(EXE).a: $$($(EXE)_OBJS)
$(call __CALL_AR,$$(or $$($(EXE)_HOST),HOST))

$(call __CLEAN_TARGET,$(EXEPATH)/$(EXE).exe)
$(call __CLEAN_TARGET,$$($(EXE)_PATH)/lib$(EXE).a)

.PHONY: $(EXE)
$(EXE) : _BUILD/GEN_TARGETS
		@$$(MAKE) $(EXEPATH)/$(EXE).exe
$(or $(strip $1),EXE)_TARGETS += $(EXEPATH)/$(EXE).exe

EXE:=
OBJS:=

endef

# ADD_EXE_OBJ
#    This macro will add additional objects to an executable's dependency list.
# Usage:
#    $(call ADD_EXE_OBJ,exe,file1.o file2.o)
ADD_EXE_OBJ = $(eval $(call __ADD_EXE_OBJ,$(1),$(2)))
# Order of operations:
#    * Prepend $(EXE)_PATH to the path for the object.
#    * Add object and corresponding .dep file to the 'clean' target list.
#    * Add generated .dep file for header-file dependencies to __DEP_INCLUDES.
define __ADD_EXE_OBJ
$(1)_OBJS += $$(addprefix $$($(1)_PATH)/,$(2))
$(foreach obj,$(2),$(call CLEAN_TARGET,$$($(1)_PATH)/$(obj))
	$(call CLEAN_TARGET,$$($(1)_PATH)/$(obj:.o=.dep)))

__DEP_INCLUDES += $$(wildcard $$(addprefix $$($(1)_PATH)/,$(2:.o=.dep)))
endef

# ADD_EXE_SRCDIR
#     This macro will add additional source paths to an executable.
# Usage:
#    $(call ADD_EXE_SRCDIR,exe,/path1 /path2)
ADD_EXE_SRCDIR = $(eval $(call __ADD_EXE_SRCDIR,$(1),$(2)))
__ADD_EXE_SRCDIR = \
	$(foreach path,$(2),$(call ___ADD_EXE_SRCDIR,$(1),$(2)))
# Order of operations:
#    * Add the path to the include list.
#    * Generate all the possible .C->.o, .c->.o, etc. pattern recipes.
define ___ADD_EXE_SRCDIR
$(call __ADD_EXE_INCDIR,$(1),$(2))
$(call __GENERATE_OBJECTRULES,$(OBJPATH)/$(1),$(2),$$(or $$($(1)_HOST),HOST))
endef

# ADD_EXE_INCDIR
#     This macro will add additional include paths to a module.
ADD_EXE_INCDIR = $(eval $(call __ADD_EXE_INCDIR,$(1),$(2)))
__ADD_EXE_INCDIR = \
	$(foreach path,$(2),$(call ___ADD_EXE_INCDIR,$(1),$(path)))
# Order of operations:
#    * Add path to -iquote COMMON flags for #include "" style include.
#    * Add path to -I COMMON flags for #include <> style include.
#    * Add path to $(exe)_INCDIRS for use by other targets.
define ___ADD_EXE_INCDIR

$(1)_COMMONFLAGS += -iquote$(2)
$(1)_COMMONFLAGS += -I$(2)
$(1)_INCDIRS += $(2)

endef
OpenPOWER on IntegriCloud