Repository: conan-io/conan-center-index

Issue:
[request] port paho-mqtt-cpp to CCI
### Package Details
  * Package Name/Version: **paho-mqtt-cpp/3.15.3**
  * Website: **https://github.com/conan-community/conan-paho-cpp**
  * Source code: **https://github.com/eclipse/paho.mqtt.cpp**

 depends on https://github.com/conan-io/conan-center-index/pull/914


Discussion:

Resolving patch:
diff --git a/recipes/paho-mqtt-c/all/conanfile.py b/recipes/paho-mqtt-c/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/paho-mqtt-c/all/conanfile.py
@@ -0,0 +1,91 @@
+import os
+from conans import CMake, ConanFile, tools
+
+class PahoMqttcConan(ConanFile):
+    name = "paho-mqtt-c"
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/eclipse/paho.mqtt.c"
+    topics = ("MQTT", "IoT", "eclipse", "SSL", "paho", "C")
+    license = "EPL-1.0"
+    description = """The Eclipse Paho project provides open-source client implementations of MQTT
+    and MQTT-SN messaging protocols aimed at new, existing, and emerging applications for the Internet
+    of Things (IoT)"""
+    exports_sources = ["CMakeLists.txt", "patches/*"]
+    generators = "cmake"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {"shared": [True, False],
+               "fPIC": [True, False],
+               "ssl": [True, False],
+               "asynchronous": [True, False]}
+    default_options = {"shared": False,
+                       "fPIC": True,
+                       "ssl": False,
+                       "asynchronous": True}
+
+    _cmake = None
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    def configure(self):
+        del self.settings.compiler.cppstd
+        del self.settings.compiler.libcxx
+
+    def requirements(self):
+        if self.options.ssl:
+            self.requires("openssl/1.1.1d")
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version])
+        extracted_dir = self.name.replace("-", ".") + "-" + 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["PAHO_ENABLE_TESTING"] = False
+        self._cmake.definitions["PAHO_BUILD_DOCUMENTATION"] = False
+        self._cmake.definitions["PAHO_BUILD_SAMPLES"] = False
+        self._cmake.definitions["PAHO_BUILD_STATIC"] = not self.options.shared
+        self._cmake.definitions["PAHO_BUILD_ASYNC"] = self.options.asynchronous
+        self._cmake.definitions["PAHO_WITH_SSL"] = self.options.ssl
+        if self.options.ssl:
+            self._cmake.definitions["OPENSSL_SEARCH_PATH"] = self.deps_cpp_info["openssl"].rootpath
+        self._cmake.configure()
+        return self._cmake
+
+    def build(self):
+        for patch in self.conan_data["patches"][self.version]:
+            tools.patch(**patch)
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        self.copy("edl-v10", src=self._source_subfolder, dst="licenses")
+        self.copy("epl-v10", src=self._source_subfolder, dst="licenses")
+        self.copy("notice.html", src=self._source_subfolder, dst="licenses")
+        cmake = self._configure_cmake()
+        cmake.install()
+        tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
+
+    def package_info(self):
+        self.cpp_info.libs = tools.collect_libs(self)
+        if self.settings.os == "Windows":
+            if not self.options.shared:
+                self.cpp_info.system_libs.append("ws2_32")
+                if self.settings.compiler == "gcc":
+                    self.cpp_info.system_libs.extend(
+                        ["wsock32", "uuid", "crypt32", "rpcrt4"])
+        else:
+            if self.settings.os == "Linux":
+                self.cpp_info.system_libs.extend(["c", "dl", "pthread"])
+            elif self.settings.os == "FreeBSD":
+                self.cpp_info.system_libs.extend(["compat", "pthread"])
+            else:
+                self.cpp_info.system_libs.extend(["c", "pthread"])


