简体   繁体   English

需要帮助在Makefile中创建多个目录

[英]Need Help Creating Multiple Directories in Makefile

I have the following block of code in a makefile: 我在makefile中有以下代码块:

param_test_dir:

    @if test -d $(BUILD_DIR); then \
        echo Build exists...; \
    else \
    echo Build directory does not exist, making build dir...; \
mkdir $(BUILD_DIR); \
fi
@if test -d $(TEST_DIR); then \
    echo Tests exists...; \
else \
    echo Tests directory does not exist, making tests dir...; \
mkdir $(TEST_DIR); \
fi
@if test -d $(TEST_DIR)/param_unit_test; then \
    echo Param unit test directory exists...; \
else \
    echo Param unit test directory does \
    not exist, making build dir...; \
    mkdir $(TEST_DIR)/param_unit_test; \
fi
@if test -d $(TEST_DIR)/param_unit_test/source; then \
    echo Param unit test source directory exists...; \
else \
    echo Param unit test source directory does \
    not exist, making build dir...; \
    mkdir $(TEST_DIR)/param_unit_test/source; \
fi
@if test -d $(TEST_DIR)/param_unit_test/obj; then \
    echo Param unit test object directory exists...; \
else \
    echo Param unit test object directory does \
    not exist, making object dir...; \
    mkdir $(TEST_DIR)/param_unit_test/obj; \
fi
@if test -d $(TEST_DIR)/param_unit_test/bin; then \
    echo Param unit test executable directory exists...; \
else \
    echo Param unit test executable directory does \
    not exist, making executable dir...; \
    mkdir $(TEST_DIR)/param_unit_test/bin; \
fi

Basically, this is necessitated as I don't my makefile to crash and die if the build directory doesn't exist -- I want it to recreate the missing parts of the directory tree. 基本上,这是必需的,因为如果构建目录不存在,我不会使makefile崩溃并死掉-我希望它重新创建目录树的缺失部分。 The nesting is due to the fact that I have a build directory, a tests directory inside the build (for module tests vs. full programs) and then within that tests directory individual directories for tests, each of which need a source, obj, and bin directory. 嵌套是由于以下事实:我有一个构建目录,一个在构建内部的测试目录(用于模块测试还是完整程序),然后在该测试目录中包含用于测试的单个目录,每个目录都需要一个源,obj和bin目录。 So a lot of stuff to create! 创造了很多东西!

Here's my question. 这是我的问题。 Is there a way to take advantage of makefile "magic" to feed it some single variable like: 有没有一种方法可以利用makefile“魔术”为它提供一些单个变量,例如:

PATH=./build/tests/param_test/bin

and have it create all the directories I need with like a one or two liner rather than that humongous if statement? 并用一个或两个划线器创建我需要的所有目录,而不是用笨拙的if语句创建它?

Thanks in advance!! 提前致谢!!

mkdir -p build/tests/param_test/bin

mkdir手册 :-p,-parents,如果存在则没有错误,根据需要创建父目录

If you need to write a portable makefile (eg that work also on Windows), you may consider a solution where you can use the same command for all platforms. 如果您需要编写一个可移植的makefile(例如,在Windows上也可以使用),则可以考虑一个解决方案,在该解决方案中,您可以在所有平台上使用相同的命令。 The solution by @Sjoerd works fine on unixish machines, but there is a small difference for Windows (I tried it on XP, Vista and Windows 7): @Sjoerd的解决方案在unixish机器上可以正常工作,但是Windows的差异很小(我在XP,Vista和Windows 7上尝试过):

# Unix solution
mkdir -p build/tests/param_test/bin
# Windows solution
mkdir build\tests\param_test\bin

No -p option, and you need backslashes. 没有-p选项,则需要反斜杠。

It does create the directories recursively, but it complains if the directory already exists. 它确实以递归方式创建目录,但是会抱怨目录是否已存在。 There is no 'silent' option to suppress the complaint. 没有“沉默”选项可以抑制投诉。

What I'm using is a solution using a Perl one-liner . 我正在使用的是使用Perl单线解决方案。 Other build steps in our system mandate Perl, so this is portable for us. 系统的其他构建步骤要求使用Perl,因此对我们而言是可移植的。 YMMV. YMMV。

perl "-MFile::Path" -e "mkpath 'build/tests/param_test/bin'"

Note that with Perl you can use forward slashes also on Windows. 请注意,在Perl中,您也可以在Windows上使用正斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM