首页 > 解决方案 > 在规则中使用 makefile 文件作为先决条件

问题描述

我对make没有经验,想复制一些第三方SDK提供的构建结构,并将其适应我自己的编译器和项目。

问题是我发现使用makefile作为先决条件生成了一些规则,例如:

...
$(eval $(1): Makefile | $(dir $(1)).) \
...

或者

$(OUTPUT_DIRECTORY)/%.inc: Makefile | $(OUTPUT_DIRECTORY)
    $(info Generating $@)
    $(NO_ECHO)$(call dump, $(call target_specific, INC_PATHS, $*)) > $@

最初,他们的 makefile 是大写的(Makefile),而我正在处理的是小写(makefile)。无论如何,当我尝试当前的更改时,会出现此错误:

*** No rule to make target 'Makefile', needed by '...'

我认为这是由于大写,所以在两个地方都改为小写并再次尝试,但这次错误是makefile被视为C文件(这是我的猜测......)

"makefile", line 1: error #171: expected a declaration
  -include makefile.local
                          ^

"makefile", line 67: error #8: missing closing quote
  ${CG_TOOL_ROOT}/include"
                         ^

"makefile", line 99: error #10: "#" not expected here
  .SUFFIXES: # ignore built-in rules
             ^

"makefile", line 100: error #10: "#" not expected here
  %.d:       # don't try to make .d files
             ^

"makefile", line 100: error #8: missing closing quote
  %.d:       # don't try to make .d files

这里可能是什么问题?有什么我想念的吗?

编辑 1: 这些是我要使用的文件。

Nordic 将这些作为其 SDK 的一部分提供,用于使用用于 ARM 平台的 arm-gcc 进行开发。Makefile每个项目都有一个,Makefile.common并且 main 中包含一个Makefile

另一方面,我试图理解和复制相同的内容,但对于另一个具有不同选项和语法的专有编译器(TI 的 cl430 用于 MSP430 平台)。从原始Makefile.common版本中,我删除了对 gnu-arm 套件的一些引用,并将其替换为适当的编译工具。在中,makefile我还尝试复制与原版相同的结构,但使用我的来源和选项。

为了清楚起见,原件是大写的,我的是小写的。

编辑2:

运行后make --debug --print-data-base我发现了这个:

首次尝试构建文件时出现错误:

Updating goal targets....
 File 'default' does not exist.
   File 'test_project' does not exist.
     File '_build/test_project.out' does not exist.
       File '_build/test_project/<SOURCE_FILE>.c.o' does not exist.
      Must remake target '_build/test_project/<SOURCE_FILE>.c.o'.
Building file: "makefile"
Invoking: MSP430 Compiler
"cl430" -vmspx --use_hw_mpy=F5 <... a lot of options ...> --obj_directory="./_build"  "makefile"
"makefile", line 1: error #171: expected a declaration
  -include makefile.local
  ^
[ ... more errors ...]

但是,从调试和数据库信息中,我发现源文件规则确实需要makefile,并且makefile不是目标,而是以某种方式试图构建它:

# Not a target:
makefile:
#  Implicit rule search has been done.
#  Last modified 2020-02-25 11:43:33.609423171
#  File has been updated.
#  Successfully updated.

_build/test_project/<SOURCE_FILE>.c.o: makefile | _build/test_project/.
#  Implicit rule search has been done.
#  Implicit/static pattern stem: '_build/test_project/<SOURCE_FILE>'
#  Modification time never checked.
#  File has been updated.
#  Failed to be updated.
# automatic
# @ := _build/test_project/<SOURCE_FILE>.c.o
# automatic
# % := 
# automatic
# * := _build/test_project/<SOURCE_FILE>
# automatic
# + := makefile
# automatic
# | := _build/test_project/.
# automatic
# < := makefile
# automatic
# ^ := makefile
# automatic
# ? := makefile
# variable set hash-table stats:
# Load=8/32=25%, Rehash=0, Collisions=1/30=3%
#  recipe to execute (from 'makefile.common', line 192):
    @echo 'Building file: "$<"'
    @echo 'Invoking: MSP430 Compiler'
    "${CG_TOOL_ROOT}/bin/cl430" -vmspx --use_hw_mpy=F5 <... a lot of options ...> "$(shell echo $<)"
    @echo 'Finished building: "$<"'
    @echo ' '

标签: cmakefilebuild

解决方案


这种依赖Makefile是为了确保它Makefile在更改时重建。例如,如果您更改或编译或链接规则,则此依赖项会触发重建(提供的目标文件依赖于CFLAGS,因为它们应该)。不依赖这些更改不会导致重建。MakefileMakefileMakefile

"makefile", line 1: error #171: expected a declaration建议形式的错误消息makefile作为源文件传递给产生错误消息的 C 编译器。

规则最有可能做到$(filter-out Makefile,$^)。您需要替换所有出现的Makefileto makefile

*** No rule to make target 'Makefile', needed by '...'意味着这Makefile仍然是某些目标的先决条件。您可以通过将--debug --print-data-base命令行选项传递给make.


推荐阅读