2
# Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
################################################################################
27
# This is the top-level entry point for our native compilation and linking.
28
# It contains the SetupNativeCompilation macro, but is supported by helper
29
# macros in the make/common/native directory.
30
################################################################################
32
ifndef _NATIVE_COMPILATION_GMK
33
_NATIVE_COMPILATION_GMK := 1
35
ifeq ($(_MAKEBASE_GMK), )
36
$(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
40
include native/CompileFile.gmk
41
include native/DebugSymbols.gmk
42
include native/Flags.gmk
43
include native/Link.gmk
44
include native/LinkMicrosoft.gmk
45
include native/Paths.gmk
47
################################################################################
48
# Setup make rules for creating a native binary (a shared library or an
51
# Parameter 1 is the name of the rule. This name is used as variable prefix,
52
# and the targets generated are listed in a variable by that name.
54
# Remaining parameters are named arguments. These include:
55
# NAME The base name for the resulting binary, excluding decorations (like *.exe)
56
# TYPE Type of binary (EXECUTABLE, LIBRARY or STATIC_LIBRARY). Default is LIBRARY.
57
# SUFFIX Override the default suffix for the output file
58
# TARGET_TYPE The type to target, BUILD or TARGET. Defaults to TARGET.
59
# LINK_TYPE The language to use for the linker, C or C++. Defaults to C.
60
# SRC one or more directory roots to scan for C/C++ files.
61
# CFLAGS the compiler flags to be used, used both for C and C++.
62
# CXXFLAGS the compiler flags to be used for c++, if set overrides CFLAGS.
63
# LDFLAGS the linker flags to be used, used both for C and C++.
64
# LDFLAGS_<toolchain> the linker flags to be used for the specified toolchain,
65
# used both for C and C++.
66
# LDFLAGS_<OS> the linker flags to be used for the specified target OS,
67
# used both for C and C++.
68
# LDFLAGS_<toolchain>_<OS> the linker flags to be used for the specified
69
# toolchain and target OS, used both for C and C++.
70
# LIBS the libraries to link to
71
# LIBS_<OS> the libraries to link to for the specified target OS,
72
# used both for C and C++.
73
# LIBS_<toolchain> the libraries to link to for the specified toolchain,
74
# used both for C and C++.
75
# LIBS_<toolchain>_<OS> the libraries to link to for the specified target
76
# OS and toolchain, used both for C and C++.
77
# OBJECT_DIR the directory where we store the object files
78
# OUTPUT_DIR the directory where the resulting binary is put
79
# SYMBOLS_DIR the directory where the debug symbols are put, defaults to OUTPUT_DIR
80
# INCLUDES only pick source from these directories
81
# EXCLUDES do not pick source from these directories
82
# INCLUDE_FILES only compile exactly these files!
83
# EXCLUDE_FILES with these names
84
# EXCLUDE_PATTERN exclude files matching any of these substrings
85
# EXTRA_FILES List of extra files not in any of the SRC dirs
86
# EXTRA_OBJECT_FILES List of extra object files to include when linking
87
# EXTRA_DEPS List of extra dependencies to be added to each compiled file
88
# EXTRA_LINK_DEPS List of extra dependencies to be added to the link stage
89
# VERSIONINFO_RESOURCE Input file for RC. Setting this implies that RC will be run
90
# RCFLAGS flags for RC.
91
# EMBED_MANIFEST if true, embed manifest on Windows.
92
# CC the C compiler to use
93
# CXX the C++ compiler to use
95
# AR the static linker to use
96
# LIB the Windows lib tool to use for creating static libraries
97
# AS the assembler to use
98
# MT the Windows MT tool to use
99
# RC the Windows RC tool to use
100
# OBJCOPY the objcopy tool for debug symbol handling
101
# STRIP the tool to use for stripping debug symbols
102
# SYSROOT_CFLAGS the compiler flags for using the specific sysroot
103
# SYSROOT_LDFLAGS the linker flags for using the specific sysroot
104
# OPTIMIZATION sets optimization level to NONE, LOW, HIGH, HIGHEST, HIGHEST_JVM, SIZE
105
# DISABLED_WARNINGS_<toolchain> Disable the given warnings for the specified toolchain
106
# DISABLED_WARNINGS_<toolchain>_<OS> Disable the given warnings for the specified
107
# toolchain and target OS
108
# DISABLED_WARNINGS_C_<toolchain> Disable the given warnings for the specified toolchain
109
# when compiling C code
110
# DISABLED_WARNINGS_C_<toolchain>_<OS> Disable the given warnings for the specified
111
# toolchain and target OS when compiling C code
112
# DISABLED_WARNINGS_CXX_<toolchain> Disable the given warnings for the specified
113
# toolchain when compiling C++ code
114
# DISABLED_WARNINGS_CXX_<toolchain>_<OS> Disable the given warnings for the specified
115
# toolchain and target OS when compiling C++ code
116
# DISABLED_WARNINGS_<toolchain>_<filename> Disable the given warnings for the specified
117
# toolchain when compiling the file specified by filename
118
# DISABLED_WARNINGS_<toolchain>_<OS>_<filename> Disable the given warnings for the specified
119
# toolchain and target OS when compiling the file specified by filename
120
# STRIP_SYMBOLS Set to false to override global strip policy and always leave
121
# symbols in the binary, if the toolchain allows for it
122
# DEBUG_SYMBOLS Set to false to disable generation of debug symbols
123
# COPY_DEBUG_SYMBOLS Set to false to override global setting of debug symbol copying
124
# ZIP_EXTERNAL_DEBUG_SYMBOLS Set to false to override global setting of debug symbol
126
# STRIPFLAGS Optionally change the flags given to the strip command
127
# PRECOMPILED_HEADER Header file to use as precompiled header
128
# PRECOMPILED_HEADER_EXCLUDE List of source files that should not use PCH
129
# BUILD_INFO_LOG_MACRO Overrides log level of the build info log message, default LogWarn
130
# STATIC_LIB_EXCLUDE_OBJS exclude objects that matches from static library
132
# After being called, some variables are exported from this macro, all prefixed
133
# with parameter 1 followed by a '_':
134
# TARGET The library or executable created by the macro
135
# TARGET_DEPS All prerequisites for the target calculated by the macro
136
# ALL_OBJS All object files
137
# IMPORT_LIBRARY The import library created for a shared library on Windows
139
SetupNativeCompilation = $(NamedParamsMacroTemplate)
140
define SetupNativeCompilationBody
141
# When reading this code, note that macros named Setup<Foo> are just setting
142
# variables, and macros called Create<Foo> are setting up rules to create
143
# files. Macros starting with any other verb are more complicated, and can do
144
# all of the above, and also call directly to the shell.
147
### Prepare for compilation and linking
150
$$(eval $$(call VerifyArguments,$1))
152
# Setup variables for the rest of this macro to work with
153
$$(eval $$(call SetupBasicVariables,$1))
155
# Setup the toolchain to be used
156
$$(eval $$(call SetupToolchain,$1))
158
# Find all source files to compile and determine the output object file names
159
$$(eval $$(call SetupSourceFiles,$1))
160
$$(eval $$(call SetupOutputFiles,$1))
162
# Setup CFLAGS/CXXFLAGS based on warnings, optimizations, extra flags etc.
163
$$(eval $$(call SetupCompilerFlags,$1))
165
# Machinery needed for the build to function properly
166
$$(eval $$(call SetupBuildSystemSupport,$1))
168
$$(eval $$(call RemoveSuperfluousOutputFiles,$1))
170
# Need to make sure TARGET is first on list before starting to create files
173
# Have make print information about the library when we start compiling
174
$$(eval $$(call PrintStartInfo,$1))
177
### Compile all native source code files
180
# Create a PCH, if requested
181
$$(eval $$(call CreatePrecompiledHeader,$1))
183
# Now call CreateCompiledNativeFile for each source file we are going to compile.
184
$$(foreach file, $$($1_SRCS), \
185
$$(eval $$(call CreateCompiledNativeFile,$1_$$(notdir $$(file)),\
191
ifeq ($(call isTargetOs, windows), true)
192
# On windows we need to create a resource file
193
$$(eval $$(call CreateWindowsResourceFile,$1))
196
# Setup a library-wide dependency file from individual object file dependency
197
# files, and import it in the makefile.
198
$$(eval $$(call CreateDependencyFile,$1))
199
$$(eval $$(call ImportDependencyFile,$1))
202
### Link the object files into a native output library/executable
205
# Handle native debug symbols
206
$$(eval $$(call CreateDebugSymbols,$1))
208
# Prepare for linking
209
$$(eval $$(call SetupLinkerFlags,$1))
210
ifneq ($(TOOLCHAIN_TYPE), microsoft)
211
$$(eval $$(call SetupLinking,$1))
214
$$(eval $$(call SetupObjectFileList,$1))
216
# Link the individually compiled files into a single unit
217
ifneq ($(TOOLCHAIN_TYPE), microsoft)
218
$$(eval $$(call CreateLinkedResult,$1))
220
$$(eval $$(call CreateLinkedResultMicrosoft,$1))
223
ifeq ($(GENERATE_COMPILE_COMMANDS_ONLY), true)
224
# Override all targets (this is a hack)
225
$1 := $$($1_ALL_OBJS_JSON)
229
################################################################################
230
# Verify that user passed arguments are valid
231
define VerifyArguments
233
$$(error NAME must not be empty in $1)
235
ifneq ($$($1_NAME), $(basename $$($1_NAME)))
236
$$(error NAME must not contain any directory path in $1)
238
ifneq ($(findstring $$($1_SUFFIX), $$($1_NAME)), )
239
$$(error NAME should be specified without suffix: $$($1_SUFFIX) in $1)
241
ifneq ($(findstring $$($1_PREFIX), $$($1_NAME)), )
242
$$(error NAME should be specified without prefix: $$($1_PREFIX) in $1)
244
ifeq ($$($1_OUTPUT_DIR), )
245
$$(error OUTPUT_DIR is missing in $1)
247
ifneq ($$($1_MANIFEST), )
248
ifeq ($$($1_MANIFEST_VERSION), )
249
$$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
254
################################################################################
255
# Setup basic variables
256
define SetupBasicVariables
257
# If type is unspecified, default to LIBRARY
262
# STATIC_LIBS is set from Main.gmk when building static versions of certain
264
ifeq ($(STATIC_LIBS), true)
265
$1_TYPE := STATIC_LIBRARY
266
# The static versions need to be redirected to different output dirs, both
267
# to not interfere with the main build as well as to not end up inside the
269
$1_OBJECT_DIR := $$($1_OBJECT_DIR)/static
270
$1_OUTPUT_DIR := $$($1_OBJECT_DIR)
273
ifeq ($$($1_TYPE), EXECUTABLE)
275
ifeq ($$($1_SUFFIX), )
276
$1_SUFFIX := $(EXECUTABLE_SUFFIX)
279
$1_PREFIX := $(LIBRARY_PREFIX)
280
ifeq ($$($1_TYPE), LIBRARY)
281
ifeq ($$($1_SUFFIX), )
282
$1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
284
else ifeq ($$($1_TYPE), STATIC_LIBRARY)
285
ifeq ($$($1_SUFFIX), )
286
$1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
291
$1_BASENAME := $$($1_PREFIX)$$($1_NAME)$$($1_SUFFIX)
292
$1_TARGET := $$($1_OUTPUT_DIR)/$$($1_BASENAME)
293
$1_NOSUFFIX := $$($1_PREFIX)$$($1_NAME)
294
$1_SAFE_NAME := $$(strip $$(subst /,_, $1))
297
################################################################################
298
# Setup the toolchain variables
300
ifeq ($$($1_TARGET_TYPE), BUILD)
301
$$(call SetIfEmpty, $1_CC, $$(BUILD_CC))
302
$$(call SetIfEmpty, $1_CXX, $$(BUILD_CXX))
303
$$(call SetIfEmpty, $1_AR, $$(BUILD_AR))
304
$$(call SetIfEmpty, $1_LIB, $$(BUILD_LIB))
305
$$(call SetIfEmpty, $1_AS, $$(BUILD_AS))
306
$$(call SetIfEmpty, $1_OBJCOPY, $$(BUILD_OBJCOPY))
307
$$(call SetIfEmpty, $1_STRIP, $$(BUILD_STRIP))
308
$$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$(BUILD_SYSROOT_CFLAGS))
309
$$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$(BUILD_SYSROOT_LDFLAGS))
310
ifeq ($$($1_LINK_TYPE), C++)
311
$$(call SetIfEmpty, $1_LD, $$(BUILD_LDCXX))
313
$$(call SetIfEmpty, $1_LD, $$(BUILD_LD))
316
$$(call SetIfEmpty, $1_CC, $$(CC))
317
$$(call SetIfEmpty, $1_CXX, $$(CXX))
318
$$(call SetIfEmpty, $1_AR, $$(AR))
319
$$(call SetIfEmpty, $1_LIB, $$(LIB))
320
$$(call SetIfEmpty, $1_AS, $$(AS))
321
$$(call SetIfEmpty, $1_MT, $$(MT))
322
$$(call SetIfEmpty, $1_RC, $$(RC))
323
$$(call SetIfEmpty, $1_OBJCOPY, $$(OBJCOPY))
324
$$(call SetIfEmpty, $1_STRIP, $$(STRIP))
325
$$(call SetIfEmpty, $1_SYSROOT_CFLAGS, $$(SYSROOT_CFLAGS))
326
$$(call SetIfEmpty, $1_SYSROOT_LDFLAGS, $$(SYSROOT_LDFLAGS))
327
ifeq ($$($1_LINK_TYPE), C++)
328
$$(call SetIfEmpty, $1_LD, $$(LDCXX))
330
$$(call SetIfEmpty, $1_LD, $$(LD))
335
################################################################################
336
# Setup machinery needed by the build system
337
define SetupBuildSystemSupport
338
# Track variable changes for all variables that affect the compilation command
339
# lines for all object files in this setup. This includes at least all the
340
# variables used in the call to add_native_source below.
341
$1_COMPILE_VARDEPS := $$($1_CFLAGS) $$($1_EXTRA_CFLAGS) $$($1_SYSROOT_CFLAGS) \
342
$$($1_CXXFLAGS) $$($1_EXTRA_CXXFLAGS) $$($1_OPT_CFLAGS) $$($1_OPT_CXXFLAGS) \
343
$$($1_CC) $$($1_CXX) $$($1_AS) $$($1_ASFLAGS)
344
$1_COMPILE_VARDEPS_FILE := $$(call DependOnVariable, $1_COMPILE_VARDEPS, \
345
$$($1_OBJECT_DIR)/$$($1_NOSUFFIX).comp.vardeps)
348
################################################################################
349
# Have make print information about the library when we start compiling
351
# Setup rule for printing progress info when compiling source files.
352
# This is a rough heuristic and may not always print accurate information.
353
# The $1_BUILD_INFO and $1_BUILD_INFO_DEPS variables are used in
354
# TestFilesCompilation.gmk.
355
$$(call SetIfEmpty, $1_BUILD_INFO_LOG_MACRO, LogWarn)
356
$1_BUILD_INFO_DEPS := $$($1_SRCS) $$($1_COMPILE_VARDEPS_FILE)
357
$1_BUILD_INFO := $$($1_OBJECT_DIR)/_build-info.marker
359
$$($1_BUILD_INFO): $$($1_BUILD_INFO_DEPS)
360
ifeq ($$(wildcard $$($1_TARGET)), )
361
$$(call $$($1_BUILD_INFO_LOG_MACRO), \
362
Creating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) from $$(words \
363
$$(filter-out %.vardeps, $$?)) file(s))
365
$$(call $$($1_BUILD_INFO_LOG_MACRO), \
366
$$(strip Updating $$(subst $$(OUTPUTDIR)/,,$$($1_TARGET)) \
367
$$(if $$(filter-out %.vardeps, $$?), \
368
due to $$(words $$(filter-out %.vardeps, $$?)) file(s), \
369
$$(if $$(filter %.vardeps, $$?), due to makefile changes))))
374
################################################################################
375
# Setup a library-wide dependency file from individual object file dependency
377
define CreateDependencyFile
378
# Create a rule to collect all the individual make dependency files into a
380
$1_DEPS_FILE := $$($1_OBJECT_DIR)/$1.d
382
$$($1_DEPS_FILE): $$($1_ALL_OBJS) $$($1_RES)
384
# CD into dir to reduce risk of hitting command length limits, which
385
# could otherwise happen if TOPDIR is a very long path.
386
$(CD) $$($1_OBJECT_DIR) && $(CAT) *.d > $$@.tmp
387
$(CD) $$($1_OBJECT_DIR) && $(CAT) *.d.targets | $(SORT) -u >> $$@.tmp
388
# After generating the file, which happens after all objects have been
389
# compiled, copy it to .old extension. On the next make invocation, this
390
# .old file will be included by make.
391
$(CP) $$@.tmp $$@.old
394
$1 += $$($1_DEPS_FILE)
397
################################################################################
398
# Import the dependency file into the makefile
399
define ImportDependencyFile
400
# The include must be on the .old file, which represents the state from the
401
# previous invocation of make. The file being included must not have a rule
402
# defined for it as otherwise make will think it has to run the rule before
403
# being able to include the file, which would be wrong since we specifically
404
# need the file as it was generated by a previous make invocation.
405
ifneq ($$(wildcard $$($1_DEPS_FILE).old), )
406
$1_DEPS_FILE_LOADED := true
407
-include $$($1_DEPS_FILE).old
411
endif # _NATIVE_COMPILATION_GMK