Repository: conan-io/conan-center-index

Issue:
[request] libyaml/0.2.5
### Package Details
  * Package Name/Version: **libyaml/0.2.5**
  * Website: **https://pyyaml.org/wiki/LibYAMLl**
  * Source code: **https://github.com/yaml/libyaml**

This is an existing conan-libyaml library at https://github.com/bincrafters/conan-libyaml which is at version 0.2.2, and the upstream version is 0.2.5. It would be nice the bincrafters recipe could be brought over to CCI.

### Description Of The Library / Tool

LibYAML is a YAML parser and emitter library.


Discussion:
Everyone can migrate Bincrafters' recipes. Just give credits within the PR to confirm with the MIT license (and to be respectful)

Contributors welcome 😄 

Resolving patch:
diff --git a/recipes/libyaml/all/conanfile.py b/recipes/libyaml/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/libyaml/all/conanfile.py
@@ -0,0 +1,72 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class LibYAMLConan(ConanFile):
+    name = "libyaml"
+    description = "LibYAML is a YAML parser and emitter library."
+    topics = ("conan", "libyaml", "yaml", "parser", "emitter")
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/yaml/libyaml"
+    license = "MIT"
+    exports_sources = ["CMakeLists.txt"]
+    generators = "cmake"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {"shared": [True, False], "fPIC": [True, False]}
+    default_options = {"shared": False, "fPIC": True}
+
+    _cmake = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _build_subfolder(self):
+        return "build_subfolder"
+
+    def config_options(self):
+        if self.settings.os == 'Windows':
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+        del self.settings.compiler.libcxx
+        del self.settings.compiler.cppstd
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version])
+        extracted_dir = '{}-{}'.format(self.name, self.version)
+        os.rename(extracted_dir, self._source_subfolder)
+
+    def _configure_cmake(self):
+        if self._cmake:
+            return self._cmake
+        self._cmake = CMake(self)
+        self._cmake.definitions["INSTALL_CMAKE_DIR"] = 'lib/cmake/libyaml'
+        self._cmake.definitions["YAML_STATIC_LIB_NAME"] = "yaml"
+        self._cmake.configure(build_folder=self._build_subfolder)
+        return self._cmake
+
+    def build(self):
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        # 0.2.2 has LICENSE, 0.2.5 has License, so ignore case
+        self.copy(pattern="License", dst="licenses",
+                  src=self._source_subfolder, ignore_case=True)
+        cmake = self._configure_cmake()
+        cmake.install()
+        tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
+
+    def package_info(self):
+        self.cpp_info.components["yaml"].libs = ["yaml"]
+        if self.settings.compiler == "Visual Studio":
+            self.cpp_info.components["yaml"].defines = [
+                "YAML_DECLARE_EXPORT" if self.options.shared
+                else "YAML_DECLARE_STATIC"
+            ]
+        self.cpp_info.names["cmake_find_package"] = "yaml"
+        self.cpp_info.names["cmake_find_package_multi"] = "yaml"


Test patch:
diff --git a/recipes/libyaml/all/test_package/CMakeLists.txt b/recipes/libyaml/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/libyaml/all/test_package/CMakeLists.txt
@@ -0,0 +1,10 @@
+cmake_minimum_required(VERSION 2.8.12)
+project(test_package)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+find_package(yaml REQUIRED)
+
+add_executable(${PROJECT_NAME} test_package.c)
+target_link_libraries(${PROJECT_NAME} yaml::yaml)
diff --git a/recipes/libyaml/all/test_package/conanfile.py b/recipes/libyaml/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/libyaml/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", "cmake_find_package"
+
+    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/libyaml/all/test_package/test_package.c b/recipes/libyaml/all/test_package/test_package.c
new file mode 100644
--- /dev/null
+++ b/recipes/libyaml/all/test_package/test_package.c
@@ -0,0 +1,101 @@
+#include <yaml.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct fruit {
+    char *name;
+    char *color;
+    int count;
+};
+struct fruit data[] = {
+    {"apple", "red", 12},
+    {"orange", "orange", 3},
+    {"banana", "yellow", 4},
+    {"mango", "green", 1},
+    {NULL, NULL, 0}
+};
+
+int main(void)
+{
+    yaml_emitter_t emitter;
+    yaml_event_t event;
+    struct fruit *f;
+    char buffer[64];
+
+    yaml_emitter_initialize(&emitter);
+    yaml_emitter_set_output_file(&emitter, stdout);
+
+    yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
+        1, YAML_ANY_MAPPING_STYLE);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
+        (yaml_char_t *)"fruit", strlen("fruit"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
+       1, YAML_ANY_SEQUENCE_STYLE);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    for (f = data; f->name; f++) {
+        yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
+            1, YAML_ANY_MAPPING_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
+            (yaml_char_t *)"name", strlen("name"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
+            (yaml_char_t *)f->name, strlen(f->name), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
+            (yaml_char_t *)"color", strlen("color"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
+            (yaml_char_t *)f->color, strlen(f->color), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
+            (yaml_char_t *)"count", strlen("count"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        sprintf(buffer, "%d", f->count);
+        yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_INT_TAG,
+            (yaml_char_t *)buffer, strlen(buffer), 1, 0, YAML_PLAIN_SCALAR_STYLE);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+        yaml_mapping_end_event_initialize(&event);
+        if (!yaml_emitter_emit(&emitter, &event)) goto error;
+    }
+
+    yaml_sequence_end_event_initialize(&event);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_mapping_end_event_initialize(&event);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_document_end_event_initialize(&event, 0);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_stream_end_event_initialize(&event);
+    if (!yaml_emitter_emit(&emitter, &event)) goto error;
+
+    yaml_emitter_delete(&emitter);
+    return 0;
+
+error:
+    fprintf(stderr, "Failed to emit event %d: %s\n", event.type, emitter.problem);
+    yaml_emitter_delete(&emitter);
+    return 1;
+}
+