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

发布于 2024-09-09 08:32:59 字数 1721 浏览 2 评论 0原文

我的 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

基本上,这是必要的,因为如果构建目录不存在,我的 makefile 不会崩溃并终止 - 我希望它重新创建目录树的缺失部分。嵌套是因为我有一个构建目录,构建内有一个测试目录(用于模块测试与完整程序),然后在该测试目录内有用于测试的各个目录,每个目录都需要一个源、obj 和bin 目录。有很多东西要创造!

这是我的问题。有没有一种方法可以利用 makefile 的“魔力”来为其提供一些单个变量,例如:

PATH=./build/tests/param_test/bin

并让它使用一两个行而不是庞大的 if 语句创建我需要的所有目录?

提前致谢!!

I have the following block of code in a 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. 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. 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:

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?

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够爱げ你 2024-09-16 08:32:59
mkdir -p build/tests/param_test/bin

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

mkdir -p build/tests/param_test/bin

mkdir manual: -p, --parents,
no error if existing, make parent directories as needed

︶ ̄淡然 2024-09-16 08:32:59

如果您需要编写一个可移植的 makefile(例如,也可以在 Windows 上运行),您可以考虑一种可以在所有平台上使用相同命令的解决方案。 @Sjoerd 的解决方案在 unixish 机器上运行良好,但对于 Windows 有一点小小的差异(我在 XP、Vista 和 Windows 7 上尝试过):

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

没有 -p 选项,并且您需要反斜杠。

它确实递归地创建目录,但如果目录已经存在,它会抱怨。没有“沉默”选项可以抑制投诉。

我正在使用的是一种使用Perl 单行的解决方案。我们系统中的其他构建步骤需要 Perl,因此这对我们来说是可移植的。 YMMV。

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

请注意,使用 Perl,您也可以在 Windows 上使用正斜杠。

If you need to write a portable makefile (e.g. that work also on Windows), you may consider a solution where you can use the same command for all platforms. 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):

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

No -p option, and you need backslashes.

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. Other build steps in our system mandate Perl, so this is portable for us. YMMV.

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

Note that with Perl you can use forward slashes also on Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文