gpt4 book ai didi

makefile - 如何在make中分割字符串?

转载 作者:行者123 更新时间:2023-12-02 12:00:14 29 4
gpt4 key购买 nike

我需要在 Makefile 中采用一个由主机标识符组成的参数,格式为

host[:port]

其中冒号和端口是可选的。因此以下所有内容均有效:

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

等等

我想在可选冒号上拆分字符串并将值分配给变量,以便 HOST 包含 foo.example.comssl.example .comlocalhost等,PORT分别包含80(默认端口)、443和500。

最佳答案

# 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

关于makefile - 如何在make中分割字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8540485/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com