diff --git a/CMakeLists.txt b/CMakeLists.txt index a0703082..48c7c362 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,16 @@ cmake_minimum_required( VERSION 3.5 FATAL_ERROR ) -project (urdfdom CXX C) -set (URDF_MAJOR_VERSION 5) -set (URDF_MINOR_VERSION 0) -set (URDF_PATCH_VERSION 2) +# get package version from package.xml +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +include(GetPackageXmlVersion) +get_package_xml_version(${CMAKE_SOURCE_DIR}/package.xml PACKAGE_XML) + +project (urdfdom VERSION ${PACKAGE_XML_VERSION} + LANGUAGES CXX C) + +set (URDF_MAJOR_VERSION ${PROJECT_VERSION_MAJOR}) +set (URDF_MINOR_VERSION ${PROJECT_VERSION_MINOR}) +set (URDF_PATCH_VERSION ${PROJECT_VERSION_PATCH}) set (URDF_VERSION ${URDF_MAJOR_VERSION}.${URDF_MINOR_VERSION}.${URDF_PATCH_VERSION}) set (URDF_MAJOR_MINOR_VERSION ${URDF_MAJOR_VERSION}.${URDF_MINOR_VERSION}) @@ -43,8 +50,6 @@ if(MSVC OR MSVC90 OR MSVC10) set(MSVC ON) endif (MSVC OR MSVC90 OR MSVC10) -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") - find_package(TinyXML2 REQUIRED) find_package(urdfdom_headers REQUIRED) diff --git a/cmake/GetPackageXmlVersion.cmake b/cmake/GetPackageXmlVersion.cmake new file mode 100644 index 00000000..7b32a228 --- /dev/null +++ b/cmake/GetPackageXmlVersion.cmake @@ -0,0 +1,57 @@ +# Copyright (C) 2024 Open Source Robotics Foundation +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Copied from https://github.com/gazebosim/gz-cmake/blob/gz-cmake5_5.0.0/cmake/GzGetPackageXmlVersion.cmake +# and renamed. + +################################################# +# get_package_xml_version( ) +# +# Given the path to a package.xml file in , +# extract the version number and return the following variables +# prefixed by : +# - _VERSION: the full version number (Major.Minor.Patch) +# - _VERSION_MAJOR: the major version number +# - _VERSION_MINOR: the minor version number +# - _VERSION_PATCH: the patch version number +function(get_package_xml_version package_xml_path version_var_prefix) + + if(NOT Python3_Interpreter_FOUND) + find_package(Python3 COMPONENTS Interpreter REQUIRED) + endif() + execute_process( + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_SOURCE_DIR}/print_package_xml_version.py" + "${package_xml_path}" + OUTPUT_VARIABLE PACKAGE_XML_version + ERROR_VARIABLE PACKAGE_XML_error + RESULT_VARIABLE PACKAGE_XML_result) + if(NOT ${PACKAGE_XML_result} EQUAL 0) + message("") + message(FATAL_ERROR "Failed to parse version number from package.xml: ${PACKAGE_XML_error}") + endif() + # split version number into list of three numbers + string(REPLACE "." ";" PACKAGE_XML_version_list ${PACKAGE_XML_version}) + + # Create variables for major, minor, and patch version numbers + list(GET PACKAGE_XML_version_list 0 PACKAGE_XML_version_major) + list(GET PACKAGE_XML_version_list 1 PACKAGE_XML_version_minor) + list(GET PACKAGE_XML_version_list 2 PACKAGE_XML_version_patch) + + # Return variables for the full version number as well as major, minor, and patch version numbers + set(${version_var_prefix}_VERSION ${PACKAGE_XML_version} PARENT_SCOPE) + set(${version_var_prefix}_VERSION_MAJOR ${PACKAGE_XML_version_major} PARENT_SCOPE) + set(${version_var_prefix}_VERSION_MINOR ${PACKAGE_XML_version_minor} PARENT_SCOPE) + set(${version_var_prefix}_VERSION_PATCH ${PACKAGE_XML_version_patch} PARENT_SCOPE) + +endfunction() diff --git a/print_package_xml_version.py b/print_package_xml_version.py new file mode 100755 index 00000000..c7eabf60 --- /dev/null +++ b/print_package_xml_version.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# Copyright (C) 2024 Open Source Robotics Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License") +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import re +import sys +from xml.etree import ElementTree as ET + +if len(sys.argv) != 2: + raise RuntimeError('Expected one argument with the path to a package.xml file') + +file_name = sys.argv[1] + +doc = ET.parse(file_name) +root = doc.getroot() +if root.tag != 'package': + raise RuntimeError('Invalid package.xml file, root tag <%s> should be ' % root.tag) + +if root.find('version') is None: + raise RuntimeError('Invalid package.xml file, no tag found.') + +version_str = root.find('version').text + +# validate version string using regex from catkin_pkg +# https://github.com/ros-infrastructure/catkin_pkg/blob/1.0.0/src/catkin_pkg/package_version.py#L55-L58 +match = re.match(r'^(\d+)\.(\d+)\.(\d+)$', version_str) +if match is None: + raise ValueError('Invalid version string, must be int.int.int: "%s"' % version_str) + +print(version_str, end='')