我如何从一个 GNU makefile 和大部分相同的源构建两个交互式网站?

How can I build two interactive web-sites from one GNU makefile and mostly the same source?

我正在使用 GNU Make 构建动态网站,但我需要构建两个版本。作为最终结果,目前我 运行 我的 makefile 使用两个命令行咒语。这是低效的并且可能导致错误(我并不总是记得 运行 两个版本或者输入错误并且它们失败)因此我想把它做成一个版本。

命令行指令是:

sudo make release build=test
sudo make release build=release

这两个咒语激活了设置路径和修改一些文件的 ifeq 块。 大大简化(以提高可读性)的顶级 makefile 的一部分:

subs = today tomorrow
files = index.php foot.php

ifeq ($(build),test)
export path = /var/www/html/cgi-test
$(shell cp -f head-test.php head.php)
$(shell sed -i '/"DB"/c\  define("DB", "db_test");' subs.php)
else ifeq ($(build),release)
export path = /var/www/html/cgi-new
$(shell cp -f head-release.php head.php)
$(shell sed -i '/"DB"/c\  define("DB", "db_new");' subs.php)
endif

.PHONY: all go       

all:
    $(MAKE) go
    @for ALL in $(subs);\
        do $(MAKE) -C $$ALL all || exit $$?;\
    done;

go:
    cp $(files) $(path)/.

子 makefile 具有非常相似的结构,但不需要 ifeq 块,因为文件和路径已经设置。

我想我可以简单地将 shell 命令移动到 .PHONY 规则中,但我不能对导出执行此操作,因为我收到错误“export:: bad variable name”。

我可以使用批处理文件来完成并调用 makefile 两次,但这只是回避问题而不是解决问题,我希望从过程中学习。

谁能告诉我在 makefile 中执行此操作的方法?

感谢 Tripleee,这里是最终移植过来以匹配我的起始 post 的答案。一个主要的变化是我已经回到 'all' 作为规则,我希望开始养成习惯! - 谢谢

.PHONY: all both rel-test rel-release

cgi-test := cgi-test
db-test := db_test

cgi-release := cgi-new
db-release := db_new

subs = today tomorrow
files = index.php foot.php

all: both

both: rel-test rel-release

rel-test rel-release: rel-%:
    cp -f head-$*.php head.php
    sed -i '/"DB"/c\  define("DB", "$(db-$*)");' subs.php
    $(MAKE) go path=/var/www/html/strutts/$(cgi-$*)
    @for ALL in $(subs);\
      do $(MAKE) build=$* path=/var/www/html/strutts/$(cgi-$*) -C $$ALL all || exit $$?;\
    done;

是这样的吗?

.PHONY: both rel-test rel-release
both: rel-test rel-release

cgi-test := cgi-test
db-test := db_test

cgi-release := cgi-new
db-release := db_new

rel-%:
    cp -f head-$*.php head.php
    sed -i '/"DB"/c\    define("DB", "$(db-$*)")' subs.php
    $(MAKE) release build=$* path=/var/www/html/$(cgi-$*)

无法将 export 移入配方的原因是您使用的是 make 本身的 export 命令,而不是 shell 的同名命令。

你绝对不应该使用 sudo,除非你明确要求输出文件由 root 拥有并且只能由其写入。即便如此,运行 尽可能像普通用户一样保持卫生;也许在 Makefile 中添加 sudo 命令以将文件复制到它们的最终位置。