Cmake and Qt
Total Page:16
File Type:pdf, Size:1020Kb
CMake and Qt Kevin Funk April 17 The build system for Qt 6 is moving to CMake. Should you use it yourself? What are the benefits of CMake, and how can it help transform your build system? Here are the basic facts you need to know to take advantage of CMake for building Qt applications. If you’ve read any of the Qt development press Like QMake, CMake is a build system generator. releases, forums, or wikis, you may be aware However, QMake was starting to show its age that Qt 6.0 is in active development with a since it was only receiving maintenance fixes planned release at the end of 2020. While and critical updates. It had a number of issues those discussions often focus on some pretty in handling distributed builds, implementing interesting developments – from major QML configuration checks, and scaling for large upgrades to new graphical underpinnings – projects. However, a bigger concern was that there is one small detail that often goes under maintaining a home-grown build system is not the radar: the build system for Qt 6 itself will be a core expertise of The Qt Company or the Qt ported over from QMake to CMake. In fact, at project team. Cross-compilation, something the time of writing, building Qt exclusively with that Qt is known for, was challenging to set up CMake became significantly closer to reality with in QMake for all but the simplest cases. These a merge of that project into the main develop- issues and others led The Qt Company to plan ment branch. the move away from QMake, a fully custom Qt-specific solution, to an industry standard KDAB | the Qt, OpenGL and C++ experts 1 CMake makes it much easier to manage dependencies from other modules and libraries, allowing you to detect and use third-party packages on a variety of platforms. build system. CMake was a strong contender as get better than it already is. At the same time, a build system with a lot of flexibility to handle CMake is also automatically getting upstream many of the problems that QMake could not. Its patches from the Qt Project. (An example is the further status as the de facto build system for addition of CMake’s multi-config Ninja support C++ made CMake the most reasonable QMake feature.) These changes serve the whole C++ replacement. ecosystem, not just Qt, which is welcome. People who build Qt from source should also Good news! find things easier to manage. Since the Qt build Why is Qt’s use of CMake going to be good is driven by CMake exclusively now, there are news for Qt developers? If you’re not familiar no extra configuration scripts that need to run with CMake, a little introduction is in order. before invoking CMake. People who already know CMake will mostly feel at home; it’s just CMake allows developers to use their preferred like any other big CMake project such as LLVM, tools, it is used in hundreds of projects, and it’s OpenCV, or any of the KDE frameworks. Of an open source tool supported by a huge com- course, if you’re already a CMake user, you munity. Because CMake directly supports IDE can expect Qt support and Qt integrations for project exports for your choice of IDE, like VS or CMake to continue to improve. XCode, interactive debugging on your platform of choice will become much easier. Since CMake support in Microsoft’s popular Visual Studio IDE Bad news? has recently seen significant improvements, it The selection of build systems supported by makes CMake a good choice if you’re developing Qt is shrinking. Qbs has been deprecated as on the Windows platform. of December 2019, and QMake won’t have any future development except bug fixes. On the CMake also makes it much easier to manage bright side, having to maintain and extend three dependencies from other modules and libraries. separate build systems diluted the available This is because CMake ships with a lot of find development talent pool that was addressing modules – CMake scripts that allow you to Qt’s build tools, which left each tool needing a detect and use third-party packages on a variety bit of attention. Simplifying things down to one of platforms. main build system will help focus efforts on a Last but not least, CMake also has tight integra- single tool. Not to mention, it will make it easier tion with source code analysis tools that can for new Qt developers to pick up. be run as part of a regular build, such as Clang- Another item to discuss is CMake syntax. CMake Tidy, cpplint, and cppcheck. Having CMake has some operational quirks that make it a chal- features and reporting for these tools is espe- lenge to learn and its language is not always the cially handy when using continuous integration most pleasant or obvious. As an example of the systems that automatically build your projects. latter, let’s say you want to spawn parallel builds Now that Qt itself is being built with CMake, with one more than the number of processor we can expect CMake support within Qt to cores of the build machine. KDAB | the Qt, OpenGL and C++ experts 2 CMake makes file manipulation easy: copying files to a network drive, computing and comparing hashes, and uploading packages to a cloud repository are examples. if(CPUS) tempted to write full programs in it, you’re math(EXPR CPUS "${CPUS} + 1") probably doing something wrong.) set(BLD_FLAGS "${BLD_FLAGS} -j${CPUS}") That said, it’s rare that you’ll need to create your endif() own build configurations from scratch anyway. Ugh – with a mix of strings and awkward syntax The benefit of being a widely used tool is that just to do a simple increment, it doesn’t look too most common build problems problems have pretty. It’s not very fun for coding up non-trivial been already solved. It’s rather easy to find an logic either, since CMake functions have to example CMakeList.txt structure that can be use global variables in order to return a value modified to meet your needs. back to the caller. On the other hand, there is so much functionality built into CMake that the Mo’ better majority of build configurations are handled If you’re starting to get convinced that CMake without needing to resort to these features. might be for you, here are a few more benefits That’s true especially in CMake version 3.12 or you can look forward to. greater, since over all the years it has been in development CMake has significantly improved its capability as well as simplified the burden on • Requirements are attached to the targets the user. (Watch Deniz Bahadir’s great side- and are automatically propagated as neces- by-side comparison of traditional and modern sary through the build, which includes any CMake for some specifics.) requirements from third party libraries. This makes creating a complex build much less While it might not win awards for orthogonal structure and inherent beauty, the fact that error-prone. CMake can do cross-platform math and string • CMake is aware of modern C++. If you need manipulation at all puts it far ahead of plain particular compiler features in your code makefiles. This can be a life saver to any build or are using a particular language standard master who’s been forced to resort to sed (like C++11), you can determine this when scripts or custom utilities to trick make and creating your build instead of waiting for a nmake into doing their bidding. Knowing that failure at compile time. you have the ability to delve into more complex logic if need be gives you confidence that no • File manipulation is easy. If you’ve had trou- matter how complex the tasks needed by your bles bending QMake to your will at the end build process, you’ll be able to manage them. of a build, you’ll welcome CMake’s compre- While CMake is definitely Turing-complete, it hensive file command. Copying files to a isn’t designed for heavy-duty calculation. You network drive, computing and comparing won’t find it adhering to in-vogue programming hashes, generating header files, and up- paradigms, and it won’t have the safety nets loading packages to a cloud repository are you may have come to expect in modern pro- similarly trivial. gramming environments. (So if you find yourself KDAB | the Qt, OpenGL and C++ experts 3 As a library builder, you can specify your library’s usage requirements so that users will automatically use the right dependencies, linker options, or compiler features. Libraries under CMake find_package(somecoollib) One of the biggest places where CMake is worth target_link_libraries(main somecoollib) its weight in gold is in incorporating external Many common libraries that you might need libraries. A big reason is because as a library are easily detected using CMake’s find_package builder, you can specify not only your library’s command. The 3.16 release includes built-in build requirements but its usage requirements find module support for 162 commonly used – everything that users of your library will need libraries: BZip, Boost, curl, gif, Glut, KDE, to incorporate it into their builds. This can go Lua, MFC, OpenSSL, Qt, Threads, Wget and much further than just the include and library many more. (The full list for your particular paths: it can also include things like additional cmake version can be generated with dependencies, linker options, pre-processor cmake --help-module-list.) Even if your macros, necessary language standard (such as favorite library isn’t there, there’s a pretty C++11, C++14, or C++17), or required compiler decent chance that someone has already made features (like u8 literals, ABI version, or code a CMake find module for it – Google it! As a generation switches).