200 lines · plain
1# This allows us to work with the newline character:2define newline3 4 5endef6newline := $(newline)7 8# nl-escape9#10# Usage: escape = $(call nl-escape[,escape])11#12# This is used as the common way to specify13# what should replace a newline when escaping14# newlines; the default is a bizarre string.15#16nl-escape = $(if $(1),$(1),m822df3020w6a44id34bt574ctac44eb9f4n)17 18# escape-nl19#20# Usage: escaped-text = $(call escape-nl,text[,escape])21#22# GNU make's $(shell ...) function converts to a23# single space each newline character in the output24# produced during the expansion; this may not be25# desirable.26#27# The only solution is to change each newline into28# something that won't be converted, so that the29# information can be recovered later with30# $(call unescape-nl...)31#32escape-nl = $(subst $(newline),$(call nl-escape,$(2)),$(1))33 34# unescape-nl35#36# Usage: text = $(call unescape-nl,escaped-text[,escape])37#38# See escape-nl.39#40unescape-nl = $(subst $(call nl-escape,$(2)),$(newline),$(1))41 42# shell-escape-nl43#44# Usage: $(shell some-command | $(call shell-escape-nl[,escape]))45#46# Use this to escape newlines from within a shell call;47# the default escape is a bizarre string.48#49# NOTE: The escape is used directly as a string constant50# in an `awk' program that is delimited by shell51# single-quotes, so be wary of the characters52# that are chosen.53#54define shell-escape-nl55awk 'NR==1 {t=$$0} NR>1 {t=t "$(nl-escape)" $$0} END {printf t}'56endef57 58# shell-unescape-nl59#60# Usage: $(shell some-command | $(call shell-unescape-nl[,escape]))61#62# Use this to unescape newlines from within a shell call;63# the default escape is a bizarre string.64#65# NOTE: The escape is used directly as an extended regular66# expression constant in an `awk' program that is67# delimited by shell single-quotes, so be wary68# of the characters that are chosen.69#70# (The bash shell has a bug where `{gsub(...),...}' is71# misinterpreted as a brace expansion; this can be72# overcome by putting a space between `{' and `gsub').73#74define shell-unescape-nl75awk 'NR==1 {t=$$0} NR>1 {t=t "\n" $$0} END { gsub(/$(nl-escape)/,"\n",t); printf t }'76endef77 78# escape-for-shell-sq79#80# Usage: embeddable-text = $(call escape-for-shell-sq,text)81#82# This function produces text that is suitable for83# embedding in a shell string that is delimited by84# single-quotes.85#86escape-for-shell-sq = $(subst ','\'',$(1))87 88# shell-sq89#90# Usage: single-quoted-and-escaped-text = $(call shell-sq,text)91#92shell-sq = '$(escape-for-shell-sq)'93 94# shell-wordify95#96# Usage: wordified-text = $(call shell-wordify,text)97#98# For instance:99#100# |define text101# |hello102# |world103# |endef104# |105# |target:106# | echo $(call shell-wordify,$(text))107#108# At least GNU make gets confused by expanding a newline109# within the context of a command line of a makefile rule110# (this is in constrast to a `$(shell ...)' function call,111# which can handle it just fine).112#113# This function avoids the problem by producing a string114# that works as a shell word, regardless of whether or115# not it contains a newline.116#117# If the text to be wordified contains a newline, then118# an intrictate shell command substitution is constructed119# to render the text as a single line; when the shell120# processes the resulting escaped text, it transforms121# it into the original unescaped text.122#123# If the text does not contain a newline, then this function124# produces the same results as the `$(shell-sq)' function.125#126shell-wordify = $(if $(findstring $(newline),$(1)),$(_sw-esc-nl),$(shell-sq))127define _sw-esc-nl128"$$(echo $(call escape-nl,$(shell-sq),$(2)) | $(call shell-unescape-nl,$(2)))"129endef130 131# is-absolute132#133# Usage: bool-value = $(call is-absolute,path)134#135is-absolute = $(shell echo $(shell-sq) | grep -q ^/ && echo y)136 137# lookup138#139# Usage: absolute-executable-path-or-empty = $(call lookup,path)140#141# (It's necessary to use `sh -c' because GNU make messes up by142# trying too hard and getting things wrong).143#144lookup = $(call unescape-nl,$(shell sh -c $(_l-sh)))145_l-sh = $(call shell-sq,command -v $(shell-sq) | $(call shell-escape-nl,))146 147# is-executable148#149# Usage: bool-value = $(call is-executable,path)150#151# (It's necessary to use `sh -c' because GNU make messes up by152# trying too hard and getting things wrong).153#154is-executable = $(call _is-executable-helper,$(shell-sq))155_is-executable-helper = $(shell sh -c $(_is-executable-sh))156_is-executable-sh = $(call shell-sq,test -f $(1) -a -x $(1) && echo y)157 158# get-executable159#160# Usage: absolute-executable-path-or-empty = $(call get-executable,path)161#162# The goal is to get an absolute path for an executable;163# the `command -v' is defined by POSIX, but it's not164# necessarily very portable, so it's only used if165# relative path resolution is requested, as determined166# by the presence of a leading `/'.167#168get-executable = $(if $(1),$(if $(is-absolute),$(_ge-abspath),$(lookup)))169_ge-abspath = $(if $(is-executable),$(1))170 171# get-supplied-or-default-executable172#173# Usage: absolute-executable-path-or-empty = $(call get-executable-or-default,variable,default)174#175define get-executable-or-default176$(if $($(1)),$(call _ge_attempt,$($(1)),$(1)),$(call _ge_attempt,$(2)))177endef178_ge_attempt = $(or $(get-executable),$(call _gea_err,$(2)))179_gea_err = $(if $(1),$(error Please set '$(1)' appropriately))180 181# version-ge3182#183# Usage $(call version-ge3,2.6.4,$(FLEX_VERSION))184#185# To compare if a 3 component version is greater or equal to another, first use186# was to check the flex version to see if we can use compiler warnings as187# errors for one of the cases flex generates code C compilers complains about.188 189version-ge3 = $(shell echo "$(1).$(2)" | awk -F'.' '{ printf("%d\n", (10000000 * $$1 + 10000 * $$2 + $$3) >= (10000000 * $$4 + 10000 * $$5 + $$6)) }')190 191# version-lt3192#193# Usage $(call version-lt3,2.6.2,$(FLEX_VERSION))194#195# To compare if a 3 component version is less thjan another, first use was to196# check the flex version to see if we can use compiler warnings as errors for197# one of the cases flex generates code C compilers complains about.198 199version-lt3 = $(shell echo "$(1).$(2)" | awk -F'.' '{ printf("%d\n", (10000000 * $$1 + 10000 * $$2 + $$3) < (10000000 * $$4 + 10000 * $$5 + $$6)) }')200