Check if executable is in the PATH using qmake - qt

I have a custom build target in my *.pro file:
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?

With qmake, you can try this:
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
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

Try this on your .pro file:
# 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
# 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 docs say:
To obtain the contents of an environment value when qmake is run, use the $$(...) operator...
i.e.:
PATH_VAR = $$(PATH)
DOXYGEN = "doxygen"
contains(PATH_VAR, DOXYGEN) {
message("Doxygen found")
}

Related

How to run QT's `lupdate`-tool with a `qmake CONFIG`?

This question has been posted before in the qt community: https://forum.qt.io/topic/106930/how-to-run-lupdate-with-a-qmake-config
I use such a construct in my project files:
LANGUAGES = de
TRANSLATION_NAME = authorization
include(../../gen_translations.pri)
where gen_translations.pri looks like so:
# parameters: var, prepend, append
defineReplace(prependAll) {
for(a,$$1):result += $$2$${a}$$3
return($$result)
}
TRANSLATIONS = $$prependAll(LANGUAGES, $$PWD/libs/$$TRANSLATION_NAME/translations/lib$${TRANSLATION_NAME}_, .ts)
TRANSLATIONS_FILES =
qtPrepareTool(LRELEASE, lrelease)
for(tsfile, TRANSLATIONS) {
qmfile = $$shadowed($$tsfile)
qmfile ~= s,.ts$,.qm,
qmdir = $$dirname(qmfile)
!exists($$qmdir) {
mkpath($$qmdir)|error("Aborting.")
}
command = $$LRELEASE -removeidentical $$tsfile -qm $$qmfile
system($$command)|error("Failed to run: $$command")
TRANSLATIONS_FILES += $$qmfile
}
for(qmentry, $$list($$TRANSLATIONS_FILES)) {
qmpath = $$OUT_PWD/../translations
qmpathname = $$replace(qmpath,/,)
qmpathname = $$replace(qmpathname,\.,)
qmpathname = $$replace(qmpathname,:,)
qmpathname = $$replace(qmpathname," ",)
qmentity = qmfiles_$${qmpathname}
eval($${qmentity}.files += $$qmentry)
eval($${qmentity}.path = $$qmpath)
INSTALLS *= $${qmentity}
}
It generates the *.qm files for me and moves them to a defined location with make install.
I do not want qmake to execute that whole stuff for each build on my developing machine. Therefore I want to make it conditional by wrapping it for qmake:
translate{
LANGUAGES = de
TRANSLATION_NAME = authorization
include(../../gen_translations.pri)
}
That way I can decide when I want to get *.qm files and when not.
But then I am unable to run lupdate on the project file beforehand because it is blocked by that conditional.
I am sure, that someone has a better idea to accomplish the task.
Thanks in advance.
I'm sharing here my recipe for the vmpk project. I've borrowed it from the Arora project (I think). It is much simpler than yours, and I let qmake to decide if it is necessary regenerate any .qm files when the output has been erased or the input .ts has changed, like any other compiler does.
updateqm.pri
# update translations
isEmpty(QMAKE_LRELEASE) {
win32:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\\lrelease.exe
else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease }
}
updateqm.input = TRANSLATIONS
updateqm.output = $$OUT_PWD/${QMAKE_FILE_BASE}.qm
updateqm.commands = $$QMAKE_LRELEASE ${QMAKE_FILE_IN} -qm $$OUT_PWD/${QMAKE_FILE_BASE}.qm
updateqm.CONFIG += no_link target_predeps
QMAKE_EXTRA_COMPILERS += updateqm
project.pro:
TRANSLATIONS += \
translations/project_en.ts \
translations/project_cs.ts \
translations/project_de.ts \
translations/project_es.ts \
translations/project_fr.ts \
translations/project_ru.ts
include(updateqm.pri)
With this project file you can do, as always:
lupdate project.pro
Anyway, Qt5 has a builtin CONFIG+=lrelease option that makes "updateqm.pri" deprecated.

QMake: Get full path of output executable

