1#
2# Copyright (c) 1999, 2013, 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.
8#
9# This code is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12# version 2 for more details (a copy is included in the LICENSE file that
13# accompanied this code).
14#
15# You should have received a copy of the GNU General Public License version
16# 2 along with this work; if not, write to the Free Software Foundation,
17# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18#
19# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20# or visit www.oracle.com if you need additional information or have any
21# questions.
22#
23#
24
25OS_VENDOR = $(shell uname -s)
26
27#------------------------------------------------------------------------
28# CC, CXX & AS
29
30# If a SPEC is not set already, then use these defaults.
31ifeq ($(SPEC),)
32  # When cross-compiling the ALT_COMPILER_PATH points
33  # to the cross-compilation toolset
34  ifdef CROSS_COMPILE_ARCH
35    CXX = $(ALT_COMPILER_PATH)/g++
36    CC  = $(ALT_COMPILER_PATH)/gcc
37    HOSTCXX = g++
38    HOSTCC  = gcc
39  else ifneq ($(OS_VENDOR), Darwin)
40    CXX = g++
41    CC  = gcc
42    HOSTCXX = $(CXX)
43    HOSTCC  = $(CC)
44  endif
45
46  # i486 hotspot requires -mstackrealign on Darwin.
47  # llvm-gcc supports this in Xcode 3.2.6 and 4.0.
48  # gcc-4.0 supports this on earlier versions.
49  # Prefer llvm-gcc where available.
50  ifeq ($(OS_VENDOR), Darwin)
51    ifeq ($(origin CXX), default)
52      CXX = llvm-g++
53    endif
54    ifeq ($(origin CC), default)
55      CC  = llvm-gcc
56    endif
57
58    ifeq ($(ARCH), i486)
59      LLVM_SUPPORTS_STACKREALIGN := $(shell \
60       [ "0"`llvm-gcc -v 2>&1 | grep LLVM | sed -E "s/.*LLVM build ([0-9]+).*/\1/"` -gt "2333" ] \
61       && echo true || echo false)
62
63      ifeq ($(LLVM_SUPPORTS_STACKREALIGN), true)
64        CXX32 ?= llvm-g++
65        CC32  ?= llvm-gcc
66      else
67        CXX32 ?= g++-4.0
68        CC32  ?= gcc-4.0
69      endif
70      CXX = $(CXX32)
71      CC  = $(CC32)
72    endif
73
74    ifeq ($(USE_CLANG), true)
75      CXX = clang++
76      CC  = clang
77    endif
78
79    HOSTCXX = $(CXX)
80    HOSTCC  = $(CC)
81  endif
82
83  AS   = $(CC) -c
84endif
85
86ifeq ($(OS_VENDOR), Darwin)
87  ifeq ($(DSYMUTIL),)
88    DSYMUTIL=dsymutil
89  endif
90endif
91
92ifeq ($(USE_CLANG), true)
93  CC_VER_MAJOR := $(shell $(CC) -v 2>&1 | grep version | sed "s/.*version \([0-9]*\.[0-9]*\).*/\1/" | cut -d'.' -f1)
94  CC_VER_MINOR := $(shell $(CC) -v 2>&1 | grep version | sed "s/.*version \([0-9]*\.[0-9]*\).*/\1/" | cut -d'.' -f2)
95else
96  # -dumpversion in gcc-2.91 shows "egcs-2.91.66". In later version, it only
97  # prints the numbers (e.g. "2.95", "3.2.1")
98  CC_VER_MAJOR := $(shell $(CC) -dumpversion | sed 's/egcs-//' | cut -d'.' -f1)
99  CC_VER_MINOR := $(shell $(CC) -dumpversion | sed 's/egcs-//' | cut -d'.' -f2)
100endif
101
102ifeq ($(USE_CLANG), true)
103  # clang has precompiled headers support by default, but the user can switch
104  # it off by using 'USE_PRECOMPILED_HEADER=0'.
105  ifdef LP64
106    ifeq ($(USE_PRECOMPILED_HEADER),)
107      USE_PRECOMPILED_HEADER=1
108    endif
109  else
110    # We don't support precompiled headers on 32-bit builds because there some files are
111    # compiled with -fPIC while others are compiled without (see 'NONPIC_OBJ_FILES' rules.make)
112    # Clang produces an error if the PCH file was compiled with other options than the actual compilation unit.
113    USE_PRECOMPILED_HEADER=0
114  endif
115
116  ifeq ($(USE_PRECOMPILED_HEADER),1)
117
118    ifndef LP64
119      $(error " Precompiled Headers only supported on 64-bit platforms!")
120    endif
121
122    PRECOMPILED_HEADER_DIR=.
123    PRECOMPILED_HEADER_SRC=$(GAMMADIR)/src/share/vm/precompiled/precompiled.hpp
124    PRECOMPILED_HEADER=$(PRECOMPILED_HEADER_DIR)/precompiled.hpp.pch
125
126    PCH_FLAG = -include precompiled.hpp
127    PCH_FLAG/DEFAULT = $(PCH_FLAG)
128    PCH_FLAG/NO_PCH = -DNO_PCH
129    PCH_FLAG/BY_FILE = $(PCH_FLAG/$@)$(PCH_FLAG/DEFAULT$(PCH_FLAG/$@))
130
131    VM_PCH_FLAG/LIBJVM = $(PCH_FLAG/BY_FILE)
132    VM_PCH_FLAG/AOUT =
133    VM_PCH_FLAG = $(VM_PCH_FLAG/$(LINK_INTO))
134
135    # We only use precompiled headers for the JVM build
136    CFLAGS += $(VM_PCH_FLAG)
137
138    # The following files are compiled at various optimization
139    # levels due to optimization issues encountered at the
140    # 'OPT_CFLAGS_DEFAULT' level. The Clang compiler issues a compile
141    # time error if there is an optimization level specification
142    # skew between the PCH file and the C++ file.  Especially if the
143    # PCH file is compiled at a higher optimization level than
144    # the C++ file.  One solution might be to prepare extra optimization
145    # level specific PCH files for the opt build and use them here, but
146    # it's probably not worth the effort as long as only a few files
147    # need this special handling.
148    PCH_FLAG/sharedRuntimeTrig.o = $(PCH_FLAG/NO_PCH)
149    PCH_FLAG/sharedRuntimeTrans.o = $(PCH_FLAG/NO_PCH)
150
151    ifeq ($(shell expr $(CC_VER_MAJOR) = 4 \& $(CC_VER_MINOR) = 2), 1)
152      PCH_FLAG/loopTransform.o = $(PCH_FLAG/NO_PCH)
153      PCH_FLAG/unsafe.o = $(PCH_FLAG/NO_PCH)
154    endif
155    ifeq ($(shell expr $(CC_VER_MAJOR) = 6 \& $(CC_VER_MINOR) = 0), 1)
156      PCH_FLAG/unsafe.o = $(PCH_FLAG/NO_PCH)
157    endif
158    ifeq ($(shell expr $(CC_VER_MAJOR) = 10 \| $(CC_VER_MAJOR) = 11), 1)
159      ifeq ($(BUILDARCH), i486)
160        PCH_FLAG/bitMap.o = $(PCH_FLAG/NO_PCH)
161      endif
162    endif
163  endif
164else # ($(USE_CLANG), true)
165  # check for precompiled headers support
166  ifneq "$(shell expr \( $(CC_VER_MAJOR) \> 3 \) \| \( \( $(CC_VER_MAJOR) = 3 \) \& \( $(CC_VER_MINOR) \>= 4 \) \))" "0"
167    # Allow the user to turn off precompiled headers from the command line.
168    ifneq ($(USE_PRECOMPILED_HEADER),0)
169      PRECOMPILED_HEADER_DIR=.
170      PRECOMPILED_HEADER_SRC=$(GAMMADIR)/src/share/vm/precompiled/precompiled.hpp
171      PRECOMPILED_HEADER=$(PRECOMPILED_HEADER_DIR)/precompiled.hpp.gch
172    endif
173  endif
174endif
175
176# -DDONT_USE_PRECOMPILED_HEADER will exclude all includes in precompiled.hpp.
177ifeq ($(USE_PRECOMPILED_HEADER),0)
178  CFLAGS += -DDONT_USE_PRECOMPILED_HEADER
179endif
180
181# Allow for where third party packages are located
182CFLAGS+= -DPACKAGE_PATH=\"$(PACKAGE_PATH)\"
183
184#------------------------------------------------------------------------
185# Compiler flags
186
187# position-independent code
188PICFLAG = -fPIC
189
190VM_PICFLAG/LIBJVM = $(PICFLAG)
191VM_PICFLAG/AOUT   =
192VM_PICFLAG        = $(VM_PICFLAG/$(LINK_INTO))
193
194ifeq ($(JVM_VARIANT_ZERO), true)
195  CFLAGS += $(LIBFFI_CFLAGS)
196endif
197ifeq ($(JVM_VARIANT_ZEROSHARK), true)
198  CFLAGS += $(LIBFFI_CFLAGS)
199  CFLAGS += $(LLVM_CFLAGS)
200endif
201CFLAGS += $(VM_PICFLAG)
202CFLAGS += -fno-rtti
203CFLAGS += -fno-exceptions
204ifeq ($(USE_CLANG),)
205  CFLAGS += -pthread
206  CFLAGS += -fcheck-new
207  # version 4 and above support fvisibility=hidden (matches jni_x86.h file)
208  # except 4.1.2 gives pointless warnings that can't be disabled (afaik)
209  ifneq "$(shell expr \( $(CC_VER_MAJOR) \> 4 \) \| \( \( $(CC_VER_MAJOR) = 4 \) \& \( $(CC_VER_MINOR) \>= 3 \) \))" "0"
210    CFLAGS += -fvisibility=hidden
211  endif
212else
213  ifneq ($(OS_VENDOR), Darwin)
214    CFLAGS += -pthread
215  endif
216  CFLAGS += -fvisibility=hidden
217endif
218
219ifeq ($(USE_CLANG), true)
220  STACK_ALIGNMENT_OPT = -mno-omit-leaf-frame-pointer
221  ifeq ($(OS_VENDOR), Darwin)
222    # Before Clang 3.1, we had to pass the stack alignment specification directly to llvm with the help of '-mllvm'
223    # Starting with version 3.1, Clang understands the '-mstack-alignment' (and rejects '-mllvm -stack-alignment')
224    ifneq "$(shell expr \( $(CC_VER_MAJOR) \> 3 \) \| \( \( $(CC_VER_MAJOR) = 3 \) \& \( $(CC_VER_MINOR) \>= 1 \) \))" "0"
225      STACK_ALIGNMENT_OPT += -mstack-alignment=16
226    else
227      STACK_ALIGNMENT_OPT += -mllvm -stack-alignment=16
228    endif
229  endif
230endif
231
232ARCHFLAG = $(ARCHFLAG/$(BUILDARCH))
233ARCHFLAG/i486    = -m32 -march=i586
234ARCHFLAG/amd64   = -m64 $(STACK_ALIGNMENT_OPT)
235ARCHFLAG/aarch64 =
236ARCHFLAG/ia64    =
237ARCHFLAG/sparc   = -m32 -mcpu=v9
238ARCHFLAG/sparcv9 = -m64 -mcpu=v9
239ARCHFLAG/zero    = $(ZERO_ARCHFLAG)
240
241# Darwin-specific build flags
242ifeq ($(OS_VENDOR), Darwin)
243  # Ineffecient 16-byte stack re-alignment on Darwin/IA32
244  ARCHFLAG/i486 += -mstackrealign
245endif
246
247CFLAGS     += $(ARCHFLAG)
248AOUT_FLAGS += $(ARCHFLAG)
249LFLAGS     += $(ARCHFLAG)
250ASFLAGS    += $(ARCHFLAG)
251
252ifeq ($(DEBUG_BINARIES), true)
253  ASFLAGS    += $(ASFLAGS_DEBUG_SYMBOLS)
254endif
255
256ifdef E500V2
257CFLAGS += -DE500V2
258endif
259
260# Use C++ Interpreter
261ifdef CC_INTERP
262  CFLAGS += -DCC_INTERP
263endif
264
265# Keep temporary files (.ii, .s)
266ifdef NEED_ASM
267  CFLAGS += -save-temps
268else
269  CFLAGS += -pipe
270endif
271
272# Compiler warnings are treated as errors
273ifneq ($(COMPILER_WARNINGS_FATAL),false)
274  WARNINGS_ARE_ERRORS = -Werror
275endif
276
277ifeq ($(USE_CLANG), true)
278  # However we need to clean the code up before we can unrestrictedly enable this option with Clang
279  WARNINGS_ARE_ERRORS += -Wno-logical-op-parentheses -Wno-parentheses-equality -Wno-parentheses
280  WARNINGS_ARE_ERRORS += -Wno-switch -Wno-tautological-compare
281# Not yet supported by clang in Xcode 4.6.2
282#  WARNINGS_ARE_ERRORS += -Wno-tautological-constant-out-of-range-compare
283  WARNINGS_ARE_ERRORS += -Wno-delete-non-virtual-dtor -Wno-deprecated -Wno-format -Wno-dynamic-class-memaccess
284  WARNINGS_ARE_ERRORS += -Wno-empty-body
285  WARNINGS_ARE_ERRORS += -Wno-format-nonliteral
286  ifneq "$(shell expr \( $(CC_VER_MAJOR) \>= 6 \))" "0"
287    WARNINGS_ARE_ERRORS += -Wno-undefined-bool-conversion -Wno-expansion-to-defined
288    WARNINGS_ARE_ERRORS += -Wno-undefined-var-template
289  endif
290else
291  WARNINGS_ARE_ERRORS += -Wno-format
292endif
293
294WARNING_FLAGS = -Wpointer-arith -Wsign-compare -Wundef -Wunused-function -Wformat=2
295
296ifeq ($(USE_CLANG),)
297  # Since GCC 4.3, -Wconversion has changed its meanings to warn these implicit
298  # conversions which might affect the values. Only enable it in earlier versions.
299  ifeq "$(shell expr \( $(CC_VER_MAJOR) \> 4 \) \| \( \( $(CC_VER_MAJOR) = 4 \) \& \( $(CC_VER_MINOR) \>= 3 \) \))" "0"
300    WARNINGS_FLAGS += -Wconversion
301  endif
302endif
303
304CFLAGS_WARN/DEFAULT = $(WARNING_FLAGS) $(WARNINGS_ARE_ERRORS)
305# Special cases
306CFLAGS_WARN/BYFILE = $(CFLAGS_WARN/$@)$(CFLAGS_WARN/DEFAULT$(CFLAGS_WARN/$@))
307# XXXDARWIN: for _dyld_bind_fully_image_containing_address
308ifeq ($(OS_VENDOR), Darwin)
309  CFLAGS_WARN/os_bsd.o = $(CFLAGS_WARN/DEFAULT) -Wno-deprecated-declarations
310endif
311
312OPT_CFLAGS/SIZE=-Os
313OPT_CFLAGS/SPEED=-O3
314
315# Hotspot uses very unstrict aliasing turn this optimization off
316# This option is added to CFLAGS rather than OPT_CFLAGS
317# so that OPT_CFLAGS overrides get this option too.
318CFLAGS += -fno-strict-aliasing
319
320# The flags to use for an Optimized g++ build
321ifeq ($(OS_VENDOR), Darwin)
322  # use -Os by default, unless -O3 can be proved to be worth the cost, as per policy
323  # <https://wiki.openjdk.java.net/display/MacOSXPort/Compiler+Errata>
324  OPT_CFLAGS_DEFAULT ?= SIZE
325else
326  OPT_CFLAGS_DEFAULT ?= SPEED
327endif
328
329ifdef OPT_CFLAGS
330  ifneq ("$(origin OPT_CFLAGS)", "command line")
331    $(error " Use OPT_EXTRAS instead of OPT_CFLAGS to add extra flags to OPT_CFLAGS.")
332  endif
333endif
334
335OPT_CFLAGS = $(OPT_CFLAGS/$(OPT_CFLAGS_DEFAULT)) $(OPT_EXTRAS)
336
337# The gcc compiler segv's on ia64 when compiling bytecodeInterpreter.cpp
338# if we use expensive-optimizations
339ifeq ($(BUILDARCH), ia64)
340OPT_CFLAGS += -fno-expensive-optimizations
341endif
342
343OPT_CFLAGS/NOOPT=-O0
344
345# Work around some compiler bugs.
346ifeq ($(USE_CLANG), true)
347  ifeq ($(shell expr $(CC_VER_MAJOR) = 4 \& $(CC_VER_MINOR) = 2), 1)
348    OPT_CFLAGS/loopTransform.o += $(OPT_CFLAGS/NOOPT)
349    OPT_CFLAGS/unsafe.o += -O1
350  endif
351  ifeq ($(shell expr $(CC_VER_MAJOR) = 6 \& $(CC_VER_MINOR) = 0), 1)
352    OPT_CFLAGS/unsafe.o += -O1
353  endif
354  ifeq ($(shell expr $(CC_VER_MAJOR) = 10 \| $(CC_VER_MAJOR) = 11), 1)
355    ifeq ($(BUILDARCH), i486)
356      OPT_CFLAGS/bitMap.o += -O1
357    endif
358  endif
359else
360  # 6835796. Problem in GCC 4.3.0 with mulnode.o optimized compilation.
361  ifeq ($(shell expr $(CC_VER_MAJOR) = 4 \& $(CC_VER_MINOR) = 3), 1)
362    OPT_CFLAGS/mulnode.o += $(OPT_CFLAGS/NOOPT)
363  endif
364endif
365
366# Flags for generating make dependency flags.
367DEPFLAGS = -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d)
368ifeq ($(USE_CLANG),)
369  ifneq ($(CC_VER_MAJOR), 2)
370    DEPFLAGS += -fpch-deps
371  endif
372endif
373
374# -DDONT_USE_PRECOMPILED_HEADER will exclude all includes in precompiled.hpp.
375ifeq ($(USE_PRECOMPILED_HEADER),0)
376CFLAGS += -DDONT_USE_PRECOMPILED_HEADER
377endif
378
379ifeq ($(OS_VENDOR), Darwin)
380  # Setting these parameters makes it an error to link to macosx APIs that are
381  # newer than the given OS version and makes the linked binaries compatible even
382  # if built on a newer version of the OS.
383  # The expected format is X.Y.Z
384  ifeq ($(MACOSX_VERSION_MIN),)
385    MACOSX_VERSION_MIN=10.7.0
386  endif
387  # The macro takes the version with no dots, ex: 1070
388  CFLAGS += -DMAC_OS_X_VERSION_MAX_ALLOWED=$(subst .,,$(MACOSX_VERSION_MIN)) \
389            -mmacosx-version-min=$(MACOSX_VERSION_MIN)
390  LFLAGS += -mmacosx-version-min=$(MACOSX_VERSION_MIN)
391endif
392
393
394#------------------------------------------------------------------------
395# Assembler flags
396
397# Enforce prerpocessing of .s files
398ASFLAGS += -x assembler-with-cpp
399
400#------------------------------------------------------------------------
401# Linker flags
402
403# statically link libstdc++.so, work with gcc but ignored by g++
404ifeq ($(OS_VENDOR), Darwin)
405  ifeq ($(USE_CLANG), true)
406    STATIC_STDCXX = -Wl,-Bstatic -lc++ -Wl,-Bdynamic
407  else
408    STATIC_STDCXX = -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic
409  endif
410else
411  STATIC_STDCXX = -Wl,-Bstatic -lstdc++ -Wl,-Bdynamic
412endif
413
414ifeq ($(USE_CLANG),)
415  # statically link libgcc and/or libgcc_s, libgcc does not exist before gcc-3.x.
416  ifneq ("${CC_VER_MAJOR}", "2")
417    STATIC_LIBGCC += -static-libgcc
418  endif
419
420  ifeq ($(BUILDARCH), ia64)
421    LFLAGS += -Wl,-relax
422  endif
423endif
424
425# Use $(MAPFLAG:FILENAME=real_file_name) to specify a map file.
426MAPFLAG = -Xlinker --version-script=FILENAME
427
428LDFLAGS_NO_EXEC_STACK="-Wl,-z,noexecstack"
429
430#
431# Shared Library
432#
433ifeq ($(OS_VENDOR), Darwin)
434  # Standard linker flags
435  LFLAGS +=
436
437  # The apple linker has its own variant of mapfiles/version-scripts
438  MAPFLAG = -Xlinker -exported_symbols_list -Xlinker FILENAME
439
440  # Use $(SONAMEFLAG:SONAME=soname) to specify the intrinsic name of a shared obj
441  SONAMEFLAG =
442
443  # Build shared library
444  SHARED_FLAG = -Wl,-install_name,@rpath/$(@F) -dynamiclib -compatibility_version 1.0.0 -current_version 1.0.0 $(VM_PICFLAG)
445
446  # Keep symbols even they are not used
447  #AOUT_FLAGS += -Xlinker -export-dynamic
448else
449  # Enable linker optimization
450  LFLAGS += -Xlinker -O1
451
452  # Use $(SONAMEFLAG:SONAME=soname) to specify the intrinsic name of a shared obj
453  SONAMEFLAG = -Xlinker -soname=SONAME
454
455  # Build shared library
456  SHARED_FLAG = -shared $(VM_PICFLAG)
457
458  # Keep symbols even they are not used
459  AOUT_FLAGS += -Xlinker -export-dynamic
460endif
461
462#------------------------------------------------------------------------
463# Debug flags
464
465ifeq ($(USE_CLANG), true)
466  # Restrict the debug information created by Clang to avoid
467  # too big object files and speed the build up a little bit
468  # (see http://llvm.org/bugs/show_bug.cgi?id=7554)
469  CFLAGS += -flimit-debug-info
470endif
471
472# Use the stabs format for debugging information (this is the default
473# on gcc-2.91). It's good enough, has all the information about line
474# numbers and local variables, and libjvm.so is only about 16M.
475# Change this back to "-g" if you want the most expressive format.
476# (warning: that could easily inflate libjvm.so to 150M!)
477# Note: The Itanium gcc compiler crashes when using -gstabs.
478# Don't use stabs on gcc>=4.8 because it is incompatible with
479# pre-compiled-headers
480ifeq ($(USE_CLANG), true)
481  # Clang doesn't understand -gstabs
482  STABS_CFLAGS += -g
483else
484  ifeq "$(shell expr \( $(CC_VER_MAJOR) \> 4 \) \| \( \( $(CC_VER_MAJOR) = 4 \) \& \( $(CC_VER_MINOR) \>= 8 \) \))" "1"
485    # GCC >= 4.8
486    STABS_CFLAGS += -g
487  else
488    STABS_CFLAGS/ia64  = -g
489    STABS_CFLAGS/arm   = -g
490    STABS_CFLAGS/ppc   = -g
491    STABS_CFLAGS/amd64 = -g
492    STABS_CFLAGS/aarch64 = -g
493    ifeq ($(STABS_CFLAGS/$(BUILDARCH)),)
494      STABS_CFLAGS += -gstabs
495    else
496      STABS_CFLAGS += $(STABS_CFLAGS/$(BUILDARCH))
497    endif
498  endif
499endif
500
501# DEBUG_BINARIES uses full -g debug information for all configs
502ifeq ($(DEBUG_BINARIES), true)
503  CFLAGS += -g
504else
505  DEBUG_CFLAGS += ${STABS_CFLAGS}
506
507  ifeq ($(ENABLE_FULL_DEBUG_SYMBOLS),1)
508    FASTDEBUG_CFLAGS += ${STABS_CFLAGS}
509
510    OPT_CFLAGS += ${STABS_CFLAGS}
511  endif
512endif
513
514# If we are building HEADLESS, pass on to VM
515# so it can set the java.awt.headless property
516ifdef HEADLESS
517CFLAGS += -DHEADLESS
518endif
519
520# We are building Embedded for a small device
521# favor code space over speed
522ifdef MINIMIZE_RAM_USAGE
523CFLAGS += -DMINIMIZE_RAM_USAGE
524endif
525