shithub: opus

Download patch

ref: 8ad827c3ff99f08b4087cf47637c722020d3c958
parent: 5edb5aacba5c6941b66ef48002f47fbe1c3f39b7
author: Marcus Asteborg <maastebo@microsoft.com>
date: Wed Jun 24 17:25:57 EDT 2020

cmake - fix bugs around consuming Opus as a submodule in cmake and package version parsing

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,23 +1,12 @@
 cmake_minimum_required(VERSION 3.1)
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
 
-include(OpusFunctions)
+include(OpusPackageVersion)
+get_package_version(PACKAGE_VERSION PROJECT_VERSION)
 
-get_library_version(OPUS_LIBRARY_VERSION OPUS_LIBRARY_VERSION_MAJOR)
-message(STATUS "Opus library version: ${OPUS_LIBRARY_VERSION}")
-
-get_package_version(PACKAGE_VERSION)
-message(STATUS "Opus package version: ${PACKAGE_VERSION}")
-
-string(REGEX
-       REPLACE "^([0-9]+.[0-9]+\\.?([0-9]+)?).*"
-               "\\1"
-               PROJECT_VERSION
-               ${PACKAGE_VERSION})
-message(STATUS "Opus project version: ${PROJECT_VERSION}")
-
 project(Opus LANGUAGES C VERSION ${PROJECT_VERSION})
 
+include(OpusFunctions)
 include(OpusBuildtype)
 include(OpusConfig)
 include(OpusSources)
@@ -228,6 +217,9 @@
 add_library(opus ${opus_headers} ${opus_sources} ${opus_sources_float} ${Opus_PUBLIC_HEADER})
 add_library(Opus::opus ALIAS opus)
 