In my .pro file for QMake I would like to run install_name_tool to replace some library paths. For this I need to determine path to my output executable. Particularly on macx the path to executable looks like this
<build_directory>/<configuration_name>/<target_name>.app/Contents/MacOS/<target_name>
I figured out that
message("build_directory=$${OUT_PWD}")
message("target_name=$${TARGET}")
Is there a QMake variable to populate configuration_name?
By default it is supposed to return "release" for release configurations and "debug" for debug configurations. From what I saw online people just explicitly define $${DESTDIR}
debug { DESTDIR = debug }
release { DESTDIR = release }
debug_and_release { DESTDIR = bin }
if not defined message("DESTDIR=$$DESTDIR") returns empty value for DESTDIR.
This works:
CONFIG(debug, debug|release) {
DEBUG_OR_RELEASE = debug
} else {
DEBUG_OR_RELEASE = release
}
So then the full output path is:
$${OUT_PWD}/$${DEBUG_OR_RELEASE}

Qt 4.8.4 Creating release

I'm trying to build a release version of a project I'm working on. When building in Qt creator in Debug it works great. When I try building as release I get "cannot find -lQtSerialPort". Qt 4.8.4 does not include QtSerialPort. I had to add that manually. I'm assuming there is something I'm forgetting to include here. Possibly in the .pro file?
Also, I noticed in my Qt/4.8.4/lib directory I have many .prl files. There is one for QtSerialPortd.prl. This is the only one in here that seems to have a .dll version. Not sure if this is significant or not.
Makefile includes this:
Makefile: ???.pro
c:/Qt/4.8.4/mkspecs/features/serialport.prf \
$(QMAKE) -config release -o Makefile ???.pro
c:/Qt/4.8.4/mkspecs/features/serialport.prf:
serialport.prf looks like this:
qtAddLibrary(QtSerialPort)
!isEmpty(QTSERIALPORT_BUILD_ROOT) {
INCLUDEPATH -= $$QMAKE_INCDIR_QT/QtSerialPort
QMAKE_INCDIR += $$QTSERIALPORT_BUILD_ROOT/include $$QTSERIALPORT_BUILD_ROOT/include/QtSerialPort
QTSERIALPORT_BUILD_SUBDIR = src/serialport
debug_and_release_target {
CONFIG(debug, debug|release) {
QTSERIALPORT_BUILD_SUBDIR = $$QTSERIALPORT_BUILD_SUBDIR/debug
} else {
QTSERIALPORT_BUILD_SUBDIR = $$QTSERIALPORT_BUILD_SUBDIR/release
}
}
QMAKE_LIBDIR += $$QTSERIALPORT_BUILD_ROOT/$$QTSERIALPORT_BUILD_SUBDIR
}
mac {
LIBS -= -framework QtSerialPort$${QT_LIBINFIX}
if(!debug_and_release|build_pass):CONFIG(debug, debug|release) {
LIBS += -lQtSerialPort$${QT_LIBINFIX}_debug
} else {
LIBS += -lQtSerialPort$${QT_LIBINFIX}
}
}
Edit:
After figuring out I can change the name of the file by removing the d at the end, I realized that all the libraries included on the exe that is built include files that do not end in 'd.dll' with the exception of the QtSerialPortd.dll file.
I.E ldd on the debug .exe:
QtCored4.dll => /cygdrive/c/Qt/4.8.4/bin/QtCored4.dll (0x69cc0000)
QtGuid4.dll => /cygdrive/c/Qt/4.8.4/bin/QtGuid4.dll (0xf30000)
QtNetworkd4.dll => /cygdrive/c/Qt/4.8.4/bin/QtNetworkd4.dll (0x6cb40000)
QtSerialPortd.dll => /cygdrive/c/Qt/4.8.4/bin/QtSerialPortd.dll (0x63680000)
ldd on the release .exe:
QtCore4.dll => /cygdrive/c/Qt/4.8.4/bin/QtCore4.dll (0x6e0c0000)
QtGui4.dll => /cygdrive/c/Qt/4.8.4/bin/QtGui4.dll (0x67700000)
QtNetwork4.dll => /cygdrive/c/Qt/4.8.4/bin/QtNetwork4.dll (0x65c80000)
QtSerialPortd.dll => /cygdrive/c/Qt/4.8.4/bin/QtSerialPortd.dll (0x63680000)
Release vs Debug, it looks like the QtSerialPortd.dll remained the same. I'm guessing this is going to be problematic when I try this application on different machines.
I changed the name of Qt/4.8.4/lib/libQtSerialPortd.a to Qt/4.8.4/lib/libQtSerialPort.a. It builds now. Not sure if this is going to have adverse effects.

