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 # ----------------------------------------------------------------------------- 30 ifeq ($(HOST_OS),cygwin) 31 host-path = $(if $(strip $1),$(call cygwin-to-host-path,$1)) 32 else 33 host-path = $1 34 endif 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 # ----------------------------------------------------------------------------- 43 ifeq ($(HOST_OS),windows) 44 host-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) 47 else 48 host-rm = rm -f $1 49 endif 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 # ----------------------------------------------------------------------------- 58 ifeq ($(HOST_OS),windows) 59 host-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) 62 else 63 host-rmdir = rm -rf $1 64 endif 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 # ----------------------------------------------------------------------------- 73 ifeq ($(HOST_OS),windows) 74 host-mkdir = md $(subst /,\,"$1") >NUL 2>NUL || rem 75 else 76 host-mkdir = mkdir -p $1 77 endif 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 # ----------------------------------------------------------------------------- 87 ifeq ($(HOST_OS),windows) 88 host-cp = copy /b/y $(subst /,\,"$1" "$2") > NUL 89 else 90 host-cp = cp -f $1 $2 91 endif 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 # ----------------------------------------------------------------------------- 101 ifeq ($(HOST_OS),windows) 102 host-mv = move /y $(subst /,\,"$1" "$2") > NUL 103 else 104 host-mv = mv -f $1 $2 105 endif 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 # ----------------------------------------------------------------------------- 116 ifeq ($(HOST_OS),windows) 117 host-install = copy /b/y $(subst /,\,"$1" "$2") > NUL 118 else 119 host-install = install -p $1 $2 120 endif 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 # ----------------------------------------------------------------------------- 130 host-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 # ----------------------------------------------------------------------------- 141 ifeq ($(HOST_OS),cygwin) 142 host-c-includes = $(patsubst %,-I%,$(call host-path,$1)) 143 else 144 host-c-includes = $(1:%=-I%) 145 endif 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 # ----------------------------------------------------------------------------- 155 ifeq ($(HOST_OS),windows) 156 host-copy-if-differ = $(HOST_CMP) -s $1 $2 > NUL || copy /b/y $(subst /,\,"$1" "$2") > NUL 157 else 158 host-copy-if-differ = $(HOST_CMP) -s $1 $2 > /dev/null 2>&1 || cp -f $1 $2 159 endif 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. 181 ndk-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 184 ndk-windows-drive-patterns := $(foreach _drive,$(ndk-windows-drive-letters),$(_drive):%) 185 186 windows-path-is-absolute = $(if $(filter /% $(ndk-windows-drive-patterns),$(subst \,/,$1)),true) 187 188 ifeq ($(HOST_OS),windows) 189 host-path-is-absolute = $(call windows-path-is-absolute,$1) 190 else 191 host-path-is-absolute = $(if $(filter /%,$1),true) 192 endif 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