如何在make中拆分字符串?

Joe*_*haw 27 makefile gnu-make

我需要在我的Makefile中获取一个参数,该参数由表单中的主机标识符组成

host[:port]
Run Code Online (Sandbox Code Playgroud)

冒号和端口是可选的.所以以下所有内容都是有效的:

foo.example.com
ssl.example.com:443
localhost:5000
Run Code Online (Sandbox Code Playgroud)

等等

我想拆就可选结肠串并分配给变量,使HOST包含foo.example.com,ssl.example.com,localhost等,PORT分别包含80个(默认端口),443和500.

Eld*_*mov 46

# 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))
Run Code Online (Sandbox Code Playgroud)

用法:

$(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
Run Code Online (Sandbox Code Playgroud)