简体   繁体   中英

Need Help Creating Multiple Directories in Makefile

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!!

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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