web-dev-qa-db-ja.com

makeで文字列を分割するにはどうすればよいですか?

次の形式のホスト識別子で構成されるMakefileのパラメーターを取得する必要があります

Host[:port]

ここで、コロンとポートはオプションです。したがって、以下のすべてが有効です。

foo.example.com
ssl.example.com:443
localhost:5000

等.

オプションのコロンで文字列を分割し、値を変数に割り当てて、Hostfoo.example.comssl.example.comlocalhostなどが含まれ、PORTに80(デフォルトポート)、443、および500。

27
Joe Shaw
# Retrieves a Host part of the given string (without port).
# Param:
#   1. String to parse in form 'Host[:port]'.
Host = $(firstword $(subst :, ,$1))

# Returns a port (if any).
# If there is no port part in the string, returns the second argument
# (if specified).
# Param:
#   1. String to parse in form 'Host[:port]'.
#   2. (optional) Fallback value.
port = $(or $(Word 2,$(subst :, ,$1)),$(value 2))

使用法:

$(call Host,foo.example.com) # foo.example.com
$(call port,foo.example.com,80) # 80

$(call Host,ssl.example.com:443) # ssl.example.com
$(call port,ssl.example.com:443,80) # 443
46