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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#! /bin/sh # Simple script that generates a cmake project file for a Qt project # # Copyright (c) 2013 Sune Vuorela <sune@vuorela.dk> # # 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.
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…
There’s also http://sourceforge.net/projects/qmake2cmake – worked quite good last time I tried it :)
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.
You may also be interested in this qt-cmake template:
https://github.com/giddie/qt-cmake-template