【问题标题】:Looping over ifndef/endif constructs in a Makefile在 Makefile 中循环 ifndef/endif 结构
【发布时间】:2017-06-27 00:09:40
【问题描述】:

我正在尝试编写一个检查多个环境变量以确保它们被定义的 Makefile 规则。这接近Makefile variable as prerequisite,但我试图循环多个变量。换句话说,我试图写得更简洁:

check-env:
ifndef ENV1
    $(error ENV1 is undefined)
endif
ifndef ENV2
    $(error ENV2 is undefined)
endif
ifndef ENV3
    $(error ENV3 is undefined)
endif

我尝试使用foreach 没有成功,因为看起来ifndef 是在foreach 之前评估的。

【问题讨论】:

    标签: loops makefile environment-variables


    【解决方案1】:

    生成文件:

    variables := a b c
    
    fatal_if_undefined = $(if $(findstring undefined,$(origin $1)),$(error Error: variable [$1] is undefined))
    $(foreach 1,$(variables),$(fatal_if_undefined))
    
    all:
    

    跑步:

    $ make -f Makefile.sample
    Makefile.sample:4: *** Error: variable [a] is undefined.  Stop.
    
    $ make -f Makefile.sample a=10 b=2
    Makefile.sample:4: *** Error: variable [c] is undefined.  Stop.
    
    $ make -f Makefile.sample a=10 b=2 c=5
    make: Nothing to be done for 'all'.
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多