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