^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) # This allows us to work with the newline character:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) define newline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) endef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) newline := $(newline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # nl-escape
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # Usage: escape = $(call nl-escape[,escape])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # This is used as the common way to specify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # what should replace a newline when escaping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # newlines; the default is a bizarre string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) nl-escape = $(if $(1),$(1),m822df3020w6a44id34bt574ctac44eb9f4n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) # escape-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # Usage: escaped-text = $(call escape-nl,text[,escape])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # GNU make's $(shell ...) function converts to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) # single space each newline character in the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # produced during the expansion; this may not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) # desirable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # The only solution is to change each newline into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) # something that won't be converted, so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) # information can be recovered later with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) # $(call unescape-nl...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) escape-nl = $(subst $(newline),$(call nl-escape,$(2)),$(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) # unescape-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) # Usage: text = $(call unescape-nl,escaped-text[,escape])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) # See escape-nl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unescape-nl = $(subst $(call nl-escape,$(2)),$(newline),$(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # shell-escape-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # Usage: $(shell some-command | $(call shell-escape-nl[,escape]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) # Use this to escape newlines from within a shell call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) # the default escape is a bizarre string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) # NOTE: The escape is used directly as a string constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # in an `awk' program that is delimited by shell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) # single-quotes, so be wary of the characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) # that are chosen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) define shell-escape-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) awk 'NR==1 {t=$$0} NR>1 {t=t "$(nl-escape)" $$0} END {printf t}'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) endef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) # shell-unescape-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) # Usage: $(shell some-command | $(call shell-unescape-nl[,escape]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) # Use this to unescape newlines from within a shell call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) # the default escape is a bizarre string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) # NOTE: The escape is used directly as an extended regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) # expression constant in an `awk' program that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) # delimited by shell single-quotes, so be wary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) # of the characters that are chosen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) # (The bash shell has a bug where `{gsub(...),...}' is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) # misinterpreted as a brace expansion; this can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) # overcome by putting a space between `{' and `gsub').
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) define shell-unescape-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) awk 'NR==1 {t=$$0} NR>1 {t=t "\n" $$0} END { gsub(/$(nl-escape)/,"\n",t); printf t }'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) endef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) # escape-for-shell-sq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) # Usage: embeddable-text = $(call escape-for-shell-sq,text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) # This function produces text that is suitable for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) # embedding in a shell string that is delimited by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) # single-quotes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) escape-for-shell-sq = $(subst ','\'',$(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # shell-sq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) # Usage: single-quoted-and-escaped-text = $(call shell-sq,text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) shell-sq = '$(escape-for-shell-sq)'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) # shell-wordify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) # Usage: wordified-text = $(call shell-wordify,text)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) # For instance:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) # |define text
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) # |hello
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) # |world
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) # |endef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) # |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) # |target:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) # | echo $(call shell-wordify,$(text))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) # At least GNU make gets confused by expanding a newline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) # within the context of a command line of a makefile rule
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) # (this is in constrast to a `$(shell ...)' function call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) # which can handle it just fine).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) # This function avoids the problem by producing a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) # that works as a shell word, regardless of whether or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) # not it contains a newline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) # If the text to be wordified contains a newline, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) # an intrictate shell command substitution is constructed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) # to render the text as a single line; when the shell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) # processes the resulting escaped text, it transforms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) # it into the original unescaped text.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) # If the text does not contain a newline, then this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) # produces the same results as the `$(shell-sq)' function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) shell-wordify = $(if $(findstring $(newline),$(1)),$(_sw-esc-nl),$(shell-sq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) define _sw-esc-nl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "$$(echo $(call escape-nl,$(shell-sq),$(2)) | $(call shell-unescape-nl,$(2)))"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) endef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) # is-absolute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) # Usage: bool-value = $(call is-absolute,path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) is-absolute = $(shell echo $(shell-sq) | grep -q ^/ && echo y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) # lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) # Usage: absolute-executable-path-or-empty = $(call lookup,path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) # (It's necessary to use `sh -c' because GNU make messes up by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) # trying too hard and getting things wrong).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) lookup = $(call unescape-nl,$(shell sh -c $(_l-sh)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) _l-sh = $(call shell-sq,command -v $(shell-sq) | $(call shell-escape-nl,))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) # is-executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) # Usage: bool-value = $(call is-executable,path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) # (It's necessary to use `sh -c' because GNU make messes up by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) # trying too hard and getting things wrong).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) is-executable = $(call _is-executable-helper,$(shell-sq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) _is-executable-helper = $(shell sh -c $(_is-executable-sh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) _is-executable-sh = $(call shell-sq,test -f $(1) -a -x $(1) && echo y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) # get-executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) # Usage: absolute-executable-path-or-empty = $(call get-executable,path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) # The goal is to get an absolute path for an executable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) # the `command -v' is defined by POSIX, but it's not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) # necessarily very portable, so it's only used if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) # relative path resolution is requested, as determined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) # by the presence of a leading `/'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) get-executable = $(if $(1),$(if $(is-absolute),$(_ge-abspath),$(lookup)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) _ge-abspath = $(if $(is-executable),$(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) # get-supplied-or-default-executable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) # Usage: absolute-executable-path-or-empty = $(call get-executable-or-default,variable,default)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) define get-executable-or-default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) $(if $($(1)),$(call _ge_attempt,$($(1)),$(1)),$(call _ge_attempt,$(2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) endef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) _ge_attempt = $(if $(get-executable),$(get-executable),$(call _gea_err,$(2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) _gea_err = $(if $(1),$(error Please set '$(1)' appropriately))