1# Copyright (C) 2009 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15 16# These definitions contain a few host-specific functions. I.e. they are 17# typically used to generate shell commands during the build and their 18# implementation will depend on the value of the HOST_OS variable. 19# 20 21# ----------------------------------------------------------------------------- 22# Function : host-path 23# Arguments: 1: file path 24# Returns : file path, as understood by the host file system 25# Usage : $(call host-path,<path>) 26# Rationale: This function is used to translate Cygwin paths into 27# Cygwin-specific ones. On other platforms, it will just 28# return its argument. 29# ----------------------------------------------------------------------------- 30ifeq ($(HOST_OS),cygwin) 31host-path = $(if $(strip $1),$(call cygwin-to-host-path,$1)) 32else 33host-path = $1 34endif 35 36# ----------------------------------------------------------------------------- 37# Function : host-rm 38# Arguments: 1: list of files 39# Usage : $(call host-rm,<files>) 40# Rationale: This function expands to the host-specific shell command used 41# to remove some files. 42# ----------------------------------------------------------------------------- 43ifeq ($(HOST_OS),windows) 44host-rm = \ 45 $(eval __host_rm_files := $(foreach __host_rm_file,$1,$(subst /,\,$(wildcard $(__host_rm_file)))))\ 46 $(if $(__host_rm_files),del /f/q $(__host_rm_files) >NUL 2>NUL) 47else 48host-rm = rm -f $1 49endif 50 51# ----------------------------------------------------------------------------- 52# Function : host-rmdir 53# Arguments: 1: list of files or directories 54# Usage : $(call host-rm,<files>) 55# Rationale: This function expands to the host-specific shell command used 56# to remove some files _and_ directories. 57# ----------------------------------------------------------------------------- 58ifeq ($(HOST_OS),windows) 59host-rmdir = \ 60 $(eval __host_rmdir_files := $(foreach __host_rmdir_file,$1,$(subst /,\,$(wildcard $(__host_rmdir_file)))))\ 61 $(if $(__host_rmdir_files),del /f/s/q $(__host_rmdir_files) >NUL 2>NUL) 62else 63host-rmdir = rm -rf $1 64endif 65 66# ----------------------------------------------------------------------------- 67# Function : host-mkdir 68# Arguments: 1: directory path 69# Usage : $(call host-mkdir,<path> 70# Rationale: This function expands to the host-specific shell command used 71# to create a path if it doesn't exist. 72# ----------------------------------------------------------------------------- 73ifeq ($(HOST_OS),windows) 74host-mkdir = md $(subst /,\,"$1") >NUL 2>NUL || rem 75else 76host-mkdir = mkdir -p $1 77endif 78 79# ----------------------------------------------------------------------------- 80# Function : host-cp 81# Arguments: 1: source file 82# 2: target file 83# Usage : $(call host-cp,<src-file>,<dst-file>) 84# Rationale: This function expands to the host-specific shell command used 85# to copy a single file 86# ----------------------------------------------------------------------------- 87ifeq ($(HOST_OS),windows) 88host-cp = copy /b/y $(subst /,\,"$1" "$2") > NUL 89else 90host-cp = cp -f $1 $2 91endif 92 93# ----------------------------------------------------------------------------- 94# Function : host-mv 95# Arguments: 1: source file 96# 2: target file 97# Usage : $(call host-mv,<src-file>,<dst-file>) 98# Rationale: This function expands to the host-specific shell command used 99# to move a single file 100# ----------------------------------------------------------------------------- 101ifeq ($(HOST_OS),windows) 102host-mv = move /y $(subst /,\,"$1" "$2") > NUL 103else 104host-mv = mv -f $1 $2 105endif 106 107# ----------------------------------------------------------------------------- 108# Function : host-install 109# Arguments: 1: source file 110# 2: target file 111# Usage : $(call host-install,<src-file>,<dst-file>) 112# Rationale: This function expands to the host-specific shell command used 113# to install a file or directory, while preserving its timestamps 114# (if possible). 115# ----------------------------------------------------------------------------- 116ifeq ($(HOST_OS),windows) 117host-install = copy /b/y $(subst /,\,"$1" "$2") > NUL 118else 119host-install = install -p $1 $2 120endif 121 122# ----------------------------------------------------------------------------- 123# Function : host-echo-build-step 124# Arguments: 1: ABI 125# 2: Step description (e.g. 'Compile C++', or 'StaticLibrary') 126# Usage : ---->|$(call host-echo-build-step,Compile) ....other text... 127# Rationale: This function expands to the host-specific shell command used 128# to print the prefix of a given build step / command. 129# ----------------------------------------------------------------------------- 130host-echo-build-step = @ $(HOST_ECHO) [$1] $(call left-justify-quoted-15,$2): 131 132# ----------------------------------------------------------------------------- 133# Function : host-c-includes 134# Arguments: 1: list of file paths (e.g. "foo bar") 135# Returns : list of include compiler options (e.g. "-Ifoo -Ibar") 136# Usage : $(call host-c-includes,<paths>) 137# Rationale: This function is used to translate Cygwin paths into 138# Cygwin-specific ones. On other platforms, it will just 139# return its argument. 140# ----------------------------------------------------------------------------- 141ifeq ($(HOST_OS),cygwin) 142host-c-includes = $(patsubst %,-I%,$(call host-path,$1)) 143else 144host-c-includes = $(1:%=-I%) 145endif 146 147# ----------------------------------------------------------------------------- 148# Function : host-copy-if-differ 149# Arguments: 1: source file 150# 2: destination file 151# Usage : $(call host-copy-if-differ,<src-file>,<dst-file>) 152# Rationale: This function copy source file to destination file if contents are 153# different. 154# ----------------------------------------------------------------------------- 155ifeq ($(HOST_OS),windows) 156host-copy-if-differ = $(HOST_CMP) -s $1 $2 > NUL || copy /b/y $(subst /,\,"$1" "$2") > NUL 157else 158host-copy-if-differ = $(HOST_CMP) -s $1 $2 > /dev/null 2>&1 || cp -f $1 $2 159endif 160 161 162# ----------------------------------------------------------------------------- 163# Function : host-path-is-absolute 164# Arguments: 1: file path 165# Usage : $(call host-path-is-absolute,<path>) 166# Rationale: This function returns a non-empty result if the input path is 167# absolute on the host filesystem. 168# ----------------------------------------------------------------------------- 169 170# On Windows, we need to take care drive prefix in file paths, e.g.: 171# /foo -> top-level 'foo' directory on current drive. 172# //bar/foo -> top-level 'foo' on network share 'bar' 173# c:/foo -> top-level 'foo' directory on C drive. 174# c:foo -> 'foo' subdirectory on C drive's current directory. 175# 176# Treat all of them as absolute. Filtering the first two cases is easy 177# by simply looking at the first character. The other ones are more 178# complicated and the simplest way is still to try all alphabet letters 179# directly. Anything else involves very complicated GNU Make parsing 180# voodoo. 181ndk-windows-drive-letters := a b c d e f g h i j k l m n o p q r s t u v w x y z \ 182 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 183 184ndk-windows-drive-patterns := $(foreach _drive,$(ndk-windows-drive-letters),$(_drive):%) 185 186windows-path-is-absolute = $(if $(filter /% $(ndk-windows-drive-patterns),$(subst \,/,$1)),true) 187 188ifeq ($(HOST_OS),windows) 189host-path-is-absolute = $(call windows-path-is-absolute,$1) 190else 191host-path-is-absolute = $(if $(filter /%,$1),true) 192endif 193 194-test-host-path-is-absolute.relative-paths = \ 195 $(call test-expect,,$(call host-path-is-absolute,foo))\ 196 $(call test-expect,,$(call host-path-is-absolute,foo/bar))\ 197 $(call test-expect,,$(call host-path-is-absolute,.))\ 198 $(call test-expect,,$(call host-path-is-absolute,..)) 199 200-test-host-path-is-absolute.absolute-paths = \ 201 $(call test-expect,true,$(call host-path-is-absolute,/))\ 202 $(call test-expect,true,$(call host-path-is-absolute,/foo))\ 203 $(call test-expect,true,$(call host-path-is-absolute,/foo/bar))\ 204 $(call test-expect,true,$(call host-path-is-absolute,//foo))\ 205 $(call test-expect,true,$(call host-path-is-absolute,/.)) 206 207-test-host-path-is-asbolute.windows-relative-paths = \ 208 $(call test-expect,$(call windows-path-is-absolute,foo))\ 209 $(call test-expect,$(call windows-path-is-absolute,foo/bar))\ 210 $(call test-expect,$(call windows-path-is-absolute,.))\ 211 $(call test-expect,$(call windows-path-is-absolute,..)) 212 213-test-host-path-is-asbolute.windows-absolute-paths = \ 214 $(call test-expect,true,$(call windows-path-is-absolute,c:/))\ 215 $(call test-expect,true,$(call windows-path-is-absolute,x:))\ 216 $(call test-expect,true,$(call windows-path-is-absolute,K:foo))\ 217 $(call test-expect,true,$(call windows-path-is-absolute,C:\Foo\Bar))\ 218 $(call test-expect,true,$(call windows-path-is-absolute,\Foo)) 219