1# Find the local dir of the make file
2GET_LOCAL_DIR    = $(patsubst %/,%,$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))))
3
4# makes sure the target dir exists
5MKDIR = if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi
6
7# prepends the BUILD_DIR var to each item in the list
8TOBUILDDIR = $(addprefix $(BUILDDIR)/,$(1))
9
10# converts specified variable to boolean value
11TOBOOL = $(if $(filter-out 0 false,$1),true,false)
12
13# try to find a Trusty external at external/trusty first
14# (if we moved it there), otherwise try the old directory
15FIND_EXTERNAL = $(if $(wildcard external/trusty/$1),external/trusty/$1,external/$1)
16
17# try to find a Rust crate at external/rust/crates/$CRATE and fall back to
18# trusty/user/base/host/$CRATE and then trusty/user/base/lib/$CRATE-rust
19FIND_CRATE = $(patsubst %/,%,$(dir $(firstword $(wildcard external/rust/android-crates-io/extra_versions/crates/$1/rules.mk external/rust/android-crates-io/crates/$1/rules.mk external/rust/crates/$1/rules.mk trusty/user/base/host/$1/rules.mk trusty/user/base/host/$1-rust/rules.mk))))
20
21# checks if module with a given path exists
22FIND_MODULE = $(wildcard $1/rules.mk)$(wildcard $(addsuffix /$1/rules.mk,$(.INCLUDE_DIRS)))
23
24COMMA := ,
25EMPTY :=
26SPACE := $(EMPTY) $(EMPTY)
27
28define NEWLINE
29
30
31endef
32
33# Remove last comma in $1 if it is at the end or before a newline at the end.
34STRIP_TRAILING_COMMA = \
35	$(subst END_OF_LIST_MARKER_FOR_STRIP_TRAILING_COMMA,,\
36		$(subst $(COMMA)\nEND_OF_LIST_MARKER_FOR_STRIP_TRAILING_COMMA,\n,\
37			$(subst $(COMMA)END_OF_LIST_MARKER_FOR_STRIP_TRAILING_COMMA,,\
38				$(strip $(1))END_OF_LIST_MARKER_FOR_STRIP_TRAILING_COMMA)))
39
40# return $1 with the first word removed
41rest-of-words = $(wordlist 2,$(words $1),$1)
42# map $1 onto zipped pairs of items from lists $2 and $3
43pairmap = $(and $(strip $2),$(strip $3),\
44	$(call $1,$(firstword $2),$(firstword $3)) $(call pairmap,$1,$(call rest-of-words,$2),$(call rest-of-words,$3)))
45
46# Normalize rust cfg, Uppercase everything and swap (`-` and `/` with `_`)
47normalize-rust-cfg = $(subst /,_,$(subst -,_,$(shell echo $1 | tr '[:lower:]' '[:upper:]')))
48
49# test if two files are different, replacing the first
50# with the second if so
51# args: $1 - temporary file to test
52#       $2 - file to replace
53define TESTANDREPLACEFILE
54	if [ -f "$2" ]; then \
55		if cmp "$1" "$2"; then \
56			rm -f $1; \
57		else \
58			mv $1 $2; \
59		fi \
60	else \
61		mv $1 $2; \
62	fi
63endef
64
65# generate a header file at $1 with an expanded variable in $2
66define MAKECONFIGHEADER
67	$(MKDIR); \
68	rm -f $1.tmp; \
69	LDEF=`echo $1 | tr '/\\.-' '_' | sed "s/C++/CPP/g;s/c++/cpp/g"`; \
70	echo \#ifndef __$${LDEF}_H > $1.tmp; \
71	echo \#define __$${LDEF}_H >> $1.tmp; \
72	for d in `echo $($2) | tr '[:lower:]' '[:upper:]'`; do \
73		echo "#define $$d" | sed "s/=/\ /g;s/-/_/g;s/\//_/g;s/\./_/g;s/\//_/g;s/C++/CPP/g" >> $1.tmp; \
74	done; \
75	echo \#endif >> $1.tmp; \
76	$(call TESTANDREPLACEFILE,$1.tmp,$1)
77endef
78
79# Map LK's arch names into a more common form.
80define standard_name_for_arch
81ifeq ($(2),arm)
82$(1) := arm
83else
84ifeq ($(2),arm64)
85$(1) := aarch64
86else
87ifeq ($(2),x86)
88ifeq ($(3),x86-64)
89$(1) := x86_64
90else
91ifeq ($(3),x86-32)
92$(1) := i386
93else
94$$(error "unknown arch: $(2) / $(3)")
95endif
96endif
97else
98$$(error "unknown arch: $(2) / $(3)")
99endif
100endif
101endif
102endef
103
104# prints task status message
105# Format: INFO/ECHO module, status, message
106ifneq ($(LOG_POSTPROCESSING),)
107# this output will be postprocessed by python later, insert extra markers
108# for easier parsing
109LOG_PREFIX=@log@
110LOG_DONE=@done@
111LOG_SDONE=@sdone@
112LOG_PRINT=@print@
113LOG_SEPARATOR=@:@
114INFO_LOG = $(info $(LOG_PREFIX)$(LOG_PRINT)$1)
115INFO = $(info $(LOG_PREFIX)$1$(LOG_SEPARATOR)$2$(LOG_SEPARATOR)$3)
116INFO_DONE = $(info $(LOG_PREFIX)$(LOG_DONE)$1$(LOG_SEPARATOR)$2$(LOG_SEPARATOR)$3)
117INFO_DONE_SILENT = $(info $(LOG_PREFIX)$(LOG_SDONE)$1$(LOG_SEPARATOR)$2$(LOG_SEPARATOR)$3)
118ECHO_LOG = echo $(LOG_PREFIX)$(LOG_PRINT)$1
119ECHO = echo $(LOG_PREFIX)$1$(LOG_SEPARATOR)$2$(LOG_SEPARATOR)$3
120ECHO_DONE = echo $(LOG_PREFIX)$(LOG_DONE)$1$(LOG_SEPARATOR)$2$(LOG_SEPARATOR)$3
121ECHO_DONE_SILENT = echo $(LOG_PREFIX)$(LOG_SDONE)$1$(LOG_SEPARATOR)$2$(LOG_SEPARATOR)$3
122else
123# just output as regular
124INFO_LOG = $(info $1)
125INFO = $(info $2 $3 for $1)
126INFO_DONE = $(info $2 $3 for $1)
127INFO_DONE_SILENT =
128ECHO_LOG = echo $1
129ECHO = echo $2 $3
130ECHO_DONE = echo $2 $3
131ECHO_DONE_SILENT =
132endif
133
134