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 h开发者_运维百科umongous if statement?
Thanks in advance!!
mkdir -p build/tests/param_test/bin
mkdir manual: -p, --parents, no error if existing, make parent directories as needed
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.
精彩评论