How to add source files to an external project in CMake? - sqlite

I want to integrate SQLite into my project using ExternalProject_Add.
cmake_minimum_required(VERSION 2.8.8)
include(ExternalProject)
# Download, configure, build and install SQLite
ExternalProject_Add(SQLite
PREFIX ${CMAKE_SOURCE_DIR}
TMP_DIR ${CMAKE_SOURCE_DIR}/temp
STAMP_DIR ${CMAKE_SOURCE_DIR}/stamp
#--Download step--------------
DOWNLOAD_DIR ${CMAKE_SOURCE_DIR}/download
URL http://www.sqlite.org/2014/sqlite-autoconf-3080704.tar.gz
URL_HASH SHA1=70ca0b8884a6b145b7f777724670566e2b4f3cde
#--Update/Patch step----------
UPDATE_COMMAND ""
#--Configure step-------------
SOURCE_DIR ${CMAKE_SOURCE_DIR}/source
CONFIGURE_COMMAND "" # How to add sqlite3.c to the target here?
#--Build step-----------------
BINARY_DIR ${CMAKE_SOURCE_DIR}/build
BUILD_COMMAND "cmake --build ."
#--Install step---------------
INSTALL_DIR ${CMAKE_SOURCE_DIR}/install
)
The build command would use the native compiler to build all source files added to the target SQLite. However, there are non. How can I add the only source file sqlite3.c to the external project within the CONFIGURE_COMMAND?

ExternalProject_Add assumes that the project you want to pull in already ships with a (possibly complex, possibly non-CMake-based) working build system.
You have two possibilities here:
You can stick with the amalgamated autoconf version of sqlite that you are currently using. In that case the CONFIGURE_COMMAND would invoke configure and the BUILD_COMMAND would invoke make. Note that this approach will not be portable to platforms that do not have autoconf installed.
You can switch to the bare-source amalgamated version of sqlite and provide your own CMakeLists.txt for building. Since sqlite can be built with a minimum of configuration and the amalgamation only consists of a single source and header file, this is not as hard as it may sound. In this case you can simply invoke cmake for configuation and building.
Note however that you cannot provide this information in-line with ExternalProject_Add. You will need an external build script, whether that is CMake, autoconf or something else.

Building on the correct answer above, this is what I came up with. Instead of adding a second file to my repository, it gets generated from the existing CMake file. Since the source directory of the external project gets cleaned on build, the generated file must be stored in a temporary location and copied into the source directory in a later step of the external project, in this case the update command.
# SQLite
cmake_minimum_required(VERSION 2.8.8)
include(ExternalProject)
# Add CMake project file
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/temp)
file(WRITE ${CMAKE_SOURCE_DIR}/temp/CMakeLists.txt
"cmake_minimum_required(VERSION 2.8.8)\n"
"set(PROJECT_NAME sqlite)\n"
"include_directories(${CMAKE_SOURCE_DIR}/source)\n"
"add_library(sqlite3 ${CMAKE_SOURCE_DIR}/source/sqlite3.c)\n"
"install(TARGETS sqlite3 DESTINATION lib)\n"
"install(FILES sqlite3.h DESTINATION include)\n")
# Download, configure, build and install.
ExternalProject_Add(SQLite
# DEPENDS
PREFIX ${CMAKE_SOURCE_DIR}
TMP_DIR ${CMAKE_SOURCE_DIR}/temp
STAMP_DIR ${CMAKE_SOURCE_DIR}/stamp
#--Download step--------------
DOWNLOAD_DIR ${SFML_PREFIX}/download
URL http://www.sqlite.org/2014/sqlite-autoconf-3080704.tar.gz
URL_HASH SHA1=70ca0b8884a6b145b7f777724670566e2b4f3cde
#--Update/Patch step----------
UPDATE_COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/temp/CMakeLists.txt
${CMAKE_SOURCE_DIR}/source/CMakeLists.txt
#--Configure step-------------
SOURCE_DIR ${CMAKE_SOURCE_DIR}/source
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_SOURCE_DIR}/install
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
#--Build step-----------------
BINARY_DIR ${CMAKE_SOURCE_DIR}/build
BUILD_COMMAND ${CMAKE_COMMAND} --build .
#--Install step---------------
INSTALL_DIR ${CMAKE_SOURCE_DIR}/install
)

Related

Qt6 qt_generate_deploy_app_script plugin DLL copying