How can i add custom build steps in Qt-Creator?

After build my app, i want copy it to specific directory (on Windows 7).
Custom build step
cmd.exe \c \k copy MyPlugin.dll ..\..\..\HostApp\Debug\plugins
But I have error:
Can't run process "cmd.exe \c \k copy MyPlugin.dll ..\..\..\HostApp\Debug\plugins"
That's wrong?
One way to do it would be to change the build output directory in the .pro file.
Something like
CONFIG(debug, debug|release) {
DESTDIR = C:/myApp/debug
} else {
DESTDIR = C:/myApp/release
}
Or in your particular case
CONFIG(debug, debug|release) {
DESTDIR = ..\..\..\HostApp\Debug\plugins
} else {
DESTDIR = ..\..\..\HostApp\Release\plugins
}
Edit:
This question has some good alternatives to my answer.

Equivalent of *Nix 'which' command in PowerShell?

How do I ask PowerShell where something is?
For instance, "which notepad" and it returns the directory where the notepad.exe is run from according to the current paths.
The very first alias I made once I started customizing my profile in PowerShell was 'which'.
New-Alias which get-command
To add this to your profile, type this:
"`nNew-Alias which get-command" | add-content $profile
The `n at the start of the last line is to ensure it will start as a new line.
Here is an actual *nix equivalent, i.e. it gives *nix-style output.
Get-Command <your command> | Select-Object -ExpandProperty Definition
Just replace with whatever you're looking for.
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
When you add it to your profile, you will want to use a function rather than an alias because you can't use aliases with pipes:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
Now, when you reload your profile you can do this:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
I usually just type:
gcm notepad
or
gcm note*
gcm is the default alias for Get-Command.
On my system, gcm note* outputs:
[27] » gcm note*
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\notepad.exe
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application Notepad2.exe C:\Utils\Notepad2.exe
Application Notepad2.ini C:\Utils\Notepad2.ini
You get the directory and the command that matches what you're looking for.
Try this example:
(Get-Command notepad.exe).Path
My proposition for the Which function:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
A quick-and-dirty match to Unix which is
New-Alias which where.exe
But it returns multiple lines if they exist so then it becomes
function which {where.exe command | select -first 1}
I like Get-Command | Format-List, or shorter, using aliases for the two and only for powershell.exe:
gcm powershell | fl
You can find aliases like this:
alias -definition Format-List
Tab completion works with gcm.
To have tab list all options at once:
set-psreadlineoption -editmode emacs
This seems to do what you want (I found it on http://huddledmasses.org/powershell-find-path/):
Function Find-Path($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
## You could comment out the function stuff and use it as a script instead, with this line:
#param($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
if($(Test-Path $Path -Type $type)) {
return $path
} else {
[string[]]$paths = #($pwd);
$paths += "$pwd;$env:path".split(";")
$paths = Join-Path $paths $(Split-Path $Path -leaf) | ? { Test-Path $_ -Type $type }
if($paths.Length -gt 0) {
if($All) {
return $paths;
} else {
return $paths[0]
}
}
}
throw "Couldn't find a matching path of type $type"
}
Set-Alias find Find-Path
Check this PowerShell Which.
The code provided there suggests this:
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
Try the where command on Windows 2003 or later (or Windows 2000/XP if you've installed a Resource Kit).
BTW, this received more answers in other questions:
Is there an equivalent of 'which' on Windows?
PowerShell equivalent to Unix which command?
If you want a comamnd that both accepts input from pipeline or as paramater, you should try this:
function which($name) {
if ($name) { $input = $name }
Get-Command $input | Select-Object -ExpandProperty Path
}
copy-paste the command to your profile (notepad $profile).
Examples:
❯ echo clang.exe | which
C:\Program Files\LLVM\bin\clang.exe
❯ which clang.exe
C:\Program Files\LLVM\bin\clang.exe
I have this which advanced function in my PowerShell profile:
function which {
<#
.SYNOPSIS
Identifies the source of a PowerShell command.
.DESCRIPTION
Identifies the source of a PowerShell command. External commands (Applications) are identified by the path to the executable
(which must be in the system PATH); cmdlets and functions are identified as such and the name of the module they are defined in
provided; aliases are expanded and the source of the alias definition is returned.
.INPUTS
No inputs; you cannot pipe data to this function.
.OUTPUTS
.PARAMETER Name
The name of the command to be identified.
.EXAMPLE
PS C:\Users\Smith\Documents> which Get-Command
Get-Command: Cmdlet in module Microsoft.PowerShell.Core
(Identifies type and source of command)
.EXAMPLE
PS C:\Users\Smith\Documents> which notepad
C:\WINDOWS\SYSTEM32\notepad.exe
(Indicates the full path of the executable)
#>
param(
[String]$name
)
$cmd = Get-Command $name
$redirect = $null
switch ($cmd.CommandType) {
"Alias" { "{0}: Alias for ({1})" -f $cmd.Name, (. { which $cmd.Definition } ) }
"Application" { $cmd.Source }
"Cmdlet" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Function" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Workflow" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"ExternalScript" { $cmd.Source }
default { $cmd }
}
}
Use:
function Which([string] $cmd) {
$path = (($Env:Path).Split(";") | Select -uniq | Where { $_.Length } | Where { Test-Path $_ } | Get-ChildItem -filter $cmd).FullName
if ($path) { $path.ToString() }
}
# Check if Chocolatey is installed
if (Which('cinst.bat')) {
Write-Host "yes"
} else {
Write-Host "no"
}
Or this version, calling the original where command.
This version also works better, because it is not limited to bat files:
function which([string] $cmd) {
$where = iex $(Join-Path $env:SystemRoot "System32\where.exe $cmd 2>&1")
$first = $($where -split '[\r\n]')
if ($first.getType().BaseType.Name -eq 'Array') {
$first = $first[0]
}
if (Test-Path $first) {
$first
}
}
# Check if Curl is installed
if (which('curl')) {
echo 'yes'
} else {
echo 'no'
}
You can install the which command from https://goprogram.co.uk/software/commands, along with all of the other UNIX commands.
If you have scoop you can install a direct clone of which:
scoop install which
which notepad
There also always the option of using which. there are actually three ways to access which from Windows powershell, the first (not necessarily the best) wsl -e which command (this requires installation of windows subsystem for Linux and a running distro). B. gnuwin32 which is a port of several gnu binaries in .exe format as standle alone bundled lanunchers option three, install msys2 (cross compiler platform) if you go where it installed in /usr/bin you'll find many many gnu utils that are more up-to-date. most of them work as stand alone exe and can be copied from the bin folder to your home drive somewhere amd added to your PATH.
There also always the option of using which. there are actually three ways to access which from Windows powershell
The first, (though not the best) is wsl(windows subsystem for linux)
wsl -e which command
This requires installation of windows subsystem for Linux and a running distro.
Next is gnuwin32 which is a port of several gnu binaries in .exe format as standle alone bundled lanunchers
Third, install msys2 (cross compiler platform) if you go where it installed in /usr/bin you'll find many many gnu utils that are more up-to-date. most of them work as stand alone exe and can be copied from the bin folder to your home drive somewhere amd added to your PATH.

Resources