-
Notifications
You must be signed in to change notification settings - Fork 306
/
macros.mk
104 lines (89 loc) · 3.47 KB
/
macros.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright 2016 The Fuchsia Authors
# Copyright (c) 2008-2015 Travis Geiselbrecht
#
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT
# Find the local dir of the make file
GET_LOCAL_DIR = $(patsubst %/,%,$(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))))
# makes sure the target dir exists
MKDIR = if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi
# prepends the BUILD_DIR var to each item in the list
TOBUILDDIR = $(addprefix $(BUILDDIR)/,$(1))
TOMODULEDIR = $(addprefix $(MODULE_BUILDDIR)/,$(1))
# converts specified variable to boolean value
TOBOOL = $(if $(filter-out 0 false,$1),true,false)
COMMA := ,
SPACE :=
SPACE +=
# lower case and upper case translation
LC = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
UC = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1))))))))))))))))))))))))))
# conditionally echo text passed in
ifeq ($(call TOBOOL,$(QUIET)),false)
BUILDECHO = @echo $(1)
CMP_QUIET =
else
BUILDECHO =
CMP_QUIET = ">/dev/null"
endif
# test if two files are different, replacing the first
# with the second if so
# args: $1 - temporary file to test
# $2 - file to replace
define TESTANDREPLACEFILE
if [ -f "$2" ]; then \
if cmp "$1" "$2$(CMP_QUIET)"; then \
rm -f $1; \
else \
mv $1 $2; \
fi \
else \
mv $1 $2; \
fi
endef
# replace all characters or sequences of letters in defines to convert to a proper C style variable
MAKECVAR=$(subst C++,CPP,$(subst -,_,$(subst /,_,$(subst .,_,$1))))
# generate a header file at $1 with an expanded variable in $2
# $3 provides an (optional) raw footer to append to the end
# NOTE: the left side of the variable will be upper cased and some symbols replaced
# to be valid C names (see MAKECVAR above).
# The right side of the #define can be any valid C but cannot contain spaces, even
# inside a string.
define MAKECONFIGHEADER
$(MKDIR); \
echo '#pragma once' > $1.tmp; \
$(foreach var,$($(2)), \
echo \#define \
$(firstword $(subst =,$(SPACE),$(call MAKECVAR,$(call UC,$(var))))) \
$(if $(findstring =,$(var)),$(subst $(firstword $(subst =,$(SPACE),$(var)))=,,$(var))) \
>> $1.tmp;) \
echo $3 >> $1.tmp; \
$(call TESTANDREPLACEFILE,$1.tmp,$1)
endef
# invoke a command $(1), using arguments $(2), putting those arguments
# into a command file if this version of Make supports it.
ifeq (4.0,$(firstword $(sort $(MAKE_VERSION) 4.0)))
define BUILDCMD =
$(shell $(MKDIR))
$(if $(NOECHO),,$(info echo "$(2)" > [email protected]))
$(file >[email protected],$(2))
$(NOECHO)$(1) @[email protected]
endef
else
BUILDCMD = $(NOECHO)$(1) $(2)
endif
define generate-copy-dst-src
$1: $2
@$$(MKDIR)
$(call BUILDECHO,installing $$@)
$$(NOECHO) cp -f $$< $$@
endef
copy-dst-src = $(eval $(call generate-copy-dst-src,$(strip $1),$(strip $2)))
HOST_PLATFORM := $(shell uname -s | tr '[:upper:]' '[:lower:]')
ifeq ($(HOST_PLATFORM), zircon)
SHELLEXEC = /boot/bin/sh
else
SHELLEXEC =
endif
HOST_ARCH := $(shell uname -m)