使用qmake检查可执行文件是否在PATH中

时间:2021-01-19 06:58:12

I have a custom build target in my *.pro file:

我在* .pro文件中有一个自定义构建目标:

docs.commands = doxygen $$PWD/../docs/Doxyfile

QMAKE_EXTRA_TARGETS += docs
POST_TARGETDEPS += docs

which runs Doxygen as a post build event. The problem is, if someone builds the project and hasn't installed doxygen the build fails. Is it possible to check whether or not doxygen is installed on the machine that builds the project so that I run the doxygen command only if doxygen is installed and added to the system PATH?

运行Doxygen作为后期构建事件。问题是,如果有人构建项目并且没有安装doxygen,则构建失败。是否可以检查是否在构建项目的机器上安装了doxygen,以便仅在安装doxygen并添加到系统路径时才运行doxygen命令?

3 个解决方案

#1


With qmake, you can try this:

使用qmake,您可以尝试这样做:

DOXYGEN_BIN = $$system(which doxygen)

isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
}

Another option could be the following one:

另一个选择可能是以下一个:

DOXYGEN_BIN = $$system( echo $$(PATH) | grep doxygen )

isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
}

BTW, if you are using CMake

顺便说一下,如果你正在使用CMake

You can achieve that using

你可以实现这一点

find_package(Doxygen)

Example:

FIND_PACKAGE(Doxygen)
if (NOT DOXYGEN_FOUND)
    message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()

You have more information in this site:

您在此站点中有更多信息:

http://www.cmake.org/cmake/help/v3.0/module/FindDoxygen.html

#2


Try this on your .pro file:

在.pro文件上试试这个:

# Check if Doxygen is installed on the default Windows location
win32 {
    exists( "C:\Program Files\doxygen\bin\doxygen.exe" ) {
        message( "Doxygen exists")
        # execute your logic here
    }
}
# same idea for Mac
macx {
    exists( "/Applications/doxygen.app/ ... " ) {
        message( "Doxygen exists")
    }
}

Update

Using @Tarod answer you can make it cross compatible with the following

使用@Tarod答案,您可以使其与以下内容交叉兼容

# Check if Doxygen is installed on Windows (tested on Win7)
win32 {
    DOXYGEN_BIN = $$system(where doxygen)

    isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
        # execute your logic here
    } else {
        message("Doxygen exists in " $$DOXYGEN_BIN)
    }
}

# Check if Doxygen is installed on Linux or Mac (tested on Ubuntu, not yet on the Mac)
unix|max {
    DOXYGEN_BIN = $$system(which doxygen)

    isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
        # execute your logic here
    } else {
        message("Doxygen exists in " $$DOXYGEN_BIN)
    }
}

#3


Qt docs say:

Qt文档说:

To obtain the contents of an environment value when qmake is run, use the $$(...) operator...

要在运行qmake时获取环境值的内容,请使用$$(...)运算符...

i.e.:

PATH_VAR = $$(PATH)
DOXYGEN = "doxygen"
contains(PATH_VAR, DOXYGEN) {
    message("Doxygen found")
}

#1


With qmake, you can try this:

使用qmake,您可以尝试这样做:

DOXYGEN_BIN = $$system(which doxygen)

isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
}

Another option could be the following one:

另一个选择可能是以下一个:

DOXYGEN_BIN = $$system( echo $$(PATH) | grep doxygen )

isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
}

BTW, if you are using CMake

顺便说一下,如果你正在使用CMake

You can achieve that using

你可以实现这一点

find_package(Doxygen)

Example:

FIND_PACKAGE(Doxygen)
if (NOT DOXYGEN_FOUND)
    message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()

You have more information in this site:

您在此站点中有更多信息:

http://www.cmake.org/cmake/help/v3.0/module/FindDoxygen.html

#2


Try this on your .pro file:

在.pro文件上试试这个:

# Check if Doxygen is installed on the default Windows location
win32 {
    exists( "C:\Program Files\doxygen\bin\doxygen.exe" ) {
        message( "Doxygen exists")
        # execute your logic here
    }
}
# same idea for Mac
macx {
    exists( "/Applications/doxygen.app/ ... " ) {
        message( "Doxygen exists")
    }
}

Update

Using @Tarod answer you can make it cross compatible with the following

使用@Tarod答案,您可以使其与以下内容交叉兼容

# Check if Doxygen is installed on Windows (tested on Win7)
win32 {
    DOXYGEN_BIN = $$system(where doxygen)

    isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
        # execute your logic here
    } else {
        message("Doxygen exists in " $$DOXYGEN_BIN)
    }
}

# Check if Doxygen is installed on Linux or Mac (tested on Ubuntu, not yet on the Mac)
unix|max {
    DOXYGEN_BIN = $$system(which doxygen)

    isEmpty(DOXYGEN_BIN) {
        message("Doxygen not found")
        # execute your logic here
    } else {
        message("Doxygen exists in " $$DOXYGEN_BIN)
    }
}

#3


Qt docs say:

Qt文档说:

To obtain the contents of an environment value when qmake is run, use the $$(...) operator...

要在运行qmake时获取环境值的内容,请使用$$(...)运算符...

i.e.:

PATH_VAR = $$(PATH)
DOXYGEN = "doxygen"
contains(PATH_VAR, DOXYGEN) {
    message("Doxygen found")
}