shithub: opus

Download patch

ref: c350cec965dd316f8515a8e2b619cb4da12f8825
parent: d00d866e8c56d5a8acf764e34ce04dc54c790ac8
author: Marcus Asteborg <maastebo@microsoft.com>
date: Mon Apr 27 04:52:57 EDT 2020

cmake - move all compiler feature detection to OpusConfig

Signed-off-by: Mark Harris <mark.hsj@gmail.com>

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,7 +20,6 @@
 include(OpusBuildtype)
 
 option(OPUS_BUILD_SHARED_LIBRARY "Build shared library" OFF)
-option(OPUS_STACK_PROTECTOR "Use stack protection" ON)
 option(OPUS_CUSTOM_MODES "Enable non-Opus modes, e.g. 44.1 kHz & 2^n frames"
        OFF)
 option(OPUS_BUILD_PROGRAMS "Build programs" OFF)
@@ -66,16 +65,14 @@
 cmake_dependent_option(OPUS_FAST_MATH
                        "Enable fast math"
                        ON
-                       "OPUS_FLOAT_APPROX; OPUS_FAST_MATH"
+                       "OPUS_FLOAT_APPROX; OPUS_FAST_MATH; FAST_MATH_SUPPORTED"
                        OFF)
 
-if(OPUS_FAST_MATH)
-  if(MSVC)
-    check_and_set_flag(FAST_MATH /fp:fast)
-  else()
-    check_and_set_flag(FAST_MATH -ffast-math)
-  endif()
-endif()
+cmake_dependent_option(OPUS_STACK_PROTECTOR
+                       "Use stack protection"
+                       ON
+                       "STACK_PROTECTOR_SUPPORTED"
+                       OFF)
 
 if(OPUS_BUILD_SHARED_LIBRARY OR BUILD_SHARED_LIBS OR OPUS_BUILD_FRAMEWORK)
   # Global flag to cause add_library() to create shared libraries if on.
@@ -88,23 +85,6 @@
   set(BUILD_TESTING ON)
 endif()
 
-if(OPUS_STACK_PROTECTOR)
-  if(MSVC) # GC on by default on MSVC
-    check_and_set_flag(STACK_PROTECTOR /GS)
-  else()
-    check_and_set_flag(STACK_PROTECTOR -fstack-protector-strong)
-  endif()
-else()
-  if(MSVC)
-    check_and_set_flag(STACK_PROTECTOR_DISABLED /GS-)
-    if (STACK_PROTECTOR_DISABLED_SUPPORTED)
-      set(STACK_PROTECTOR_SUPPORTED OFF)
-    endif()
-  else()
-    set(STACK_PROTECTOR_SUPPORTED OFF)
-  endif()
-endif()
-
 if(OPUS_CPU_X86 OR OPUS_CPU_X64)
   cmake_dependent_option(OPUS_X86_MAY_HAVE_SSE
                          "Does runtime check for SSE1 support"
@@ -175,7 +155,7 @@
                        "required to set up package version")
 
 add_feature_info(OPUS_BUILD_SHARED_LIBRARY OPUS_BUILD_SHARED_LIBRARY "Build shared library")
-add_feature_info(OPUS_STACK_PROTECTOR STACK_PROTECTOR_SUPPORTED "Use stack protection")
+add_feature_info(OPUS_STACK_PROTECTOR OPUS_STACK_PROTECTOR "Use stack protection")
 add_feature_info(OPUS_VAR_ARRAYS OPUS_VAR_ARRAYS 
                  "Use variable length arrays for stack arrays")
 add_feature_info(OPUS_USE_ALLOCA OPUS_USE_ALLOCA
@@ -194,7 +174,7 @@
   "compile with the floating point API (for machines with float library)")
 add_feature_info(OPUS_FLOAT_APPROX OPUS_FLOAT_APPROX
   "Enable floating point approximations (Ensure your platform supports IEEE 754 before enabling)")
-add_feature_info(OPUS_FAST_MATH FAST_MATH_SUPPORTED "Enable fast math, (depending on OPUS_FLOAT_APPROX to be enabled)")
+add_feature_info(OPUS_FAST_MATH OPUS_FAST_MATH "Enable fast math, (depending on OPUS_FLOAT_APPROX to be enabled)")
 add_feature_info(OPUS_INSTALL_PKG_CONFIG_MODULE OPUS_INSTALL_PKG_CONFIG_MODULE
                  "install PkgConfig module")
 add_feature_info(OPUS_INSTALL_CMAKE_CONFIG_MODULE OPUS_INSTALL_CMAKE_CONFIG_MODULE
@@ -279,15 +259,29 @@
   target_compile_definitions(opus PRIVATE CUSTOM_MODES)
 endif()
 
+if(OPUS_FAST_MATH)
+  if(MSVC)
+    target_compile_options(opus PRIVATE /fp:fast)
+  else()
+    target_compile_options(opus PRIVATE -ffast-math)
+  endif()
+endif()
+
+if(OPUS_STACK_PROTECTOR)
+  if(MSVC)
+    target_compile_options(opus PRIVATE /GS)
+  else()
+    target_compile_options(opus PRIVATE -fstack-protector-strong)
+  endif()
+elseif(STACK_PROTECTOR_DISABLED_SUPPORTED)
+  target_compile_options(opus PRIVATE /GS-)
+endif()
+
 if(BUILD_SHARED_LIBS)
   if(WIN32)
     target_compile_definitions(opus PRIVATE DLL_EXPORT)
-  else()
-    include(CheckCCompilerFlag)
-    check_c_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
-    if(COMPILER_HAS_HIDDEN_VISIBILITY)
-      set_target_properties(opus PROPERTIES C_VISIBILITY_PRESET hidden)
-    endif()
+  elseif(HIDDEN_VISIBILITY_SUPPORTED)
+    set_target_properties(opus PROPERTIES C_VISIBILITY_PRESET hidden)
   endif()
 endif()
 
@@ -408,7 +402,6 @@
 endif()
 
 if(COMPILER_SUPPORT_NEON AND OPUS_USE_NEON)
-
   if(OPUS_MAY_HAVE_NEON)
     if(RUNTIME_CPU_CAPABILITY_DETECTION)
       message(STATUS "OPUS_MAY_HAVE_NEON enabling runtime detection")
--- a/cmake/OpusConfig.cmake
+++ b/cmake/OpusConfig.cmake
@@ -66,3 +66,13 @@
     endif()
   endif()
 endif()
+
+if(MSVC)
+  check_flag(FAST_MATH /fp:fast)
+  check_flag(STACK_PROTECTOR /GS)
+  check_flag(STACK_PROTECTOR_DISABLED /GS-)
+else()
+  check_flag(FAST_MATH -ffast-math)
+  check_flag(STACK_PROTECTOR -fstack-protector-strong)
+  check_flag(HIDDEN_VISIBILITY -fvisibility=hidden)
+endif()
--- a/cmake/OpusFunctions.cmake
+++ b/cmake/OpusFunctions.cmake
@@ -92,14 +92,6 @@
   set(PACKAGE_VERSION 0 PARENT_SCOPE)
 endfunction()
 
-function(check_and_set_flag NAME FLAG)
-  include(CheckCCompilerFlag)
-  check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)
-  if(${NAME}_SUPPORTED)
-    add_definitions(${FLAG})
-  endif()
-endfunction()
-
 function(check_flag NAME FLAG)
   include(CheckCCompilerFlag)
   check_c_compiler_flag(${FLAG} ${NAME}_SUPPORTED)