From 35db9faeebb3454ee280f225c91511743133dc70 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 12 Jul 2024 16:34:27 +0900 Subject: [PATCH] Add conan related files and adapt CMakeLists.txt This is put in a new "conan" branch. Changes to the conan build process will only occur on this branch. --- .gitignore | 2 ++ CMakeLists.txt | 23 ++------------- conanfile.py | 57 +++++++++++++++++++++++++++++++++++++ test_package/CMakeLists.txt | 7 +++++ test_package/conanfile.py | 26 +++++++++++++++++ test_package/src/test.cpp | 10 +++++++ 6 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 conanfile.py create mode 100644 test_package/CMakeLists.txt create mode 100644 test_package/conanfile.py create mode 100644 test_package/src/test.cpp diff --git a/.gitignore b/.gitignore index 3957bdb..6ce0270 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ compile_commands.json *.o doxygen_out/ /tags + +CMakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ed0671..3b226fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,26 +37,9 @@ if(WIN32) endif() endif() -if(UDPC_DISABLE_LIBSODIUM) - message(STATUS "libsodium disabled") -elseif(DEFINED M_LIBSODIUM_LIBRARIES AND DEFINED M_LIBSODIUM_INCLUDE_DIRS) - message(STATUS "libsodium manual paths detected, using them") - target_compile_definitions(UDPC PUBLIC UDPC_LIBSODIUM_ENABLED) - target_link_libraries(UDPC PUBLIC ${M_LIBSODIUM_LIBRARIES}) - target_include_directories(UDPC PUBLIC ${M_LIBSODIUM_INCLUDE_DIRS}) -else() - find_package(PkgConfig REQUIRED) - pkg_check_modules(LIBSODIUM QUIET libsodium) - if(LIBSODIUM_FOUND) - target_compile_definitions(UDPC PUBLIC UDPC_LIBSODIUM_ENABLED) - target_link_libraries(UDPC PUBLIC ${LIBSODIUM_LIBRARIES}) - target_include_directories(UDPC PUBLIC ${LIBSODIUM_INCLUDE_DIRS}) - target_compile_options(UDPC PUBLIC ${LIBSODIUM_CFLAGS_OTHER}) - message(STATUS "libsodium enabled") - else() - message(STATUS "libsodium not found, UDPC will be compiled without libsodium support") - endif() -endif() +find_package(libsodium REQUIRED) +target_compile_definitions(UDPC PUBLIC UDPC_LIBSODIUM_ENABLED) +target_link_libraries(UDPC PUBLIC libsodium::libsodium) if(CMAKE_BUILD_TYPE MATCHES "Debug") set(UDPC_UnitTest_SOURCES diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000..3f293ba --- /dev/null +++ b/conanfile.py @@ -0,0 +1,57 @@ +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps +import os + + +class udpcRecipe(ConanFile): + name = "udpc" + version = "1.0" + package_type = "library" + + # Optional metadata + license = "MIT" + author = "Stephen Seo stephen@seodisparate.com" + url = "https://git.seodisparate.com/stephenseo/UDPConnection" + description = "Implements a connection over UDP" + topics = ("UDPC", "UDP", "Network", "Network Connection") + + # Binary configuration + settings = "os", "compiler", "build_type", "arch" + options = {"shared": [True, False], "fPIC": [True, False]} + default_options = {"shared": False, "fPIC": True} + + # Sources are located in the same place as this recipe, copy them to the recipe + exports_sources = "CMakeLists.txt", "src/*" + + def config_options(self): + if self.settings.os == "Windows": + self.options.rm_safe("fPIC") + + def configure(self): + if self.options.shared: + self.options.rm_safe("fPIC") + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires("libsodium/cci.20220430") + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs = ["UDPC"] + diff --git a/test_package/CMakeLists.txt b/test_package/CMakeLists.txt new file mode 100644 index 0000000..ec4e9b7 --- /dev/null +++ b/test_package/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.15) +project(PackageTest CXX) + +find_package(udpc CONFIG REQUIRED) + +add_executable(example src/test.cpp) +target_link_libraries(example udpc::udpc) diff --git a/test_package/conanfile.py b/test_package/conanfile.py new file mode 100644 index 0000000..8ed1eda --- /dev/null +++ b/test_package/conanfile.py @@ -0,0 +1,26 @@ +import os + +from conan import ConanFile +from conan.tools.cmake import CMake, cmake_layout +from conan.tools.build import can_run + + +class udpcTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "CMakeDeps", "CMakeToolchain" + + def requirements(self): + self.requires(self.tested_reference_str) + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def layout(self): + cmake_layout(self) + + def test(self): + if can_run(self): + cmd = os.path.join(self.cpp.build.bindir, "example") + self.run(cmd, env="conanrun") diff --git a/test_package/src/test.cpp b/test_package/src/test.cpp new file mode 100644 index 0000000..e16f74b --- /dev/null +++ b/test_package/src/test.cpp @@ -0,0 +1,10 @@ +#include "UDPC.h" + +int main() { + auto ctx = UDPC_init(UDPC_create_id_easy("127.0.0.1", 0), 1, 1); + if (UDPC_is_valid_context(ctx) == 0) { + return 1; + } + UDPC_destroy(ctx); + return 0; +}