Back to home page

LXR

 
 

    


0001 # This allows us to work with the newline character:
0002 define newline
0003 
0004 
0005 endef
0006 newline := $(newline)
0007 
0008 # nl-escape
0009 #
0010 # Usage: escape = $(call nl-escape[,escape])
0011 #
0012 # This is used as the common way to specify
0013 # what should replace a newline when escaping
0014 # newlines; the default is a bizarre string.
0015 #
0016 nl-escape = $(if $(1),$(1),m822df3020w6a44id34bt574ctac44eb9f4n)
0017 
0018 # escape-nl
0019 #
0020 # Usage: escaped-text = $(call escape-nl,text[,escape])
0021 #
0022 # GNU make's $(shell ...) function converts to a
0023 # single space each newline character in the output
0024 # produced during the expansion; this may not be
0025 # desirable.
0026 #
0027 # The only solution is to change each newline into
0028 # something that won't be converted, so that the
0029 # information can be recovered later with
0030 # $(call unescape-nl...)
0031 #
0032 escape-nl = $(subst $(newline),$(call nl-escape,$(2)),$(1))
0033 
0034 # unescape-nl
0035 #
0036 # Usage: text = $(call unescape-nl,escaped-text[,escape])
0037 #
0038 # See escape-nl.
0039 #
0040 unescape-nl = $(subst $(call nl-escape,$(2)),$(newline),$(1))
0041 
0042 # shell-escape-nl
0043 #
0044 # Usage: $(shell some-command | $(call shell-escape-nl[,escape]))
0045 #
0046 # Use this to escape newlines from within a shell call;
0047 # the default escape is a bizarre string.
0048 #
0049 # NOTE: The escape is used directly as a string constant
0050 #       in an `awk' program that is delimited by shell
0051 #       single-quotes, so be wary of the characters
0052 #       that are chosen.
0053 #
0054 define shell-escape-nl
0055 awk 'NR==1 {t=$$0} NR>1 {t=t "$(nl-escape)" $$0} END {printf t}'
0056 endef
0057 
0058 # shell-unescape-nl
0059 #
0060 # Usage: $(shell some-command | $(call shell-unescape-nl[,escape]))
0061 #
0062 # Use this to unescape newlines from within a shell call;
0063 # the default escape is a bizarre string.
0064 #
0065 # NOTE: The escape is used directly as an extended regular
0066 #       expression constant in an `awk' program that is
0067 #       delimited by shell single-quotes, so be wary
0068 #       of the characters that are chosen.
0069 #
0070 # (The bash shell has a bug where `{gsub(...),...}' is
0071 #  misinterpreted as a brace expansion; this can be
0072 #  overcome by putting a space between `{' and `gsub').
0073 #
0074 define shell-unescape-nl
0075 awk 'NR==1 {t=$$0} NR>1 {t=t "\n" $$0} END { gsub(/$(nl-escape)/,"\n",t); printf t }'
0076 endef
0077 
0078 # escape-for-shell-sq
0079 #
0080 # Usage: embeddable-text = $(call escape-for-shell-sq,text)
0081 #
0082 # This function produces text that is suitable for
0083 # embedding in a shell string that is delimited by
0084 # single-quotes.
0085 #
0086 escape-for-shell-sq =  $(subst ','\'',$(1))
0087 
0088 # shell-sq
0089 #
0090 # Usage: single-quoted-and-escaped-text = $(call shell-sq,text)
0091 #
0092 shell-sq = '$(escape-for-shell-sq)'
0093 
0094 # shell-wordify
0095 #
0096 # Usage: wordified-text = $(call shell-wordify,text)
0097 #
0098 # For instance:
0099 #
0100 #  |define text
0101 #  |hello
0102 #  |world
0103 #  |endef
0104 #  |
0105 #  |target:
0106 #  |    echo $(call shell-wordify,$(text))
0107 #
0108 # At least GNU make gets confused by expanding a newline
0109 # within the context of a command line of a makefile rule
0110 # (this is in constrast to a `$(shell ...)' function call,
0111 # which can handle it just fine).
0112 #
0113 # This function avoids the problem by producing a string
0114 # that works as a shell word, regardless of whether or
0115 # not it contains a newline.
0116 #
0117 # If the text to be wordified contains a newline, then
0118 # an intrictate shell command substitution is constructed
0119 # to render the text as a single line; when the shell
0120 # processes the resulting escaped text, it transforms
0121 # it into the original unescaped text.
0122 #
0123 # If the text does not contain a newline, then this function
0124 # produces the same results as the `$(shell-sq)' function.
0125 #
0126 shell-wordify = $(if $(findstring $(newline),$(1)),$(_sw-esc-nl),$(shell-sq))
0127 define _sw-esc-nl
0128 "$$(echo $(call escape-nl,$(shell-sq),$(2)) | $(call shell-unescape-nl,$(2)))"
0129 endef
0130 
0131 # is-absolute
0132 #
0133 # Usage: bool-value = $(call is-absolute,path)
0134 #
0135 is-absolute = $(shell echo $(shell-sq) | grep -q ^/ && echo y)
0136 
0137 # lookup
0138 #
0139 # Usage: absolute-executable-path-or-empty = $(call lookup,path)
0140 #
0141 # (It's necessary to use `sh -c' because GNU make messes up by
0142 #  trying too hard and getting things wrong).
0143 #
0144 lookup = $(call unescape-nl,$(shell sh -c $(_l-sh)))
0145 _l-sh = $(call shell-sq,command -v $(shell-sq) | $(call shell-escape-nl,))
0146 
0147 # is-executable
0148 #
0149 # Usage: bool-value = $(call is-executable,path)
0150 #
0151 # (It's necessary to use `sh -c' because GNU make messes up by
0152 #  trying too hard and getting things wrong).
0153 #
0154 is-executable = $(call _is-executable-helper,$(shell-sq))
0155 _is-executable-helper = $(shell sh -c $(_is-executable-sh))
0156 _is-executable-sh = $(call shell-sq,test -f $(1) -a -x $(1) && echo y)
0157 
0158 # get-executable
0159 #
0160 # Usage: absolute-executable-path-or-empty = $(call get-executable,path)
0161 #
0162 # The goal is to get an absolute path for an executable;
0163 # the `command -v' is defined by POSIX, but it's not
0164 # necessarily very portable, so it's only used if
0165 # relative path resolution is requested, as determined
0166 # by the presence of a leading `/'.
0167 #
0168 get-executable = $(if $(1),$(if $(is-absolute),$(_ge-abspath),$(lookup)))
0169 _ge-abspath = $(if $(is-executable),$(1))
0170 
0171 # get-supplied-or-default-executable
0172 #
0173 # Usage: absolute-executable-path-or-empty = $(call get-executable-or-default,variable,default)
0174 #
0175 define get-executable-or-default
0176 $(if $($(1)),$(call _ge_attempt,$($(1)),$(1)),$(call _ge_attempt,$(2)))
0177 endef
0178 _ge_attempt = $(if $(get-executable),$(get-executable),$(call _gea_err,$(2)))
0179 _gea_err  = $(if $(1),$(error Please set '$(1)' appropriately))