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

Check if executable is in the PATH using qmake

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

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

QMAKE_EXTRA_TARGETS += docs
POST_TARGETDEPS += docs

which 运行s Doxygen 作为 post 构建事件。问题是,如果有人构建项目但尚未安装 doxygen,构建将失败。是否可以检查 doxygen 是否安装在构建项目的机器上,以便我 运行 只有 doxygen 安装并添加到 doxygen 命令系统 PATH?

有了qmake,你可以试试这个:

DOXYGEN_BIN = $$system(which doxygen)

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

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

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

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

顺便说一句,如果你使用的是 CMake

您可以使用

实现
find_package(Doxygen)

示例:

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

您在本站有更多信息:

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

在您的 .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")
    }
}

更新

使用@Tarod answer你可以让它与以下交叉兼容

# 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)
    }
}

Qt 文档说:

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

即:

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