2
# Copyright (c) 2018, 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
ifndef _JDK_NATIVE_COMPILATION_GMK
27
_JDK_NATIVE_COMPILATION_GMK := 1
29
ifeq ($(_MAKEBASE_GMK), )
30
$(error You must include MakeBase.gmk prior to including JdkNativeCompilation.gmk)
33
include NativeCompilation.gmk
35
# Hook to include the corresponding custom file, if present.
36
$(eval $(call IncludeCustomExtension, common/JdkNativeCompilation.gmk))
38
FindSrcDirsForComponent += \
39
$(call uniq, $(wildcard \
40
$(TOPDIR)/src/$(strip $1)/$(OPENJDK_TARGET_OS)/native/$(strip $2) \
41
$(TOPDIR)/src/$(strip $1)/$(OPENJDK_TARGET_OS_TYPE)/native/$(strip $2) \
42
$(TOPDIR)/src/$(strip $1)/share/native/$(strip $2)))
44
# Find a library. Used for declaring dependencies on libraries in different
46
# Param 1 - module name
47
# Param 2 - library name
48
# Param 3 - optional subdir for library
50
$(call FindLibDirForModule, \
51
$(strip $1))$(strip $3)/$(LIBRARY_PREFIX)$(strip $2)$(SHARED_LIBRARY_SUFFIX)
53
# Find a static library
54
# Param 1 - module name
55
# Param 2 - library name
56
# Param 3 - optional subdir for library
58
$(addprefix $(SUPPORT_OUTPUTDIR)/native/, \
59
$(strip $1)$(strip $3)/$(LIBRARY_PREFIX)$(strip $2)$(STATIC_LIBRARY_SUFFIX))
61
# If only generating compile_commands.json, make these return empty to avoid
62
# declaring dependencies.
63
ifeq ($(GENERATE_COMPILE_COMMANDS_ONLY), true)
67
# If building static versions of libraries, make these return empty to avoid
68
# declaring dependencies.
69
ifeq ($(STATIC_LIBS), true)
74
# Returns the module specific java header dir if it exists.
75
# Param 1 - module name
77
$(if $(strip $1),$(wildcard $(SUPPORT_OUTPUTDIR)/headers/$(strip $1)))
79
HOTSPOT_INCLUDE_DIR := $(TOPDIR)/src/hotspot/share/include \
80
$(TOPDIR)/src/hotspot/os/$(HOTSPOT_TARGET_OS_TYPE)/include
82
# Preprocess a source dir destination. Pass on absolute paths unchanged. Expand
83
# description such as "java.base:headers" into a set of proper absolute paths,
84
# by looking in $MODULE/[shared|$OS|$OSTYPE]/native.
85
# Treat *:libjvm as a special case, and replace it with the proper hotspot
88
$(if $(findstring :, $1), \
89
$(if $(filter %:libjvm, $1), \
90
$(HOTSPOT_INCLUDE_DIR) \
92
$(call FindSrcDirsForComponent, $(firstword $(subst :, , $1)), \
93
$(lastword $(subst :, , $1))) \
96
$(if $(filter /%, $1), \
99
$(if $(filter libjvm, $1), \
100
$(HOTSPOT_INCLUDE_DIR) \
102
$(call FindSrcDirsForComponent, $(MODULE), $1) \
107
# Create a proper LIBPATH for the given library. Sets result in $1_$2_LIBPATH.
108
# $1: The name of the rule (namespace)
109
# $2: The safe namespace of the library
111
# Determine proper LIBPATH. This is quite messy due to historical reasons,
112
# and because we need to have special treatment for "gtest:" and ":jvm".
113
ifneq ($$($1_$2_MODULE), gtest)
114
ifneq ($$($1_$2_NAME), jvm)
115
# This is the common case
116
ifeq ($$(call isTargetOs, windows), true)
117
$1_$2_LIBPATH := $$(SUPPORT_OUTPUTDIR)/native/$$($1_$2_MODULE)/lib$$($1_$2_NAME)
119
ifeq ($(STATIC_LIBS), true)
120
$1_$2_LIBPATH := $$(SUPPORT_OUTPUTDIR)/native/$$($1_$2_MODULE)/lib$$($1_$2_NAME)/static
121
else ifeq ($$($1_$2_STATIC_LIBRARY), true)
122
$1_$2_LIBPATH := $$(SUPPORT_OUTPUTDIR)/native/$$($1_$2_MODULE)
124
$1_$2_LIBPATH := $$(SUPPORT_OUTPUTDIR)/modules_libs/$$($1_$2_MODULE)
128
# Special treatment for hotspot
129
ifeq ($(CREATING_BUILDJDK), true)
130
# When building a buildjdk, it's always only the server variant
131
$1_$2_JVM_VARIANT_PATH := server
133
$1_$2_JVM_VARIANT_PATH := $(JVM_VARIANT_MAIN)
135
ifeq ($$(call isTargetOs, windows), true)
136
ifeq ($(STATIC_LIBS), true)
137
$1_$2_LIBPATH := $$(HOTSPOT_OUTPUTDIR)/variant-$$($1_$2_JVM_VARIANT_PATH)/libjvm/objs/static
139
$1_$2_LIBPATH := $$(SUPPORT_OUTPUTDIR)/modules_libs/$$($1_$2_MODULE)
142
ifeq ($(STATIC_LIBS), true)
143
$1_$2_LIBPATH := $$(HOTSPOT_OUTPUTDIR)/variant-$$($1_$2_JVM_VARIANT_PATH)/libjvm/objs/static
145
$1_$2_LIBPATH := $$(SUPPORT_OUTPUTDIR)/modules_libs/$$($1_$2_MODULE)/$$($1_$2_JVM_VARIANT_PATH)
150
# Special treatment for virtual module "gtest"
151
ifeq ($$($1_$2_NAME), jvm)
152
ifeq ($$(call isTargetOs, windows), true)
153
ifeq ($(STATIC_LIBS), true)
154
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/gtest/objs/static
156
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/gtest/objs
159
ifeq ($(STATIC_LIBS), true)
160
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/gtest/objs/static
162
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/gtest
165
else ifeq ($$($1_$2_NAME), gtest)
166
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/libgtest
167
ifeq ($(STATIC_LIBS), true)
168
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/libgtest/objs/static
170
$1_$2_LIBPATH := $$(JVM_OUTPUTDIR)/libgtest
176
# Process a JDK library designation such as "java.desktop:jawt" into the needed
177
# additions to EXTRA_LINK_DEPS, LDFLAGS, LIBS and EXTRA_HEADER_DIRS.
178
# $1: The name of the rule (namespace)
179
# $2: The safe namespace of the library
180
# $3: The actual string given to represent the library
182
$1_$2_NAME := $$(strip $$(lastword $$(subst :, , $3)))
183
$1_$2_MODULE := $$(strip $$(patsubst %$$($1_$2_NAME), %, $3))
185
ifeq ($$(filter lib%, $$($1_$2_NAME)),)
186
$$(error Library name $$($1_$2_NAME) missing lib prefix in $1)
188
$1_$2_NAME := $$(strip $$(patsubst lib%, %, $$($1_$2_NAME)))
190
ifeq ($$($1_$2_MODULE),)
191
$1_$2_MODULE := $$(MODULE)
193
$1_$2_MODULE := $$(strip $$(patsubst %:, %, $$($1_$2_MODULE)))
196
# Determine if the library in question is static.
197
# Ideally, we should not hardcode these
198
ifeq ($(call isTargetOs, aix)+$$($1_$2_MODULE):$$($1_$2_NAME), true+java.base:jli)
199
$1_$2_STATIC_LIBRARY := true
201
ifeq ($$($1_$2_MODULE):$$($1_$2_NAME), gtest:gtest)
202
$1_$2_STATIC_LIBRARY := true
205
# Setup $1_$2_LIBPATH.
206
$$(eval $$(call ResolveLibPath,$1,$2))
208
$1_EXTRA_HEADER_DIRS += $$($1_$2_MODULE):lib$$($1_$2_NAME)
210
ifneq ($(STATIC_LIBS), true)
211
ifeq ($$(call isTargetOs, windows), true)
212
ifeq ($$(filter -libpath:$$($1_$2_LIBPATH), $$($1_LDFLAGS)), )
213
$1_LDFLAGS += -libpath:$$($1_$2_LIBPATH)
215
$1_LIBS += $$($1_$2_NAME)$(STATIC_LIBRARY_SUFFIX)
216
$1_$2_LIBFILE := $(LIBRARY_PREFIX)$$($1_$2_NAME)$(STATIC_LIBRARY_SUFFIX)
218
ifeq ($$(filter -L$$($1_$2_LIBPATH), $$($1_LDFLAGS)), )
219
$1_LDFLAGS += -L$$($1_$2_LIBPATH)
221
$1_LIBS += -l$$($1_$2_NAME)
222
ifeq ($$($1_$2_STATIC_LIBRARY), true)
223
$1_$2_LIBFILE := $(LIBRARY_PREFIX)$$($1_$2_NAME)$(STATIC_LIBRARY_SUFFIX)
225
$1_$2_LIBFILE := $(LIBRARY_PREFIX)$$($1_$2_NAME)$(SHARED_LIBRARY_SUFFIX)
229
$1_EXTRA_LINK_DEPS += $$($1_$2_LIBPATH)/$$($1_$2_LIBFILE)
233
GLOBAL_VERSION_INFO_RESOURCE := $(TOPDIR)/src/java.base/windows/native/common/version.rc
235
JDK_RCFLAGS=$(RCFLAGS) \
236
-D"JDK_VERSION_STRING=$(VERSION_STRING)" \
237
-D"JDK_COMPANY=$(JDK_RC_COMPANY_NAME)" \
238
-D"JDK_VER=$(VERSION_NUMBER_FOUR_POSITIONS)" \
239
-D"JDK_COPYRIGHT=Copyright \xA9 $(COPYRIGHT_YEAR)" \
240
-D"JDK_NAME=$(JDK_RC_NAME) $(VERSION_SHORT)" \
241
-D"JDK_FVER=$(subst .,$(COMMA),$(VERSION_NUMBER_FOUR_POSITIONS))"
243
# Setup make rules for creating a native binary with suitable defaults
244
# for the OpenJDK project. This macro is mostly considered to be an internal
245
# support version. Please use SetupJdkLibrary or SetupJdkExecutable instead.
247
# Parameter 1 is the name of the rule. This name is used as variable prefix,
248
# and the targets generated are listed in a variable by that name.
250
# Remaining parameters are named arguments. These are all passed on to
251
# SetupNativeCompilation. This macro also adds the following additional
254
# SRC -- this is passed on after preprocessing. The preprocessing will pass on
255
# absolute paths unchanged, but relative paths will be expanded to include
256
# all directories with this name in $MODULE/[shared|$OS|$OSTYPE]/native.
257
# EXTRA_SRC -- additional directories to look for source in. This is processed
259
# EXCLUDE_SRC_PATTERNS -- exclude source dirs matching these patterns from
261
# EXTRA_HEADER_DIRS -- additional directories to look for headers in. This can
262
# be specified either as an absolute path, or relative directory names which
263
# are preprocessed like SRC, or in the format <module>:<directory>, which
264
# will be processed like SRC but for the given module. The names
265
# "java.base:libjvm" and "gtest:libjvm" symbolise virtual libraries that
266
# will be replaced with proper values for hotspot.
267
# HEADERS_FROM_SRC -- if false, does not add source dirs automatically as
268
# header include dirs. (Defaults to true.)
269
# JDK_LIBS -- libraries generated by the JDK build system to link against.
270
# These take the form <module>:<basename>. For the current module, the
271
# module name and colon can be omitted. The basename should be specified
272
# as the source directory, e.g. "libjava". The gtest module is a virtual
273
# module that will be replaced with correct values for gtest. When
274
# specifying JDK_LIBS, suitable values for EXTRA_LINK_DEPS, LDFLAGS, LIBS
275
# and EXTRA_HEADER_DIRS will be added.
276
# JDK_LIBS_<os> or JDK_LIBS_<osType> -- additional JDK_LIBS for the given OS
278
# EXTRA_RCFLAGS -- additional RCFLAGS to append.
279
# RC_FILEDESC -- override the default FILEDESC for Windows version.rc
280
# DEFAULT_LIBCXX -- if false, do not add LIBCXX to LIBS for C++ compilations
281
# DEFAULT_CFLAGS -- if false, do not add default CFLAGS and CXXFLAGS
282
# DEFAULT_LDFLAGS -- if false, do not add default LDFLAGS
283
# CFLAGS_FILTER_OUT -- flags to filter out from default CFLAGS
284
# CXXFLAGS_FILTER_OUT -- flags to filter out from default CXXFLAGS
285
# LDFLAGS_FILTER_OUT -- flags to filter out from default LDFLAGS
286
# LD_SET_ORIGIN -- if false, do not add SET_*_ORIGIN flags to LDFLAGS
287
# APPEND_LDFLAGS -- a quirk to have additional LDFLAGS that will be set after
289
# DEFAULT_VERSIONINFO_RESOURCE -- if false, do not set the default
290
# VERSIONINFO_RESOURCE
292
SetupJdkNativeCompilation = $(NamedParamsMacroTemplate)
293
define SetupJdkNativeCompilationBody
294
ifeq ($$($1_TYPE), EXECUTABLE)
295
$1_NATIVE_DIR_PREFIX :=
296
$1_MODULES_PATH := modules_cmds
300
$1_NATIVE_DIR_PREFIX := lib
301
$1_MODULES_PATH := modules_libs
306
ifeq ($$($1_OUTPUT_DIR), )
308
ifeq ($$($1_TYPE), STATIC_LIBRARY)
309
$1_OUTPUT_DIR := $$(SUPPORT_OUTPUTDIR)/native/$$(MODULE)
311
$1_OUTPUT_DIR := $$(SUPPORT_OUTPUTDIR)/$$($1_MODULES_PATH)/$$(strip $$(MODULE))
314
$$(error Must specify OUTPUT_DIR in a MODULE free context)
318
ifeq ($$($1_OBJECT_DIR), )
320
$1_OBJECT_DIR := $$(SUPPORT_OUTPUTDIR)/native/$$(MODULE)/$$($1_NATIVE_DIR_PREFIX)$$($1_NAME)
322
$$(error Must specify OBJECT_DIR in a MODULE free context)
326
ifneq ($$($1_DEFAULT_LIBCXX), false)
327
ifeq ($$($1_LINK_TYPE), C++)
334
$1_SRC := $$(call FindSrcDirsForComponent, $$(MODULE), $$($1_NATIVE_DIR_PREFIX)$$($1_NAME))
336
ifeq ($$($1_EXTRA_FILES), )
337
$$(error Must specify SRC or EXTRA_FILES in a MODULE free context)
341
$1_SRC := $$(foreach dir, $$($1_SRC), $$(call ProcessSrcDir, $$(dir)))
343
ifneq ($$($1_EXTRA_SRC), )
344
$1_SRC += $$(foreach dir, $$($1_EXTRA_SRC), $$(call ProcessSrcDir, $$(dir)))
347
ifneq ($$($1_EXCLUDE_SRC_PATTERNS), )
348
$1_SRC_WITHOUT_WORKSPACE_ROOT := $$(patsubst $$(WORKSPACE_ROOT)/%, %, $$($1_SRC))
349
$1_EXCLUDE_SRC := $$(addprefix %, $$(call containing, $$($1_EXCLUDE_SRC_PATTERNS), \
350
$$($1_SRC_WITHOUT_WORKSPACE_ROOT)))
351
$1_SRC := $$(filter-out $$($1_EXCLUDE_SRC), $$($1_SRC))
354
ifneq ($$($1_DEFAULT_VERSIONINFO_RESOURCE), false)
355
ifeq ($$($1_VERSIONINFO_RESOURCE), )
356
$1_VERSIONINFO_RESOURCE := $$(GLOBAL_VERSION_INFO_RESOURCE)
360
ifeq ($$($1_RC_FILEDESC), )
361
$1_RC_FILEDESC := $(JDK_RC_NAME) binary
363
$1_RCFLAGS := $(JDK_RCFLAGS) \
364
-D"JDK_FILEDESC=$$($1_RC_FILEDESC)" \
365
-D"JDK_FNAME=$$($1_NAME).$$($1_RC_EXT)" \
366
-D"JDK_INTERNAL_NAME=$$($1_NAME)" \
367
-D"JDK_FTYPE=$$($1_RC_FTYPE)" \
368
-I$(TOPDIR)/src/java.base/windows/native/common \
371
ifneq ($$($1_HEADERS_FROM_SRC), false)
372
$1_SRC_HEADER_FLAGS := $$(addprefix -I, $$(wildcard $$($1_SRC)))
375
# Add the module specific java header dir
377
$1_SRC_HEADER_FLAGS += $$(addprefix -I, $$(call GetJavaHeaderDir, $$(MODULE)))
380
$1_JDK_LIBS += $$($1_JDK_LIBS_$$(OPENJDK_TARGET_OS))
381
$1_JDK_LIBS += $$($1_JDK_LIBS_$$(OPENJDK_TARGET_OS_TYPE))
382
$$(foreach lib, $$($1_JDK_LIBS), \
383
$$(eval $$(call AddJdkLibrary,$1,$$(subst :,_,$$(lib)),$$(lib))) \
386
# Add the include path for jni.h to all native compilations
387
$1_EXTRA_HEADER_DIRS += \
388
$(SUPPORT_OUTPUTDIR)/modules_include/java.base \
392
$1_PROCESSED_EXTRA_HEADER_DIRS := $$(foreach dir, $$($1_EXTRA_HEADER_DIRS), \
393
$$(call ProcessSrcDir, $$(dir)))
394
$1_EXTRA_HEADER_FLAGS := $$(addprefix -I, $$($1_PROCESSED_EXTRA_HEADER_DIRS))
396
ifneq ($$($1_DEFAULT_CFLAGS), false)
397
ifeq ($$($1_TYPE), EXECUTABLE)
398
# Set the default flags first to be able to override
399
$1_CFLAGS := $$(filter-out $$($1_CFLAGS_FILTER_OUT), $$(CFLAGS_JDKEXE)) $$($1_CFLAGS)
401
# Set the default flags first to be able to override
402
$1_CXXFLAGS := $$(filter-out $$($1_CXXFLAGS_FILTER_OUT), $$(CXXFLAGS_JDKEXE)) $$($1_CXXFLAGS)
404
# Set the default flags first to be able to override
405
$1_CFLAGS := $$(filter-out $$($1_CFLAGS_FILTER_OUT), $$(CFLAGS_JDKLIB)) $$($1_CFLAGS)
407
# Set the default flags first to be able to override
408
$1_CXXFLAGS := $$(filter-out $$($1_CXXFLAGS_FILTER_OUT), $$(CXXFLAGS_JDKLIB)) $$($1_CXXFLAGS)
412
ifneq ($$($1_CFLAGS), )
413
$1_CFLAGS += $$($1_SRC_HEADER_FLAGS) $$($1_EXTRA_HEADER_FLAGS)
415
ifneq ($$($1_CXXFLAGS), )
416
$1_CXXFLAGS += $$($1_SRC_HEADER_FLAGS) $$($1_EXTRA_HEADER_FLAGS)
418
ifeq ($$($1_CFLAGS)$$($1_CXXFLAGS), )
419
$1_CFLAGS += $$($1_SRC_HEADER_FLAGS) $$($1_EXTRA_HEADER_FLAGS)
422
ifneq ($$($1_DEFAULT_LDFLAGS), false)
423
ifeq ($$($1_TYPE), EXECUTABLE)
424
# Set the default flags first to be able to override
425
$1_LDFLAGS := $$(filter-out $$($1_LDFLAGS_FILTER_OUT), $$(LDFLAGS_JDKEXE)) $$($1_LDFLAGS)
427
# Set the default flags first to be able to override
428
$1_LDFLAGS := $$(filter-out $$($1_LDFLAGS_FILTER_OUT), $$(LDFLAGS_JDKLIB)) $$($1_LDFLAGS)
432
ifneq ($$($1_LD_SET_ORIGIN), false)
433
ifeq ($$($1_TYPE), EXECUTABLE)
434
$1_LDFLAGS += $$(call SET_EXECUTABLE_ORIGIN)
436
$1_LDFLAGS += $$(call SET_SHARED_LIBRARY_ORIGIN)
439
# APPEND_LDFLAGS, if it exists, must be set after the origin flags
440
# This is a workaround to keep existing behavior
441
$1_LDFLAGS += $$($1_APPEND_LDFLAGS)
443
# Since we reuse the rule name ($1), all our arguments will pass through.
444
# We lose in transparency, but gain in brevity in this call...
445
$$(eval $$(call SetupNativeCompilation, $1))
448
# Setup make rules for creating a native library with suitable defaults
449
# for the OpenJDK project. The default is to create a shared library,
450
# but by passing TYPE := STATIC_LIBARY, a static library can be created.
452
# Parameter 1 is the name of the rule. This name is used as variable prefix,
453
# and the targets generated are listed in a variable by that name.
455
# Remaining parameters are named arguments. These are all passed on to
456
# SetupJdkNativeCompilation. Please see that macro for details.
457
SetupJdkLibrary = $(NamedParamsMacroTemplate)
458
define SetupJdkLibraryBody
459
# If type is unspecified, default to LIBRARY
464
# Since we reuse the rule name ($1), all our arguments will pass through.
465
# We lose in transparency, but gain in brevity in this call...
466
$$(eval $$(call SetupJdkNativeCompilation, $1))
469
# Setup make rules for creating a native executable with suitable defaults
470
# for the OpenJDK project.
472
# Parameter 1 is the name of the rule. This name is used as variable prefix,
473
# and the targets generated are listed in a variable by that name.
475
# Remaining parameters are named arguments. These are all passed on to
476
# SetupJdkNativeCompilation. Please see that macro for details.
477
SetupJdkExecutable = $(NamedParamsMacroTemplate)
478
define SetupJdkExecutableBody
479
$1_TYPE := EXECUTABLE
481
# Since we reuse the rule name ($1), all our arguments will pass through.
482
# We lose in transparency, but gain in brevity in this call...
483
$$(eval $$(call SetupJdkNativeCompilation, $1))
486
endif # _JDK_NATIVE_COMPILATION_GMK