2016-11-22 17 views
0

いくつかの条件に基づいてforeachループで変数を割り当てたいと思います。gnu make - いくつかの条件に基づいてforeachで変数を定義する方法

実際には、システムパスに見つかったかどうかを確認するために、すべての必要なツール(gcc g ++ ld)を調べるために使用します。もしそうであればそれを保持し、そうでなければユーザーが提供したパス接頭辞を追加してみましょう。それが見つかった場合は、変数を変更してフルパスを使用します。一般的な答えに

答えて

0

私が思い付く:

TEST_ARRAY = AA BB 
K := $(foreach myTest,$(TEST_ARRAY),\ 
     $(if $(filter AA,$(myTest)),\ 
      $(eval $(myTest) := match),\ 
      $(eval $(myTest) := mismatch))) 

$(info "AA: ${AA}") 
$(info "BB: ${BB}") 

出力は次のようになります。私のより具体的な質問に

"AA: match" 
"BB: mismatch" 

答えはかなり長いです - 作業コードスニペットは、このようなものです:

#on widows 
DEVNUL := NUL 
WHICH := where 
#on linux 
#DEVNUL := /dev/null 
#WHICH := which 

# set path to be searched when command is not found in system PATH 
GNU_PATH := c:\NSS\GNU_Tools_ARM_Embedded\5.4 2016q3\bin2 
# optionally set command prefix - for example all your tools are not called "gcc" but "arm-gcc" so you would fill here "arm-" 
GNU_PREFIX := arm-none-eabi- 
# set command suffix - for example on windows all executable files have suffix ".exe" 
GNU_SUFFIX := .exe 

# escape spaces in path because make $(wildcard) can not handle them :(
empty := 
space := $(empty) $(empty) 
GNU_PATH_ESCAPED := $(subst $(space),\ ,$(GNU_PATH)) 

# define used tool-chain commands 
CC    := gccx 
AS    := as 
AR    := ar 
LD    := ld 
NM    := nm 
OBJDUMP   := objdump 
OBJCOPY   := objcopy 
SIZE   := size 

# detect if tool-chain commands are installed and fill correct path (prefer system PATH, then try to find them in suggested GNU_PATH) 
# if not found on neither system path nor on user provided GNU_PATH then early error is reported to user 
EXECUTABLES = CC AS AR LD NM OBJDUMP OBJCOPY SIZE 
$(foreach myTestCommand,$(EXECUTABLES),\ 
    $(if $(shell ${WHICH} ${GNU_PREFIX}$($(myTestCommand)) 2>${DEVNUL}),\ 
     $(eval $(myTestCommand) := ${GNU_PREFIX}$($(myTestCommand))),\ 
     $(if $(wildcard $(GNU_PATH_ESCAPED)\${GNU_PREFIX}$($(myTestCommand))$(GNU_SUFFIX)),\ 
      $(eval $(myTestCommand) := '$(GNU_PATH)/${GNU_PREFIX}$($(myTestCommand))$(GNU_SUFFIX)'),\ 
      $(error "Can not find tool ${GNU_PREFIX}$($(myTestCommand))$(GNU_SUFFIX), either make in in your system PATH or provide correct path in variable GNU_PATH")))) 

# just print what tools will be used 
$(foreach myTestCommand,$(EXECUTABLES),\ 
    $(info found tool $($(myTestCommand)))) 

default: 
    @$(CC) --version 

私のテストケースでは、gccx以外のすべてのツールが私のパス上にあります。 GNU_PATHで提供されているユーザーのフォルダにあります。

found tool 'c:\NSS\GNU_Tools_ARM_Embedded\5.4 2016q3\bin2/arm-none-eabi-gccx.exe' 
found tool arm-none-eabi-as 
found tool arm-none-eabi-ar 
found tool arm-none-eabi-ld 
found tool arm-none-eabi-nm 
found tool arm-none-eabi-objdump 
found tool arm-none-eabi-objcopy 
found tool arm-none-eabi-size 
関連する問題