Wednesday, 7 August 2019

The Meson Build System: part #1


In an earlier post, I have stated that I installed Fedora to learn gtk programming. The first step was to try GNOME Builder. I have starter gtkmm project generated and I have learned, that preferred build tool for GNOME apps is Meson. After deciding to give it a try, I had rewritten one of my side projects into it. In this post, I will compare CMake and Meson code used to generate the same library and installing it. Because a picture is worth a thousand words, and code speaks louder than picture, below there are both: CMake and Meson codes.

CMake

cmake_minimum_required (VERSION 3.10)

project(
    "madterm"
    VERSION 0.0.0.1
    LANGUAGES CXX
)

# Project dependencies

# Project definition
add_library(
    madterm
    src/enable_formatting.cpp
    src/terminal_sequence.cpp
    src/text/effects.cpp
    src/text/modification.cpp
    src/text/charset.cpp
    src/cursor/move.cpp
    src/cursor/mode.cpp
    src/window/window.cpp
    src/screen_buffer/screen_buffer.cpp
)
target_compile_features(
    madterm
    PUBLIC cxx_std_17
)
target_include_directories(
    madterm
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)

# Install part
include(CMakePackageConfigHelpers)

add_library(madterm::madterm ALIAS madterm)

configure_package_config_file(
    "madtermConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/madtermConfig.cmake"
    INSTALL_DESTINATION lib/cmake/madterm
)
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/madtermConfigVersion.cmake"
    VERSION ${madterm_VERSION}
    COMPATIBILITY SameMajorVersion
)
install(
    TARGETS madterm EXPORT madtermTargets
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)
install(
    EXPORT madtermTargets
    FILE madtermTargets.cmake
    NAMESPACE madterm::
    DESTINATION lib/cmake/madterm
)
install(
    FILES
        "${CMAKE_CURRENT_BINARY_DIR}/madtermConfig.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/madtermConfigVersion.cmake"
    DESTINATION lib/cmake/madterm
)
INSTALL (
    DIRECTORY include/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h*"
)

Meson

project('madterm', 'cpp', version: '0.0.0.1', default_options: ['cpp_std=c++17'])

madterm_includes = include_directories('include')
madterm_lib = library(
    'madterm',
    'src/cursor/mode.cpp',
    'src/cursor/move.cpp',
    'src/enable_formatting.cpp',
    'src/screen_buffer/screen_buffer.cpp',
    'src/terminal_sequence.cpp',
    'src/text/effects.cpp',
    'src/text/modification.cpp',
    'src/text/charset.cpp',
    'src/window/window.cpp',
    include_directories: madterm_includes,
    install: true
)
madterm = declare_dependency(
    link_with : madterm_lib, include_directories : madterm_includes
)
install_subdir('include/madterm', install_dir: 'include')


CMake vs Meson

It is obvious, that Meson is more expressive and shorter, even thou I have omitted additional CMake file madtermConfig.cmake.in. Moreover, Meson is a typed language, so IDEs can better suggest its code and Meson functions do not look like a sh script. What is more, writing good CMake file is hard! The Internet is full of tutorials, which teach how to code in CMake in outdated and unmaintainable fashion! To write good CMake file, You have to know Modern CMake term and spend long hours searching through the web.

It is noteworthy, to compare CMake's behaviour and Meson's. Adding CMake's target is sufficient to use it later as a dependency, providing both: include directory and built library. On the other hand, in Meson, You have to be explicit: create include directory object, library and dependency with declare_dependency function.

Bad things in Meson

Unfortunately, there are some downsides of using Meson. It is not as widespread as CMake, so it has not had as good IDEs support as CMake. Moreover, when using Windows, You have to install ninja and launch Developers Console, to properly build Meson project. Because of a switch to Linux, these are not deal-breakers to me, so I will play with Meson for a while and post my afterthoughts.

No comments:

Post a Comment