【问题标题】:Makefile: Regexp pattern for prerequisite file(s)Makefile:必备文件的正则表达式模式)
【发布时间】:2022-08-18 18:23:14
【问题描述】:

我正在尝试为asm 文件的交叉编译编写一个make 配方。问题是,我想使用正则表达式作为输出目标的 makefile 先决条件,因为这些程序集文件是以某种自动方式生成的。

因此,它们的名称遵循以下模式:

1_mps.S # i want this to be compiled into 1_mps.elf
2_mps.S # i want this to be compiled into 2_mps.elf
3_mps.S # i want this to be compiled into 3_mps.elf

等等...

我试图在 Makefile 文档中找到对这个有点动态的规则定义的适当处理,但我有点迷茫,我需要一些帮助。

那么,我怎样才能为此定义一个先决条件并像这样命名输出文件呢?

到目前为止,我已经尝试使用通配符和% 模式,但没有成功。

例如:

out.elf : %_mps.S, vectors.S, syscalls.c
      ... gcc cross-compiler invocation here...
out.elf : *_mps.S, vectors.S, syscalls.c
      ... gcc cross-compiler invocation here...

当然,这两个示例都不起作用,而且它们不会产生各自的输出文件。例如,即使他们会工作,他们也会在所有情况下生成名称 out.elf(即,对于每个 n_mps.S 输入文件)

更新#1

我的(不工作)尝试:

RISCV_EXE_PREFIX         = $(RISCV)/bin/riscv32-unknown-elf-

all: %_mps.hex

$_mps.elf: %_mps.o syscalls.c vectors.S
    $(RISCV_EXE_PREFIX)gcc -o $@ \\
        -T link.ld \\
        -static \\
        $^  \\
        -I $(RISCV)/riscv32-unknown-elf/include \\
        -L $(RISCV)/riscv32-unknown-elf/lib 

%_mps.o : %_mps.S
    $(RISCV_EXE_PREFIX)gcc -march=rv32imcxpulpv2 -c -w -Os -g -nostdlib \\
    -I $(RISCV)/riscv32-unknown-elf/include \\
    -L $(RISCV)/riscv32-unknown-elf/lib \\
    -lc -lm -lgcc

%_mps.hex: %_mps.elf
    $(RISCV_EXE_PREFIX)objcopy --output-target=verilog $< $@

.PHONY:
clean:
    rm -rf $(PROGRAM_NAME).elf $(PROGRAM_NAME).hex

make: *** 没有规则来制作目标 \'%_mps.hex\',\'all\' 需要它。停止。

更新#2

我的工作尝试

RISCV            ?= ~/.riscv
RISCV_EXE_PREFIX  = $(RISCV)/bin/riscv32-unknown-elf-
LINKER_SCRIPT     = link.ld
CC                = gcc
LFLAGS            = -lc -lm -lgcc -nostdlib 

.PRECIOUS: %.o

all: $(patsubst %.S,%.hex,$(wildcard *_mps.S))

%_mps.hex: %_mps.elf
    @echo \"@@@@@@@@ BIN2HEX @@@@@@@@\"
    $(RISCV_EXE_PREFIX)objcopy \\
    --output-target=verilog $< $@

%_mps.elf: %_mps.o syscalls.c vectors.S
    @echo \"@@@@@@@@ LINKING @@@@@@@@\"
    $(RISCV_EXE_PREFIX)$(CC) \\
    -march=rv32imcxpulpv2 \\
    -T $(LINKER_SCRIPT) \\
    -static $^ \\
    -I $(RISCV)/riscv32-unknown-elf/include \\
    -L $(RISCV)riscv32-unknown-elf/lib \\
    $(LFLAGS) \\
    -g -w \\
    -o $@

%_mps.o : %_mps.S
    @echo \"@@@@@@@@ X-COMPILING @@@@@@@@\"
    $(RISCV_EXE_PREFIX)$(CC) \\
    -march=rv32imcxpulpv2 \\
    -c $< \\
    -g -w -Os

.PHONY:
clean: 
    @echo \"@@@@@@@@ CLEAN @@@@@@@@\"
    rm -rf *.elf *.hex

  • 我看不出什么可以解释为什么你需要一个正则表达式而不是一个基本的模式规则。

标签: regex makefile


【解决方案1】:

您可能想使用pattern rules 先将源文件编译为目标文件,然后将目标文件链接到 ELF 对象:

all : 1_mps.elf 2_mps.elf 3_mps.elf

%_mps.S :
    # Create .S
    touch $@

%_mps.o : %_mps.S
    # Compile .S into .o
    echo "$^" > $@

%_mps.elf : %_mps.o
    # Link .o into .elf
    echo "$^" > $@

.PHONY: all
.PRECIOUS: %_mps.S %_mps.o # Needed because it makes source files with touch.

输出:

$ make
# Create .S
touch 1_mps.S
# Compile .S into .o
echo "1_mps.S" > 1_mps.o
# Link .o into .elf
echo "1_mps.o" > 1_mps.elf
# Create .S
touch 2_mps.S
# Compile .S into .o
echo "2_mps.S" > 2_mps.o
# Link .o into .elf
echo "2_mps.o" > 2_mps.elf
# Create .S
touch 3_mps.S
# Compile .S into .o
echo "3_mps.S" > 3_mps.o
# Link .o into .elf
echo "3_mps.o" > 3_mps.elf

$ make
make: Nothing to be done for 'all'.

【讨论】:

  • 根据您的建议,我用Makefile 更新了我的问题。但是pattern 似乎不起作用。
  • 模式本身应该可以正常工作,但您不能使用图案作为all 的先决条件。尝试类似all: $(patsubst %.S,%.hex,$(wildcard *_mps.S))
  • 听起来您忘记包含 %_mps.hex 模式规则。
  • @ex1led 为您添加了一个工作示例。
  • @MaximEgorushkin 我已经采纳了您的建议,并提出了经过编辑的(原始文本)食谱。我非常感谢您的建议。这确实不是一项微不足道的任务。
猜你喜欢
  • 2017-05-14
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
相关资源
最近更新 更多