[studxml-users] [PATCH] build: Add Linux CMake build with system
Expat and pkg-config metadata
Ajrat Makhmutov
rauty at altlinux.org
Tue Nov 10 07:18:21 EST 2026
Linux distributions need a build path using their packaged Expat.
Scilab's Autotools build also needs the public include paths, compiler
definitions and transitive dependencies of the installed library.
Keeping this information with libstudxml avoids duplicating its build
requirements in each consumer.
---
CMakeLists.txt | 64 ++++++++++++++++++++++++++++++++++++++++++++++++
INSTALL | 10 ++++++++
buildfile | 3 ++-
libstudxml.pc.in | 11 +++++++++
4 files changed, 87 insertions(+), 1 deletion(-)
create mode 100644 CMakeLists.txt
create mode 100644 libstudxml.pc.in
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..98c2df7
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,64 @@
+cmake_minimum_required(VERSION 3.20)
+
+file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/manifest" release_version REGEX "^version: ")
+if(NOT release_version MATCHES "^version: ([0-9]+\\.[0-9]+\\.[0-9]+)$")
+ message(FATAL_ERROR "The CMake build requires a stable release version in manifest")
+endif()
+project(libstudxml VERSION "${CMAKE_MATCH_1}" LANGUAGES C CXX)
+
+if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
+ message(FATAL_ERROR "The libstudxml CMake build supports only Linux; use build2 on other platforms")
+endif()
+
+include(GNUInstallDirs)
+find_package(EXPAT 2.1 REQUIRED)
+
+set(LIBSTUDXML_ABI_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
+math(EXPR libstudxml.version.project_number
+ "${PROJECT_VERSION_MAJOR} * 100000000000000 + ${PROJECT_VERSION_MINOR} * 1000000000 + ${PROJECT_VERSION_PATCH} * 10000")
+set(libstudxml.version.project "${PROJECT_VERSION}")
+set(libstudxml.version.project_id "${PROJECT_VERSION}")
+set(libstudxml.version.major "${PROJECT_VERSION_MAJOR}")
+set(libstudxml.version.minor "${PROJECT_VERSION_MINOR}")
+set(libstudxml.version.patch "${PROJECT_VERSION_PATCH}")
+set(libstudxml.version.pre_release false)
+set(libstudxml.version.snapshot_sn 0)
+set(libstudxml.version.snapshot_id "")
+file(READ "${CMAKE_CURRENT_SOURCE_DIR}/libstudxml/version.hxx.in" version_header)
+string(REGEX REPLACE "\\$([a-z_.]+)\\$" "@\\1@" version_header "${version_header}")
+string(CONFIGURE "${version_header}" version_header @ONLY)
+file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/libstudxml")
+file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/libstudxml/version.hxx" "${version_header}")
+
+file(GLOB_RECURSE STUDXML_SOURCES CONFIGURE_DEPENDS "libstudxml/*.cxx")
+add_library(studxml SHARED ${STUDXML_SOURCES}
+ libstudxml/details/genx/genx.c
+ libstudxml/details/genx/char-props.c)
+set_target_properties(studxml PROPERTIES OUTPUT_NAME "studxml-${LIBSTUDXML_ABI_VERSION}")
+target_compile_features(studxml PUBLIC cxx_std_11)
+target_compile_definitions(studxml
+ PUBLIC LIBSTUDXML_EXTERNAL_EXPAT
+ PRIVATE LIBSTUDXML_SHARED_BUILD
+ INTERFACE LIBSTUDXML_SHARED)
+target_include_directories(studxml PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
+target_link_libraries(studxml PUBLIC EXPAT::EXPAT)
+add_custom_command(TARGET studxml POST_BUILD
+ COMMAND "${CMAKE_COMMAND}" -E create_symlink
+ "$<TARGET_FILE_NAME:studxml>" "$<TARGET_FILE_DIR:studxml>/libstudxml.so"
+ VERBATIM)
+
+if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
+ configure_file(libstudxml.pc.in libstudxml.pc @ONLY)
+ install(TARGETS studxml LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
+ install(FILES "$<TARGET_FILE_DIR:studxml>/libstudxml.so" DESTINATION ${CMAKE_INSTALL_LIBDIR})
+ install(DIRECTORY libstudxml/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libstudxml
+ FILES_MATCHING PATTERN "*.hxx" PATTERN "*.ixx" PATTERN "*.txx" PATTERN "*.h"
+ PATTERN "version.hxx" EXCLUDE PATTERN "expat" EXCLUDE)
+ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libstudxml/version.hxx"
+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libstudxml)
+ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libstudxml.pc"
+ DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
+endif()
diff --git a/INSTALL b/INSTALL
index 7f65e33..8acd618 100644
--- a/INSTALL
+++ b/INSTALL
@@ -4,3 +4,13 @@ $ bpkg build libstudxml
But if you don't want to use the package manager, then you can also build it
manually using the standard build2 build system.
+
+On Linux, an alternative CMake build uses system Expat (2.1 or later) and
+requires a C++11 compiler:
+
+$ cmake -S . -B cmake-build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr
+$ cmake --build cmake-build
+$ cmake --install cmake-build
+
+This builds a shared library with the private Genx implementation and
+installs headers and libstudxml.pc for pkg-config consumers.
diff --git a/buildfile b/buildfile
index 46857d8..8e94bc6 100644
--- a/buildfile
+++ b/buildfile
@@ -1,7 +1,8 @@
# file : buildfile
# license : MIT; see accompanying LICENSE file
-./: {*/ -build/} doc{INSTALL NEWS README} legal{LICENSE} manifest
+./: {*/ -build/} doc{INSTALL NEWS README} legal{LICENSE} manifest \
+ file{CMakeLists.txt libstudxml.pc.in}
# Don't install examples, tests or the INSTALL file.
#
diff --git a/libstudxml.pc.in b/libstudxml.pc.in
new file mode 100644
index 0000000..c1d21b8
--- /dev/null
+++ b/libstudxml.pc.in
@@ -0,0 +1,11 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=@CMAKE_INSTALL_FULL_LIBDIR@
+includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
+
+Name: libstudxml
+Description: Streaming XML pull parser and serializer for C++
+Version: @PROJECT_VERSION@
+Requires: expat >= 2.1.0
+Libs: -L${libdir} -lstudxml
+Cflags: -I${includedir} -DLIBSTUDXML_EXTERNAL_EXPAT -DLIBSTUDXML_SHARED
--
2.50.1
More information about the studxml-users
mailing list