Home | History | Annotate | Download | only in core
      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-install
     95 # Arguments: 1: source file
     96 #            2: target file
     97 # Usage    : $(call host-install,<src-file>,<dst-file>)
     98 # Rationale: This function expands to the host-specific shell command used
     99 #            to install a file or directory, while preserving its timestamps
    100 #            (if possible).
    101 # -----------------------------------------------------------------------------
    102 ifeq ($(HOST_OS),windows)
    103 host-install = copy /b/y $(subst /,\,"$1" "$2") > NUL
    104 else
    105 host-install = install -p $1 $2
    106 endif
    107 
    108 # -----------------------------------------------------------------------------
    109 # Function : host-c-includes
    110 # Arguments: 1: list of file paths (e.g. "foo bar")
    111 # Returns  : list of include compiler options (e.g. "-Ifoo -Ibar")
    112 # Usage    : $(call host-c-includes,<paths>)
    113 # Rationale: This function is used to translate Cygwin paths into
    114 #            Cygwin-specific ones. On other platforms, it will just
    115 #            return its argument.
    116 # -----------------------------------------------------------------------------
    117 ifeq ($(HOST_OS),cygwin)
    118 host-c-includes = $(patsubst %,-I%,$(call host-path,$1))
    119 else
    120 host-c-includes = $(1:%=-I%)
    121 endif
    122 
    123 # -----------------------------------------------------------------------------
    124 # Function : host-copy-if-differ
    125 # Arguments: 1: source file
    126 #            2: destination file
    127 # Usage    : $(call host-copy-if-differ,<src-file>,<dst-file>)
    128 # Rationale: This function copy source file to destination file if contents are
    129 #            different.
    130 # -----------------------------------------------------------------------------
    131 ifeq ($(HOST_OS),windows)
    132 host-copy-if-differ = $(HOST_CMP) -s $1 $2 > NUL || copy /b/y $(subst /,\,"$1" "$2") > NUL
    133 else
    134 host-copy-if-differ = $(HOST_CMP) -s $1 $2 > /dev/null 2>&1 || cp -f $1 $2
    135 endif
    136 
    137 
    138 # -----------------------------------------------------------------------------
    139 # Function : host-path-is-absolute
    140 # Arguments: 1: file path
    141 # Usage    : $(call host-path-is-absolute,<path>)
    142 # Rationale: This function returns a non-empty result if the input path is
    143 #            absolute on the host filesystem.
    144 # -----------------------------------------------------------------------------
    145 
    146 # On Windows, we need to take care drive prefix in file paths, e.g.:
    147 #    /foo       -> top-level 'foo' directory on current drive.
    148 #    //bar/foo  -> top-level 'foo' on network share 'bar'
    149 #    c:/foo     -> top-level 'foo' directory on C drive.
    150 #    c:foo      -> 'foo' subdirectory on C drive's current directory.
    151 #
    152 # Treat all of them as absolute. Filtering the first two cases is easy
    153 # by simply looking at the first character. The other ones are more
    154 # complicated and the simplest way is still to try all alphabet letters
    155 # directly. Anything else involves very complicated GNU Make parsing
    156 # voodoo.
    157 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 \
    158                              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
    159 
    160 ndk-windows-drive-patterns := $(foreach _drive,$(ndk-windows-drive-letters),$(_drive):%)
    161 
    162 windows-path-is-absolute = $(if $(filter /% $(ndk-windows-drive-patterns),$(subst \,/,$1)),true)
    163 
    164 ifeq ($(HOST_OS),windows)
    165 host-path-is-absolute = $(call windows-path-is-absolute,$1)
    166 else
    167 host-path-is-absolute = $(if $(filter /%,$1),true)
    168 endif
    169 
    170 -test-host-path-is-absolute.relative-paths = \
    171   $(call test-expect,,$(call host-path-is-absolute,foo))\
    172   $(call test-expect,,$(call host-path-is-absolute,foo/bar))\
    173   $(call test-expect,,$(call host-path-is-absolute,.))\
    174   $(call test-expect,,$(call host-path-is-absolute,..))
    175 
    176 -test-host-path-is-absolute.absolute-paths = \
    177   $(call test-expect,true,$(call host-path-is-absolute,/))\
    178   $(call test-expect,true,$(call host-path-is-absolute,/foo))\
    179   $(call test-expect,true,$(call host-path-is-absolute,/foo/bar))\
    180   $(call test-expect,true,$(call host-path-is-absolute,//foo))\
    181   $(call test-expect,true,$(call host-path-is-absolute,/.))
    182 
    183 -test-host-path-is-asbolute.windows-relative-paths = \
    184   $(call test-expect,$(call windows-path-is-absolute,foo))\
    185   $(call test-expect,$(call windows-path-is-absolute,foo/bar))\
    186   $(call test-expect,$(call windows-path-is-absolute,.))\
    187   $(call test-expect,$(call windows-path-is-absolute,..))
    188 
    189 -test-host-path-is-asbolute.windows-absolute-paths = \
    190   $(call test-expect,true,$(call windows-path-is-absolute,c:/))\
    191   $(call test-expect,true,$(call windows-path-is-absolute,x:))\
    192   $(call test-expect,true,$(call windows-path-is-absolute,K:foo))\
    193   $(call test-expect,true,$(call windows-path-is-absolute,C:\Foo\Bar))\
    194   $(call test-expect,true,$(call windows-path-is-absolute,\Foo))
    195