【问题标题】:Is it possible to run shell command once before compilation in GNU Make 4.3?是否可以在 GNU Make 4.3 编译之前运行一次 shell 命令?
【发布时间】:2021-05-15 22:42:30
【问题描述】:

应该很简单:我有一个编译和链接静态库的 Makefile。我想实现以下行为:

如果 .c/.h 文件没有更改:

make: Nothing to be done for [target]

如果有变化:

==> Creating target [target]...  
  -> Compiling source files...  
[compiler output goes here]  
  -> Linking static library [target]
==> Finished making: [target]

是的,我确实知道依赖项如何工作以及如何在编译期间生成 .d 文件,我需要的是一种在任何编译完成之前运行 shell 命令 一次 的方法(如果有的话编译)。

【问题讨论】:

  • 你的问题不清楚。你运行一次的这个 shell 命令会做什么?
  • @MadScientist 打印状态消息,仅此而已。我在想可能设置一些变量来保存消息,然后在第一个 .o 文件编译后在配方中修改它以仅包含一个空字符串...

标签: shell gnu-make


【解决方案1】:
PREBUILD_MSG = "$(GREEN)==>$(WHITE) Creating target $(NAME)...$(NOCOLOR)\n"
ifneq ($(findstring $(MAKECMDGOALS), so $(NAMESO)),)
    PREBUILD_MSG = "$(GREEN)==>$(WHITE) Creating target $(NAMESO)...$(NOCOLOR)\n"
endif
PRECOMPILE_MSG = "  $(BLUE)->$(NOCOLOR) Compiling source files...\n"

all: $(NAME)
$(NAME): $(OBJS)
    @printf $(PREBUILD_MSG)
    @printf "  $(BLUE)->$(NOCOLOR) Creating static library archive...\n"
    @$(AR) $@ $?
    @printf "$(GREEN)==>$(WHITE) Finished making: $(NAME)\n"
    @-rm -f .status
.PHONY: all

so: CFLAGS += -fpic
so: $(NAMESO)
$(NAMESO): $(OBJS)
    @printf $(PREBUILD_MSG)
    @printf "  $(BLUE)->$(NOCOLOR) Creating dynamically linked shared library...\n"
    @$(CC) -shared -o $@ $^
    @printf "$(GREEN)==>$(WHITE) Finished making: $(NAMESO)\n"
    @-rm -f .status
.PHONY: so

$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d | $(OBJDIR) $(DEPDIR)
    @printf $(PREBUILD_MSG)
    @printf $(PRECOMPILE_MSG)
    @$(eval PREBUILD_MSG="")
    @$(eval PRECOMPILE_MSG="")
    $(CC) $(CFLAGS) $(INCLUDE) -MMD -MF $(DEPDIR)/$*.tmp -c -o $@ $<
    @$(POST_COMPILE)

【讨论】:

  • 注意:它仍然没有完全发挥作用,就像你并行运行 make 一样,有时它会在打印状态消息之前吐出一些编译器消息。但我仍然对结果感到满意。现在我的 Makefile 看起来有点吓人......酷! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 2014-07-10
  • 2014-05-26
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多