Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conan 2.x recipe #1308

Merged
merged 15 commits into from
Oct 25, 2024
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ option(RUN_LDCONFIG "Run ldconfig after installation" ON)
option(DPP_INSTALL "Generate the install target" ON)
option(DPP_BUILD_TEST "Build the test program" ON)
option(DPP_NO_VCPKG "No VCPKG" OFF)
option(DPP_NO_CONAN "No Conan" OFF)
option(CONAN_EXPORTED "Exported via Conan - DO NOT SET MANUALLY" OFF)
option(DPP_CORO "Support for C++20 coroutines" OFF)
option(DPP_FORMATTERS "Support for C++20 formatters" OFF)
option(DPP_USE_EXTERNAL_JSON "Use an external installation of nlohmann::json" OFF)
Expand Down Expand Up @@ -64,6 +66,10 @@ else()
endif()
endif()

if (DPP_NO_CONAN)
message("-- INFO: Explicitly disabling Conan as running inside the CI action.")
endif()

if (WIN32 AND NOT MINGW AND BUILD_SHARED_LIBS)
message("-- INFO: Configuring .rc resource script")
configure_file("${DPP_ROOT_PATH}/src/dpp/dpp.rc.in" "${DPP_ROOT_PATH}/src/dpp/dpp.rc" NEWLINE_STYLE WIN32)
Expand Down
87 changes: 87 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
from conan import ConanFile
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.scm import Git
from conan.tools.files import download, unzip

required_conan_version = ">=2.0"

class DPPConan(ConanFile):
name = "dpp"
version = "10.0.34"
license = "Apache-2.0"
url = "https://github.com/brainboxdotcc/DPP"
description = "D++ is a lightweight and efficient library for Discord"
topics = ("discord")
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": True, "fPIC": True}

@property
def _min_cppstd(self):
return 17

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, self._min_cppstd)

def requirements(self):
self.requires("nlohmann_json/3.11.2")
self.requires("openssl/3.1.2")
self.requires("zlib/1.3")
self.requires("opus/1.4")

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 export(self):
git = Git(self, self.recipe_folder)
git.coordinates_to_conandata()

def source(self):
# This environment variable should only be set by D++ library developers to ensure that conan builds succeed
# without having to wait for release. It will check out the development branch where conanfile.py is being
# developed and tested. If you are NOT sure what this does, DO NOT SET IT. You won't get the D++ release
# you expect!
if 'DPP_CONAN_TESTING' in os.environ:
git = Git(self)
git.clone(url="https://github.com/brainboxdotcc/DPP.git", target=".")
git.checkout(commit="conan-the-librarian")
zip_name = "DPP.zip"
else:
download(self, f"https://github.com/brainboxdotcc/DPP/archive/refs/tags/v{self.version}.zip", zip_name)
unzip(self, zip_name, '.', False, None, True)
os.unlink(zip_name)

def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc = CMakeToolchain(self)
tc.cache_variables["CONAN_EXPORTED"] = True
tc.cache_variables["BUILD_VOICE_SUPPORT"] = True
tc.cache_variables["DPP_BUILD_TEST"] = False
tc.cache_variables["BUILD_SHARED_LIBS"] = True
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 = ["dpp"]
self.cpp_info.includedirs = ["include/dpp-10.0"]
self.cpp_info.libdirs = ["lib/dpp-10.0"]
113 changes: 68 additions & 45 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,40 @@ else()
message("-- Building ${Green}dynamic${ColourReset} library.")
endif()


if (CONAN_EXPORTED)
message("-- INFO: ${Green}Conan detected${ColourReset}... finding packages...")
find_package(OpenSSL REQUIRED COMPONENTS SSL Crypto)
find_package(ZLIB REQUIRED)
find_package(Opus)
find_package(Threads REQUIRED)
find_package(Git QUIET)
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
endif()

if(WIN32 AND NOT MINGW)
# Fake an ssl version number to satisfy MLSPP
set(OPENSSL_VERSION "1.1.1f")
if (NOT WINDOWS_32_BIT)
message("-- Building for windows with precompiled packaged dependencies")
#set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set(ZLIB_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib")
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPENSSL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPENSSL_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib")
#ADD_DEFINITIONS(/bigobj)

link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/libssl.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/libcrypto.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/zlib.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/opus.lib")

