对于初学者来说,GNU make 中的这个练习无疑只是一个练习,而不是实用性,因为一个简单的 bash 脚本就足够了。 但是,它带来了我不太理解的有趣行为。
我编写了一个看似简单的 Makefile 来处理 SSL 密钥/证书对的生成 。 我的目标是 make
生成 -key.pem
、-cert.pem
,以及任何其他必要的文件(特别是 CA 对,如果其中任何一个丢失或需要更新,这会导致另一个有趣的后续练习,即处理反向部门以重新颁发由丢失/更新的 CA 证书签名的任何证书)。
按预期执行所有规则后,make 似乎过于积极地识别要删除的中间文件; 它删除了一个我认为“安全”的文件,因为它应该是作为我调用的主要规则的先决条件生成的。 (谦虚地翻译一下,我可能误解了 make 的 记录的行为 以满足我的期望,但不明白如何;-)
编辑(谢谢,Chris!) 将 %-cert.pem
添加到 .PRECIOUS
当然可以防止删除。 (我一直使用错误的语法。)
Makefile:
OPENSSL = /usr/bin/openssl
# Corrected, thanks Chris!
.PHONY: clean
default: ca
clean:
rm -I *.pem
%: %-key.pem %-cert.pem
@# Placeholder (to make this implicit create a rule and not cancel one)
Makefile:
@# Prevent the catch-all from matching Makefile
ca-cert.pem: ca-key.pem
$(OPENSSL) req -new -x509 -nodes -days 1000 -key ca-key.pem > $@
%-key.pem:
$(OPENSSL) genrsa 2048 > $@
%-cert.pem: %-csr.pem ca-cert.pem ca-key.pem
$(OPENSSL) x509 -req -in $ $@
输出:
$ make host1
/usr/bin/openssl genrsa 2048 > ca-key.pem
/usr/bin/openssl req -new -x509 -nodes -days 1000 -key ca-key.pem > ca-cert.pem
/usr/bin/openssl genrsa 2048 > host1-key.pem
/usr/bin/openssl req -new -days 1000 -nodes -key host1-key.pem > host1-csr.pem
/usr/bin/openssl x509 -req -in host1-csr.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > host1-cert.pem
rm host1-csr.pem host1-cert.pem
这让我发疯,我很乐意尝试任何建议并发布结果。 如果我对这个完全不感兴趣,请随意嘲笑。 你不可能伤害我的感情。 :)
For starters, this exercise in GNU make was admittedly just that: an exercise rather than a practicality, since a simple bash script would have sufficed. However, it brought up interesting behavior I don't quite understand.
I've written a seemingly simple Makefile to handle generation of SSL key/cert pairs as necessary for MySQL. My goal was for make <name>
to result in <name>-key.pem
, <name>-cert.pem
, and any other necessary files (specifically, the CA pair if any of it is missing or needs updating, which leads into another interesting follow-up exercise of handling reverse deps to reissue any certs that had been signed by a missing/updated CA cert).
After executing all rules as expected, make seems to be too aggressive at identifying intermediate files for removal; it removes a file I thought would be "safe" since it should have been generated as a prereq to the main rule I'm invoking. (Humbly translated, I likely have misinterpreted make's documented behavior to suit my expectation, but don't understand how. ;-)
Edited (thanks, Chris!) Adding %-cert.pem
to .PRECIOUS
does, of course, prevent the deletion. (I had been using the wrong syntax.)
Makefile:
OPENSSL = /usr/bin/openssl
# Corrected, thanks Chris!
.PHONY: clean
default: ca
clean:
rm -I *.pem
%: %-key.pem %-cert.pem
@# Placeholder (to make this implicit create a rule and not cancel one)
Makefile:
@# Prevent the catch-all from matching Makefile
ca-cert.pem: ca-key.pem
$(OPENSSL) req -new -x509 -nodes -days 1000 -key ca-key.pem > $@
%-key.pem:
$(OPENSSL) genrsa 2048 > $@
%-cert.pem: %-csr.pem ca-cert.pem ca-key.pem
$(OPENSSL) x509 -req -in $ $@
Output:
$ make host1
/usr/bin/openssl genrsa 2048 > ca-key.pem
/usr/bin/openssl req -new -x509 -nodes -days 1000 -key ca-key.pem > ca-cert.pem
/usr/bin/openssl genrsa 2048 > host1-key.pem
/usr/bin/openssl req -new -days 1000 -nodes -key host1-key.pem > host1-csr.pem
/usr/bin/openssl x509 -req -in host1-csr.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > host1-cert.pem
rm host1-csr.pem host1-cert.pem
This is driving me crazy, and I'll happily try any suggestions and post results. If I'm just totally noobing out on this one, feel free to jibe away. You can't possibly hurt my feelings. :)
发布评论
评论(4)
http://www.gnu.org/ software/automake/manual/make/Chained-Rules.html#Chained-Rules
http://www.gnu.org/software/automake/手册/make/Special-Targets.html
http://www.gnu.org/software/automake/manual/make/Chained-Rules.html#Chained-Rules
http://www.gnu.org/software/automake/manual/make/Special-Targets.html
我注意到的第一件事是你的行:
应该是:
参见: http://www.gnu.org/software/automake/manual/make/Phony-Targets.html
The first thing i noticed is that your line:
should be:
See: http://www.gnu.org/software/automake/manual/make/Phony-Targets.html
请注意,像
%: %-key.pem %-cert.pem
这样的模式规则是隐式规则,它应用中间清理规则。对于真正要构建的目标的文件,请使其明确,即不要使用模式规则。
在你的情况下,Makefile 应该是:
Note that pattern rules like
%: %-key.pem %-cert.pem
are implicit rules, which apply the intermediate clean up rule.For the files that are truly targets to be built, make them explicit, i.e. don't use pattern rules.
In your case, the Makefile should be:
尝试:
Try: