jdk

Форк
0
/
NativeCompilation.gmk 
411 строк · 16.9 Кб
1
#
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.
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
################################################################################
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
################################################################################
31

32
ifndef _NATIVE_COMPILATION_GMK
33
_NATIVE_COMPILATION_GMK := 1
34

35
ifeq ($(_MAKEBASE_GMK), )
36
  $(error You must include MakeBase.gmk prior to including NativeCompilation.gmk)
37
endif
38

39
include MakeIO.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
46

47
################################################################################
48
# Setup make rules for creating a native binary (a shared library or an
49
# executable).
50
#
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.
53
#
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
94
#   LD the Linker 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
125
#       zipping
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
131
#
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
138
#
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.
145

146
  ###
147
  ### Prepare for compilation and linking
148
  ###
149

150
  $$(eval $$(call VerifyArguments,$1))
151

152
  # Setup variables for the rest of this macro to work with
153
  $$(eval $$(call SetupBasicVariables,$1))
154

155
  # Setup the toolchain to be used
156
  $$(eval $$(call SetupToolchain,$1))
157

158
  # Find all source files to compile and determine the output object file names
159
  $$(eval $$(call SetupSourceFiles,$1))
160
  $$(eval $$(call SetupOutputFiles,$1))
161

162
  # Setup CFLAGS/CXXFLAGS based on warnings, optimizations, extra flags etc.
163
  $$(eval $$(call SetupCompilerFlags,$1))
164

165
  # Machinery needed for the build to function properly
166
  $$(eval $$(call SetupBuildSystemSupport,$1))
167

168
  $$(eval $$(call RemoveSuperfluousOutputFiles,$1))
169

170
  # Need to make sure TARGET is first on list before starting to create files
171
  $1 := $$($1_TARGET)
172

173
  # Have make print information about the library when we start compiling
174
  $$(eval $$(call PrintStartInfo,$1))
175

176
  ###
177
  ### Compile all native source code files
178
  ###
179

180
  # Create a PCH, if requested
181
  $$(eval $$(call CreatePrecompiledHeader,$1))
182

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)),\
186
          FILE := $$(file), \
187
          BASE := $1, \
188
      )) \
189
  )
190

191
  ifeq ($(call isTargetOs, windows), true)
192
    # On windows we need to create a resource file
193
    $$(eval $$(call CreateWindowsResourceFile,$1))
194
  endif
195

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))
200

201
  ###
202
  ### Link the object files into a native output library/executable
203
  ###
204

205
  # Handle native debug symbols
206
  $$(eval $$(call CreateDebugSymbols,$1))
207

208
  # Prepare for linking
209
  $$(eval $$(call SetupLinkerFlags,$1))
210
  ifneq ($(TOOLCHAIN_TYPE), microsoft)
211
    $$(eval $$(call SetupLinking,$1))
212
  endif
213

214
  $$(eval $$(call SetupObjectFileList,$1))
215

216
  # Link the individually compiled files into a single unit
217
  ifneq ($(TOOLCHAIN_TYPE), microsoft)
218
    $$(eval $$(call CreateLinkedResult,$1))
219
  else
220
    $$(eval $$(call CreateLinkedResultMicrosoft,$1))
221
  endif
222

223
  ifeq ($(GENERATE_COMPILE_COMMANDS_ONLY), true)
224
    # Override all targets (this is a hack)
225
    $1 := $$($1_ALL_OBJS_JSON)
226
  endif
227
endef
228

229
################################################################################
230
# Verify that user passed arguments are valid
231
define VerifyArguments
232
  ifeq ($$($1_NAME), )
233
    $$(error NAME must not be empty in $1)
234
  endif
235
  ifneq ($$($1_NAME), $(basename $$($1_NAME)))
236
    $$(error NAME must not contain any directory path in $1)
237
  endif
238
  ifneq ($(findstring $$($1_SUFFIX), $$($1_NAME)), )
239
    $$(error NAME should be specified without suffix: $$($1_SUFFIX) in $1)
240
  endif
241
  ifneq ($(findstring $$($1_PREFIX), $$($1_NAME)), )
242
    $$(error NAME should be specified without prefix: $$($1_PREFIX) in $1)
243
  endif
244
  ifeq ($$($1_OUTPUT_DIR), )
245
    $$(error OUTPUT_DIR is missing in $1)
246
  endif
247
  ifneq ($$($1_MANIFEST), )
248
    ifeq ($$($1_MANIFEST_VERSION), )
249
      $$(error If MANIFEST is provided, then MANIFEST_VERSION is required in $1)
250
    endif
251
  endif
252
endef
253

254
################################################################################
255
# Setup basic variables
256
define SetupBasicVariables
257
  # If type is unspecified, default to LIBRARY
258
  ifeq ($$($1_TYPE), )
259
    $1_TYPE := LIBRARY
260
  endif
261

262
  # STATIC_LIBS is set from Main.gmk when building static versions of certain
263
  # native libraries.
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
268
    # jmods.
269
    $1_OBJECT_DIR := $$($1_OBJECT_DIR)/static
270
    $1_OUTPUT_DIR := $$($1_OBJECT_DIR)
271
  endif
272

273
  ifeq ($$($1_TYPE), EXECUTABLE)
274
    $1_PREFIX :=
275
    ifeq ($$($1_SUFFIX), )
276
      $1_SUFFIX := $(EXECUTABLE_SUFFIX)
277
    endif
278
  else
279
    $1_PREFIX := $(LIBRARY_PREFIX)
280
    ifeq ($$($1_TYPE), LIBRARY)
281
      ifeq ($$($1_SUFFIX), )
282
        $1_SUFFIX := $(SHARED_LIBRARY_SUFFIX)
283
      endif
284
    else ifeq ($$($1_TYPE), STATIC_LIBRARY)
285
      ifeq ($$($1_SUFFIX), )
286
        $1_SUFFIX := $(STATIC_LIBRARY_SUFFIX)
287
      endif
288
    endif
289
  endif
290

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))
295
endef
296

297
################################################################################
298
# Setup the toolchain variables
299
define SetupToolchain
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))
312
    else
313
      $$(call SetIfEmpty, $1_LD, $$(BUILD_LD))
314
    endif
315
  else
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))
329
    else
330
      $$(call SetIfEmpty, $1_LD, $$(LD))
331
    endif
332
  endif
333
endef
334

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)
346
endef
347

348
################################################################################
349
# Have make print information about the library when we start compiling
350
define PrintStartInfo
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
358

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))
364
        else
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))))
370
        endif
371
	$(TOUCH) $$@
372
endef
373

374
################################################################################
375
# Setup a library-wide dependency file from individual object file dependency
376
# files
377
define CreateDependencyFile
378
  # Create a rule to collect all the individual make dependency files into a
379
  # single makefile.
380
  $1_DEPS_FILE := $$($1_OBJECT_DIR)/$1.d
381

382
  $$($1_DEPS_FILE): $$($1_ALL_OBJS) $$($1_RES)
383
	$(RM) $$@
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
392
	$(MV) $$@.tmp $$@
393

394
  $1 += $$($1_DEPS_FILE)
395
endef
396

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
408
  endif
409
endef
410

411
endif # _NATIVE_COMPILATION_GMK
412

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.