2011-12-16 20 views
19

Ben formda kolon ve port isteğe bağlıdırBir dizeyi make olarak nasıl bölerim?

host[:port] 

bir konak tanımlayıcı oluşur benim Makefile bir parametre almak gerekir. Yani aşağıdakilerin tümünü geçerlidir:

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

vb

Ben, isteğe bağlı kolon üzerinde dize bölmek ve değişkenlere değer atamak istediğiniz HOST içerecek şekilde foo.example.com, ssl.example.com, localhost vs. ve PORT sırasıyla 80 (varsayılan bağlantı noktası), 443 ve 500 içerir.

cevap

34
# 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)) 

Kullanımı:

$(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 
İlgili konular