CMake skeleton for small Qt projects

For small Qt-projects, I have for a long time been using qmake, mostly because qmake -project gives me a working build in most cases, but once the project grows a bit, I have switched to cmake because there is just things there I like better.

So, I thought how to start with cmake instead, and ended up writing a small script. I guess I should share it:

#! /bin/sh

# Simple script that generates a cmake project file for a Qt project

#
# Copyright (c) 2013 Sune Vuorela
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the “Software”), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#

CPPFILES=”$(ls *.cpp 2>/dev/null)”

UIFILES=”$(ls *.ui 2>/dev/null)”

QRCFILES=”$(ls *.qrc 2>/dev/null)”

NAME=”$(basename $(pwd))”

error() {
echo ERROR: “$@”
exit 1
}

debug() {
[ -z "${DEBUG}" ] && return
echo DEBUG: “$@” 1>&2
}

# sanity checks

if [ -e "CMakeLists.txt" ]
then
error CMakeLists.txt found. Doing nothing.
fi

if [ -z "${CPPFILES}" ]
then
error “No cpp files found”
fi

debug NAME ${NAME}
debug CPPFILES ${CPPFILES}
debug UIFILES ${UIFILES}
debug QRCFILES ${QRCFILES}

# First part of cmake files
cat >> CMakeLists.txt << __EOHEADER__
project(${NAME})

cmake_minimum_required(VERSION 2.8.11)

find_package(Qt5 CONFIG REQUIRED Widgets)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

__EOHEADER__

# Handle ui files if any
if [ -n "${UIFILES}" ]
then
cat >> CMakeLists.txt << __EOUI__
set(${NAME}_ui ${UIFILES})
qt5_wrap_ui(${NAME}_wrapped_ui \${${NAME}_ui})

__EOUI__

UIWRAPPED="\${${NAME}_wrapped_ui}"
debug prepared UIWRAPPED ${UIWRAPPED}
fi

# Handle qrc files if any
if [ -n "${QRCFILES}" ]
then

cat >> CMakeLists.txt << __EOQRC__
set(${NAME}_qrc ${QRCFILES})
qt5_add_resources(${NAME}_wrapped_qrc \${${NAME}_qrc})

__EOQRC__

QRCWRAPPED="\${${NAME}_wrapped_qrc}"
debug prepared QRCWRAPPED ${QRCWRAPPED}
fi

# Build the actual library
cat >> CMakeLists.txt << __EOCPP__
set(${NAME}_src ${CPPFILES})

add_executable(${NAME} \${${NAME}_src} ${UIWRAPPED} ${QRCWRAPPED} )

target_link_libraries(${NAME} Qt5::Widgets )
__EOCPP__

I’ve saved it locally as cmake-qtproject – and it works with at least cmake 2.8.11.

I hope wordpress doesn’t make too much mess of it. Have fun.

Posted in debian, english, kde
4 comments on “CMake skeleton for small Qt projects
  1. Frederik says:

    Thanks, this is really a great idea and a missing feature of cmake – I have the same, using qmake -project is just so damn fast. Now I have no excuse left…

  2. mck182 says:

    There’s also http://sourceforge.net/projects/qmake2cmake – worked quite good last time I tried it :)

  3. Tim says:

    Mh, have you take a look at qbs? I like it much better than cmake. I was
    much faster in setting up a qbs project than a cmake project.

  4. Paul Dann says:

    You may also be interested in this qt-cmake template:

    https://github.com/giddie/qt-cmake-template

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>