Test patch:
diff --git a/recipes/paho-mqtt-c/all/test_package/CMakeLists.txt b/recipes/paho-mqtt-c/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/paho-mqtt-c/all/test_package/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(test_package C)
+cmake_minimum_required(VERSION 2.8.11)
+
+set(CMAKE_VERBOSE_MAKEFILE TRUE)
+
+option(BUILD_ASYNC "Build asynchronous example" ON)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+if (BUILD_ASYNC)
+    add_executable(${PROJECT_NAME} test_package_async.c)
+else()
+    add_executable(${PROJECT_NAME} test_package_client.c)
+endif()
+target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
diff --git a/recipes/paho-mqtt-c/all/test_package/conanfile.py b/recipes/paho-mqtt-c/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/paho-mqtt-c/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.definitions["BUILD_ASYNC"] = self.options["paho-mqtt-c"].asynchronous
+        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)
\ No newline at end of file
diff --git a/recipes/paho-mqtt-c/all/test_package/test_package_async.c b/recipes/paho-mqtt-c/all/test_package/test_package_async.c
new file mode 100644
--- /dev/null
+++ b/recipes/paho-mqtt-c/all/test_package/test_package_async.c
@@ -0,0 +1,122 @@
+// This example does not work for library option 'paho-mqtt-c:async=False'
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "MQTTAsync.h"
+
+#if !defined(WIN32)
+#include <unistd.h>
+#else
+#include <windows.h>
+#endif
+
+#if defined(_WRS_KERNEL)
+#include <OsWrapper.h>
+#endif
+
+#define ADDRESS "tcp://iot.eclipse.org:1883"
+#define CLIENTID "ExampleClientPub"
+#define TOPIC "MQTT Examples"
+#define PAYLOAD "Hello World!"
+#define QOS 1
+#define TIMEOUT 10000L
+
+volatile MQTTAsync_token deliveredtoken;
+
+int finished = 0;
+
+void connlost(void *context, char *cause)
+{
+    MQTTAsync client = (MQTTAsync)context;
+    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
+    int rc;
+
+    printf("\nConnection lost\n");
+    printf("     cause: %s\n", cause);
+
+    printf("Reconnecting\n");
+    conn_opts.keepAliveInterval = 20;
+    conn_opts.cleansession = 1;
+    if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
+    {
+        printf("Failed to start connect, return code %d\n", rc);
+        finished = 1;
+    }
+}
+
+void onDisconnect(void *context, MQTTAsync_successData *response)
+{
+    printf("Successful disconnection\n");
+    finished = 1;
+}
+
+void onSend(void *context, MQTTAsync_successData *response)
+{
+    MQTTAsync client = (MQTTAsync)context;
+    MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
+    int rc;
+
+    printf("Message with token value %d delivery confirmed\n", response->token);
+
+    opts.onSuccess = onDisconnect;
+    opts.context = client;
+
+    if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
+    {
+        printf("Failed to start sendMessage, return code %d\n", rc);
+        exit(EXIT_FAILURE);
+    }
+}
+
+void onConnectFailure(void *context, MQTTAsync_failureData *response)
+{
+    printf("Connect failed, rc %d\n", response ? response->code : 0);
+    finished = 1;
+}
+
+void onConnect(void *context, MQTTAsync_successData *response)
+{
+    MQTTAsync client = (MQTTAsync)context;
+    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
+    MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
+    int rc;
+
+    printf("Successful connection\n");
+
+    opts.onSuccess = onSend;
+    opts.context = client;
+
+    pubmsg.payload = PAYLOAD;
+    pubmsg.payloadlen = strlen(PAYLOAD);
+    pubmsg.qos = QOS;
+    pubmsg.retained = 0;
+    deliveredtoken = 0;
+
+    if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
+    {
+        printf("Failed to start sendMessage, return code %d\n", rc);
+        exit(EXIT_FAILURE);
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    MQTTAsync client;
+    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
+    int rc;
+
+    MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
+
+    MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);
+
+    conn_opts.keepAliveInterval = 20;
+    conn_opts.cleansession = 1;
+    conn_opts.onSuccess = onConnect;
+    conn_opts.onFailure = onConnectFailure;
+    conn_opts.context = client;
+
+    printf("finished!");
+
+    MQTTAsync_destroy(&client);
+    return 0;
+}
\ No newline at end of file
diff --git a/recipes/paho-mqtt-c/all/test_package/test_package_client.c b/recipes/paho-mqtt-c/all/test_package/test_package_client.c
new file mode 100644
--- /dev/null
+++ b/recipes/paho-mqtt-c/all/test_package/test_package_client.c
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2017 IBM Corp.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v1.0 which accompany this distribution.
+ *
+ * The Eclipse Public License is available at
+ *   http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ *   http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *    Ian Craggs - initial contribution
+ *******************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "MQTTClient.h"
+
+#define CLIENTID    "conanpahoc"
+#define ADDRESS     "tcp://iot.eclipse.org:1883"
+
+void connlost(void *context, char *cause)
+{
+    printf("\nConnection lost\n");
+    printf("     cause: %s\n", cause);
+}
+
+int main(int argc, char* argv[])
+{
+    MQTTClient client;
+    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
+    int rc;
+
+    printf("Connecting client %s to address %s\n", CLIENTID, ADDRESS);
+    rc = MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
+    if (rc == MQTTCLIENT_SUCCESS) {
+        conn_opts.keepAliveInterval = 20;
+        conn_opts.cleansession = 1;
+
+        printf("Set callbacks\n");
+        MQTTClient_setCallbacks(client, NULL, connlost, NULL, NULL);
+
+        MQTTClient_destroy(&client);
+    }
+
+    return EXIT_SUCCESS;
+}