Commit 6c2e02ff authored by François Grisez's avatar François Grisez
Browse files

Add CMake utilitary fuctions and macros for source archive generation

parent 25f4a51e
############################################################################
# CMakeLists.txt
# Copyright (C) 2016 Belledonne Communications, Grenoble France
# Copyright (C) 2010-2019 Belledonne Communications, Grenoble France
#
############################################################################
#
......@@ -20,23 +20,18 @@
#
############################################################################
cmake_minimum_required(VERSION 3.2) #since cmake 3.2 is available as a patch for yocto Fido
cmake_minimum_required(VERSION 3.11) # we need CMake 3.11 for defining 'package_source' target as custom target
set(CMAKE_MACOSX_RPATH ON) # Before cmake 3.0.0, MACOSX_RPATH was not set to ON by default - however this is no good reason to not enable it by default
project(bctoolbox)
project(bctoolbox VERSION 4.3.0)
set(BCTOOLBOX_VERSION_MAJOR 0)
set(BCTOOLBOX_VERSION_MINOR 6)
set(BCTOOLBOX_VERSION_PATCH 0)
set(BCTOOLBOX_VERSION "${PROJECT_VERSION}")
set(BCTOOLBOX_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(BCTOOLBOX_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(BCTOOLBOX_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(BCTOOLBOX_SO_VERSION 1)
set(BCTOOLBOXTESTER_SO_VERSION 1)
set(BCTOOLBOX_VERSION "${BCTOOLBOX_VERSION_MAJOR}.${BCTOOLBOX_VERSION_MINOR}.${BCTOOLBOX_VERSION_PATCH}")
set(PROJECT_VERSION_MAJOR ${BCTOOLBOX_VERSION_MAJOR})
set(PROJECT_VERSION_MINOR ${BCTOOLBOX_VERSION_MINOR})
set(PROJECT_VERSION_PATCH ${BCTOOLBOX_VERSION_PATCH})
set(PROJECT_VERSION "${BCTOOLBOX_VERSION}")
option(ENABLE_SHARED "Build shared library." ON)
option(ENABLE_STATIC "Build static library." ON)
......@@ -191,6 +186,8 @@ install(EXPORT ${EXPORT_TARGETS_NAME}Targets
)
install(FILES
"cmake/MakeArchive.cmake"
"cmake/ConfigureSpecfile.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}ConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}CMakeUtils.cmake"
......
############################################################################
# CMakeLists.txt
# Copyright (C) 2016 Belledonne Communications, Grenoble France
# Copyright (C) 2010-2019 Belledonne Communications, Grenoble France
#
############################################################################
#
......@@ -20,36 +20,18 @@
#
############################################################################
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/BcToolboxCMakeUtils.cmake)
set(BCTOOLBOX_CMAKE_DIR "${PROJECT_SOURCE_DIR}/cmake")
set(BCTOOLBOX_CMAKE_UTILS "${BCTOOLBOX_CMAKE_DIR}/BcToolboxCMakeUtils.cmake")
include("${BCTOOLBOX_CMAKE_DIR}/BcToolboxCMakeUtils.cmake")
if(NOT CPACK_PACKAGE_NAME)
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
endif()
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/../COPYING")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${PROJECT_VERSION})
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_IGNORE_FILES
"^${CMAKE_BINARY_DIR}"
"^${PROJECT_SOURCE_DIR}/.git*"
)
bc_project_build_version(${PROJECT_VERSION} PROJECT_VERSION_BUILD)
if(PROJECT_VERSION_BUILD)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${PROJECT_VERSION_BUILD}")
endif()
message("-- Package file name is ${CPACK_PACKAGE_FILE_NAME}" )
set(CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_FILE_NAME})
bc_generate_rpm_specfile("rpm/bctoolbox.spec.cmake" "${PROJECT_SOURCE_DIR}/bctoolbox.spec")
include(CPack)
bc_make_package_source_target()
......@@ -2,6 +2,7 @@
%define _prefix @CMAKE_INSTALL_PREFIX@
%define pkg_prefix @BC_PACKAGE_NAME_PREFIX@
%define package_name @CPACK_PACKAGE_NAME@-${FULL_VERSION}
# re-define some directories for older RPMBuild versions which don't. This messes up the doc/ dir
# taken from https://fedoraproject.org/wiki/Packaging:RPMMacros?rd=Packaging/RPMMacros
......@@ -9,22 +10,16 @@
%define _datadir %{_datarootdir}
%define _docdir %{_datadir}/doc
%define build_number @PROJECT_VERSION_BUILD@
%if %{build_number}
%define build_number_ext -%{build_number}
%endif
Name: @CPACK_PACKAGE_NAME@
Version: @PROJECT_VERSION@
Release: %{build_number}%{?dist}
Summary: Belr is language recognition library for ABNF based protocols.
Version: ${RPM_VERSION}
Release: ${RPM_RELEASE}%{?dist}
Summary: OS abstraction layer and utilities
Group: Applications/Communications
License: GPL
URL: http://www.linphone.org
Source0: %{name}-%{version}%{?build_number_ext}.tar.gz
Source0: %{package_name}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
Requires: %{pkg_prefix}bctoolbox
......@@ -54,7 +49,7 @@ Libraries and headers required to develop software with bctoolbox
%custom_debug_package
%prep
%setup -n %{name}-%{version}%{?build_number_ext}
%setup -n %{package_name}
%build
%{expand:%%%cmake_name} . -DCMAKE_BUILD_TYPE=@CMAKE_BUILD_TYPE@ -DCMAKE_PREFIX_PATH:PATH=%{_prefix} @RPM_ALL_CMAKE_OPTIONS@
......
############################################################################
# BcToolboxCMakeUtils.cmake
# Copyright (C) 2017 Belledonne Communications, Grenoble France
# Copyright (C) 2010-2019 Belledonne Communications, Grenoble France
#
############################################################################
#
......@@ -22,6 +22,10 @@
set(BCTOOLBOX_CMAKE_UTILS_DIR "${CMAKE_CURRENT_LIST_DIR}")
macro(bc_check_git)
find_program(GIT_EXECUTABLE git NAMES Git CMAKE_FIND_ROOT_PATH_BOTH)
endmacro()
macro(bc_init_compilation_flags CPP_FLAGS C_FLAGS CXX_FLAGS STRICT_COMPILATION)
set(${CPP_FLAGS} )
set(${C_FLAGS} )
......@@ -73,6 +77,7 @@ macro(bc_git_version PROJECT_NAME PROJECT_VERSION)
endmacro()
# TODO remove this macro
macro(bc_project_build_version PROJECT_VERSION PROJECT_BUILD_VERSION)
find_program (WC wc)
......@@ -128,3 +133,109 @@ macro(bc_generate_rpm_specfile SOURCE DEST)
unset(_variableNames)
endif()
endmacro()
function(bc_parse_full_version version major minor patch)
if ("${version}" MATCHES "^(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$")
set(${major} "${CMAKE_MATCH_1}" PARENT_SCOPE)
set(${minor} "${CMAKE_MATCH_2}" PARENT_SCOPE)
set(${patch} "${CMAKE_MATCH_3}" PARENT_SCOPE)
if (ARGC GREATER 4)
set(${ARGV4} "${CMAKE_MATCH_4}" PARENT_SCOPE)
endif()
if (ARGC GREATER 5)
set(${ARGV5} "${CMAKE_MATCH_5}" PARENT_SCOPE)
endif()
else()
message(FATAL_ERROR "invalid full version '${version}'")
endif()
endfunction()
function(bc_compute_full_version OUTPUT_VERSION)
bc_check_git()
if(GIT_EXECUTABLE)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "describe" "--abbrev=0"
OUTPUT_VARIABLE GIT_OUTPUT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
if (DEFINED GIT_OUTPUT_VERSION)
set(output_version "${GIT_OUTPUT_VERSION}")
bc_compute_commits_count_since_latest_tag(${GIT_OUTPUT_VERSION} COMMIT_COUNT)
if (NOT ${COMMIT_COUNT} STREQUAL "0")
execute_process(
COMMAND "${GIT_EXECUTABLE}" "rev-parse" "--short" "HEAD"
OUTPUT_VARIABLE COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
string(APPEND output_version ".${COMMIT_COUNT}+${COMMIT_HASH}")
else()
#If commit count diff is 0, it means we are on the tag. Keep the version untouched
endif()
set(version_major )
set(version_minor )
set(version_patch )
bc_parse_full_version("${output_version}" version_major version_minor version_patch)
set(short_version "${version_major}.${version_minor}.${version_patch}")
if (PROJECT_VERSION AND NOT (short_version VERSION_EQUAL PROJECT_VERSION))
message(FATAL_ERROR "project and git version mismatch (project: '${PROJECT_VERSION}', git: '${short_version}')")
endif()
set(${OUTPUT_VERSION} "${output_version}" PARENT_SCOPE)
endif()
endif()
endfunction()
macro(bc_compute_commits_count_since_latest_tag LATEST_TAG OUTPUT_COMMITS_COUNT)
bc_check_git()
if(GIT_EXECUTABLE)
execute_process(
COMMAND "${GIT_EXECUTABLE}" "rev-list" "${LATEST_TAG}..HEAD" "--count"
OUTPUT_VARIABLE ${OUTPUT_COMMITS_COUNT}
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
endif()
endmacro()
function(bc_make_package_source_target)
set(basename "")
string(TOLOWER "${CMAKE_PROJECT_NAME}" basename)
if (DEFINED BC_SPECFILE_NAME)
set(specfile_name "${BC_SPECFILE_NAME}")
else()
set(specfile_name "${basename}.spec")
endif()
set(specfile_target "${basename}-rpm-spec")
bc_generate_rpm_specfile("rpm/${specfile_name}.cmake" "rpm/${specfile_name}.cmake")
add_custom_target(${specfile_target}
COMMAND ${CMAKE_COMMAND}
"-DPROJECT_VERSION=${PROJECT_VERSION}"
"-DBCTOOLBOX_CMAKE_UTILS=${BCTOOLBOX_CMAKE_UTILS}"
"-DSRC=${CMAKE_CURRENT_BINARY_DIR}/rpm/${specfile_name}.cmake"
"-DDEST=${PROJECT_SOURCE_DIR}/${specfile_name}"
-P "${BCTOOLBOX_CMAKE_DIR}/ConfigureSpecfile.cmake"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
BYPRODUCTS "${PROJECT_SOURCE_DIR}/${specfile_name}"
)
add_custom_target(package_source
COMMAND ${CMAKE_COMMAND}
"-DBCTOOLBOX_CMAKE_UTILS=${BCTOOLBOX_CMAKE_UTILS}"
"-DPROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}"
"-DPROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}"
"-DPROJECT_VERSION=${PROJECT_VERSION}"
"-DCPACK_PACKAGE_NAME=${CPACK_PACKAGE_NAME}"
"-DEXCLUDE_PATTERNS='${CPACK_SOURCE_IGNORE_FILES}'"
-P "${BCTOOLBOX_CMAKE_DIR}/MakeArchive.cmake"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
DEPENDS ${specfile_target}
)
endfunction()
############################################################################
# BcToolboxConfig.cmake
# Copyright (C) 2015 Belledonne Communications, Grenoble France
# Copyright (C) 2010-2019 Belledonne Communications, Grenoble France
#
############################################################################
#
......@@ -93,4 +93,6 @@ foreach(comp ${bctoolbox_FIND_COMPONENTS})
endif()
endforeach()
include("${CMAKE_CURRENT_LIST_DIR}/bctoolboxCMakeUtils.cmake")
set(BCTOOLBOX_CMAKE_DIR "${CMAKE_CURRENT_LIST_DIR}")
set(BCTOOLBOX_CMAKE_UTILS "${CMAKE_CURRENT_LIST_DIR}/bctoolboxCMakeUtils.cmake")
include("${BCTOOLBOX_CMAKE_UTILS}")
############################################################################
# ConfigureSpecfile.cmake
# Copyright (C) 2010-2019 Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
############################################################################
include("${BCTOOLBOX_CMAKE_UTILS}")
set(FULL_VERSION )
bc_compute_full_version(FULL_VERSION)
set(version_major )
set(version_minor )
set(version_patch )
set(identifiers )
set(metadata )
bc_parse_full_version(FULL_VERSION version_major version_minor version_patch identifiers metadata)
set(RPM_VERSION ${version_major}.${version_minor}.${version_patch})
if (NOT identifiers)
set(RPM_RELEASE 1)
else()
string(SUBSTRING "${identifiers}" 1 -1 identifiers)
set(RPM_RELEASE "0.${identifiers}${metadata}")
endif()
configure_file("${SRC}" "${DEST}")
############################################################################
# MakeArchive.cmake
# Copyright (C) 2010-2019 Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
############################################################################
include("${BCTOOLBOX_CMAKE_UTILS}")
bc_compute_full_version(version)
set(archive_name "${CPACK_PACKAGE_NAME}-${version}")
set(archive_path "${PROJECT_BINARY_DIR}/${archive_name}.tar.gz")
find_program(TAR tar)
set(EXCLUDE_ARGS )
foreach (pattern ${EXCLUDE_PATTERNS})
list(APPEND EXCLUDE_ARGS "--exclude=${pattern}")
endforeach()
execute_process(COMMAND ${TAR} -C "${PROJECT_SOURCE_DIR}" -cz -f "${archive_path}" "--transform" "s,^\\.,${archive_name}," ${EXCLUDE_ARGS} .)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment