From e665f96c76ac765a0ee13f54448de751816694c4 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Mon, 2 Jan 2023 17:10:19 +0100 Subject: [PATCH] Let cmake retrieve version and variable information from meson build system Signed-off-by: Ralf Habacker --- CMakeLists.txt | 53 ++++----- cmake/config.h.cmake | 10 +- cmake/modules/MacrosAutotools.cmake | 168 ---------------------------- cmake/modules/MacrosMeson.cmake | 52 +++++++++ meson.build | 4 + 5 files changed, 82 insertions(+), 205 deletions(-) delete mode 100644 cmake/modules/MacrosAutotools.cmake create mode 100644 cmake/modules/MacrosMeson.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2dd95e989..d3ec71be4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,39 +3,32 @@ cmake_minimum_required(VERSION 3.9) # we do not need to have WIN32 defined set(CMAKE_LEGACY_CYGWIN_WIN32 0) -project(dbus) - # where to look first for cmake modules, before ${CMAKE_ROOT}/Modules/ is checked -list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") -list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") - -option(DBUS_BUILD_TESTS "enable unit test code" ON) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") # detect version -include(MacrosAutotools) -autoinit(configure.ac) -autoversion(dbus) - -# replacement for AC_C_BIGENDIAN -include (TestBigEndian) -test_big_endian(WORDS_BIGENDIAN) +include(MacrosMeson) +meson_init(meson.build) +meson_version(dbus) +set(DBUS_PATCH_VERSION 0) +set(VERSION "${DBUS_VERSION}") -if(EXISTS ${PROJECT_SOURCE_DIR}/config.h.in) - autoheaderchecks(${PROJECT_SOURCE_DIR}/config.h.in ${PROJECT_SOURCE_DIR}/cmake/ConfigureChecks.cmake ${PROJECT_SOURCE_DIR}/cmake/config.h.cmake) +if(WIN32) + set(LANGUAGES C CXX) else() - message(STATUS "Generate config.h.in with autogen.sh to enable cmake header difference check.") + set(LANGUAGES C) endif() +project(dbus + VERSION ${DBUS_VERSION} + LANGUAGES C CXX +) -# used by file version info -set(DBUS_PATCH_VERSION "0") +option(DBUS_BUILD_TESTS "enable unit test code" ON) -# set PACKAGE_... variables -autopackage( - dbus - ${DBUS_VERSION_STRING} - "http://dbus.freedesktop.org" - "https://gitlab.freedesktop.org/dbus/dbus/issues" -) +# replacement for AC_C_BIGENDIAN +include (TestBigEndian) +test_big_endian(WORDS_BIGENDIAN) include(Macros) string(TIMESTAMP DBUS_BUILD_TIMESTAMP "%Y%m%d%H%M" UTC) @@ -111,6 +104,7 @@ set(BUILD_SHARED_LIBS ON) set(INSTALL_TARGETS_DEFAULT_ARGS EXPORT DBus1Targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) if(CYGWIN) + # TODO cygwin is not using WIN32 api set(WIN32) endif() @@ -381,8 +375,9 @@ if(UNIX AND NOT DBUS_DISABLE_ASSERT) endif() if(DBUS_WITH_GLIB) - autodefine(GLIB_VERSION_MIN_REQUIRED) - autodefine(GLIB_VERSION_MAX_ALLOWED) + # keep in sync with meson.build + set(GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_38) + set(GLIB_VERSION_MAX_ALLOWED "G_ENCODE_VERSION(2,44)") endif() string(APPEND CMAKE_C_FLAGS_DEBUG " -D_DEBUG") @@ -674,10 +669,10 @@ set(DBUS_INTERNAL_LIBRARIES dbus-internal) set(DBUS_INTERNAL_ADD_LIBRARY_OPTIONS STATIC) set(DBUS_INTERNAL_CLIENT_DEFINITIONS "-DDBUS_COMPILATION") -configure_file(cmake/config.h.cmake ${PROJECT_BINARY_DIR}/config.h ) +configure_file(cmake/config.h.cmake ${PROJECT_BINARY_DIR}/config.h) if(WIN32) -configure_file(cmake/dbus-env.bat.cmake ${PROJECT_BINARY_DIR}/bin/dbus-env.bat ) +configure_file(cmake/dbus-env.bat.cmake ${PROJECT_BINARY_DIR}/bin/dbus-env.bat) install(FILES ${PROJECT_BINARY_DIR}/bin/dbus-env.bat DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() diff --git a/cmake/config.h.cmake b/cmake/config.h.cmake index 43771b999..a4811b540 100644 --- a/cmake/config.h.cmake +++ b/cmake/config.h.cmake @@ -3,6 +3,8 @@ #ifndef _DBUS_CONFIG_H #define _DBUS_CONFIG_H +#cmakedefine VERSION "@VERSION@" + /* On Windows, we expect to be using msvcrt.dll-compatible printf * (%I64u instead of %llu) unless otherwise specified. This must be * done near the beginning of config.h, before we have included any @@ -11,14 +13,6 @@ # define __USE_MINGW_ANSI_STDIO 0 #endif -@AUTOPACKAGE_CONFIG_H_TEMPLATE@ - -/* - * Variables defined by AC_DEFINE in ../configure.ac - * should be placed in this file -*/ - -/* AC_C_BIGENDIAN */ #cmakedefine WORDS_BIGENDIAN /* Opt-in to modern APIs and thread-safety for Solaris. In the Autotools diff --git a/cmake/modules/MacrosAutotools.cmake b/cmake/modules/MacrosAutotools.cmake deleted file mode 100644 index ab4f1c1b5..000000000 --- a/cmake/modules/MacrosAutotools.cmake +++ /dev/null @@ -1,168 +0,0 @@ -# -# cmake package for autotools support -# -# @Author Ralf Habacker -# - -# -# load autotools configure file into an internal list named _configure_ac -# -macro(autoinit config) - set(_configure_ac_name ${config}) - file(READ ${config} _configure_ac_raw) - # Convert file contents into a CMake list (where each element in the list - # is one line of the file) - string(REGEX REPLACE ";" "\\\\;" _configure_ac "${_configure_ac_raw}") - string(REGEX REPLACE "\n" ";" _configure_ac "${_configure_ac}") -endmacro() - -# extracts version information from autoconf config file -# and set related cmake variables -# -# returns -# ${prefix}_VERSION -# ${prefix}_VERSION_STRING -# ${prefix}_MAJOR_VERSION -# ${prefix}_MINOR_VERSION -# ${prefix}_MICRO_VERSION -# ${prefix}_LIBRARY_AGE -# ${prefix}_LIBRARY_REVISION -# ${prefix}_LIBRARY_CURRENT -# -macro(autoversion prefix) - string(TOUPPER ${prefix} prefix_upper) - string(REGEX REPLACE ".*${prefix}_major_version], .([0-9]+).*" "\\1" ${prefix_upper}_MAJOR_VERSION ${_configure_ac_raw}) - string(REGEX REPLACE ".*${prefix}_minor_version], .([0-9]+).*" "\\1" ${prefix_upper}_MINOR_VERSION ${_configure_ac_raw}) - string(REGEX REPLACE ".*${prefix}_micro_version], .([0-9]+).*" "\\1" ${prefix_upper}_MICRO_VERSION ${_configure_ac_raw}) - set(${prefix_upper}_VERSION ${${prefix_upper}_MAJOR_VERSION}.${${prefix_upper}_MINOR_VERSION}.${${prefix_upper}_MICRO_VERSION}) - set(${prefix_upper}_VERSION_STRING "${${prefix_upper}_VERSION}") - string(REGEX REPLACE ".*LT_AGE=([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_AGE ${_configure_ac_raw}) - string(REGEX REPLACE ".*LT_CURRENT=([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_CURRENT ${_configure_ac_raw}) - string(REGEX REPLACE ".*LT_REVISION=([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_REVISION ${_configure_ac_raw}) -endmacro() - -# -# Defines package related variables (PACKAGE_..., PACKAGE and VERSION) -# as done by autotools. -# -# Additional it defines a cmake variable named PACKAGE_CONFIG_H_TEMPLATE -# which could be placed in config.h templates to have those variables -# defined at code level like shown below: -# -# config.h.template -# ... -# @AUTOPACKAGE_CONFIG_H_TEMPLATE@ -# ... -# -macro(autopackage name version url support_url) - # Define to the full name of this package. - set(PACKAGE_NAME ${name}) - - # Define to the version of this package. - set(PACKAGE_VERSION ${version}) - - # Define to the home page for this package. - set(PACKAGE_URL ${url}) - - # Define to the address where bug reports for this package should be sent. - set(PACKAGE_BUGREPORT ${support_url}) - - # Define to the full name and version of this package. - set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") - - # Define to the one symbol short name of this package. - set(PACKAGE_TARNAME ${PACKAGE_NAME}) - - set(PACKAGE ${name}) - set(VERSION ${DBUS_VERSION_STRING}) - - string(CONFIGURE "/* generated by cmake macro autopackage */\n -/* Define to the address where bug reports for this package should be sent. */ -#define PACKAGE_BUGREPORT \"@PACKAGE_BUGREPORT@\" - -/* Define to the full name of this package. */ -#define PACKAGE_NAME \"@PACKAGE_NAME@\" - -/* Define to the full name and version of this package. */ -#define PACKAGE_STRING \"@PACKAGE_STRING@\" - -/* Define to the one symbol short name of this package. */ -#define PACKAGE_TARNAME \"@PACKAGE_TARNAME@\" - -/* Define to the home page for this package. */ -#define PACKAGE_URL \"@PACKAGE_URL@\" - -/* Define to the version of this package. */ -#define PACKAGE_VERSION \"@PACKAGE_VERSION@\" - -/* defined by autotools package */ -#define PACKAGE \"@PACKAGE@\" -#define VERSION \"@VERSION@\" -" AUTOPACKAGE_CONFIG_H_TEMPLATE) -endmacro() - -# -# define a cmake variable from autotools AC_DEFINE statement -# -macro(autodefine name) - foreach(line ${_configure_ac}) - if(line MATCHES ".*AC_DEFINE(.*${name}.*).*") - string(REGEX REPLACE ".*AC_DEFINE(.*).*" "\\1" value ${line}) - string(REGEX REPLACE "[^[]*\\[[^]]*\\], *\\[([^]]*)\\],.*" "\\1" value2 ${value}) - string(REPLACE "[" "" value3 ${value2}) - string(REPLACE "]" "" value4 ${value3}) - set(${name} ${value4}) - endif() - endforeach() -endmacro() - -macro(autoheaderchecks config_h_in configure_checks_file config_h_cmake) - file(READ ${configure_checks_file} configure_checks_file_raw) - file(READ ${config_h_in} _config_h_in_raw) - file(READ ${config_h_cmake} _config_h_cmake_raw) - string(REGEX REPLACE ";" "\\\\;" _config_h_in "${_config_h_in_raw}") - string(REGEX REPLACE "\n" ";" _config_h_in "${_config_h_in}") - foreach(line ${_config_h_in}) - #message(STATUS ${line}) - if(line MATCHES ".*HAVE_.*_H.*") - string(REGEX REPLACE ".*HAVE_(.*)_H.*" "\\1" key ${line}) - set(full_key "HAVE_${key}_H") - if(key MATCHES ".*_.*") - string(REGEX MATCH "^[A-Z0-9]+" dir ${key}) - string(REGEX MATCH "[A-Z0-9]+$" file ${key}) - string(TOLOWER ${dir} dirname) - string(TOLOWER ${file} filename) - set(check "check_include_file(${dirname}/${filename}.h ${full_key})") - set(config_define "#cmakedefine ${full_key}") - else() - set(file ${key}) - string(TOLOWER ${file} filename) - set(check "check_include_file(${filename}.h ${full_key})") - set(config_define "#cmakedefine ${full_key}") - endif() - if(NOT configure_checks_file_raw MATCHES ".*${full_key}.*") - message("${check}") - endif() - if(NOT _config_h_cmake_raw MATCHES "${full_key}") - message("${config_define}") - endif() - endif() - endforeach() -endmacro() - -# -# parses config.h template and create cmake equivalent -# not implemented yet -# -macro(autoconfig template output) - file(READ ${template} contents) - # Convert file contents into a CMake list (where each element in the list - # is one line of the file) - string(REGEX REPLACE ";" "\\\\;" contents "${contents}") - string(REGEX REPLACE "\n" ";" contents "${contents}") - foreach(line contents) - message(STATUS ${line}) - # find #undef lines - # append to config.h #define - endforeach() -endmacro() diff --git a/cmake/modules/MacrosMeson.cmake b/cmake/modules/MacrosMeson.cmake new file mode 100644 index 000000000..d26617d2b --- /dev/null +++ b/cmake/modules/MacrosMeson.cmake @@ -0,0 +1,52 @@ +# +# cmake package for meson support +# +# SPDX-FileCopyrightText: © 2023 Ralf Habacker +# SPDX-License-Identifier: BSD-3-Clause + +# +# load meson build file into an internal list named _meson_build +# +macro(meson_init config) + set(_meson_build_name ${config}) + file(READ ${config} _meson_build_raw) + # Convert file contents into a CMake list (where each element in the list + # is one line of the file) + string(REGEX REPLACE ";" "\\\\;" _meson_build "${_meson_build_raw}") + string(REGEX REPLACE "\n" ";" _meson_build "${_meson_build}") +endmacro() + +# extracts version information from autoconf config file +# and set related cmake variables +# +# returns +# ${prefix}_VERSION +# ${prefix}_VERSION_STRING +# ${prefix}_MAJOR_VERSION +# ${prefix}_MINOR_VERSION +# ${prefix}_MICRO_VERSION +# ${prefix}_LIBRARY_AGE +# ${prefix}_LIBRARY_REVISION +# ${prefix}_LIBRARY_CURRENT +# +macro(meson_version prefix) + set(WS "[ \t\r\n]") + string(TOUPPER ${prefix} prefix_upper) + string(REGEX REPLACE ".*${WS}version:${WS}*'([0-9.]+)'.*" "\\1" ${prefix_upper}_VERSION ${_meson_build_raw}) + string(REPLACE "." ";" VERSION_LIST ${${prefix_upper}_VERSION}) + list(GET VERSION_LIST 0 ${prefix_upper}_MAJOR_VERSION) + list(GET VERSION_LIST 1 ${prefix_upper}_MINOR_VERSION) + list(GET VERSION_LIST 2 ${prefix_upper}_MICRO_VERSION) + set(${prefix_upper}_VERSION_STRING "${${prefix_upper}_VERSION}") + string(REGEX REPLACE ".*${WS}lt_age${WS}*=${WS}*([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_AGE ${_meson_build_raw}) + string(REGEX REPLACE ".*${WS}lt_current${WS}*=*${WS}*([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_CURRENT ${_meson_build_raw}) + string(REGEX REPLACE ".*${WS}lt_revision${WS}*=${WS}*([0-9]+).*" "\\1" ${prefix_upper}_LIBRARY_REVISION ${_meson_build_raw}) + message(STATUS "fetched variable from meson.build - ${prefix_upper}_VERSION = ${${prefix_upper}_VERSION} ") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_VERSION_STRING = ${${prefix_upper}_VERSION_STRING} ") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_MAJOR_VERSION = ${${prefix_upper}_MAJOR_VERSION} ") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_MINOR_VERSION = ${${prefix_upper}_MINOR_VERSION} ") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_MICRO_VERSION = ${${prefix_upper}_MICRO_VERSION} ") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_LIBRARY_AGE = ${${prefix_upper}_LIBRARY_AGE} ") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_LIBRARY_REVISION = ${${prefix_upper}_LIBRARY_REVISION}") + message(STATUS "fetched variable from meson.build - ${prefix_upper}_LIBRARY_CURRENT = ${${prefix_upper}_LIBRARY_CURRENT} ") +endmacro() diff --git a/meson.build b/meson.build index e9a3ddc40..5438b1843 100644 --- a/meson.build +++ b/meson.build @@ -61,6 +61,9 @@ config.set_quoted('DBUS_DAEMON_NAME', 'dbus-daemon') ############################################################################### # libtool versioning - this applies to libdbus # http://sources.redhat.com/autobook/autobook/autobook_91.html#SEC91 +# +# These variables are parsed automatically by cmake/modules/MacrosMeson.cmake, +# be careful if changing the formatting ## increment if the interface has additions, changes, removals. lt_current = 40 @@ -804,6 +807,7 @@ config.set('DBUS_DISABLE_CHECKS', not checks ? 1 : false) config.set('G_DISABLE_CHECKS', not checks ? 1 : false) config.set('HAVE_GIO_UNIX', have_gio_unix ? 1 : false) # Ignore post-2.38 deprecations, prevent use of post-2.44 APIs. +# keep in sync with CMakeLists.txt config.set('GLIB_VERSION_MIN_REQUIRED', 'GLIB_VERSION_2_38') config.set('GLIB_VERSION_MAX_ALLOWED', 'G_ENCODE_VERSION(2,44)') -- 2.47.3