如果找到某个文件,如何产生构建错误

发布于 2024-12-03 12:24:16 字数 733 浏览 0 评论 0原文

如果在当前目录中找到一个或多个与特定模式匹配的文件,我想生成构建错误。

为了说明这一点,我的 make 文件目标当前如下所示:

generate-java:
    swig -c++ -java interface.i

swig 实用程序为它在 C++ 源代码中遇到的每个 C++ 类生成 Java 类。但是,如果它遇到未找到定义的 C++ 类的使用,它将生成一个虚拟类。虚拟类的名称通常类似于 SWIGTYPE_p_MyClass

如果发生这种情况,那么我想将其视为错误。因此,如果找到名为 SWIGTYPE_p*.java 或包含字符串 SWIGTYPE_p 的文件,那么我希望构建失败。

我想我需要将构建目标更改为这样的:

generate-java:
    swig -c++ -java interface.i
    find . -name "SWIGTYPE_p.*\.java" --> generate build error if found
    find . -name "*\.java" | xargs grep SWIGTYPE_p --> generate build error if 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 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?

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

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

发布评论

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

评论(2

余生共白头 2024-12-10 12:24:16

您可以使用 shell 的 test 命令来测试 find 命令的输出是否为空。如果 find 返回某些内容,这将发出失败状态 (!=0)。

generate-java:
        swig -c++ -java interface.i
        test -z "$(shell find -name 'SWIGTYPE_p.*\.java')"

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')"
も星光 2024-12-10 12:24:16

一种可能的方法是使用 wc 对从 find 返回的文件进行计数,如果计数大于零,则使用您选择的错误号调用 exit。< br>
例如,您的规则可以是:

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  

如果找到任何文件,make 将退出错误 make: *** [generate-java] Error 3
您可以选择退出错误代码,因为您选择的数量和 echo 语句也取决于您的选择。

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.

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