common/makefiles/NativeCompilation.gmk

changeset 425
e1830598f0b7
child 445
efd26e051e50
equal deleted inserted replaced
419:42f275168fa5 425:e1830598f0b7
1 #
2 # Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 #
5 # This code is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License version 2 only, as
7 # published by the Free Software Foundation. Oracle designates this
8 # particular file as subject to the "Classpath" exception as provided
9 # by Oracle in the LICENSE file that accompanied this code.
10 #
11 # This code is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # version 2 for more details (a copy is included in the LICENSE file that
15 # accompanied this code).
16 #
17 # You should have received a copy of the GNU General Public License version
18 # 2 along with this work; if not, write to the Free Software Foundation,
19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 #
21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 # or visit www.oracle.com if you need additional information or have any
23 # questions.
24 #
25
26 # When you read this source. Remember that $(sort ...) has the side effect
27 # of removing duplicates. It is actually this side effect that is
28 # desired whenever sort is used below!
29
30 ifeq (,$(_MAKEBASE_GMK))
31 $(error You must include MakeBase.gmk prior to including JavaCompilation.gmk)
32 endif
33
34 ifeq ($(COMPILER_TYPE),CC)
35 COMPILING_MSG=echo Compiling $1
36 LINKING_MSG=echo Linking $1
37 LINKING_EXE_MSG=echo Linking executable $1
38 ARCHIVING_MSG=echo Archiving $1
39 else
40 COMPILING_MSG=
41 LINKING_MSG=
42 LINKING_EXE_MSG=
43 ARCHIVING_MSG=
44 endif
45
46 define add_native_source
47 # param 1 = BUILD_MYPACKAGE
48 # parma 2 = the source file name (..../alfa.c or .../beta.cpp)
49 # param 3 = the bin dir that stores all .o (.obj) and .d files.
50 # param 4 = the c flags to the compiler
51 # param 5 = the c compiler
52 # param 6 = the c++ flags to the compiler
53 # param 7 = the c++ compiler
54
55 ifneq (,$$(filter %.c,$2))
56 # Compile as a C file
57 $1_$2_FLAGS=$4
58 $1_$2_COMP=$5
59 else
60 # Compile as a C++ file
61 $1_$2_FLAGS=$6
62 $1_$2_COMP=$7
63 endif
64 # Generate the .o (.obj) file name and place it in the bin dir.
65 $1_$2_OBJ:=$3/$$(patsubst %.cpp,%$(OBJ_SUFFIX),$$(patsubst %.c,%$(OBJ_SUFFIX),$$(notdir $2)))
66 # Only continue if this object file hasn't been processed already. This lets the first found
67 # source file override any other with the same name.
68 ifeq (,$$(findstring $$($1_$2_OBJ),$$($1_OBJS_SO_FAR)))
69 $1_OBJS_SO_FAR+=$$($1_$2_OBJ)
70 # And this is the dependency file for this obj file.
71 $1_$2_DEP:=$$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_$2_OBJ))
72 # Include previously generated dependency information. (if it exists)
73 -include $$($1_$2_DEP)
74
75 ifeq ($(COMPILER_TYPE),CL)
76 $1_$2_DEBUG_OUT_FLAGS:=-Fd$$(patsubst %$(OBJ_SUFFIX),%.pdb,$$($1_$2_OBJ)) \
77 -Fm$$(patsubst %$(OBJ_SUFFIX),%.map,$$($1_$2_OBJ))
78 endif
79
80 $$($1_$2_OBJ) : $2
81 ifeq ($(COMPILER_TYPE),CC)
82 $$(call COMPILING_MSG,$$(notdir $2))
83 $$($1_$2_COMP) $$($1_$2_FLAGS) -MMD -MF $$($1_$2_DEP) -c $(CC_OUT_OPTION)$$($1_$2_OBJ) $2
84 endif
85 ifeq ($(COMPILER_TYPE),CL)
86 $$(call COMPILING_MSG,$$(notdir $2))
87 $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEBUG_OUT_FLAGS) -c $(CC_OUT_OPTION)$$($1_$2_OBJ) $2
88 endif
89 endif
90 endef
91
92 define SetupNativeCompilation
93 # param 1 is for example BUILD_MYPACKAGE
94 # param 2,3,4,5,6,7,8 are named args.
95 # SRC one or more directory roots to scan for C/C++ files.
96 # LANG C or C++
97 # CFLAGS the compiler flags to be used, used both for C and C++.
98 # CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
99 # LDFLAGS the linker flags to be used, used both for C and C++.
100 # LDFLAGS_SUFFIX the linker flags to be added last on the commandline
101 # typically the libraries linked to.
102 # ARFLAGS the archiver flags to be used
103 # BIN the directory where we store the object files
104 # LIB the resulting library file
105 # EXE the resulting exec file
106 # INCLUDES only pick source from these directories
107 # EXCLUDES do not pick source from these directories
108 # INCLUDE_FILES only compile exactly these files!
109 # EXCLUDE_FILES with these names
110 # VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
111 # RC_FLAGS flags for RC.
112 $(if $2,$1_$(strip $2))
113 $(if $3,$1_$(strip $3))
114 $(if $4,$1_$(strip $4))
115 $(if $5,$1_$(strip $5))
116 $(if $6,$1_$(strip $6))
117 $(if $7,$1_$(strip $7))
118 $(if $8,$1_$(strip $8))
119 $(if $9,$1_$(strip $9))
120 $(if $(10),$1_$(strip $(10)))
121 $(if $(11),$1_$(strip $(11)))
122 $(if $(12),$1_$(strip $(12)))
123 $(if $(13),$1_$(strip $(13)))
124 $(if $(14),$1_$(strip $(14)))
125 $(if $(15),$1_$(strip $(15)))
126 $(if $(16),$1_$(strip $(16)))
127 $(if $(17),$1_$(strip $(17)))
128 $(if $(18),$1_$(strip $(18)))
129 $(if $(19),$1_$(strip $(19)))
130 $(if $(20),$1_$(strip $(20)))
131
132 ifeq (,$$($1_LANG))
133 $$(error You have to specify LANG for native compilation $1)
134 endif
135 ifeq (C,$$($1_LANG))
136 $1_LD:=$(LD)
137 $1_LDEXE:=$(LDEXE)
138 else
139 ifeq (C++,$$($1_LANG))
140 $1_LD:=$(LDCXX)
141 $1_LDEXE:=$(LDEXECXX)
142 else
143 $$(error Unknown native language $$($1_LANG) for $1)
144 endif
145 endif
146
147 # Make sure the dirs exist.
148 $$(shell $(MKDIR) -p $$($1_SRC) $$($1_BIN) $$(dir $$($1_LIB)) $$(dir $$($1_EXE)))
149 # Find all files in the source trees. Sort to remove duplicates.
150 $1_ALL_SRCS := $$(sort $$(foreach i,$$($1_SRC), $$(shell $(FIND) $$i -type f)))
151 # Extract the C/C++ files.
152 $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
153 $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
154 ifneq ($$($1_EXCLUDE_FILES),)
155 $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES))
156 endif
157 $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter %.c %.cpp,$$($1_ALL_SRCS)))
158 ifneq (,$$(strip $$($1_INCLUDE_FILES)))
159 $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS))
160 endif
161 ifeq (,$$($1_SRCS))
162 $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
163 endif
164 # There can be only a single bin dir root, no need to foreach over the roots.
165 $1_BINS := $$(wildcard $$($1_BIN)/*$(OBJ_SUFFIX))
166 # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
167 # and we have a list of all existing object files: $$($1_BINS)
168
169 # Prepend the source/bin path to the filter expressions. Then do the filtering.
170 ifneq ($$($1_INCLUDES),)
171 $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
172 $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
173 endif
174 ifneq ($$($1_EXCLUDES),)
175 $1_SRC_EXCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
176 $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
177 endif
178
179 # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides
180 # a reproducable order on the input files to the linker).
181 $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_BIN)/,$$(patsubst %.cpp,%$(OBJ_SUFFIX),$$(patsubst %.c,%$(OBJ_SUFFIX),$$(notdir $$($1_SRCS))))))
182 $1 := $$($1_EXPECTED_OBJS)
183 # Are there too many object files on disk? Perhaps because some source file was removed?
184 $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
185 # Clean out the superfluous object files.
186 $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
187
188 # Pickup extra HOST_OS_API and/or PLATFORM dependent variables for CFLAGS.
189 $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(HOST_OS_API)) $$($1_CFLAGS_$(PLATFORM))
190 ifneq ($(DEBUG_LEVEL),release)
191 # Pickup extra debug dependent variables for CFLAGS
192 $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
193 $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(HOST_OS_API)_debug)
194 $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(PLATFORM)_debug)
195 else
196 $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
197 $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(HOST_OS_API)_release)
198 $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(PLATFORM)_release)
199 endif
200
201 # Pickup extra HOST_OS_API and/or PLATFORM dependent variables for CXXFLAGS.
202 $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(HOST_OS_API)) $$($1_CXXFLAGS_$(PLATFORM))
203 ifneq ($(DEBUG_LEVEL),release)
204 # Pickup extra debug dependent variables for CXXFLAGS
205 $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
206 $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(HOST_OS_API)_debug)
207 $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(PLATFORM)_debug)
208 else
209 $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
210 $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(HOST_OS_API)_release)
211 $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(PLATFORM)_release)
212 endif
213
214 ifeq ($$($1_CXXFLAGS),)
215 $1_CXXFLAGS:=$$($1_CFLAGS)
216 endif
217 ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
218 $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
219 endif
220
221 # Now create a list of the packages that are about to compile. Used when sending source
222 # in a batch to the compiler.
223 $$(shell $(RM) $$($1_BIN)/_the.list_of_sources)
224 $$(eval $$(call ListPathsSafelyNow,$1_SRCS,\n, >> $$($1_BIN)/_the.list_of_sources))
225
226 # Now call add_native_source for each source file we are going to compile.
227 $$(foreach p,$$($1_SRCS),\
228 $$(eval $$(call add_native_source,$1,$$p,$$($1_BIN),\
229 $$($1_CFLAGS) $$($1_EXTRA_CFLAGS),$(CC),\
230 $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS),$(CXX))))
231
232 # On windows we need to create a resource file
233 ifeq ($(HOST_OS_API), winapi)
234 ifneq (,$$($1_VERSIONINFO_RESOURCE))
235 ifneq (,$$($1_LIB))
236 ifeq (dynamic,$$(patsubst %$(SHARED_LIBRARY_SUFFIX),dynamic,$$($1_LIB)))
237 $1_RES:=$$(patsubst %$(SHARED_LIBRARY_SUFFIX),%.res,$$($1_LIB))
238 else
239 $1_RES:=$$(patsubst %$(STATIC_LIBRARY_SUFFIX),%.res,$$($1_LIB))
240 endif
241 endif
242 ifneq (,$$($1_EXE))
243 $1_RES:=$$(patsubst %$(EXE_SUFFIX),%.res,$$($1_EXE))
244 endif
245 $$($1_RES): $$($1_VERSIONINFO_RESOURCE)
246 $(RC) $$($1_RC_FLAGS) $(CC_OUT_OPTION)$$@ $$($1_VERSIONINFO_RESOURCE)
247 endif
248 endif
249
250 # Pickup extra HOST_OS_API dependent variables (posix or winapi) and
251 # (linux,solaris,windows,bsd) for LDFLAGS and LDFLAGS_SUFFIX
252 $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(HOST_OS_API)) $$($1_LDFLAGS_$(PLATFORM))
253 $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(HOST_OS_API)) $$($1_LDFLAGS_SUFFIX_$(PLATFORM))
254 ifneq (,$$($1_LIB))
255 ifeq (dynamic,$$(patsubst %$(SHARED_LIBRARY_SUFFIX),dynamic,$$($1_LIB)))
256 # Generating a dynamic library.
257 $1_EXTRA_LDFLAGS+=$$(call SET_SHARED_LIBRARY_NAME,$$(notdir $$($1_LIB)))
258 $$($1_LIB) : $$($1_EXPECTED_OBJS) $$($1_RES)
259 $$(call LINKING_MSG,$$(notdir $$($1_LIB)))
260 $$($1_LD) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(LD_OUT_OPTION)$$($1_LIB) \
261 $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
262 else
263 # Generating a static library, ie object file archive.
264 $$($1_LIB) : $$($1_EXPECTED_OBJS) $$($1_RES)
265 $$(call ARCHIVING_MSG,$$(notdir $$($1_LIB)))
266 $(AR) $$($1_AR_FLAGS) $(AR_OUT_OPTION)$$($1_LIB) $$($1_EXPECTED_OBJS) \
267 $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
268 endif
269 endif
270 ifneq (,$$($1_EXE))
271 # A executable binary has been specified, setup the target for it.
272 $$($1_EXE) : $$($1_EXPECTED_OBJS) $$($1_RES)
273 $$(call LINKING_EXE_MSG,$$(notdir $$($1_EXE)))
274 $$($1_LDEXE) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(EXE_OUT_OPTION)$$($1_EXE) \
275 $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
276 endif
277 endef

mercurial