如何在Makefile中使用条件语句

8

我想使用一个命令行变量来选择我们用来编译的工具包。在命令行中,我可以使用如下行:

make all-arm OUR_TOOLKIT=1

并且,在每个隐含的makefile中,我都加入了这个include。

include ARM_Compiler.inc

然后,在每个Makefile文件中,
all: setToolkit $(otherOperations)

ARM_Compiler的内容是选择编译器的逻辑:
setToolkit: 
ifdef OUR_TOOLKIT
    TOOLKIT=1
endif
ifdef CUSTOMER_TOOLKIT
    TOOLKIT=2
endif

ifeq ($(TOOLKIT), 1)
    $(info "=========Our toolkit selected======================")
    rm=/bin/rm -f
    CC= arm-linux-c++ -fPIC
    CXX= arm-linux-c++ -fPIC
    LINK= arm-linux-c++ -shared -Wl
    AR= ar cq
    RANLIB= ranlib
    STRIP=arm-linux-strip 

    # para que se utilicen las herramientas y librerias del cross compiler
    PATH:=$(PATH):/path/to/our/toolkit
    LD_LIBRAY_PATH:=$(LD_LIBRAY_PATH):/path/to/our/toolkit          
endif

ifeq ($(TOOLKIT), 2)
    $(info "================Customer toolkit selected====================")
    rm=/bin/rm -f
    CC= arm-none-linux-gnueabi-c++ -fPIC
    CXX= arm-none-linux-gnueabi-c++ -fPIC
    LINK= arm-none-linux-gnueabi-c++ -shared -Wl
    AR= ar cq
    RANLIB= ranlib
    STRIP= arm-none-linux-gnueabi-strip 

    # para que se utilicen las herramientas y librerias del cross compiler
    PATH:=$(PATH):/path/to/other/toolkit
    LD_LIBRAY_PATH:=$(LD_LIBRAY_PATH):/path/to/other/toolkit
endif

在0A0D的帮助下,我发现TOOLKIT值始终为空。我稍微修改了代码。现在的问题是make命令抛出了错误。

../makefile-includes/ARM-compiler.inc:10: *** commands commence before first target

在这一行:
ifeq ($(TOOLKIT), 1)

有人有一些想法吗? 谢谢。

2
类似的:https://dev59.com/bWoy5IYBdhLWcg3wCpsz - user195488
1个回答

10
这个问题的变体经常出现。
每个命令在它自己的子shell中执行;在一个命令中设置的变量不能在另一个命令中使用。
但是你可以在规则之外设置变量:只需从条件语句前面删除所有前导TAB。这对除了PATH和LD_LIBRARY_PATH之外的所有内容都有效。在我看来,Make不应该干预这两个变量,但有办法实现你想要的效果。你可以像这样处理PATH:
ifeq ($(TOOLKIT), 1)
  TOOLKITPATH = /path/to/our/toolkit
endif
...

sometarget:
    $(TOOLKITPATH)/sometool somearg

或者像这样:
all:
    export PATH=$$PATH:$(TOOLKITPATH) ; $(MAKE) $(otherOperations)

你最好根本不要使用 LD_LIBRARY_PATH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接