Browse Source

libopus: fix clang-cl build (#8101)

* libopus: fix clang-cl build

* add check

* update architecture pattern
star9029 1 day ago
parent
commit
6d14ed635f

+ 47 - 0
packages/l/libopus/patches/1.5.2/add-sse4.1-flag-when-using-clang-cl.patch

@@ -0,0 +1,47 @@
+Backported from https://github.com/xiph/opus/commit/51b99f6d3c6f455ec32cb9c323ccc0ecd9d4e8c5
+Add sse4.1 flag when using clang-cl on MSVC targets
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 8535ad5f..702a7ec8 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -14,6 +14,14 @@ include(GNUInstallDirs)
+ include(CMakeDependentOption)
+ include(FeatureSummary)
+ 
++# Detect clang-cl
++set(MSVC_CLANG_CL false BOOL)
++if (MSVC)
++    if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
++        set(MSVC_CLANG_CL true BOOL)
++    endif()
++endif()
++
+ set(OPUS_BUILD_SHARED_LIBRARY_HELP_STR "build shared library.")
+ option(OPUS_BUILD_SHARED_LIBRARY ${OPUS_BUILD_SHARED_LIBRARY_HELP_STR} OFF)
+ if(OPUS_BUILD_SHARED_LIBRARY OR BUILD_SHARED_LIBS OR OPUS_BUILD_FRAMEWORK)
+@@ -484,20 +492,20 @@ if(NOT OPUS_DISABLE_INTRINSICS)
+         add_sources_group(opus lpcnet ${dnn_sources_sse4_1})
+       endif()
+       target_compile_definitions(opus PRIVATE OPUS_X86_MAY_HAVE_SSE4_1)
+-      if(NOT MSVC)
++      if(NOT MSVC OR MSVC_CLANG_CL)
+         set_source_files_properties(${celt_sources_sse4_1} ${silk_sources_sse4_1} ${dnn_sources_sse4_1} PROPERTIES COMPILE_FLAGS -msse4.1)
+       endif()
+ 
+       if(OPUS_FIXED_POINT)
+         add_sources_group(opus silk ${silk_sources_fixed_sse4_1})
+-        if(NOT MSVC)
++        if(NOT MSVC OR MSVC_CLANG_CL)
+           set_source_files_properties(${silk_sources_fixed_sse4_1} PROPERTIES COMPILE_FLAGS -msse4.1)
+         endif()
+       endif()
+     endif()
+     if(OPUS_X86_PRESUME_SSE4_1)
+       target_compile_definitions(opus PRIVATE OPUS_X86_PRESUME_SSE4_1)
+-      if(NOT MSVC)
++      if(NOT MSVC OR MSVC_CLANG_CL)
+         target_compile_options(opus PRIVATE -msse4.1)
+       endif()
+     endif()
+ 

+ 46 - 13
packages/l/libopus/xmake.lua

@@ -1,12 +1,11 @@
 package("libopus")
     set_homepage("https://opus-codec.org")
     set_description("Modern audio compression for the internet.")
+    set_license("BSD-3-Clause")
 
-    add_urls("https://downloads.xiph.org/releases/opus/opus-$(version).tar.gz",
-             {alias = "home"})
-    add_urls("https://github.com/xiph/opus/archive/refs/tags/v$(version).tar.gz",
-             "https://github.com/xiph/opus.git",
-             {alias = "github"})
+    add_urls("https://github.com/xiph/opus/archive/refs/tags/$(version).tar.gz", {alias = "github"})
+    add_urls("https://github.com/xiph/opus.git", {alias = "git"})
+    add_urls("https://downloads.xiph.org/releases/opus/opus-$(version).tar.gz", {alias = "home"})
 
     add_versions("home:1.5.2", "65c1d2f78b9f2fb20082c38cbe47c951ad5839345876e46941612ee87f9a7ce1")
     add_versions("home:1.5.1", "b84610959b8d417b611aa12a22565e0a3732097c6389d19098d844543e340f85")
@@ -14,12 +13,15 @@ package("libopus")
     add_versions("home:1.4", "c9b32b4253be5ae63d1ff16eea06b94b5f0f2951b7a02aceef58e3a3ce49c51f")
     add_versions("home:1.3.1", "65b58e1e25b2a114157014736a3d9dfeaad8d41be1c8179866f144a2fb44ff9d")
 
-    add_versions("github:1.5.2", "9480e329e989f70d69886ded470c7f8cfe6c0667cc4196d4837ac9e668fb7404")
-    add_versions("github:1.5.1", "7ce44ef3d335a3268f26be7d53bb3bed7205b34eaf80bf92a99e69d490afe9d9")
-    add_versions("github:1.5", "d7de528957dde0ba40e9dec9e25b679232bfaf19fb6a02ed8358845007d7075e")
-    add_versions("github:1.4", "659e6b223e42a51b0a898632b9a5f406ccd5c2e00aa526ddd1264789774b94e5")
-    add_versions("github:1.3.1", "4834a8944c33a7ecab5cad9454eeabe4680ca1842cb8f5a2437572dbf636de8f")
+    add_versions("github:v1.5.2", "9480e329e989f70d69886ded470c7f8cfe6c0667cc4196d4837ac9e668fb7404")
+    add_versions("github:v1.5.1", "7ce44ef3d335a3268f26be7d53bb3bed7205b34eaf80bf92a99e69d490afe9d9")
+    add_versions("github:v1.5", "d7de528957dde0ba40e9dec9e25b679232bfaf19fb6a02ed8358845007d7075e")
+    add_versions("github:v1.4", "659e6b223e42a51b0a898632b9a5f406ccd5c2e00aa526ddd1264789774b94e5")
+    add_versions("github:v1.3.1", "4834a8944c33a7ecab5cad9454eeabe4680ca1842cb8f5a2437572dbf636de8f")
 
+    add_versions("git:1.5.2", "v1.5.2")
+
+    add_patches("1.5.2", path.join(os.scriptdir(), "patches", "1.5.2", "add-sse4.1-flag-when-using-clang-cl.patch"), "a5810124b43a3a80dc311192a49027869bcd244859a42f8338d62ae0525ab45b")
     add_patches("1.3.1", path.join(os.scriptdir(), "patches", "1.3.1", "cmake.patch"), "79fba5086d7747d0441f7f156b88e932b662e2d2ccd825279a5a396a2840d3a2")
 
     add_configs("avx", { description = "AVX supported", default = true, type = "boolean" })
@@ -29,16 +31,47 @@ package("libopus")
         add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
     end
 
+    if is_plat("mingw") and is_subhost("msys") then
+        add_extsources("pacman::opus")
+    elseif is_plat("linux") then
+        add_extsources("pacman::opus", "apt::libopus-dev")
+    elseif is_plat("macosx") then
+        add_extsources("brew::opus")
+    end
+
     add_deps("cmake")
 
-    on_install("windows", "linux", "macosx", "iphoneos", "mingw", "android", "wasm", function (package)
+    if on_check then
+        on_check(function (package)
+            local version = package:version()
+            if not version then
+                return
+            end
+
+            if package:is_plat("linux", "cross") and package:is_arch("arm.*") then
+                if version:le("1.4") then
+                    raise("package(libopus 1.4) unsupported arch")
+                end
+            end
+
+            if (package:is_plat("android") and package:is_arch("armeabi-v7a")) or package:is_plat("wasm") then
+                if version:eq("1.3.1") then
+                    raise("package(libopus 1.3.1) unsupported platform")
+                end
+            end
+        end)
+    end
+
+    on_install(function (package)
         io.replace("CMakeLists.txt", [[set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")]], "", {plain = true})
         io.replace("CMakeLists.txt", [[set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")]], "", {plain = true})
 
         local configs = {}
-        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
+        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
         table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
-        table.insert(configs, "-DCMAKE_POSITION_INDEPENDENT_CODE=ON")
+        if not package:gitref() and package:version() then
+            table.insert(configs, "-DOPUS_PACKAGE_VERSION=" .. package:version():shortstr())
+        end
         table.insert(configs, "-DAVX_SUPPORTED=" .. (package:config("avx") and "ON" or "OFF"))
         table.insert(configs, "-DOPUS_X86_MAY_HAVE_AVX=" .. (package:config("check_avx") and "ON" or "OFF"))
         if package:is_plat("mingw", "wasm") then