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