set(OPUS_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPUS_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/opus.lib")
set(HAVE_OPUS_OPUS_H "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include/opus/opus.h")
set(OPUS_FOUND 1)
if (NOT CONAN_EXPORTED)
set(ZLIB_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib")
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPENSSL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPENSSL_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib")

link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/libssl.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/libcrypto.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/zlib.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/opus.lib")

set(OPUS_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPUS_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/opus.lib")
set(HAVE_OPUS_OPUS_H "${CMAKE_CURRENT_SOURCE_DIR}/../win32/include/opus/opus.h")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
endif()

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../win32/include")
set(OPUS_FOUND 1)

add_compile_options("/bigobj")
add_compile_definitions(OPENSSL_SYS_WIN32)
Expand Down Expand Up @@ -154,31 +163,39 @@ string(ASCII 27 Esc)

set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package(Threads REQUIRED)
if (NOT CONAN_EXPORTED)
find_package(Threads REQUIRED)
endif()
if(MINGW OR NOT WIN32)
find_package(ZLIB REQUIRED)
if (NOT CONAN_EXPORTED)
find_package(ZLIB REQUIRED)
endif()
message("-- ZLIB: ${Green}${ZLIB_LIBRARIES}${ColourReset}")
endif(MINGW OR NOT WIN32)

if(APPLE)
if(CMAKE_APPLE_SILICON_PROCESSOR EQUAL arm64)
set(OPENSSL_ROOT_DIR "/opt/homebrew/opt/openssl")
else()
set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")
endif()
find_package(OpenSSL REQUIRED)
else()
if(MINGW OR NOT WIN32)
if(NOT BUILD_SHARED_LIBS)
set(OPENSSL_USE_STATIC_LIBS TRUE)
if (NOT CONAN_EXPORTED)
if(APPLE)
if(CMAKE_APPLE_SILICON_PROCESSOR EQUAL arm64)
set(OPENSSL_ROOT_DIR "/opt/homebrew/opt/openssl")
else()
set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")
endif()
find_package(OpenSSL REQUIRED)
else()
if(MINGW OR NOT WIN32)
if(NOT BUILD_SHARED_LIBS)
set(OPENSSL_USE_STATIC_LIBS TRUE)
endif()
find_package(OpenSSL REQUIRED)
endif()
endif()
endif()

include_directories(${OPENSSL_INCLUDE_DIR} ${ZLIB_INCLUDE_DIRS})

find_package(Git QUIET)
if (NOT CONAN_EXPORTED)
find_package(Git QUIET)
endif()

if(NOT GIT_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../.git")
message(FATAL_ERROR "You are using a git version of D++ but do not have git installed. Install git (not 'gh') and try again.")
Expand Down Expand Up @@ -263,7 +280,7 @@ foreach (fullmodname ${subdirlist})
)
endif()

if (WIN32 AND NOT MINGW)
if (WIN32 AND NOT MINGW AND NOT CONAN_EXPORTED)

if (NOT WINDOWS_32_BIT)
target_link_libraries(${modname} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../win32/lib/libssl.lib"
Expand All @@ -278,7 +295,11 @@ foreach (fullmodname ${subdirlist})
endif()

else()
target_link_libraries(${modname} PUBLIC ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY} ${ZLIB_LIBRARIES})
if (CONAN_EXPORTED)
target_link_libraries(${modname} PUBLIC openssl::openssl ZLIB::ZLIB Opus::opus)
else()
target_link_libraries(${modname} PUBLIC ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY} ${ZLIB_LIBRARIES})
endif()
if (MINGW)
target_link_libraries(${modname} PUBLIC wsock32 ws2_32 crypt32)
endif ()
Expand Down Expand Up @@ -435,18 +456,20 @@ if (DPP_BUILD_TEST)
COMMAND unittest
)
endif()

if(WIN32 AND NOT MINGW)
if (NOT WINDOWS_32_BIT)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/zlib1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/libcrypto-1_1-x64.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/libssl-1_1-x64.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/opus.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
else()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/zlib1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/libcrypto-1_1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/libssl-1_1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/opus.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)

if (NOT CONAN_EXPORTED)
if(WIN32 AND NOT MINGW)
if (NOT WINDOWS_32_BIT)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/zlib1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/libcrypto-1_1-x64.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/libssl-1_1-x64.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/bin/opus.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
else()
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/zlib1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/libcrypto-1_1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/libssl-1_1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/../win32/32/bin/opus.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
endif()
endif()
endif()

Expand Down
Loading