+get_library_version(OPUS_LIBRARY_VERSION OPUS_LIBRARY_VERSION_MAJOR)
+message(STATUS "Opus library version: ${OPUS_LIBRARY_VERSION}")
+
 set_target_properties(opus
                       PROPERTIES SOVERSION
                                  ${OPUS_LIBRARY_VERSION_MAJOR}
@@ -490,7 +482,7 @@
   include(CMakePackageConfigHelpers)
 
   set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
-  configure_package_config_file(${CMAKE_SOURCE_DIR}/cmake/OpusConfig.cmake.in
+  configure_package_config_file(${PROJECT_SOURCE_DIR}/cmake/OpusConfig.cmake.in
                                 OpusConfig.cmake
                                 INSTALL_DESTINATION
                                 ${CMAKE_INSTALL_PACKAGEDIR}
--- a/Makefile.am
+++ b/Makefile.am
@@ -220,6 +220,7 @@
              cmake/OpusConfig.cmake \
              cmake/OpusConfig.cmake.in \
              cmake/OpusFunctions.cmake \
+             cmake/OpusPackageVersion.cmake \
              cmake/OpusSources.cmake \
              cmake/config.h.cmake.in \
              cmake/vla.c \
--- a/cmake/OpusConfig.cmake
+++ b/cmake/OpusConfig.cmake
@@ -5,7 +5,7 @@
 
 include(OpusFunctions)
 
-configure_file(${CMAKE_SOURCE_DIR}/cmake/config.h.cmake.in config.h @ONLY)
+configure_file(cmake/config.h.cmake.in config.h @ONLY)
 add_definitions(-DHAVE_CONFIG_H)
 
 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
@@ -89,4 +89,4 @@
       add_compile_options(${WARNING_FLAG})
     endif()
   endforeach()
-endif()
\ No newline at end of file
+endif()
--- a/cmake/OpusFunctions.cmake
+++ b/cmake/OpusFunctions.cmake
@@ -41,57 +41,6 @@
   set(OPUS_LIBRARY_VERSION_MAJOR ${OPUS_LIBRARY_VERSION_MAJOR} PARENT_SCOPE)
 endfunction()
 
-function(get_package_version PACKAGE_VERSION)
-  find_package(Git)
-  if(Git_FOUND AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
-    execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "v*"
-                    OUTPUT_VARIABLE OPUS_PACKAGE_VERSION)
-    if(OPUS_PACKAGE_VERSION)
-      string(STRIP ${OPUS_PACKAGE_VERSION}, OPUS_PACKAGE_VERSION)
-      string(REPLACE \n
-                     ""
-                     OPUS_PACKAGE_VERSION
-                     ${OPUS_PACKAGE_VERSION})
-      string(REPLACE ,
-                     ""
-                     OPUS_PACKAGE_VERSION
-                     ${OPUS_PACKAGE_VERSION})
-
-      string(SUBSTRING ${OPUS_PACKAGE_VERSION}
-                       1
-                       -1
-                       OPUS_PACKAGE_VERSION)
-      set(PACKAGE_VERSION ${OPUS_PACKAGE_VERSION} PARENT_SCOPE)
-      return()
-    endif()
-  endif()
-
-  if(EXISTS "${CMAKE_SOURCE_DIR}/package_version")
-    # Not a git repo, lets' try to parse it from package_version file if exists
-    file(STRINGS package_version opus_package_version_string
-         LIMIT_COUNT 1
-         REGEX "PACKAGE_VERSION=")
-    string(REPLACE "PACKAGE_VERSION="
-                   ""
-                   opus_package_version_string
-                   ${opus_package_version_string})
-    string(REPLACE "\""
-                   ""
-                   opus_package_version_string
-                   ${opus_package_version_string})
-    # In case we have a unknown dist here we just replace it with 0
-    string(REPLACE "unknown"
-                   "0"
-                   opus_package_version_string
-                   ${opus_package_version_string})
-    set(PACKAGE_VERSION ${opus_package_version_string} PARENT_SCOPE)
-    return()
-  endif()
-
-  # if all else fails set to 0
-  set(PACKAGE_VERSION 0 PARENT_SCOPE)
-endfunction()
-
 function(check_flag NAME FLAG)
   include(CheckCCompilerFlag)
   check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)
--- /dev/null
+++ b/cmake/OpusPackageVersion.cmake
@@ -1,0 +1,70 @@
+if(__opus_version)
+  return()
+endif()
+set(__opus_version INCLUDED)
+
+function(get_package_version PACKAGE_VERSION PROJECT_VERSION)
+
+  find_package(Git)
+  if(GIT_FOUND AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
+    execute_process(COMMAND ${GIT_EXECUTABLE}
+                    --git-dir=${CMAKE_CURRENT_LIST_DIR}/.git describe
+                    --tags --match "v*" OUTPUT_VARIABLE OPUS_PACKAGE_VERSION)
+    if(OPUS_PACKAGE_VERSION)
+      string(STRIP ${OPUS_PACKAGE_VERSION}, OPUS_PACKAGE_VERSION)
+      string(REPLACE \n
+                     ""
+                     OPUS_PACKAGE_VERSION
+                     ${OPUS_PACKAGE_VERSION})
+      string(REPLACE ,
+                     ""
+                     OPUS_PACKAGE_VERSION
+                     ${OPUS_PACKAGE_VERSION})
+
+      string(SUBSTRING ${OPUS_PACKAGE_VERSION}
+                       1
+                       -1
+                       OPUS_PACKAGE_VERSION)
+      message(STATUS "Opus package version from git repo: ${OPUS_PACKAGE_VERSION}")
+    endif()
+
+  elseif(EXISTS "${CMAKE_CURRENT_LIST_DIR}/package_version"
+         AND NOT OPUS_PACKAGE_VERSION)
+    # Not a git repo, lets' try to parse it from package_version file if exists
+    file(STRINGS package_version OPUS_PACKAGE_VERSION
+         LIMIT_COUNT 1
+         REGEX "PACKAGE_VERSION=")
+    string(REPLACE "PACKAGE_VERSION="
+                   ""
+                   OPUS_PACKAGE_VERSION
+                   ${OPUS_PACKAGE_VERSION})
+    string(REPLACE "\""
+                   ""
+                   OPUS_PACKAGE_VERSION
+                   ${OPUS_PACKAGE_VERSION})
+    # In case we have a unknown dist here we just replace it with 0
+    string(REPLACE "unknown"
+                   "0"
+                   OPUS_PACKAGE_VERSION
+                   ${OPUS_PACKAGE_VERSION})
+      message(STATUS "Opus package version from package_version file: ${OPUS_PACKAGE_VERSION}")
+  endif()
+
+  if(OPUS_PACKAGE_VERSION)
+    string(REGEX
+      REPLACE "^([0-9]+.[0-9]+\\.?([0-9]+)?).*"
+               "\\1"
+               OPUS_PROJECT_VERSION
+               ${OPUS_PACKAGE_VERSION})
+  else()
+    # fail to parse version from git and package version
+    message(WARNING "Could not get package version.")
+    set(OPUS_PACKAGE_VERSION 0)
+    set(OPUS_PROJECT_VERSION 0)
+  endif()
+
+  message(STATUS "Opus project version: ${OPUS_PROJECT_VERSION}")
+
+  set(PACKAGE_VERSION ${OPUS_PACKAGE_VERSION} PARENT_SCOPE)
+  set(PROJECT_VERSION ${OPUS_PROJECT_VERSION} PARENT_SCOPE)
+endfunction()