在 makefile 中启动其他 makefile

发布于 2024-09-16 15:50:41 字数 698 浏览 4 评论 0原文

由于我对 Linux 上的构建过程/makefile 不太有经验,所以我遇到了以下问题:

设置: 我有一个 makefile A,它需要在运行之前设置一些环境变量,这是通过运行 来完成的。 ./set_A_vars.sh (set_A_vars.sh 包含许多 export 行)在运行 make -f A 之前

现在我需要在 makefile B 中创建项目 A。 我尝试了以下 makefile B 设置:

all: debug release

A_debug:
    . ./set_A_vars.sh && make -f A DEBUG=1

A_release:
    . ./set_A_vars.sh && make -f A DEBUG=0

debug:   A_debug some_B_stuff_debug
release: A_release some_B_stuff_debug

但是我收到了很多错误,这听起来像是 set_A_vars.sh 中的环境变量尚未为 B 中的 make -f A ... 设置。

怎么办?我从 makefile B 调用 makefile A,并使用 makefile B 中设置的 set_A_vars.sh 中的环境变量?

任何帮助表示赞赏。

Since i am not so experienced with the building process / makefiles on linux i ran in follow problem:

the Setup:
i have an makefile A, which needs some enviroment variables set before running, this is done by running . ./set_A_vars.sh (set_A_vars.sh contains many export lines) before running make -f A

now i need to make project A within a makefile B.
i tried the following setup for makefile B:

all: debug release

A_debug:
    . ./set_A_vars.sh && make -f A DEBUG=1

A_release:
    . ./set_A_vars.sh && make -f A DEBUG=0

debug:   A_debug some_B_stuff_debug
release: A_release some_B_stuff_debug

however i get lots of errors, which sound like the enviroment variables in set_A_vars.sh have not been set for make -f A ... in B.

How can i call makefile A from makefile B with the enviroment variables in set_A_vars.sh set in makefile B ??

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-09-23 15:50:41

通过这些附加条件,您的 makefile 看起来不错:

  1. 当您从 makefile 调用 make 时,请使用宏调用 ${MAKE} 而不是普通的 make。 (这确保了并行 make 工作,并且也意味着即使您的 make 有另一个名称(比如说 GNUmake )它仍然可以工作。)

  2. 如果您的目标与实际文件不对应,则将它们标记为.PHONY(见下文)。

  3. some_B_stuff_debug 是否需要先构建 A?然后你必须告诉 make this。

    some_B_stuff_debug:A_debug
    some_B_stuff_debug:A_release

    这显然是错误的。一种方法是通过 shell 强制执行排序。

尝试这样的事情:

.PHONY: debug
debug:
    . ./set_A_vars.sh && ${MAKE} -f A DEBUG=1
    ${MAKE} some_B_stuff_debug

.PHONY: release
release:
    . ./set_A_vars.sh && ${MAKE} -f A DEBUG=0
    ${MAKE} some_B_stuff_debug

.PHONY: some_B_stuff_debug
∶

Your makefile looks good with these provisos:

  1. When you call make from a makefile, please use the macro invocation ${MAKE} rather than plain make. (This ensures parallel make works, and also means it still works even if your make has another name (GNUmake say).)

  2. If your targets do not correspond to actual files, then mark them with .PHONY (see below).

  3. Does some_B_stuff_debug require A to be built first? Then you must tell make this.

    some_B_stuff_debug: A_debug
    some_B_stuff_debug: A_release

    This is clearly wrong. One way is to enforce the ordering via the shell.

Try something like this:

.PHONY: debug
debug:
    . ./set_A_vars.sh && ${MAKE} -f A DEBUG=1
    ${MAKE} some_B_stuff_debug

.PHONY: release
release:
    . ./set_A_vars.sh && ${MAKE} -f A DEBUG=0
    ${MAKE} some_B_stuff_debug

.PHONY: some_B_stuff_debug
∶
冷情 2024-09-23 15:50:41

你的 makefile 应该可以工作。我建议您尝试以下操作:

  • 尝试从命令行运行 set_A_vars.sh

  • 验证您想要设置的变量是否已设置。

  • make -f MakefileA,验证 MakefileA 确实能够很好地处理这些变量集。

  • MakefileB 中尝试一条规则来测试其中一个变量,例如 FOO

    <前><代码>测试变量:
    @echo FOO 是 $(FOO)

    如果您刚刚运行set_vars.sh,这应该可以工作。如果没有,那么有一些事情可能是错误的......

  • 现在清除变量(包括 FOO)并在 MakefileB 中尝试此规则:

    set_vars_and_test_them:
        ./set_A_vars.sh && echo FOO 是 $(FOO)
    

  • 现在把它们放在一起:

    <前><代码>A_debug:
    ./set_A_vars.sh && make -f MakefileA DEBUG=1

    (我建议不要将 makefile 称为“A”。)

  • Your makefiles should work. I suggest you try the following:

  • Try running set_A_vars.sh from the command line.

  • Verify that the variables you wanted set are set.

  • make -f MakefileA, to verify that MakefileA really does work nicely with these variables set.

  • Try a rule in MakefileB that will test one of the variables, say FOO:

    test_var:
        @echo FOO is $(FOO)
    

    This should work if you have just run set_vars.sh. If it doesn't, then there are a couple of things that could be wrong...

  • Now clear the variables (including FOO) and try this rule in MakefileB:

    set_vars_and_test_them:
        ./set_A_vars.sh && echo FOO is $(FOO)
    
  • Now put it together:

    A_debug:
        ./set_A_vars.sh && make -f MakefileA DEBUG=1
    

    (I recommend against calling a makefile "A".)

  • ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文