NativeCompilation.gmk revision 1234:d52ae6b38836
1168404Spjd#
2168404Spjd# Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3168404Spjd# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4168404Spjd#
5185029Spjd# This code is free software; you can redistribute it and/or modify it
6185029Spjd# under the terms of the GNU General Public License version 2 only, as
7168404Spjd# published by the Free Software Foundation.  Oracle designates this
8168404Spjd# particular file as subject to the "Classpath" exception as provided
9168404Spjd# by Oracle in the LICENSE file that accompanied this code.
10168404Spjd#
11168404Spjd# This code is distributed in the hope that it will be useful, but WITHOUT
12168404Spjd# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13168404Spjd# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14168404Spjd# version 2 for more details (a copy is included in the LICENSE file that
15168404Spjd# accompanied this code).
16168404Spjd#
17168404Spjd# You should have received a copy of the GNU General Public License version
18168404Spjd# 2 along with this work; if not, write to the Free Software Foundation,
19168404Spjd# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20168404Spjd#
21168404Spjd# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22219089Spjd# or visit www.oracle.com if you need additional information or have any
23339146Smav# questions.
24168404Spjd#
25168404Spjd
26168404Spjd# When you read this source. Remember that $(sort ...) has the side effect
27168404Spjd# of removing duplicates. It is actually this side effect that is
28168404Spjd# desired whenever sort is used below!
29168404Spjd
30168404Spjdifndef _NATIVE_COMPILATION_GMK
31168404Spjd_NATIVE_COMPILATION_GMK := 1
32168404Spjd
33168404Spjdifeq (,$(_MAKEBASE_GMK))
34168404Spjd  $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
35168404Spjdendif
36168404Spjd
37168404Spjd# Extensions of files handled by this macro.
38168404SpjdNATIVE_SOURCE_EXTENSIONS := %.s %.c %.cpp %.m %.mm
39168404Spjd
40168404Spjd# Replaces native source extensions with the object file extension in a string.
41168404Spjd# Param 1: the string containing source file names with extensions
42339146Smav# The surrounding strip is needed to keep additional whitespace out
43168404Spjddefine replace_with_obj_extension
44168404Spjd$(strip \
45168404Spjd  $(foreach extension, $(NATIVE_SOURCE_EXTENSIONS), \
46168404Spjd      $(patsubst $(extension),%$(OBJ_SUFFIX),$(filter $(extension),$1))) \
47168404Spjd)
48168404Spjdendef
49168404Spjd
50168404Spjdifeq ($(OPENJDK_BUILD_OS_ENV), windows.cygwin)
51168404Spjd  UNIX_PATH_PREFIX := /cygdrive
52168404Spjdelse ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys)
53168404Spjd  UNIX_PATH_PREFIX :=
54168404Spjdendif
55168404Spjd
56168404SpjdWINDOWS_SHOWINCLUDE_SED_PATTERN := \
57168404Spjd    -e '/^Note: including file:/!d' \
58168404Spjd    -e 's|Note: including file: *||' \
59168404Spjd    -e 's|\\|/|g' \
60168404Spjd    -e 's|^\([a-zA-Z]\):|$(UNIX_PATH_PREFIX)/\1|g' \
61168404Spjd    -e '/$(subst /,\/,$(TOPDIR))/!d' \
62168404Spjd    -e 's|$$$$| \\|g' \
63168404Spjd    #
64168404Spjd
65168404Spjddefine add_native_source
66168404Spjd  # param 1 = BUILD_MYPACKAGE
67168404Spjd  # parma 2 = the source file name (..../alfa.c or .../beta.cpp)
68168404Spjd  # param 3 = the bin dir that stores all .o (.obj) and .d files.
69185029Spjd  # param 4 = the c flags to the compiler
70185029Spjd  # param 5 = the c compiler
71185029Spjd  # param 6 = the c++ flags to the compiler
72185029Spjd  # param 7 = the c++ compiler
73168404Spjd  # param 8 = the objc compiler
74185029Spjd  # param 9 = the flags to the assembler
75168404Spjd
76168404Spjd  ifneq (,$$(filter %.c,$2))
77168404Spjd    # Compile as a C file
78168404Spjd    $1_$2_FLAGS=$4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$(<F)"' -c
79168404Spjd    $1_$2_COMP=$5
80168404Spjd    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
81168404Spjd  else ifneq (,$$(filter %.m,$2))
82168404Spjd    # Compile as a objective-c file
83168404Spjd    $1_$2_FLAGS=-x objective-c $4 $$($1_$(notdir $2)_CFLAGS) -DTHIS_FILE='"$$(<F)"' -c
84168404Spjd    $1_$2_COMP=$8
85168404Spjd    $1_$2_DEP_FLAG:=$(C_FLAG_DEPS)
86168404Spjd  else ifneq (,$$(filter %.s,$2))
87168404Spjd    # Compile as assembler file
88168404Spjd    $1_$2_FLAGS=$9 -DTHIS_FILE='"$$(<F)"'
89168404Spjd    $1_$2_COMP=$(AS)
90168404Spjd    $1_$2_DEP_FLAG:=
91168404Spjd  else ifneq (,$$(filter %.cpp,$2)$$(filter %.mm,$2))
92168404Spjd    # Compile as a C++ file
93168404Spjd    $1_$2_FLAGS=$6 $$($1_$(notdir $2)_CXXFLAGS) -DTHIS_FILE='"$$(<F)"' -c
94168404Spjd    $1_$2_COMP=$7
95168404Spjd    $1_$2_DEP_FLAG:=$(CXX_FLAG_DEPS)
96168404Spjd  else
97168404Spjd    $$(error Internal error in NativeCompilation.gmk: no compiler for file $2)
98168404Spjd  endif
99168404Spjd  # Generate the .o (.obj) file name and place it in the bin dir.
100168404Spjd  $1_$2_OBJ := $3/$$(call replace_with_obj_extension, $$(notdir $2))
101168404Spjd  # Only continue if this object file hasn't been processed already. This lets the first found
102168404Spjd  # source file override any other with the same name.
103168404Spjd  ifeq (,$$(findstring $$($1_$2_OBJ),$$($1_ALL_OBJS)))
104168404Spjd    $1_ALL_OBJS+=$$($1_$2_OBJ)
105168404Spjd    ifeq (,$$(filter %.s,$2))
106168404Spjd      # And this is the dependency file for this obj file.
107168404Spjd      $1_$2_DEP:=$$(patsubst %$(OBJ_SUFFIX),%.d,$$($1_$2_OBJ))
108168404Spjd      # Include previously generated dependency information. (if it exists)
109168404Spjd      -include $$($1_$2_DEP)
110168404Spjd
111168404Spjd      ifeq ($(TOOLCHAIN_TYPE), microsoft)
112168404Spjd        $1_$2_DEBUG_OUT_FLAGS:=-Fd$$(patsubst %$(OBJ_SUFFIX),%.pdb,$$($1_$2_OBJ)) \
113168404Spjd            -Fm$$(patsubst %$(OBJ_SUFFIX),%.map,$$($1_$2_OBJ))
114168404Spjd      endif
115168404Spjd    endif
116168404Spjd
117168404Spjd    $$($1_$2_OBJ) : $2 | $$($1_BUILD_INFO)
118168404Spjd	$(ECHO) $(LOG_INFO) "Compiling $$(notdir $2) (for $$(notdir $$($1_TARGET)))"
119168404Spjd        ifneq ($(TOOLCHAIN_TYPE), microsoft)
120168404Spjd          # The Solaris studio compiler doesn't output the full path to the object file in the
121168404Spjd          # generated deps files. Fixing it with sed. If compiling assembly, don't try this.
122168404Spjd          ifeq ($(TOOLCHAIN_TYPE)$$(filter %.s,$2), solstudio)
123168404Spjd	    $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP).tmp $(CC_OUT_OPTION)$$($1_$2_OBJ) $2
124168404Spjd	    $(SED) 's|^$$(@F):|$$@:|' $$($1_$2_DEP).tmp > $$($1_$2_DEP)
125168404Spjd          else
126168404Spjd	    $$($1_$2_COMP) $$($1_$2_FLAGS) $$($1_$2_DEP_FLAG) $$($1_$2_DEP) $(CC_OUT_OPTION)$$($1_$2_OBJ) $2
127168404Spjd          endif
128168404Spjd        endif
129168404Spjd        # The Visual Studio compiler lacks a feature for generating make dependencies, but by
130168404Spjd        # setting -showIncludes, all included files are printed. These are filtered out and
131168404Spjd        # parsed into make dependences.
132168404Spjd        ifeq ($(TOOLCHAIN_TYPE), microsoft)
133168404Spjd	  ($$($1_$2_COMP) $$($1_$2_FLAGS) -showIncludes $$($1_$2_DEBUG_OUT_FLAGS) \
134168404Spjd	      $(CC_OUT_OPTION)$$($1_$2_OBJ) $2 ; echo $$$$? > $$($1_$2_DEP).exitvalue) \
135168404Spjd	      | $(TEE) $$($1_$2_DEP).raw | $(GREP) -v -e "^Note: including file:" \
136168404Spjd	      -e "^$(notdir $2)$$$$" ; exit `cat $$($1_$2_DEP).exitvalue`
137168404Spjd	  $(RM) $$($1_$2_DEP).exitvalue
138168404Spjd	  ($(ECHO) $$@: \\ \
139168404Spjd	  && $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_$2_DEP).raw) > $$($1_$2_DEP)
140168404Spjd        endif
141168404Spjd  endif
142168404Spjdendef
143168404Spjd
144168404Spjd# Setup make rules for creating a native binary (a shared library or an
145168404Spjd# executable).
146168404Spjd#
147168404Spjd# Parameter 1 is the name of the rule. This name is used as variable prefix,
148168404Spjd# and the targets generated are listed in a variable by that name.
149168404Spjd#
150168404Spjd# Remaining parameters are named arguments. These include:
151168404Spjd#   SRC one or more directory roots to scan for C/C++ files.
152168404Spjd#   LANG C or C++
153168404Spjd#   CFLAGS the compiler flags to be used, used both for C and C++.
154168404Spjd#   CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
155168404Spjd#   LDFLAGS the linker flags to be used, used both for C and C++.
156168404Spjd#   LDFLAGS_SUFFIX the linker flags to be added last on the commandline
157168404Spjd#       typically the libraries linked to.
158168404Spjd#   ARFLAGS the archiver flags to be used
159168404Spjd#   OBJECT_DIR the directory where we store the object files
160168404Spjd#   LIBRARY the resulting library file
161168404Spjd#   PROGRAM the resulting exec file
162219089Spjd#   INCLUDES only pick source from these directories
163219089Spjd#   EXCLUDES do not pick source from these directories
164168404Spjd#   INCLUDE_FILES only compile exactly these files!
165168404Spjd#   EXCLUDE_FILES with these names
166168404Spjd#   EXTRA_FILES List of extra files not in any of the SRC dirs
167168404Spjd#   VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
168168404Spjd#   RC_FLAGS flags for RC.
169168404Spjd#   MAPFILE mapfile
170168404Spjd#   REORDER reorder file
171168404Spjd#   DEBUG_SYMBOLS add debug symbols (if configured on)
172168404Spjd#   CC the compiler to use, default is $(CC)
173168404Spjd#   LDEXE the linker to use for linking executables, default is $(LDEXE)
174168404Spjd#   OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST
175168404Spjddefine SetupNativeCompilation
176168404Spjd  $(if $(27),$(error Internal makefile error: Too many arguments to SetupNativeCompilation, please update NativeCompilation.gmk))
177168404Spjd  $(call EvalDebugWrapper,$(strip $1),$(call SetupNativeCompilationInner,$(strip $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)))
178168404Spjdendef
179168404Spjd
180168404Spjddefine SetupNativeCompilationInner
181168404Spjd  $(foreach i,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, $(if $($i),$1_$(strip $($i)))$(NEWLINE))
182168404Spjd  $(call LogSetupMacroEntry,SetupNativeCompilation($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))
183168404Spjd  $(if $(27),$(error Internal makefile error: Too many arguments to SetupNativeCompilation, please update NativeCompilation.gmk))
184168404Spjd
185168404Spjd  ifneq (,$$($1_BIN))
186168404Spjd    $$(error BIN has been replaced with OBJECT_DIR)
187168404Spjd  endif
188168404Spjd
189168404Spjd  ifneq (,$$($1_LIB))
190168404Spjd    $$(error LIB has been replaced with LIBRARY)
191168404Spjd  endif
192168404Spjd
193168404Spjd  ifneq (,$$($1_EXE))
194168404Spjd    $$(error EXE has been replaced with PROGRAM)
195168404Spjd  endif
196168404Spjd
197185029Spjd  ifneq (,$$($1_LIBRARY))
198185029Spjd    ifeq (,$$($1_OUTPUT_DIR))
199185029Spjd      $$(error LIBRARY requires OUTPUT_DIR)
200168404Spjd    endif
201168404Spjd
202168404Spjd    ifneq ($$($1_LIBRARY),$(basename $$($1_LIBRARY)))
203219089Spjd      $$(error directory of LIBRARY should be specified using OUTPUT_DIR)
204168404Spjd    endif
205168404Spjd
206168404Spjd    ifneq (,$(findstring $(SHARED_LIBRARY_SUFFIX),$$($1_LIBRARY)))
207168404Spjd      $$(error LIBRARY should be specified without SHARED_LIBRARY_SUFFIX: $(SHARED_LIBRARY_SUFFIX))
208168404Spjd    endif
209168404Spjd
210168404Spjd    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_LIBRARY)))
211168404Spjd      $$(error LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
212168404Spjd    endif
213168404Spjd
214168404Spjd    ifeq ($$($1_SUFFIX), )
215168404Spjd      $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
216168404Spjd    endif
217168404Spjd
218168404Spjd    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_LIBRARY)$$($1_SUFFIX)
219168404Spjd    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
220168404Spjd    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_LIBRARY)
221168404Spjd  endif
222168404Spjd
223168404Spjd  ifneq (,$$($1_STATIC_LIBRARY))
224168404Spjd    ifeq (,$$($1_OUTPUT_DIR))
225168404Spjd      $$(error STATIC_LIBRARY requires OUTPUT_DIR)
226168404Spjd    endif
227168404Spjd
228168404Spjd    ifneq ($$($1_STATIC_LIBRARY),$(basename $$($1_STATIC_LIBRARY)))
229168404Spjd      $$(error directory of STATIC_LIBRARY should be specified using OUTPUT_DIR)
230168404Spjd    endif
231168404Spjd
232168404Spjd    ifneq (,$(findstring $(STATIC_LIBRARY_SUFFIX),$$($1_STATIC_LIBRARY)))
233185029Spjd      $$(error STATIC_LIBRARY should be specified without STATIC_LIBRARY_SUFFIX: $(STATIC_LIBRARY_SUFFIX))
234185029Spjd    endif
235185029Spjd
236185029Spjd    ifneq (,$(findstring $(LIBRARY_PREFIX),$$($1_STATIC_LIBRARY)))
237168404Spjd      $$(error STATIC_LIBRARY should be specified without LIBRARY_PREFIX: $(LIBRARY_PREFIX))
238185029Spjd    endif
239185029Spjd
240185029Spjd    ifeq ($$($1_SUFFIX), )
241185029Spjd      $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
242219089Spjd    endif
243185029Spjd
244168404Spjd    $1_BASENAME:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)$$($1_SUFFIX)
245185029Spjd    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
246219089Spjd    $1_NOSUFFIX:=$(LIBRARY_PREFIX)$$($1_STATIC_LIBRARY)
247168404Spjd  endif
248168404Spjd
249185029Spjd  ifneq (,$$($1_PROGRAM))
250168404Spjd    ifeq (,$$($1_OUTPUT_DIR))
251168404Spjd      $$(error PROGRAM requires OUTPUT_DIR)
252168404Spjd    endif
253168404Spjd
254168404Spjd    ifneq ($$($1_PROGRAM),$(basename $$($1_PROGRAM)))
255168404Spjd      $$(error directory of PROGRAM should be specified using OUTPUT_DIR)
256168404Spjd    endif
257168404Spjd
258168404Spjd    ifneq (,$(findstring $(EXE_SUFFIX),$$($1_PROGRAM)))
259168404Spjd      $$(error PROGRAM should be specified without EXE_SUFFIX: $(EXE_SUFFIX))
260168404Spjd    endif
261168404Spjd
262168404Spjd    ifeq ($$($1_SUFFIX), )
263168404Spjd      $1_SUFFIX := $(EXE_SUFFIX)
264168404Spjd    endif
265168404Spjd
266168404Spjd    $1_BASENAME:=$$($1_PROGRAM)$$($1_SUFFIX)
267168404Spjd    $1_TARGET:=$$($1_OUTPUT_DIR)/$$($1_BASENAME)
268168404Spjd    $1_NOSUFFIX:=$$($1_PROGRAM)
269168404Spjd  endif
270168404Spjd
271168404Spjd  ifeq (,$$($1_TARGET))
272168404Spjd    $$(error Neither PROGRAM, LIBRARY nor STATIC_LIBRARY has been specified for SetupNativeCompilation)
273168404Spjd  endif
274168404Spjd
275185029Spjd  ifeq (,$$($1_LANG))
276185029Spjd    $$(error You have to specify LANG for native compilation $1)
277185029Spjd  endif
278168404Spjd  ifeq (C,$$($1_LANG))
279236884Smm    ifeq ($$($1_LDEXE),)
280236884Smm      $1_LDEXE:=$(LDEXE)
281236884Smm    endif
282236884Smm    ifeq ($$($1_LD),)
283236884Smm      $1_LD:=$(LD)
284236884Smm    endif
285236884Smm  else
286236884Smm    ifeq (C++,$$($1_LANG))
287248571Smm      ifeq ($$($1_LD),)
288236884Smm        $1_LD:=$(LDCXX)
289236884Smm      endif
290236884Smm      ifeq ($$($1_LDEXE),)
291236884Smm        $1_LDEXE:=$(LDEXECXX)
292236884Smm      endif
293236884Smm    else
294236884Smm      $$(error Unknown native language $$($1_LANG) for $1)
295236884Smm    endif
296236884Smm  endif
297236884Smm
298236884Smm  ifeq ($$($1_CC),)
299236884Smm    $1_CC:=$(CC)
300236884Smm  endif
301236884Smm  ifeq ($$($1_CXX),)
302236884Smm    $1_CXX:=$(CXX)
303236884Smm  endif
304236884Smm  ifeq ($$($1_OBJC),)
305236884Smm    $1_OBJC:=$(OBJC)
306236884Smm  endif
307236884Smm
308236884Smm  # Make sure the dirs exist.
309236884Smm  $$(eval $$(call MakeDir,$$($1_OBJECT_DIR) $$($1_OUTPUT_DIR)))
310236884Smm  $$(foreach d,$$($1_SRC), $$(if $$(wildcard $$d),,$$(error SRC specified to SetupNativeCompilation $1 contains missing directory $$d)))
311236884Smm
312236884Smm  # Find all files in the source trees. Sort to remove duplicates.
313236884Smm  $1_ALL_SRCS := $$(sort $$(call CacheFind,$$($1_SRC)))
314236884Smm  # Extract the C/C++ files.
315236884Smm  $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
316236884Smm  $1_INCLUDE_FILES:=$$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
317236884Smm  ifneq ($$($1_EXCLUDE_FILES),)
318236884Smm    $1_EXCLUDE_FILES:=$$(addprefix %,$$($1_EXCLUDE_FILES))
319236884Smm  endif
320236884Smm  $1_SRCS := $$(filter-out $$($1_EXCLUDE_FILES),$$(filter $$(NATIVE_SOURCE_EXTENSIONS),$$($1_ALL_SRCS)))
321236884Smm  ifneq (,$$(strip $$($1_INCLUDE_FILES)))
322236884Smm    $1_SRCS := $$(filter $$($1_INCLUDE_FILES),$$($1_SRCS))
323236884Smm  endif
324236884Smm  ifeq (,$$($1_SRCS))
325236884Smm    $$(error No sources found for $1 when looking inside the dirs $$($1_SRC))
326236884Smm  endif
327236884Smm  # There can be only a single bin dir root, no need to foreach over the roots.
328236884Smm  $1_BINS := $$(wildcard $$($1_OBJECT_DIR)/*$(OBJ_SUFFIX))
329236884Smm  # Now we have a list of all c/c++ files to compile: $$($1_SRCS)
330236884Smm  # and we have a list of all existing object files: $$($1_BINS)
331236884Smm
332236884Smm  # Prepend the source/bin path to the filter expressions. Then do the filtering.
333236884Smm  ifneq ($$($1_INCLUDES),)
334236884Smm    $1_SRC_INCLUDES := $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_INCLUDES))))
335236884Smm    $1_SRCS := $$(filter $$($1_SRC_INCLUDES),$$($1_SRCS))
336236884Smm  endif
337236884Smm  ifneq ($$($1_EXCLUDES),)
338236884Smm    $1_SRC_EXCLUDES := $$(addsuffix /%,$$($1_EXCLUDES))
339236884Smm    $1_SRC_EXCLUDES += $$(foreach i,$$($1_SRC),$$(addprefix $$i/,$$(addsuffix /%,$$($1_EXCLUDES))))
340236884Smm    $1_SRCS := $$(filter-out $$($1_SRC_EXCLUDES),$$($1_SRCS))
341236884Smm  endif
342236884Smm
343236884Smm  $1_SRCS += $$($1_EXTRA_FILES)
344236884Smm
345236884Smm  # Calculate the expected output from compiling the sources (sort to remove duplicates. Also provides
346236884Smm  # a reproducable order on the input files to the linker).
347168404Spjd  $1_EXPECTED_OBJS_FILENAMES := $$(call replace_with_obj_extension, $$(notdir $$($1_SRCS)))
348168404Spjd  $1_EXPECTED_OBJS:=$$(sort $$(addprefix $$($1_OBJECT_DIR)/,$$($1_EXPECTED_OBJS_FILENAMES)))
349168404Spjd  # Are there too many object files on disk? Perhaps because some source file was removed?
350168404Spjd  $1_SUPERFLOUS_OBJS:=$$(sort $$(filter-out $$($1_EXPECTED_OBJS),$$($1_BINS)))
351168404Spjd  # Clean out the superfluous object files.
352  ifneq ($$($1_SUPERFLUOUS_OBJS),)
353    $$(shell $(RM) -f $$($1_SUPERFLUOUS_OBJS))
354  endif
355
356  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CFLAGS.
357  $1_EXTRA_CFLAGS:=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CFLAGS_$(OPENJDK_TARGET_OS))
358  ifneq ($(DEBUG_LEVEL),release)
359    # Pickup extra debug dependent variables for CFLAGS
360    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_debug)
361    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
362    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_debug)
363  else
364    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_release)
365    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
366    $1_EXTRA_CFLAGS+=$$($1_CFLAGS_$(OPENJDK_TARGET_OS)_release)
367  endif
368
369  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables for CXXFLAGS.
370  $1_EXTRA_CXXFLAGS:=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_CXXFLAGS_$(OPENJDK_TARGET_OS))
371  ifneq ($(DEBUG_LEVEL),release)
372    # Pickup extra debug dependent variables for CXXFLAGS
373    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_debug)
374    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_debug)
375    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_debug)
376  else
377    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_release)
378    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS_TYPE)_release)
379    $1_EXTRA_CXXFLAGS+=$$($1_CXXFLAGS_$(OPENJDK_TARGET_OS)_release)
380  endif
381
382  ifeq ($$($1_DEBUG_SYMBOLS), true)
383    ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
384      ifdef OPENJDK
385        # Always add debug symbols
386        $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
387        $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
388      else
389        # Programs don't get the debug symbols added in the old build. It's not clear if
390        # this is intentional.
391        ifeq ($$($1_PROGRAM),)
392          $1_EXTRA_CFLAGS+=$(CFLAGS_DEBUG_SYMBOLS)
393          $1_EXTRA_CXXFLAGS+=$(CXXFLAGS_DEBUG_SYMBOLS)
394        endif
395      endif
396    endif
397  endif
398
399  ifeq ($$($1_CXXFLAGS),)
400    $1_CXXFLAGS:=$$($1_CFLAGS)
401  endif
402  ifeq ($$(strip $$($1_EXTRA_CXXFLAGS)),)
403    $1_EXTRA_CXXFLAGS:=$$($1_EXTRA_CFLAGS)
404  endif
405
406  ifneq (,$$($1_REORDER))
407    $1_EXTRA_CFLAGS += $$(C_FLAG_REORDER)
408    $1_EXTRA_CXXFLAGS += $$(CXX_FLAG_REORDER)
409  endif
410
411  ifeq (NONE, $$($1_OPTIMIZATION))
412    $1_EXTRA_CFLAGS += $(C_O_FLAG_NONE)
413    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NONE)
414  else ifeq (LOW, $$($1_OPTIMIZATION))
415    $1_EXTRA_CFLAGS += $(C_O_FLAG_NORM)
416    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_NORM)
417  else ifeq (HIGH, $$($1_OPTIMIZATION))
418    $1_EXTRA_CFLAGS += $(C_O_FLAG_HI)
419    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HI)
420  else ifeq (HIGHEST, $$($1_OPTIMIZATION))
421    $1_EXTRA_CFLAGS += $(C_O_FLAG_HIGHEST)
422    $1_EXTRA_CXXFLAGS += $(CXX_O_FLAG_HIGHEST)
423  else ifneq (, $$($1_OPTIMIZATION))
424    $$(error Unknown value for OPTIMIZATION: $$($1_OPTIMIZATION))
425  endif
426
427  $1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
428
429  # Setup rule for printing progress info when compiling source files.
430  # This is a rough heuristic and may not always print accurate information.
431  $$($1_BUILD_INFO): $$($1_SRCS)
432        ifeq ($$(wildcard $$($1_TARGET)),)
433	  $(ECHO) 'Creating $$($1_BASENAME) from $$(words $$?) file(s)'
434        else
435	  $(ECHO) 'Updating $$($1_BASENAME) from $$(words $$?) file(s)'
436        endif
437	$(TOUCH) $$@
438
439  # Now call add_native_source for each source file we are going to compile.
440  $$(foreach p,$$($1_SRCS), \
441      $$(eval $$(call add_native_source,$1,$$p,$$($1_OBJECT_DIR), \
442          $(SYSROOT_CFLAGS) $$($1_CFLAGS) $$($1_EXTRA_CFLAGS),$$($1_CC), \
443          $(SYSROOT_CFLAGS) $$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS),$$($1_CXX),$$($1_OBJC),$$($1_ASFLAGS))))
444
445  # On windows we need to create a resource file
446  ifeq ($(OPENJDK_TARGET_OS), windows)
447    ifneq (,$$($1_VERSIONINFO_RESOURCE))
448      $1_RES:=$$($1_OBJECT_DIR)/$$($1_BASENAME).res
449      $1_RES_DEP:=$$($1_RES).d
450      -include $$($1_RES_DEP)
451      $$($1_RES): $$($1_VERSIONINFO_RESOURCE)
452		$(ECHO) $(LOG_INFO) "Compiling resource $$(notdir $$($1_VERSIONINFO_RESOURCE)) (for $$(notdir $$($1_TARGET)))"
453		$(RC) $$($1_RC_FLAGS) $(CC_OUT_OPTION)$$@ $$($1_VERSIONINFO_RESOURCE)
454                # Windows RC compiler does not support -showIncludes, so we mis-use CL for this.
455		$(CC) $$($1_RC_FLAGS) -showIncludes -nologo -TC \
456		    $(CC_OUT_OPTION)$$($1_RES_DEP).obj $$($1_VERSIONINFO_RESOURCE) > $$($1_RES_DEP).raw 2>&1 || exit 0
457		($(ECHO) $$($1_RES): \\ \
458		&& $(SED) $(WINDOWS_SHOWINCLUDE_SED_PATTERN) $$($1_RES_DEP).raw) > $$($1_RES_DEP)
459    endif
460    ifneq (,$$($1_MANIFEST))
461      $1_GEN_MANIFEST:=$$($1_OBJECT_DIR)/$$($1_PROGRAM).manifest
462      IMVERSIONVALUE:=$(JDK_MINOR_VERSION).$(JDK_MICRO_VERSION).$(JDK_UPDATE_VERSION).$(COOKED_BUILD_NUMBER)
463      $$($1_GEN_MANIFEST): $$($1_MANIFEST)
464		$(SED) 's%IMVERSION%$$(IMVERSIONVALUE)%g;s%PROGRAM%$$($1_PROGRAM)%g' $$< > $$@
465    endif
466  endif
467
468  # mapfile doesnt seem to be implemented on macosx (yet??)
469  ifneq ($(OPENJDK_TARGET_OS),macosx)
470    ifneq ($(OPENJDK_TARGET_OS),windows)
471      $1_REAL_MAPFILE:=$$($1_MAPFILE)
472      ifneq (,$$($1_REORDER))
473        $1_REAL_MAPFILE:=$$($1_OBJECT_DIR)/mapfile
474
475        $$($1_REAL_MAPFILE) : $$($1_MAPFILE) $$($1_REORDER)
476		$$(MKDIR) -p $$(@D)
477		$$(CP) $$($1_MAPFILE) $$@.tmp
478		$$(SED) -e 's=OUTPUTDIR=$$($1_OBJECT_DIR)=' $$($1_REORDER) >> $$@.tmp
479		$$(MV) $$@.tmp $$@
480      endif
481    endif
482  endif
483
484  # Pickup extra OPENJDK_TARGET_OS_TYPE and/or OPENJDK_TARGET_OS dependent variables
485  # for LDFLAGS and LDFLAGS_SUFFIX
486  $1_EXTRA_LDFLAGS:=$$($1_LDFLAGS_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_$(OPENJDK_TARGET_OS))
487  $1_EXTRA_LDFLAGS_SUFFIX:=$$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS_TYPE)) $$($1_LDFLAGS_SUFFIX_$(OPENJDK_TARGET_OS))
488  ifneq (,$$($1_REAL_MAPFILE))
489    $1_EXTRA_LDFLAGS += $(call SET_SHARED_LIBRARY_MAPFILE,$$($1_REAL_MAPFILE))
490  endif
491
492  # Need to make sure TARGET is first on list
493  $1 := $$($1_TARGET)
494  ifeq ($$($1_STATIC_LIBRARY),)
495    ifeq ($$($1_DEBUG_SYMBOLS), true)
496      ifeq ($(ENABLE_DEBUG_SYMBOLS), true)
497        ifneq ($(OPENJDK_TARGET_OS), macosx) # no MacOS X support yet
498          ifneq ($$($1_OUTPUT_DIR),$$($1_OBJECT_DIR))
499            # The dependency on TARGET is needed on windows for debuginfo files
500            # to be rebuilt properly.
501            $$($1_OUTPUT_DIR)/% : $$($1_OBJECT_DIR)/% $$($1_TARGET)
502		$(CP) $$< $$@
503          endif
504
505          # Generate debuginfo files.
506          ifeq ($(OPENJDK_TARGET_OS), windows)
507            $1_EXTRA_LDFLAGS += "-pdb:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb" \
508                "-map:$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map"
509            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).pdb \
510                $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).map
511
512            # This dependency dance ensures that windows debug info files get rebuilt
513            # properly if deleted.
514            $$($1_TARGET): $$($1_DEBUGINFO_FILES)
515            $$($1_DEBUGINFO_FILES): $$($1_EXPECTED_OBJS)
516
517          else ifeq ($(OPENJDK_TARGET_OS), solaris)
518            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
519            # gobjcopy crashes on "empty" section headers with the SHF_ALLOC flag set.
520            # Use $(FIX_EMPTY_SEC_HDR_FLAGS) to clear the SHF_ALLOC flag (if set) from
521            # empty section headers until a fixed $(OBJCOPY) is available.
522            # An empty section header has sh_addr == 0 and sh_size == 0.
523            # This problem has only been seen on Solaris X64, but we call this tool
524            # on all Solaris builds just in case.
525            #
526            # $(OBJCOPY) --add-gnu-debuglink=... corrupts SUNW_* sections.
527            # Use $(ADD_GNU_DEBUGLINK) until a fixed $(OBJCOPY) is available.
528            $$($1_DEBUGINFO_FILES): $$($1_TARGET) \
529                $(FIX_EMPTY_SEC_HDR_FLAGS) $(ADD_GNU_DEBUGLINK)
530			$(RM) $$@
531			$(FIX_EMPTY_SEC_HDR_FLAGS) $(LOG_INFO) $$<
532			$(OBJCOPY) --only-keep-debug $$< $$@
533			$(CD) $$(@D) && $(ADD_GNU_DEBUGLINK) $(LOG_INFO) $$(@F) $$<
534			$(TOUCH) $$@
535
536          else ifeq ($(OPENJDK_TARGET_OS), linux)
537            $1_DEBUGINFO_FILES := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).debuginfo
538            $$($1_DEBUGINFO_FILES): $$($1_TARGET)
539			$(RM) $$@
540			$(OBJCOPY) --only-keep-debug $$< $$@
541			$(CD) $$(@D) && $(OBJCOPY) --add-gnu-debuglink=$$(@F) $$<
542			$(TOUCH) $$@
543
544          endif # No MacOS X support
545
546          ifeq ($(ZIP_DEBUGINFO_FILES), true)
547            $1_DEBUGINFO_ZIP := $$($1_OBJECT_DIR)/$$($1_NOSUFFIX).diz
548            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_ZIP))
549
550            # The dependency on TARGET is needed on windows for debuginfo files
551            # to be rebuilt properly.
552            $$($1_DEBUGINFO_ZIP): $$($1_DEBUGINFO_FILES) $$($1_TARGET)
553		$(CD) $$($1_OBJECT_DIR) \
554		&& $(ZIP) -q $$@ $$(notdir $$($1_DEBUGINFO_FILES))
555
556          else
557            $1 += $$(subst $$($1_OBJECT_DIR),$$($1_OUTPUT_DIR),$$($1_DEBUGINFO_FILES))
558          endif
559        endif
560      endif # !MacOS X
561    endif # $1_DEBUG_SYMBOLS
562  endif # !STATIC_LIBRARY
563
564  ifneq (,$$($1_LIBRARY))
565    # Generating a dynamic library.
566    $1_EXTRA_LDFLAGS+=$$(call SET_SHARED_LIBRARY_NAME,$$($1_BASENAME))
567    ifeq ($(OPENJDK_TARGET_OS), windows)
568      $1_EXTRA_LDFLAGS+="-implib:$$($1_OBJECT_DIR)/$$($1_LIBRARY).lib"
569    endif
570
571    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
572
573    $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_REAL_MAPFILE)
574	$(ECHO) $(LOG_INFO) "Linking $$($1_BASENAME)"
575	$$($1_LD) $(SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(LD_OUT_OPTION)$$@ \
576	    $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \
577	    $$($1_EXTRA_LDFLAGS_SUFFIX)
578        # Touch target to make sure it has a later time stamp than the debug
579        # symbol files to avoid unnecessary relinking on rebuild.
580        ifeq ($(OPENJDK_TARGET_OS), windows)
581	  $(TOUCH) $$@
582        endif
583
584  endif
585
586  ifneq (,$$($1_STATIC_LIBRARY))
587    # Generating a static library, ie object file archive.
588    $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES)
589	$(ECHO) $(LOG_INFO) "Archiving $$($1_STATIC_LIBRARY)"
590	$(AR) $$($1_ARFLAGS) $(AR_OUT_OPTION)$$($1_TARGET) $$($1_EXPECTED_OBJS) \
591	    $$($1_RES) $$($1_LDFLAGS_SUFFIX) $$($1_EXTRA_LDFLAGS_SUFFIX)
592  endif
593
594  ifneq (,$$($1_PROGRAM))
595    # A executable binary has been specified, setup the target for it.
596    $1_EXTRA_LDFLAGS_SUFFIX += $(GLOBAL_LDFLAGS_SUFFIX)
597
598    $$($1_TARGET) : $$($1_EXPECTED_OBJS) $$($1_RES) $$($1_GEN_MANIFEST)
599	$(ECHO) $(LOG_INFO) "Linking executable $$($1_BASENAME)"
600	$$($1_LDEXE) $(SYSROOT_LDFLAGS) $$($1_LDFLAGS) $$($1_EXTRA_LDFLAGS) $(EXE_OUT_OPTION)$$($1_TARGET) \
601	$$($1_EXPECTED_OBJS) $$($1_RES) $$($1_LDFLAGS_SUFFIX) \
602	$$($1_EXTRA_LDFLAGS_SUFFIX)
603        ifneq (,$$($1_GEN_MANIFEST))
604	  $(MT) -nologo -manifest $$($1_GEN_MANIFEST) -outputresource:$$@;#1
605        endif
606        # This only works if the openjdk_codesign identity is present on the system. Let
607        # silently fail otherwise.
608        ifneq (,$(CODESIGN))
609          ifneq (,$$($1_CODESIGN))
610	    $(CODESIGN) -s openjdk_codesign $$@
611          endif
612        endif
613        # Touch target to make sure it has a later time stamp than the debug
614        # symbol files to avoid unnecessary relinking on rebuild.
615        ifeq ($(OPENJDK_TARGET_OS), windows)
616	  $(TOUCH) $$@
617        endif
618
619  endif
620endef
621
622endif # _NATIVE_COMPILATION_GMK
623