使目标取决于GNU Make中的可选文件

发布于 2025-02-06 06:36:49 字数 1425 浏览 1 评论 0原文

我有一个简单的makefile,可以从.proto文件中生成Protobuf存根。

PROTOC := protoc.exe
PYTHON := python

NANOPB_GEN := ../lib/generator/
NANOPB_PROTO := $(NANOPB_GEN)proto 
NANOPB_SCRIPT := $(NANOPB_GEN)nanopb_generator.py
CPP_PATH := ../../Firmware/protobuf/
CS_PATH := ../../Console/Networking/DataExchange/

SRCS := $(wildcard *.proto)
CPP_TARGETS := $(patsubst %.proto, $(CPP_PATH)%.pb.cpp, $(SRCS))
CS_TARGETS := $(patsubst %.proto, $(CS_PATH)%.cs, $(SRCS))

$(CPP_PATH)%.pb.cpp: %.proto
    $(PYTHON) $(NANOPB_GEN) -H .h -S .cpp -D $(CPP_PATH) --strip-path $< 

$(CS_PATH)%.cs: %.proto
    $(PROTOC) -I./ -I=$(NANOPB_GEN) -I=$(NANOPB_PROTO) --csharp_out=$(CS_PATH) $<

all: cpp cs
    @echo "making all"

cpp: $(CPP_TARGETS)
    @echo "generating C++ stubs"

cs: $(CS_TARGETS)
    @echo "generating C# stubs"

它可以正常工作,但是如果相应的 .options 文件更改除了 .proto 文件,我也希望将makefile重建存根。例如, tag.proto 可能具有可选的 tag.options 文件,该文件 nanopb 生成器期望。如果 .options 文件更改,我想修改makefile以重建存根,但如果没有 .options 文件,也可以正常工作。

我尝试了此规则:

$(cpp_path)%。pb.cpp:%.proto $$(patsubst%.proto,%.options,$$&lt;)

但是,这不处理 .proto 没有相应的*。选项文件。

犯错:

make:***没有规则制作目标../../ striceware/protobuf/tag.pb.cpp', cpp'需要。停止。

如何定义可以使用 .ptions 的规则?

谢谢你!

I have a simple Makefile that generates protobuf stubs from .proto files.

PROTOC := protoc.exe
PYTHON := python

NANOPB_GEN := ../lib/generator/
NANOPB_PROTO := $(NANOPB_GEN)proto 
NANOPB_SCRIPT := $(NANOPB_GEN)nanopb_generator.py
CPP_PATH := ../../Firmware/protobuf/
CS_PATH := ../../Console/Networking/DataExchange/

SRCS := $(wildcard *.proto)
CPP_TARGETS := $(patsubst %.proto, $(CPP_PATH)%.pb.cpp, $(SRCS))
CS_TARGETS := $(patsubst %.proto, $(CS_PATH)%.cs, $(SRCS))

$(CPP_PATH)%.pb.cpp: %.proto
    $(PYTHON) $(NANOPB_GEN) -H .h -S .cpp -D $(CPP_PATH) --strip-path 
lt; 

$(CS_PATH)%.cs: %.proto
    $(PROTOC) -I./ -I=$(NANOPB_GEN) -I=$(NANOPB_PROTO) --csharp_out=$(CS_PATH) 
lt;

all: cpp cs
    @echo "making all"

cpp: $(CPP_TARGETS)
    @echo "generating C++ stubs"

cs: $(CS_TARGETS)
    @echo "generating C# stubs"

It works fine, but I also want the makefile to rebuild the stubs if a corresponding .options file changes in addition to the .proto file. For instance, Tag.proto may have an optional Tag.options file that nanopb generator expects. I want to modify the Makefile to rebuild the stubs if .options file changes, but also works if there is no .options file.

I tried this rule:

$(CPP_PATH)%.pb.cpp: %.proto $$(patsubst %.proto, %.options, $$<)

However, this does not handle .proto files that don't have the corresponding *.options file.

Err:

make: *** No rule to make target ../../Firmware/protobuf/Tag.pb.cpp', needed bycpp'. Stop.

How can I define a rule that can use .options ?

Thank you!

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

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

发布评论

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

评论(1

吃兔兔 2025-02-13 06:36:51

我认为最简单的方法是使用Wildcard函数,只有在存在时才考虑文件。唯一棘手的事情是您不能在辅助扩展部分内使用,但是您可以使用$$*,它将扩展到已测试的词干,即:

$ cat Makefile
.SECONDEXPANSION:
%.pb.cpp: %.proto $(wildcard $*.options)
        echo $^ > $@

这将是 :当存在 .options 文件是否存在时,处理这两个选项,例如:

$ touch foo.proto bar.proto bar.options
$ make foo.pb.cpp
echo foo.proto > foo.pb.cpp
$ make bar.pb.cpp
echo bar.proto bar.options > bar.pb.cpp

如果更改 .options 文件,也将重建:

$ make bar.pb.cpp
make: 'bar.pb.cpp' is up to date.
$ touch bar.options
$ make bar.pb.cpp
echo bar.proto bar.options > bar.pb.cpp

I think the easiest way would be to use wildcard function, that will only take a file into account if it exists. The only tricky thing is that you cannot use % within secondary expanded part, but you can use $$* instead, which will expand to the tested stem, i.e.:

$ cat Makefile
.SECONDEXPANSION:
%.pb.cpp: %.proto $(wildcard $*.options)
        echo $^ > $@

This will handle both options when the .options file exists or not, e.g.:

$ touch foo.proto bar.proto bar.options
$ make foo.pb.cpp
echo foo.proto > foo.pb.cpp
$ make bar.pb.cpp
echo bar.proto bar.options > bar.pb.cpp

This will also rebuild if .options file gets changed:

$ make bar.pb.cpp
make: 'bar.pb.cpp' is up to date.
$ touch bar.options
$ make bar.pb.cpp
echo bar.proto bar.options > bar.pb.cpp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文