1#
2# Copyright (c) 2020, Google, Inc. All rights reserved
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17# Build a userspace library for Trusty
18#
19# args:
20# MODULE : module name (required)
21# MODULE_SRCS : list of source files, local path (not required for header-only
22# 		libraries)
23# MODULE_LIBRARY_DEPS : libraries that this module depends on. These libraries
24# 		must be built using the new library.mk system (i.e. include
25# 		make/library.mk at the end of the library's rules)
26# MODULE_DEPS : legacy dependencies that do not use the new library.mk system.
27# 		These dependencies will be built exclusively for this module and not
28# 		shared with other modules). Do not use this for library dependencies
29# 		compatible with library.mk, instead use MODULE_LIBRARY_DEPS.
30# MODULE_ADD_IMPLICIT_DEPS : Add basic libraries to MODULE_LIBRARY_DEPS.
31# 		Defaults to true. (currently adds libc-trusty)
32# MODULE_USE_WHOLE_ARCHIVE : use --whole-archive when linking this module
33# MODULE_DEFINES : #defines local to this module
34# MODULE_CONSTANTS : JSON files with constants used for both the manifest and C
35# 		headers (optional) (CONSTANTS is a deprecated equivalent to
36# 		MODULE_CONSTANTS)
37# MODULE_COMPILEFLAGS : COMPILEFLAGS local to this module
38# MODULE_CFLAGS : CFLAGS local to this module
39# MODULE_CPPFLAGS : CPPFLAGS local to this module
40# MODULE_ASMFLAGS : ASMFLAGS local to this module
41# MODULE_INCLUDES : include directories local to this module
42# MODULE_SRCDEPS : extra dependencies that all of this module's files depend on
43# MODULE_EXTRA_OBJECTS : extra .o files that should be linked with the module
44# MODULE_WHOLE_ARCHIVES : extra .a libraries that need --whole-archive, e.g.,
45#		prebuilt archive dependencies
46# MODULE_ARM_OVERRIDE_SRCS : list of source files, local path that should be
47# 		force compiled with ARM (if applicable)
48# MODULE_RUST_EDITION : Rust edition to compile this crate for (optional)
49# MODULE_RUST_TESTS : If true, this module will be built as both a crate library
50#       and a Rust test service (optional, default is false)
51# MODULE_RUST_USE_CLIPPY: whether to require clippy be run for this library.
52# 		Compilation will fail if any clippy error is found.
53# MODULE_SKIP_DOCS : If true, no documentation will be generated for
54#       this module (optional, default is false)
55# MODULE_SDK_LIB_NAME : Name of library in SDK (if applicable). Default is
56# 		libMODULE_NAME where MODULE_NAME is the final path component of MODULE.
57# MODULE_SDK_HEADERS : Headers to copy into the SDK. Any headers in
58#       MODULE_EXPORT_INCLUDES will be included as well, but generated headers
59#       must be listed explicitly.
60# MODULE_SDK_HEADER_INSTALL_DIR : Path under include prefix to install SDK
61# 		headers into.
62# MODULE_LICENSES : Any additional license files for the library other than
63# 		$(MODULE)/LICENSE and $(MODULE)/NOTICE
64# MODULE_RUST_STEM: The stem of the output .rlib file for this library.
65# 	Defaults to $(MODULE_CRATE_NAME) if left empty.
66# MANIFEST : App manifest JSON file, only applicable if this module is an app
67# MANIFEST_OVERLAY : Additional manifest overlay JSON files(s)
68#
69# Exported flags:
70# The following args are the same as their corresponding variables above, but
71# will be exported to all users of this library. These flags are also prepended
72# to this module's local flags. To override an exported flag, add the
73# corresponding override to e.g. MODULE_COMPILEFLAGS.
74#
75# MODULE_EXPORT_COMPILEFLAGS
76# MODULE_EXPORT_CONSTANTS
77# MODULE_EXPORT_CFLAGS
78# MODULE_EXPORT_CPPFLAGS
79# MODULE_EXPORT_ASMFLAGS
80# MODULE_EXPORT_LDFLAGS
81# MODULE_EXPORT_INCLUDES
82# MODULE_EXPORT_SRCDEPS
83
84# the minimum library rules.mk file is as follows:
85#
86# LOCAL_DIR := $(GET_LOCAL_DIR)
87# MODULE := $(LOCAL_DIR)
88#
89# MODULE_SRCS := $(LOCAL_DIR)/source_file.c
90#
91# include make/library.mk
92
93# if QUERY_MODULE is set, the rules.mk that included us was itself included not
94# to define a module's make targets but to query the variables it sets for the
95# rest of the build. in this case, skip all further processing
96ifeq ($(QUERY_MODULE),)
97
98ifeq ($(call TOBOOL,$(TRUSTY_NEW_MODULE_SYSTEM)),false)
99
100MODULE_LOG_NAME := $(MODULE)
101$(call INFO,$(MODULE_LOG_NAME),processing,kernel library)
102
103GLOBAL_INCLUDES += $(MODULE_EXPORT_INCLUDES)
104GLOBAL_COMPILEFLAGS += $(MODULE_EXPORT_COMPILEFLAGS)
105GLOBAL_SRCDEPS += $(MODULE_EXPORT_SRCDEPS)
106
107ifneq ($(MODULE_EXPORT_CONSTANTS),)
108$(error MODULE_EXPORT_CONSTANTS is not supported by library.mk for use in the kernel)
109endif
110ifneq ($(MODULE_EXPORT_CFLAGS),)
111$(error MODULE_EXPORT_CFLAGS is not supported by library.mk for use in the kernel)
112endif
113ifneq ($(MODULE_EXPORT_CPPFLAGS),)
114$(error MODULE_EXPORT_CPPFLAGS is not supported by library.mk for use in the kernel)
115endif
116ifneq ($(MODULE_EXPORT_ASMFLAGS),)
117$(error MODULE_EXPORT_ASMFLAGS is not supported by library.mk for use in the kernel)
118endif
119ifneq ($(MODULE_EXPORT_LDFLAGS),)
120$(error MODULE_EXPORT_LDFLAGS is not supported by library.mk for use in the kernel)
121endif
122
123# Building for the kernel, turn off independent library build and fall back to
124# lk module system.
125include make/module.mk
126
127$(call INFO_DONE_SILENT,$(MODULE_LOG_NAME),processing)
128
129else  # TRUSTY_NEW_MODULE_SYSTEM is true
130
131ifeq ($(call TOBOOL,$(BUILD_AS_RUST_TEST_MODULE)),true)
132# Disable Rust tests on architectures that do not support Rust
133ifeq ($(call TOBOOL,$(ARCH_$(ARCH)_SUPPORTS_RUST)),true)
134# Allow a project to disable rust tests
135ifeq ($(call TOBOOL,$(TRUSTY_DISABLE_RUST_TESTS)),false)
136
137MODULE_RUST_LOG_NAME := $(MODULE)
138
139$(call INFO,$(MODULE_RUST_LOG_NAME),processing,as a rust test service)
140
141MODULE := $(MODULE)-test
142MODULE_RUSTFLAGS += --test
143MODULE_RUST_CRATE_TYPES := bin
144MODULE_LIBRARY_DEPS += trusty/user/base/lib/unittest-rust
145MODULE_RUST_ENV += TRUSTY_TEST_PORT=com.android.trusty.rust.$(MODULE_CRATE_NAME).test
146MODULE_RUST_TESTS :=
147MODULE_SKIP_DOCS := true
148TRUSTY_APP_NAME := $(MODULE_CRATE_NAME)-test
149BUILD_AS_RUST_TEST_MODULE :=
150
151TRUSTY_RUST_USER_TESTS += $(MODULE)
152
153include make/trusted_app.mk
154
155$(call INFO_DONE_SILENT,$(MODULE_RUST_LOG_NAME),processing)
156
157endif
158endif
159else # Not building rust test app
160
161# Build with the new module system. Currently, the Trusty userspace libraries
162# and apps use the new module system, as does the bootloader/test-runner binary.
163$(call INFO,$(MODULE),processing,library or app)
164
165# Reset new module system marker. This will be set again in dependencies by
166# userspace_recurse.mk
167TRUSTY_NEW_MODULE_SYSTEM :=
168
169MODULE_IS_RUST := $(if $(filter %.rs,$(MODULE_SRCS)),true,false)
170
171ifeq ($(call TOBOOL,$(MODULE_IS_RUST)),true)
172ifeq ($(strip $(MODULE_RUST_CRATE_TYPES)),)
173MODULE_RUST_CRATE_TYPES := rlib
174endif
175# Disable Rust modules on architectures that do not support Rust
176ifeq ($(call TOBOOL,$(ARCH_$(ARCH)_SUPPORTS_RUST)),false)
177MODULE_DISABLED := true
178endif
179endif
180
181ifeq ($(call TOBOOL,$(MODULE_DISABLED)),false)
182
183ifneq ($(filter proc-macro,$(MODULE_RUST_CRATE_TYPES)),)
184
185# proc macros must be host libraries, and all their dependencies are as well.
186# This will be reset after we recursively include all dependencies.
187MODULE_RUST_HOST_LIB := true
188
189ifneq ($(strip $(filter-out proc-macro,$(MODULE_RUST_CRATE_TYPES))),)
190$(error $(MODULE) cannot be built as both a proc-macro and a target crate)
191endif
192endif
193
194ifeq ($(call TOBOOL,$(TRUSTY_APP)),false)
195ifeq ($(call TOBOOL,$(MODULE_RUST_HOST_LIB)),false)
196BUILDDIR := $(TRUSTY_LIBRARY_BUILDDIR)
197else
198BUILDDIR := $(TRUSTY_HOST_LIBRARY_BUILDDIR)
199endif
200endif
201
202# Skip docs for apps because dependencies for apps are setup differently than
203# for rlibs (apps do use $MODULE_RSOBJS which is the variable we use as an input
204# to the rustdoc target to ensure that dependencies are built before generating
205# docs) and currently that breaks the rustdoc builds. We don't currently need
206# generated docs for apps, but if that changes it should be possible to fix
207# this.
208ifeq ($(call TOBOOL,$(TRUSTY_APP)),true)
209MODULE_SKIP_DOCS := true
210endif
211
212ifeq ($(call TOBOOL,$(MODULE_RUST_HOST_LIB)),false)
213# Add any common flags to the module
214include make/common_flags.mk
215endif
216
217ifneq ($(INCMODULES),)
218$(error $(MODULE) should only be included from other userspace modules that use library.mk. One of the following modules needs to be updated to use the new library system: $(LIB_SAVED_MODULE) $(ALLMODULES))
219endif
220ifneq ($(GLOBAL_OPTFLAGS),)
221$(error $(MODULE) has modified GLOBAL_OPTFLAGS, this variable is deprecated)
222endif
223ifneq ($(GLOBAL_COMPILEFLAGS),)
224$(error $(MODULE) has modified GLOBAL_COMPILEFLAGS, this variable is deprecated, please use MODULE_EXPORT_COMPILEFLAGS)
225endif
226ifneq ($(GLOBAL_CFLAGS),)
227$(error $(MODULE) has modified GLOBAL_CFLAGS, this variable is deprecated, please use MODULE_EXPORT_CFLAGS)
228endif
229ifneq ($(GLOBAL_CPPFLAGS),)
230$(error $(MODULE) has modified GLOBAL_CPPFLAGS, this variable is deprecated, please use MODULE_EXPORT_CPPFLAGS)
231endif
232ifneq ($(GLOBAL_ASMFLAGS),)
233$(error $(MODULE) has modified GLOBAL_ASMFLAGS, this variable is deprecated, please use MODULE_EXPORT_ASMFLAGS)
234endif
235ifneq ($(GLOBAL_DEFINES),)
236$(error $(MODULE) has modified GLOBAL_DEFINES, this variable is deprecated)
237endif
238ifneq ($(GLOBAL_INCLUDES),)
239$(error $(MODULE) has modified GLOBAL_INCLUDES, this variable is deprecated, please use MODULE_EXPORT_INCLUDES)
240endif
241ifneq ($(MODULE_OPTFLAGS),)
242$(error $(MODULE) sets MODULE_OPTFLAGS, which is deprecated. Please move these flags to another variable.)
243endif
244
245ifneq ($(MODULE_EXPORT_RUSTFLAGS),)
246$(error $(MODULE) sets MODULE_EXPORT_RUSTFLAGS, which is not supported)
247endif
248
249ifneq ($(strip $(MODULE_DEPS)),)
250$(warning $(MODULE) is a userspace library module but has deprecated MODULE_DEPS: $(MODULE_DEPS).)
251endif
252
253# ALLMODULES is only used for the legacy dependency system, so if a library is
254# included in it, something must have gone wrong.
255ifneq ($(filter $(MODULE),$(ALLMODULES)),)
256ifeq ($(LIB_SAVED_MODULE),)
257# We don't know who our parent was because it was a legacy module, so we can't
258# give a very good error message here.
259$(error Please move $(MODULE) from MODULE_DEPS into MODULE_LIBRARY_DEPS)
260else
261$(error MODULE $(LIB_SAVED_MODULE) depends on $(MODULE) via MODULE_DEPS, but $(MODULE) is only compatible with MODULE_LIBRARY_DEPS)
262endif
263endif
264
265ifneq ($(CONSTANTS),)
266$(warning $(MODULE) has set CONSTANTS, this variable is deprecated, please use MODULE_CONSTANTS or MODULE_EXPORT_CONSTANTS)
267endif
268MODULE_CONSTANTS += $(CONSTANTS)
269
270ifneq ($(MODULE_SRCS)$(MODULE_SRCS_FIRST),)
271# Add this module to the SDK LDFLAGS and objects lists. This needs to be done
272# before including our dependencies in case of recursive deps.
273ifneq ($(filter $(MODULE),$(TRUSTY_SDK_MODULES)),)
274ifeq ($(strip $(MODULE_SDK_LIB_NAME)),)
275MODULE_SDK_LIB_NAME := $(call TOSDKLIBNAME,$(MODULE))
276endif
277
278ifeq ($(call TOBOOL,$(MODULE_IS_RUST)),false)
279# If this module isn't rust, we can link against it from the sdk using -lmodule
280MODULE_SDK_LIBS += $(TRUSTY_SDK_LIB_DIR)/lib$(MODULE_SDK_LIB_NAME).a
281MODULE_EXPORT_LDFLAGS += $(filter-out $(MODULE_EXPORT_LDFLAGS),-l$(MODULE_SDK_LIB_NAME))
282endif
283
284endif # SDK module
285endif # not header only
286
287# Add this library's headers to the SDK.
288ifneq ($(filter $(MODULE),$(TRUSTY_SDK_MODULES)),)
289MODULE_EXPORT_SDK_HEADERS :=
290
291define copy-headers-rule
292# Some libraries include symlinked headers. For now, follow
293# those symlinks and copy their targets instead so SDK users
294# can still include the symlink sources.
295HEADERS := $$(shell cd "$(1)" 2>/dev/null && find -L . -type f)
296OUTPUT_HEADERS := $$(filter-out $$(MODULE_EXPORT_SDK_HEADERS),$$(addprefix $(TRUSTY_SDK_INCLUDE_DIR)/$(MODULE_SDK_HEADER_INSTALL_DIR)/,$$(HEADERS)))
297MODULE_EXPORT_SDK_HEADERS += $$(OUTPUT_HEADERS)
298$$(OUTPUT_HEADERS): $(TRUSTY_SDK_INCLUDE_DIR)/$(MODULE_SDK_HEADER_INSTALL_DIR)/% : $(1)/% $(MODULE_SRCDEPS)
299	@$$(MKDIR)
300	$$(NOECHO)cp -L $$< $$@
301endef
302
303$(foreach include_dir,$(MODULE_EXPORT_INCLUDES),$(eval $(call copy-headers-rule,$(include_dir))))
304
305# Copy any generated headers explicitly listed in MODULE_SDK_HEADERS
306ifneq ($(strip $(MODULE_SDK_HEADERS)),)
307OUTPUT_HEADERS := $(foreach header,$(MODULE_SDK_HEADERS),$(TRUSTY_SDK_INCLUDE_DIR)/$(MODULE_SDK_HEADER_INSTALL_DIR)/$(notdir $(header)))
308MODULE_EXPORT_SDK_HEADERS += $(OUTPUT_HEADERS)
309$(OUTPUT_HEADERS): MODULE_SDK_HEADERS := $(MODULE_SDK_HEADERS)
310$(OUTPUT_HEADERS): MODULE_SDK_HEADER_INSTALL_DIR := $(MODULE_SDK_HEADER_INSTALL_DIR)
311$(OUTPUT_HEADERS): $(MODULE_SDK_HEADERS) $(MODULE_SRCDEPS)
312	@$(MKDIR)
313	$(NOECHO)cp $(MODULE_SDK_HEADERS) $(TRUSTY_SDK_INCLUDE_DIR)/$(MODULE_SDK_HEADER_INSTALL_DIR)/
314OUTPUT_HEADERS :=
315endif
316
317# Make sure we copy all SDK headers even if they are not needed by the build
318ALL_SDK_INCLUDES += $(MODULE_EXPORT_SDK_HEADERS)
319
320endif # SDK MODULE
321
322# Stem defaults to the crate name
323ifeq ($(MODULE_RUST_STEM),)
324MODULE_RUST_STEM := $(MODULE_CRATE_NAME)
325endif
326
327# Register the module in a global registry. This is used to avoid repeatedly
328# generating rules for this module from modules that depend on it.
329_MODULES_$(MODULE) := T
330
331# Cache exported flags for use in modules that depend on this library.
332_MODULES_$(MODULE)_COMPILEFLAGS := $(MODULE_EXPORT_COMPILEFLAGS)
333_MODULES_$(MODULE)_CONSTANTS := $(MODULE_EXPORT_CONSTANTS)
334_MODULES_$(MODULE)_CFLAGS := $(MODULE_EXPORT_CFLAGS)
335_MODULES_$(MODULE)_CPPFLAGS := $(MODULE_EXPORT_CPPFLAGS)
336_MODULES_$(MODULE)_ASMFLAGS := $(MODULE_EXPORT_ASMFLAGS)
337_MODULES_$(MODULE)_INCLUDES := $(MODULE_EXPORT_INCLUDES)
338_MODULES_$(MODULE)_LDFLAGS := $(MODULE_EXPORT_LDFLAGS)
339_MODULES_$(MODULE)_SRCDEPS := $(MODULE_EXPORT_SRCDEPS)
340_MODULES_$(MODULE)_RUST_STEM := $(MODULE_RUST_STEM)
341ifeq ($(filter $(MODULE),$(TRUSTY_SDK_MODULES)),)
342ifeq ($(call TOBOOL,$(MODULE_IS_RUST)),true)
343_MODULES_$(MODULE)_CRATE_NAME := $(MODULE_CRATE_NAME)
344
345# Memorize the output headers for this module so that we can add them as srcdeps
346# to dependencies
347_MODULES_$(MODULE)_SDK_HEADERS := $(MODULE_EXPORT_SDK_HEADERS)
348
349# We need to populate rlibs here, before recursing, in case we have a circular
350# dependency. This is analogous to _INCLUDES above.
351ifneq ($(filter rlib,$(MODULE_RUST_CRATE_TYPES)),)
352_MODULES_$(MODULE)_LIBRARIES := $(call TOBUILDDIR,lib$(MODULE_RUST_STEM)).rlib
353_MODULES_$(MODULE)_RLIBS := $(MODULE_CRATE_NAME)=$(call TOBUILDDIR,lib$(MODULE_RUST_STEM).rlib)
354endif
355
356ifneq ($(filter proc-macro,$(MODULE_RUST_CRATE_TYPES)),)
357_MODULES_$(MODULE)_LIBRARIES := $(call TOBUILDDIR,lib$(MODULE_RUST_STEM)).so
358_MODULES_$(MODULE)_RLIBS := $(MODULE_CRATE_NAME)=$(call TOBUILDDIR,lib$(MODULE_RUST_STEM).so)
359endif
360
361else
362_MODULES_$(MODULE)_LIBRARIES := $(call TOBUILDDIR,$(MODULE)).mod.a
363endif
364endif # not SDK module
365
366# Will contain a list of SDK libraries that this library depends on. Used for
367# dependency resolution, not for including the libraries directly in the link.
368_MODULES_$(MODULE)_SDK_LIBS := $(MODULE_SDK_LIBS)
369
370DEPENDENCY_MODULE :=
371DEPENDENCY_MODULE_PATH :=
372
373# Recurse into dependencies that this module re-exports flags from. This needs
374# to happen before we recurse into regular dependencies in the case of recursive
375# dependencies, which need to pick up this module's re-exported flags.
376$(foreach dep,$(sort $(MODULE_LIBRARY_EXPORTED_DEPS)),\
377	$(eval EXPORT_DEPENDENCY_MODULE := $(dep))\
378	$(eval include make/userspace_recurse.mk))
379
380# Re-cache exported flags after adding any flags from exported deps
381_MODULES_$(MODULE)_COMPILEFLAGS := $(MODULE_EXPORT_COMPILEFLAGS)
382_MODULES_$(MODULE)_CFLAGS := $(MODULE_EXPORT_CFLAGS)
383_MODULES_$(MODULE)_CPPFLAGS := $(MODULE_EXPORT_CPPFLAGS)
384_MODULES_$(MODULE)_ASMFLAGS := $(MODULE_EXPORT_ASMFLAGS)
385_MODULES_$(MODULE)_INCLUDES := $(MODULE_EXPORT_INCLUDES)
386_MODULES_$(MODULE)_LDFLAGS := $(MODULE_EXPORT_LDFLAGS)
387_MODULES_$(MODULE)_SDK_HEADERS := $(MODULE_EXPORT_SDK_HEADERS)
388_MODULES_$(MODULE)_SRCDEPS := $(MODULE_EXPORT_SRCDEPS)
389
390# We need to process each dependent module only once.
391# Therefore we get the realpath to avoid different relative-path references to the same module,
392# then sort to remove any duplicates.
393# Module dependencies are then make relative to to top of the build environment.
394MODULE_REAL_LIBRARY_DEPS := $(realpath $(MODULE_LIBRARY_DEPS))
395ifneq ($(words MODULE_REAL_LIBRARY_DEPS), $(words MODULE_LIBRARY_DEPS))
396	$(error some modules path do not exist)
397endif
398
399MODULE_UNIQUE_LIBRARY_DEPS := $(sort $(foreach dep, $(MODULE_REAL_LIBRARY_DEPS), $(subst $(TRUSTY_TOP)/,,$(dep))))
400$(foreach dep,$(MODULE_UNIQUE_LIBRARY_DEPS),\
401	$(eval DEPENDENCY_MODULE := $(dep))\
402	$(eval include make/userspace_recurse.mk))
403
404# Include exported flags in the local build
405MODULE_LIBRARIES := $(filter-out $(MODULE_LIBRARIES),$(MODULE_EXPORT_LIBRARIES)) $(MODULE_LIBRARIES)
406MODULE_EXTRA_OBJECTS := $(filter-out $(MODULE_EXTRA_OBJECTS),$(MODULE_EXPORT_EXTRA_OBJECTS)) $(MODULE_EXTRA_OBJECTS)
407MODULE_WHOLE_ARCHIVES := $(filter-out $(MODULE_WHOLE_ARCHIVES),$(MODULE_EXPORT_WHOLE_ARCHIVES)) $(MODULE_WHOLE_ARCHIVES)
408MODULE_RLIBS := $(filter-out $(MODULE_RLIBS),$(MODULE_EXPORT_RLIBS)) $(MODULE_RLIBS)
409MODULE_COMPILEFLAGS := $(MODULE_EXPORT_COMPILEFLAGS) $(MODULE_COMPILEFLAGS)
410MODULE_CONSTANTS := $(MODULE_EXPORT_CONSTANTS) $(MODULE_CONSTANTS)
411MODULE_CFLAGS := $(MODULE_EXPORT_CFLAGS) $(MODULE_CFLAGS)
412MODULE_CPPFLAGS := $(MODULE_EXPORT_CPPFLAGS) $(MODULE_CPPFLAGS)
413MODULE_ASMFLAGS := $(MODULE_EXPORT_ASMFLAGS) $(MODULE_ASMFLAGS)
414MODULE_LDFLAGS := $(filter-out $(MODULE_LDFLAGS),$(MODULE_EXPORT_LDFLAGS)) $(MODULE_LDFLAGS)
415MODULE_SDK_LIBS := $(filter-out $(MODULE_SDK_LIBS),$(MODULE_EXPORT_SDK_LIBS)) $(MODULE_SDK_LIBS)
416MODULE_SDK_HEADERS := $(filter-out $(MODULE_SDK_HEADERS),$(MODULE_EXPORT_SDK_HEADERS)) $(MODULE_SDK_HEADERS)
417MODULE_SRCDEPS := $(MODULE_EXPORT_SRCDEPS) $(MODULE_SRCDEPS)
418
419ifeq ($(filter $(MODULE),$(TRUSTY_SDK_MODULES)),)
420# Only add in tree header paths to this module's include path if this module
421# isn't part of the SDK
422MODULE_INCLUDES := $(MODULE_EXPORT_INCLUDES) $(MODULE_INCLUDES)
423endif
424
425# Make sure the headers this module requires are copied before the module is
426# compiled
427MODULE_SRCDEPS += $(MODULE_SDK_HEADERS)
428MODULE_EXPORT_SRCDEPS += $(MODULE_SDK_HEADERS)
429
430# Generate constant headers and manifest, if needed.
431include make/gen_manifest.mk
432
433# Generate Rust bindings with bindgen if requested
434ifneq ($(strip $(MODULE_BINDGEN_SRC_HEADER)),)
435include make/bindgen.mk
436endif
437
438ifneq ($(MODULE_SRCS)$(MODULE_SRCS_FIRST),)
439# Not a header-only library, so we need to build the source files
440
441ifeq ($(call TOBOOL,$(MODULE_IS_RUST)),true)
442
443include make/rust.mk
444
445_MODULES_$(MODULE)_RUST_STATICLIB := $(filter %.a,$(MODULE_RSOBJS))
446
447_MODULES_$(MODULE)_CRATE_INDEX := $(GLOBAL_CRATE_COUNT)
448GLOBAL_CRATE_COUNT := $(shell echo $$(($(GLOBAL_CRATE_COUNT)+1)))
449
450define CRATE_CONFIG :=
451{
452	"display_name": "$(MODULE_RUST_STEM)",
453	"root_module": "$(filter %.rs,$(MODULE_SRCS))",
454	"edition": "$(MODULE_RUST_EDITION)",
455	"deps": [
456		$(call STRIP_TRAILING_COMMA,$(foreach dep,$(sort $(MODULE_LIBRARY_DEPS) $(MODULE_LIBRARY_EXPORTED_DEPS)),\
457				$(if $(_MODULES_$(dep)_RUST_STEM),{"name": "$(_MODULES_$(dep)_RUST_STEM)"$(COMMA) "crate": $(_MODULES_$(dep)_CRATE_INDEX)}$(COMMA))))
458	]
459},
460
461endef
462RUST_ANALYZER_CONTENTS := $(RUST_ANALYZER_CONTENTS)$(CRATE_CONFIG)
463CRATE_CONFIG :=
464
465endif # MODULE_IS_RUST
466
467# Save our current module because module.mk clears it.
468LIB_SAVED_MODULE := $(MODULE)
469LIB_SAVED_MODULE_LIBRARY_DEPS := $(MODULE_LIBRARY_DEPS)
470LIB_SAVED_MODULE_SRCDEPS := $(MODULE_SRCDEPS)
471
472# Save the rust flags for use in trusted_app.mk. userspace_recurse.mk will clean
473# up after us.
474LIB_SAVED_MODULE_RUSTFLAGS := $(MODULE_RUSTFLAGS)
475LIB_SAVED_MODULE_RUSTDOCFLAGS := $(MODULE_RUSTDOCFLAGS)
476LIB_SAVED_MODULE_RUSTDOC_OBJECT := $(MODULE_RUSTDOC_OBJECT)
477LIB_SAVED_MODULE_IS_RUST := $(MODULE_IS_RUST)
478LIB_SAVED_MODULE_RUST_USE_CLIPPY := $(MODULE_RUST_USE_CLIPPY)
479
480ALLMODULE_OBJS :=
481MODULE_LIBRARY_DEPS :=
482
483include make/module.mk
484
485# Handle any MODULE_DEPS
486include make/recurse.mk
487
488MODULE_LIBRARY_DEPS := $(LIB_SAVED_MODULE_LIBRARY_DEPS)
489MODULE_SRCDEPS := $(LIB_SAVED_MODULE_SRCDEPS)
490MODULE := $(LIB_SAVED_MODULE)
491MODULE_RUSTFLAGS := $(LIB_SAVED_MODULE_RUSTFLAGS)
492MODULE_RUSTDOCFLAGS := $(LIB_SAVED_MODULE_RUSTDOCFLAGS)
493MODULE_RUSTDOC_OBJECT := $(LIB_SAVED_MODULE_RUSTDOC_OBJECT)
494MODULE_IS_RUST := $(LIB_SAVED_MODULE_IS_RUST)
495MODULE_RUST_USE_CLIPPY := $(LIB_SAVED_MODULE_RUST_USE_CLIPPY)
496
497$(BUILDDIR)/%: CC := $(CCACHE) $(CLANG_BINDIR)/clang
498$(BUILDDIR)/%: RUSTC := $(RUST_BINDIR)/rustc
499$(BUILDDIR)/%: CLIPPY_DRIVER := $(RUST_BINDIR)/clippy-driver
500$(BUILDDIR)/%.o: GLOBAL_OPTFLAGS := $(GLOBAL_SHARED_OPTFLAGS) $(GLOBAL_USER_OPTFLAGS) $(GLOBAL_USER_IN_TREE_OPTFLAGS) $(ARCH_OPTFLAGS)
501$(BUILDDIR)/%.o: GLOBAL_COMPILEFLAGS := $(GLOBAL_SHARED_COMPILEFLAGS) $(GLOBAL_USER_COMPILEFLAGS) $(GLOBAL_USER_IN_TREE_COMPILEFLAGS)
502$(BUILDDIR)/%.o: GLOBAL_CFLAGS   := $(GLOBAL_SHARED_CFLAGS) $(GLOBAL_USER_CFLAGS) $(GLOBAL_USER_IN_TREE_CFLAGS)
503$(BUILDDIR)/%.o: GLOBAL_CPPFLAGS := $(GLOBAL_SHARED_CPPFLAGS) $(GLOBAL_USER_CPPFLAGS) $(GLOBAL_USER_IN_TREE_CPPFLAGS)
504$(BUILDDIR)/%.o: GLOBAL_ASMFLAGS := $(GLOBAL_SHARED_ASMFLAGS) $(GLOBAL_USER_ASMFLAGS) $(GLOBAL_USER_IN_TREE_ASMFLAGS)
505$(BUILDDIR)/%.o: GLOBAL_INCLUDES := $(addprefix -I,$(GLOBAL_UAPI_INCLUDES) $(GLOBAL_SHARED_INCLUDES) $(GLOBAL_USER_INCLUDES))
506$(BUILDDIR)/%.o: ARCH_COMPILEFLAGS := $(ARCH_$(ARCH)_COMPILEFLAGS)
507$(BUILDDIR)/%.o: ARCH_CFLAGS := $(ARCH_$(ARCH)_CFLAGS)
508$(BUILDDIR)/%.o: THUMBCFLAGS := $(ARCH_$(ARCH)_THUMBCFLAGS)
509$(BUILDDIR)/%.o: ARCH_CPPFLAGS := $(ARCH_$(ARCH)_CPPFLAGS)
510$(BUILDDIR)/%.o: ARCH_ASMFLAGS := $(ARCH_$(ARCH)_ASMFLAGS)
511
512ifeq ($(call TOBOOL,$(MODULE_IS_RUST)),true)
513LIBRARY_ARCHIVE := $(filter %.rlib %.so,$(ALLMODULE_OBJS))
514else
515LIBRARY_ARCHIVE := $(filter %.mod.a,$(ALLMODULE_OBJS))
516endif
517
518ifneq ($(filter $(MODULE),$(TRUSTY_SDK_MODULES)),)
519# Install the library into the SDK
520
521ifeq ($(call TOBOOL,$(MODULE_IS_RUST)),true)
522# Rust modules aren't added to the SDK sysroot yet. We need to keep track of the
523# library archive here so that we can ensure it is built before its dependencies.
524#
525# TODO: Add proper support for SDK rlibs
526MODULE_EXPORT_LIBRARIES += $(LIBRARY_ARCHIVE)
527endif
528
529SDK_LIB := $(TRUSTY_SDK_LIB_DIR)/lib$(MODULE_SDK_LIB_NAME).a
530ALLMODULE_OBJS := $(filter-out $(LIBRARY_ARCHIVE),$(ALLMODULE_OBJS))
531OTHER_SDK_OBJS := $(addprefix $(TRUSTY_SDK_LIB_DIR)/,$(notdir $(ALLMODULE_OBJS)))
532$(SDK_LIB): OTHER_OBJS := $(ALLMODULE_OBJS)
533$(SDK_LIB): $(LIBRARY_ARCHIVE) $(ALLMODULE_OBJS)
534	@$(MKDIR)
535	@cp $< $@
536	@[ -z "$(OTHER_OBJS)" ] || cp $(OTHER_OBJS) $(TRUSTY_SDK_LIB_DIR)/
537
538# Ensure that any extra SDK objects are copied if they are missing
539$(OTHER_SDK_OBJS): $(SDK_LIB)
540
541MODULE_SDK_LIBS += $(OTHER_SDK_OBJS)
542ALL_SDK_LIBS += $(SDK_LIB) $(OTHER_SDK_OBJS)
543
544# Add any module licenses, if found
545MODULE_LICENSES += $(wildcard $(MODULE)/LICENSE*) $(wildcard $(MODULE)/NOTICE)
546
547# Generate the library makefile
548
549SDK_MAKEFILE := $(TRUSTY_SDK_DIR)/make/lib$(MODULE_SDK_LIB_NAME).mk
550$(SDK_MAKEFILE): MODULE_EXPORT_DEFINES := $(MODULE_EXPORT_DEFINES)
551$(SDK_MAKEFILE): MODULE_EXPORT_CFLAGS := \
552	$(MODULE_EXPORT_OPTFLAGS) $(MODULE_EXPORT_COMPILEFLAGS) $(MODULE_EXPORT_CFLAGS)
553$(SDK_MAKEFILE): MODULE_EXPORT_CXXFLAGS := \
554	$(MODULE_EXPORT_OPTFLAGS) $(MODULE_EXPORT_COMPILEFLAGS) $(MODULE_EXPORT_CPPFLAGS)
555$(SDK_MAKEFILE): MODULE_EXPORT_ASMFLAGS := \
556	$(MODULE_EXPORT_OPTFLAGS) $(MODULE_EXPORT_COMPILEFLAGS) $(MODULE_EXPORT_ASMFLAGS)
557$(SDK_MAKEFILE): MODULE_EXPORT_LDFLAGS := $(MODULE_EXPORT_LDFLAGS)
558$(SDK_MAKEFILE): MODULE_SDK_LIB_NAME := $(MODULE_SDK_LIB_NAME)
559$(SDK_MAKEFILE): OTHER_SDK_OBJS := $(addprefix $$(SDK_SYSROOT_DIR)/usr/lib/,$(notdir $(OTHER_SDK_OBJS)))
560$(SDK_MAKEFILE):
561	@$(MKDIR)
562	@$(call ECHO,sdk,generating makefile,for $(MODULE_SDK_LIB_NAME))
563	$(NOECHO)rm -f $@.tmp
564	$(NOECHO)echo DEFINES += $(call prepare-sdk-flags,$(MODULE_EXPORT_DEFINES)) >> $@.tmp
565	$(NOECHO)echo CFLAGS += $(call prepare-sdk-flags,$(MODULE_EXPORT_CFLAGS)) >> $@.tmp
566	$(NOECHO)echo CXXFLAGS += $(call prepare-sdk-flags,$(MODULE_EXPORT_CXXFLAGS)) >> $@.tmp
567	$(NOECHO)echo ASMFLAGS += $(call prepare-sdk-flags,$(MODULE_EXPORT_ASMFLAGS)) >> $@.tmp
568	$(NOECHO)echo LDFLAGS += $(call prepare-sdk-flags,$(MODULE_EXPORT_LDFLAGS)) >> $@.tmp
569	$(NOECHO)echo 'TRUSTY_APP_OBJECTS += $(OTHER_SDK_OBJS)' >> $@.tmp
570	$(NOECHO)sed -i 's/ \+$$//' $@.tmp
571	@$(call TESTANDREPLACEFILE,$@.tmp,$@)
572	@$(call ECHO_DONE_SILENT,sdk,generating makefile,for $(MODULE_SDK_LIB_NAME))
573
574ALL_SDK_EXTRA_FILES += $(SDK_MAKEFILE)
575
576else # not an SDK module
577
578# Libraries not in the SDK are included directly in the link as archives, rather
579# than via `-l`.
580MODULE_EXPORT_LIBRARIES += $(LIBRARY_ARCHIVE)
581
582endif # SDK module
583
584MODULE_EXPORT_EXTRA_OBJECTS += $(filter-out $(LIBRARY_ARCHIVE),$(ALLMODULE_OBJS))
585
586ifeq ($(call TOBOOL,$(MODULE_USE_WHOLE_ARCHIVE)),true)
587MODULE_EXPORT_WHOLE_ARCHIVES += $(LIBRARY_ARCHIVE)
588# Include the current module explicitly in MODULE_WHOLE_ARCHIVES
589# in case we were included from trusted_app.mk
590MODULE_WHOLE_ARCHIVES += $(LIBRARY_ARCHIVE)
591endif
592
593# Append dependency libraries into ALLMODULE_OBJS. This needs to happen after we
594# set up the SDK library copies, if necessary, because we need ALLMODULE_OBJS
595# without dependencies there.
596ALLMODULE_OBJS := $(ALLMODULE_OBJS) $(filter-out $(ALLMODULE_OBJS),$(MODULE_LIBRARIES))
597
598endif # MODULE is not a header-only library
599
600_MODULES_$(MODULE)_LIBRARIES := $(MODULE_EXPORT_LIBRARIES)
601_MODULES_$(MODULE)_LICENSES := $(MODULE_LICENSES)
602_MODULES_$(MODULE)_EXTRA_OBJECTS := $(MODULE_EXPORT_EXTRA_OBJECTS)
603_MODULES_$(MODULE)_WHOLE_ARCHIVES := $(MODULE_EXPORT_WHOLE_ARCHIVES)
604_MODULES_$(MODULE)_RLIBS := $(MODULE_EXPORT_RLIBS)
605_MODULES_$(MODULE)_SDK_LIBS := $(MODULE_SDK_LIBS)
606_MODULES_$(MODULE)_LDFLAGS := $(MODULE_EXPORT_LDFLAGS)
607_MODULES_$(MODULE)_SDK_HEADERS := $(MODULE_EXPORT_SDK_HEADERS)
608
609ifeq ($(call TOBOOL,$(MODULE_RUST_TESTS)),true)
610# Rebuild this module as a test service as well
611BUILD_AS_RUST_TEST_MODULE := true
612DEPENDENCY_MODULE := $(MODULE)-test
613DEPENDENCY_MODULE_PATH := $(MODULE)
614include make/userspace_recurse.mk
615endif
616endif # module is not disabled
617endif # not building rust test app
618
619$(call INFO_DONE_SILENT,$(MODULE),processing,library or app)
620
621endif # building userspace module
622
623# Reset all variables for the next module
624MODULE :=
625MODULE_CRATE_NAME :=
626MODULE_RUST_STEM :=
627MODULE_SRCDEPS :=
628MODULE_LIBRARY_DEPS :=
629MODULE_LIBRARY_EXPORTED_DEPS :=
630MODULE_USE_WHOLE_ARCHIVE :=
631MODULE_LIBRARIES :=
632MODULE_LICENSES :=
633MODULE_RLIBS :=
634MODULE_RSOBJS :=
635MODULE_RUSTDOC_OBJECT :=
636MODULE_RUSTDOCFLAGS :=
637MODULE_RUST_USE_CLIPPY :=
638MODULE_SKIP_DOCS :=
639MODULE_DISABLED :=
640MODULE_SDK_LIB_NAME :=
641MODULE_SDK_HEADER_INSTALL_DIR :=
642MODULE_SDK_HEADERS :=
643# MODULE_WHOLE_ARCHIVES is used by trusted_app.mk
644# so we intentionally do not reset it here
645
646LIB_SAVED_MODULE :=
647LIB_SAVED_ALLMODULE_OBJS :=
648
649ifneq ($(filter proc-macro,$(MODULE_RUST_CRATE_TYPES)),)
650# Reset host build state only once we finish building the proc-macro and its deps
651MODULE_RUST_HOST_LIB :=
652endif
653MODULE_RUST_CRATE_TYPES :=
654MODULE_RUST_TESTS :=
655OTHER_SDK_OBJS :=
656SDK_LIB :=
657OTHER_OBJS :=
658OTHER_SDK_OBJS :=
659
660MODULE_EXPORT_LIBRARIES :=
661MODULE_EXPORT_RLIBS :=
662MODULE_EXPORT_EXTRA_OBJECTS :=
663MODULE_EXPORT_WHOLE_ARCHIVES :=
664MODULE_EXPORT_COMPILEFLAGS :=
665MODULE_EXPORT_CONSTANTS :=
666MODULE_EXPORT_CFLAGS :=
667MODULE_EXPORT_CPPFLAGS :=
668MODULE_EXPORT_ASMFLAGS :=
669MODULE_EXPORT_INCLUDES :=
670MODULE_EXPORT_LDFLAGS :=
671MODULE_EXPORT_SDK_HEADERS :=
672MODULE_EXPORT_SRCDEPS :=
673MODULE_UNIQUE_LIBRARY_DEPS :=
674
675endif # QUERY_MODULE (this line should stay after all other processing)
676