Currently I have to manually copy the platforms and imageformats plugin folders to the directory containing the .exe that MSVC compiled. This is very tedious as the output folders often get deleted if you're working on your CMakeLists.txt or changing compilation target.
Now qt_generate_deploy_app_script seems like an official Qt solution to solve this problem, but it does not work.
I have added the CMake bits to my CMakeLists.txt as stated
qt_generate_deploy_app_script(
TARGET HiveWE
FILENAME_VARIABLE deploy_script
NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
I can see some generated deploy scripts appear under build\x64-RelWithDebInfo\.qt, but they do not seem to be run as no DLL folders get copied to where my .exe is.
Am I misinterpreting what qt_generate_deploy_app_script should do or is it simply broken?
If you want to Creat exe in windows From Qt project you should use windeployqt
To Deploy and create Exe output with QT in windows you should follow this way:
put your compiler path in your system path. for example, if you use mingw81_64, you should set it. something like Qt/tools/mingw81_64/bin
copy exe file that provides after building in release mode in one
folder and run mingw81_64 cmd (it has separate cmd) and cd to that
folder path
windeployqt app.exe
you are using Cmake So first create one release output and then use step 3.
This command will get all dll needs for your app and your exe will work .
if you use qml
windeployqt --qmldir (the path of its directory ) app.exe
and also see these youtube videos for more info:
https://www.youtube.com/watch?v=LdSTgR0xJco
https://www.youtube.com/watch?v=hCXAgB6y8eA

Unable to customise the build directory for Qt Creator/qmake

I've got problem trying to specify the build directory (the directory that is to store all the files prior to copying them to the DESTDIR path).
I've got the following values in my .pro file:
DESTDIR = E:/Development/project/build/core/debug
OUT_PWD = E:/Development/project/build/core/debug
OBJECTS_DIR = $$DESTDIR/.obj
MOC_DIR = $$DESTDIR/.moc
RCC_DIR = $$DESTDIR/.qrc
UI_DIR = $$DESTDIR/.ui
Now, all the files eventually end up in that location, however during build, the compiler is always using the "E:/Development/build/MinGW_32bit-Debug/src/core" folder (note the missing project path). This is annoying, because I want to use the /Project/build directory as this location (which is not tracked in my git repo).
Ideally, I'd like this path to be: E:\Development\project\build\src\core\debug.
The reason I want to do this is that the build process has the same location to include the compiled libs from (it's a subdirs project).
I've had a look in the Tools > Options > Build & Run > General settings, and the default build directory is: build/build-%{CurrentProject:Name}-%{CurrentKit:FileSystemName}-%{CurrentBuild:Name}
I've had a look in my project.pro.user file, and found the following line:
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">E:/Development/build/MinGW_32bit-Debug</value>
However I'm unable to change this value. If I edit this line in the file directly, as soon as I open Qt Creator again, the change has reverted back.
Is this a Qt Creator thing, or is it a qmake thing? Would I better off using a different build system such as CMake?
The build directory is "specified" by starting qmake or cmake in the build directory. There's no point to setting it in the .pro file itself.
Qt Creator stores the build directories for a project in the .user file. Any changes made to this file outside of Qt Creator, while the project is open in the Creator, will be lost. Creator loads the file when opening the project, or creates a new one if it doesn't exist.
When the Creator starts the build by invoking qmake or cmake, it starts that process in the build directory. That's also how you should be building the project manually from the command line.
Finally, it makes very little sense to override the destinations of the intermediate build results. They are somewhere within the build directory, and that's all that matters. You're not using these files directly for anything anyway.
The customary way to build a qmake project:
mkdir project-build
cd project-build
qmake ~/project-src
make -j
The build folder should not be within the source tree!
I've recently started keeping them in $TEMP / %TEMP%: manually purging the stale builds of all sort of test projects got old after a while :)

qmake error : could not find a Qt installation of ''

Error-
$ qmake
qmake: could not find a Qt installation of ''
I installed Qt Version 4.8.2
Partial File of .profile in my home directory
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
PATH="/usr/local/Trolltech/Qt-4.8.2/bin:$PATH"
PATH="/home/user/qt-everywhere-opensource-src-4.8.2/bin:$PATH"
export PATH
fi
Output-
$ which qmake
/usr/bin/qmake
Properties
Name - qmake
Type - Link to executable (application/x-executable)
Link Target - qtchooser
Location - /usr/bin
qmake is present in this location
/usr/local/Trolltech/Qt-4.8.2/bin
Properties
Name - qmake
Type - executable (application/x-executable)
Size - 3.4 MB (3,407,312 bytes)
Location - /usr/local/Trolltech/Qt-4.8.2/bin
qmake is also present in this location
/home/username/qt-everywhere-opensource-src-4.8.2/bin
Properties
Name - qmake
Type - executable (application/x-executable)
Size - 11.2 MB (11,157,974 bytes)
Location - /home/username/qt-everywhere-opensource-src-4.8.2/bin
How to make qmake work? Which one is the correct path for qmake?
This is really simple. You don't need to modify any environmental variables, you don't need to set up QTDIR. There are only two facts you need to keep in mind:
You must invoke qmake from your desired version of Qt.
There's a 1:1 relationship between a particular installed Qt version and build, and qmake executable.
After the desired qmake was run and generated the makefile, invoking make will build your project using the version of Qt the qmake came from. This will happen in spite of any environmental variable settings etc.
So, all you need to know is which Qt installation you want to use, and its path. This should be obvious since you installed Qt yourself. There lies the bin/qmake that you need.
You can select the desired qmake just as you would select a particular binary to run. The process is not any different just because it is qmake and not, say gzip:
by explicitly typing out the path to the executable,
by using a shell alias,
by putting the desired Qt's bin folder onto the PATH.
Only you know where you installed your desired version of Qt - there are at least three paths in your question. Thus it's impossible to tell which qmake are you to invoke. Obviously one of them - the one picked up by which qmake - doesn't work.
You need to invoke the one that came with the version of Qt that you installed.
That's really all there's to it.
I think the source code is in this location:
/home/user/qt-everywhere-opensource-src-4.8.2/bin
and installation directory is at:
/usr/local/Trolltech/Qt-4.8.2/bin
you need to add the installation directory to your PATH, however if you have an other version (like the one in /usr/bin) you need to use /usr/local/Trolltech/Qt-4.8.2/bin/qmake to compile Qt project using this version.
My solution is to add this code to your .profile file:
export QTDIR="/usr/local/Trolltech/Qt-4.8.2"
export PATH="$QTDIR/bin:$PATH"
alias qmake-4.8="$QTDIR/bin/qmake"
after that use qmake-4.8 instead of qmake

Copy file to destination directory in Qt Creator

I want to copy a data file from a directory in my source tree to the directory of the linked app so it's available at runtime, on Windows only. There appear to be two suggested techniques: use a post target dependency to issue a DOS copy command (Including resource files in Qt Creator build directory) or use an install step (Copy a file to the build directory after compiling project with Qt), but I cannot make either work in the way I would like.
The former requires me to use qmake path variables to generate my source and destination paths, but they contain backslash path separators, which the DOS copy command cannot handle.
The install solution forces other users of my project to set up a post build step in Qt Creator before it will work (one per configuration, in fact) and I would like to avoid this, as I want to make my project work with a default Qt Creator installation.
Is there any way to do this apparently simple task that can be wholly defined in the .pro file for the project? For example, is there a way to expand qmake path variables in a platform specific way?
Though these commands run ONLY after the executable is ACTUALLY linked, this solution doesn't require an external batch file. Note: this a Windows-only solution:
From our .pri file:
win32 {
...
# Copy the appropriate dll files into the target destination directory.
QMAKE_TBB_LIBDIR = $$quote($$PWD/MySource/MyLibs/$${PLATFORM_NAME}/vc9)
QMAKE_POST_LINK = copy /y $${replace(QMAKE_TBB_LIBDIR, /, \\)}\\*.dll > $${replace($$quote(DESTDIR), /, \\)}
...
}
This places a command in the Makefile that copies all the .dll files in MyLibs/x64 or MyLibs/Win32 into the destination directory.
However, if the executable did not need to be linked, then the .dlls are NOT copied.
The post build batch file would not have this limitation.

CMake + Qt : define the moc/ui output directory

I'm currently transferring a project built with qmake to CMake.
In the version with qmake, in the .pri file, there was
MOC_DIR = .moc/$${PLATFORM_NAME}
that permitted to produce the MOC temporary files in a given directory, keeping the sources clean. How to do the same thing with CMake?
Note: with CMake, I use the FindQt4.cmake package and the command QT4_WRAP_CPP().
As baysmith says, if your goal is to keep your source directory clean, the real solution is to use CMake's "out-of-source" builds feature. If you're on Windows, set "Where to build the binaries" to a new directory, different from the "Where is the source code" directory. If you're on Unix, it goes something like this:
cd <source directory>
mkdir build
cd build
cmake ..
make
By running CMake on a different directory, all of the build files will go into that directory, and your sources will stay clean. (Note: the build directory doesn't have to be inside the source directory. See the CMake wiki for more details.)
If "out-of-source" doesn't work for you, I was able to find one other option. Based on what I can tell from the Qt4Macros.cmake file installed with my CMake 2.8, it isn't accessible as a config parameter. Here's the relevant line:
SET(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
The workaround is to change all of your MOC include directives to specify the subfolder you'd like to build to.
#include "moc/mainwindow.moc"
After creating the moc directory inside my build directory, there were no problems building, and my MOC file was in the new directory.

Resources