How to produce a build error if a certain file was found
I want to generate a build error if one or more files matching a certain pattern are found in the current directory.
To illustrate, my make file target currently looks like this:
generate-java:
swig -c++ -java interface.i
The swig
utility generates Java classes for each C++ class it encounters in the C++ source code. However, if it encounters usage of a C++ class that for which no definition has been found it will generate a dummy class. The dummy class typically is named something like SWIGTYPE_p_MyClass
.
If this situation occurs then I want to treat it as an error. So if a file is found that is named SWIGTYPE_p*.java
or contains the string SWIGTYPE_p
then I want the build to fail.
I think I need to change the build target to someting like this:
generate-java:
swig -c++ -java interface.i
find . -name "SWIGTYPE_p.*\.java" --> generate build开发者_JS百科 error if found
find . -name "*\.java" | xargs grep SWIGTYPE_p --> generate build error if found
I think I need to compose a command that returns an exit status different from zero in case the string is found. However, I don't know how to do this. Can anyone help?
You can use the test
command of the shell to test whether the output of your find
command is empty. This will emit a fail status (!=0) if find returns something.
generate-java:
swig -c++ -java interface.i
test -z "$(shell find -name 'SWIGTYPE_p.*\.java')"
One of the possible ways could be to just count the files returned from find
using wc
and if the count is greater than zero call exit with the error number of your choice.
For instance your rule could be:
generate-java:
swig -c++ -java interface.i && \
if [ `find ./ -iname "SWIGTYPE_p.*\.java" | wc -l` -gt 0 ];then \
echo "SWIGTYPE_p*.java found!!" && exit 3; \
fi
If there are any file found make will exit the error as make: *** [generate-java] Error 3
You can have the exit error code as the number of your choice and echo statement is again a matter of your choice.
精彩评论