关于gnu make:当mkdir -p不可用时,如何在makefile中创建目录?

How to create a directory in a makefile when mkdir -p is not available?

(P)I have a make file which does the usual directory creation:(p)字母名称(P)Unfortunately when I run this under Scratchbox2 the MKDR-P Command always fails silently.(p)(P)I attempted the following kludge which doesn't work:(p)字母名称(P)This outputs:(p)字母名称(P)…The trailing slash prevents the dir function from stripping the last directory in the way I wanted.(p)(P)Short of writing a script or small c a p p to replicate the-"functionality,does anyone have any ideas for creating the substantories within the makeffile?(p)(P)无需选择MKDR将提供一个错误,因为这样做有助于创建一个成功的董事会。I can do MKDR Blah 2>(p)(P)是否有人认为为什么MKDR-P不在SCRATCHbox2下工作?(p)(P)伊迪特(p)(P)Based on suggestions by bobbogo I put this together.It looks fairly convoluted,but seems to work,even under scratchbox 2.(p)字母名称


您可以将您的mkdir森林替换为:

1
2
3
$(Release_target_OBJDIR)/%.o: %.cpp
    $(foreach d,$(subst /, ,${@D}),mkdir $d && cd $d && ):
    ∶

这将创建一个shell命令,如下所示:

1
mkdir projects && cd projects && mkdir htc && cd htc && mkdir arm && cd arm && :

这在每次编译时都会运行。不太优雅。您可以通过使用某种sentinel文件来优化这一点。例如:

1
2
3
4
5
6
$(Release_target_OBJDIR)/%.o: %.cpp ${Release_target_OBJDIR}/.sentinel
    ∶

%/.sentinel:
    $(foreach d,$(subst /, ,$*),mkdir $d && cd $d && ):
    touch $@

.sentinel在所有对象之前创建一次,并且是make -j友好的。事实上,即使mkdir -p对您有效(在这种情况下,您将使用mkdir -p,而不是$(foreach)hacksolution),您也应该这样做。


您可以告诉make忽略使用-的命令的任何失败返回代码:

1
2
3
4
5
$(Release_target_OBJDIR)/%.o: %.cpp
    -mkdir $(dir $(dir $(dir $@)))
    -mkdir $(dir $(dir $@))
    -mkdir $(dir $@)
    $(COMPILE.cpp) $< $(CFLAGS) $(INCLUDES) -o $@

(请注意,这并不能解决尾随斜杠问题。)


有一个简单的方法。您可以这样在shell函数内部调用mkdir:

1
2
foobar:
        $(shell mkdir -p mydirectoryname)

gnu make中的shell函数执行单词shell之后的命令作为shell命令。