Repository: conan-io/conan-center-index

Issue:
[request] liquid-dsp/latest (help needed)
### Package Details
  * Package Name/Version: **liquid-dsp/latest**
  * Website: **https://github.com/jgaeddert/liquid-dsp**
  * Source code: **https://github.com/jgaeddert/liquid-dsp**


### Description Of The Library / Tool

So, this is an amazing DSP library, with a generous library.

I could do the recipe but I need some help: 
- It uses autotools;
- It uses C99 `<complex.h>` so it doesn't build with MSVC. 

However, since, this a C library, it can easily be linked with MSVC.  It's possible to build it with msys2/mingw-64 and then generate a "link library" for MSVC... 
Can this workflow be made with Conan? I mean, if the compiler is MSVC, have msys2/mingw-64 as a dependency and build it with it, and then generate a link library with MSVC, and etc? 


Discussion:
@ruilvo I think you are looking for something like https://github.com/conan-io/conan-center-index/blob/c916710e54d587ba27f694a4678e7655a307ab76/recipes/re2c/all/conanfile.py#L43
I don't think so, because the library does *not* build with MSVC because of the C99 <complex.h> usage that MSVC does not have.
MSVC is C11 and C17 compliant but not C99 compliant. (yeah, go figure)

Resolving patch:
diff --git a/recipes/liquid-dsp/all/conanfile.py b/recipes/liquid-dsp/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/liquid-dsp/all/conanfile.py
@@ -0,0 +1,172 @@
+from conans import ConanFile, tools
+from contextlib import contextmanager
+import os
+
+
+class LiquidDspConan(ConanFile):
+    name = "liquid-dsp"
+    description = (
+        "Digital signal processing library for software-defined radios (and more)"
+    )
+    topics = ("conan", "dsp", "sdr", "liquid-dsp")
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/jgaeddert/liquid-dsp"
+    license = ("MIT",)
+    settings = "os", "arch", "build_type", "compiler"
+    exports_sources = ["generate_link_library.bat", "patches/**"]
+    options = {
+        "shared": [True, False],
+        "simdoverride": [True, False],
+    }
+    default_options = {
+        "shared": False,
+        "simdoverride": False,
+    }
+
+    _autotools = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _libname(self):
+        if self.settings.os == "Windows":
+            return "libliquid"
+        return "liquid"
+
+    @property
+    def _target_name(self):
+        if self.settings.os == "Macos":
+            if not self.options.shared:
+                return "libliquid.ar"
+            return "libliquid.dylib"
+        if not self.options.shared:
+            return "libliquid.a"
+        return "libliquid.so"
+
+    @property
+    def _lib_pattern(self):
+        if self.settings.os == "Macos" and not self.options.shared:
+            return "libliquid.a"
+        if self.settings.os != "Windows":
+            return self._target_name
+        return "libliquid.lib"
+
+    def configure(self):
+        del self.settings.compiler.cppstd
+        del self.settings.compiler.libcxx
+
+    def build_requirements(self):
+        if tools.os_info.is_windows and not tools.get_env("CONAN_BASH_PATH"):
+            self.build_requires("msys2/cci.latest")
+        if self.settings.compiler == "Visual Studio":
+            self.build_requires("mingw-w64/8.1")
+            self.build_requires("automake/1.16.3")
+
+    def source(self):
+        tools.get(
+            **self.conan_data["sources"][self.version],
+            destination=self._source_subfolder,
+            strip_root=True,
+        )
+
+    def _patch_sources(self):
+        if self.settings.os == "Windows":
+            for patch in self.conan_data["patches"][self.version]:
+                tools.patch(**patch)
+
+    def _gen_link_library(self):
+        if self.settings.compiler != "Visual Studio" or (not self.options.shared):
+            return
+        self.run("cmd /c generate_link_library.bat")
+        with tools.chdir(self._source_subfolder):
+            self.run(
+                "{} /def:libliquid.def /out:libliquid.lib /machine:{}".format(
+                    os.getenv("AR"), "X86" if self.settings.arch == "x86" else "X64"
+                ),
+                win_bash=tools.os_info.is_windows,
+            )
+
+    def _rename_libraries(self):
+        with tools.chdir(self._source_subfolder):
+            if self.settings.os == "Windows" and self.options.shared:
+                tools.rename("libliquid.so", "libliquid.dll")
+            elif self.settings.os == "Windows" and not self.options.shared:
+                tools.rename("libliquid.a", "libliquid.lib")
+            elif self.settings.os == "Macos" and not self.options.shared:
+                tools.rename("libliquid.ar", "libliquid.a")
+
+    @contextmanager
+    def _build_context(self):
+        if self.settings.compiler == "Visual Studio":
+            env = {
+                "CC": "gcc",
+                "CXX": "g++",
+                "LD": "ld",
+                "AR": "ar",
+            }
+            with tools.environment_append(env):
+                yield
+        else:
+            yield
+
+    @contextmanager
+    def _msvc_context(self):
+        if self.settings.compiler == "Visual Studio":
+            with tools.vcvars(self.settings):
+                env = {
+                    "CC": "cl -nologo",
+                    "CXX": "cl -nologo",
+                    "AR": "lib",
+                    "LD": "link",
+                }
+                with tools.environment_append(env):
+                    yield
+        else:
+            yield
+
+    def build(self):
+        self._patch_sources()
+        ncpus = tools.cpu_count()
+        configure_args = []
+        cflags = ["-static-libgcc"]
+        if self.settings.build_type == "Debug":
+            configure_args.append("--enable-debug-messages")
+            cflags.extend(["-g", "-O0"])
+        else:
+            cflags.extend(["-s", "-O2", "-DNDEBUG"])
+        if self.options.simdoverride:
+            configure_args.append("--enable-simdoverride")
+        if self.settings.compiler == "Visual Studio":
+            configure_args.append("CFLAGS='{}'".format(" ".join(cflags)))
+        configure_args_str = " ".join(configure_args)
+        with self._build_context():
+            with tools.chdir(self._source_subfolder):
+                self.run("./bootstrap.sh", win_bash=tools.os_info.is_windows)
+                self.run(
+                    "./configure {}".format(configure_args_str),
+                    win_bash=tools.os_info.is_windows,
+                )
+                self.run(
+                    "make {} -j{}".format(self._target_name, ncpus),
+                    win_bash=tools.os_info.is_windows,
+                )
+        self._rename_libraries()
+        with self._msvc_context():
+            self._gen_link_library()
+
+    def package(self):
+        self.copy(pattern="LICENSE", src=self._source_subfolder, dst="licenses")
+        self.copy(
+            pattern="liquid.h",
+            dst=os.path.join("include", "liquid"),
+            src=os.path.join(self._source_subfolder, "include"),
+        )
+        self.copy(pattern="libliquid.dll", dst="bin", src=self._source_subfolder)
+        self.copy(pattern=self._lib_pattern, dst="lib", src=self._source_subfolder)
+
+    def package_info(self):
+        self.cpp_info.libs = [self._libname]
+        if self.settings.os == "Linux":
+            self.cpp_info.system_libs.append("m")


Test patch:
diff --git a/recipes/liquid-dsp/all/test_package/CMakeLists.txt b/recipes/liquid-dsp/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/liquid-dsp/all/test_package/CMakeLists.txt
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 3.0)
+project(test_package)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+add_executable(${CMAKE_PROJECT_NAME} test_package.cpp)
+target_link_libraries(${CMAKE_PROJECT_NAME} ${CONAN_LIBS})
diff --git a/recipes/liquid-dsp/all/test_package/conanfile.py b/recipes/liquid-dsp/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/liquid-dsp/all/test_package/conanfile.py
@@ -0,0 +1,17 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self.settings):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)
diff --git a/recipes/liquid-dsp/all/test_package/test_package.cpp b/recipes/liquid-dsp/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/liquid-dsp/all/test_package/test_package.cpp
@@ -0,0 +1,9 @@
+#include "liquid/liquid.h"
+
+#include <iostream>
+
+int main(int argc, char **argv)
+{
+  std::cout << liquid_libversion();
+  return 0;
+}