Skip to content

Commit

Permalink
gamenetworkingsockets: migrate to Conan v2
Browse files Browse the repository at this point in the history
  • Loading branch information
valgur committed Aug 1, 2023
1 parent a547bc7 commit 2448a2d
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 149 deletions.
7 changes: 0 additions & 7 deletions recipes/gamenetworkingsockets/all/CMakeLists.txt

This file was deleted.

12 changes: 3 additions & 9 deletions recipes/gamenetworkingsockets/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
sources:
"1.3.0":
url: "https://github.com/ValveSoftware/GameNetworkingSockets/archive/refs/tags/v1.3.0.zip"
sha256: "22e409546babc449c44f492b253b547a2f5f11abe11a100686a10a990b5091cd"
patches:
"1.3.0":
- patch_file: "patches/001-disable-runtime-override.patch"
base_path: "source_subfolder"
- patch_file: "patches/002-either-static-or-shared.patch"
base_path: "source_subfolder"
"1.4.1":
url: "https://github.com/ValveSoftware/GameNetworkingSockets/archive/refs/tags/v1.4.1.tar.gz"
sha256: "1cfb2bf79c51a08ae4e8b7ff5e9c1266b43cfff6f53ecd3e7bc5e3fcb2a22503"
135 changes: 75 additions & 60 deletions recipes/gamenetworkingsockets/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,109 +1,121 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import os

required_conan_version = ">=1.33.0"
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir, replace_in_file
from conan.tools.gnu import PkgConfigDeps

required_conan_version = ">=1.53.0"


class GameNetworkingSocketsConan(ConanFile):
name = "gamenetworkingsockets"
description = "GameNetworkingSockets is a basic transport layer for games."
topics = ("networking", "game-development")
license = "BSD-3-Clause"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/ValveSoftware/GameNetworkingSockets"
license = "BSD-3-Clause"
generators = "cmake", "pkg_config"
settings = "os", "arch", "compiler", "build_type"
exports_sources = ["CMakeLists.txt", "patches/**"]
topics = ("networking", "game-development")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"encryption": ["openssl", "libsodium", "bcrypt"]
"encryption": ["openssl", "libsodium", "bcrypt"],
}

default_options = {
"shared": False,
"fPIC": True,
"encryption": "openssl"
"encryption": "openssl",
}

_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
self.options.rm_safe("fPIC")

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
self.requires("protobuf/3.21.12")
if self.options.encryption == "openssl":
self.requires("openssl/[>=1.1 <4]")
elif self.options.encryption == "libsodium":
self.requires("libsodium/cci.20220430")

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

if self.options.encryption == "bcrypt" and self.settings.os != "Windows":
raise ConanInvalidConfiguration("bcrypt is only valid on Windows")

def build_requirements(self):
self.build_requires("protobuf/3.17.1")
self.tool_requires("protobuf/<host_version>")

def requirements(self):
self.requires("protobuf/3.17.1")
def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["BUILD_STATIC_LIB"] = not self.options.shared
tc.variables["BUILD_SHARED_LIB"] = self.options.shared
tc.variables["GAMENETWORKINGSOCKETS_BUILD_EXAMPLES"] = False
tc.variables["GAMENETWORKINGSOCKETS_BUILD_TESTS"] = False
tc.variables["Protobuf_USE_STATIC_LIBS"] = not self.dependencies["protobuf"].options.shared
tc.variables["Protobuf_IMPORT_DIRS"] = os.path.join(self.source_folder, "src", "common").replace("\\", "/")
crypto = {
"openssl": "OpenSSL",
"libsodium": "libsodium",
"bcrypt": "BCrypt",
}
tc.variables["USE_CRYPTO"] = crypto[str(self.options.encryption)]
crypto25519 = {
"openssl": "OpenSSL",
"libsodium": "libsodium",
"bcrypt": "Reference",
}
tc.variables["USE_CRYPTO25519"] = crypto25519[str(self.options.encryption)]
if self.options.encryption == "openssl":
self.requires("openssl/1.1.1l")
elif self.options.encryption == "libsodium":
self.requires("libsodium/1.0.18")
tc.variables["OPENSSL_NEW_ENOUGH"] = True
tc.variables["OPENSSL_HAS_25519_RAW"] = True
tc.generate()

def source(self):
tools.get(**self.conan_data["sources"][self.version], strip_root=True, destination=self._source_subfolder)
tc = CMakeDeps(self)
tc.generate()

tc = PkgConfigDeps(self)
tc.generate()

def _patch_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
# Disable MSVC runtime override
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
"configure_msvc_runtime()", "")

def build(self):
self._patch_sources()
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure()
cmake.build()

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["BUILD_STATIC"] = not self.options.shared
self._cmake.definitions["BUILD_SHARED"] = self.options.shared
self._cmake.definitions["GAMENETWORKINGSOCKETS_BUILD_EXAMPLES"] = False
self._cmake.definitions["GAMENETWORKINGSOCKETS_BUILD_TESTS"] = False
self._cmake.definitions["Protobuf_USE_STATIC_LIBS"] = not self.options["protobuf"].shared
crypto = {"openssl": "OpenSSL", "libsodium": "libsodium", "bcrypt": "BCrypt"}
self._cmake.definitions["USE_CRYPTO"] = crypto[str(self.options.encryption)]
crypto25519 = {"openssl": "OpenSSL", "libsodium": "libsodium", "bcrypt": "Reference"}
self._cmake.definitions["USE_CRYPTO25519"] = crypto25519[str(self.options.encryption)]
if self.options.encryption == "openssl":
self._cmake.definitions["OPENSSL_NEW_ENOUGH"] = True
self._cmake.definitions["OPENSSL_HAS_25519_RAW"] = True
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake

def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
copy(self, "LICENSE",
dst=os.path.join(self.package_folder, "licenses"),
src=self.source_folder)
cmake = CMake(self)
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.names["cmake_find_package"] = "GameNetworkingSockets"
self.cpp_info.names["cmake_find_package_multi"] = "GameNetworkingSockets"
self.cpp_info.names["pkg_config"] = "GameNetworkingSockets"
self.cpp_info.set_property("cmake_file_name", "GameNetworkingSockets")
self.cpp_info.set_property("cmake_target_name", "GameNetworkingSockets::GameNetworkingSockets")
self.cpp_info.set_property("pkg_config_name", "GameNetworkingSockets")
self.cpp_info.includedirs.append(os.path.join("include", "GameNetworkingSockets"))
if self.options.shared:
self.cpp_info.libs = ["GameNetworkingSockets"]
Expand All @@ -123,4 +135,7 @@ def package_info(self):
self.cpp_info.system_libs = ["ws2_32", "crypt32", "winmm"]
if self.options.encryption == "bcrypt":
self.cpp_info.system_libs += ["bcrypt"]


# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.names["cmake_find_package"] = "GameNetworkingSockets"
self.cpp_info.names["cmake_find_package_multi"] = "GameNetworkingSockets"

This file was deleted.

This file was deleted.

7 changes: 2 additions & 5 deletions recipes/gamenetworkingsockets/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(GameNetworkingSockets REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
target_link_libraries(${PROJECT_NAME} GameNetworkingSockets::GameNetworkingSockets)
19 changes: 14 additions & 5 deletions recipes/gamenetworkingsockets/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/
${CMAKE_CURRENT_BINARY_DIR}/test_package/)
17 changes: 17 additions & 0 deletions recipes/gamenetworkingsockets/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
2 changes: 1 addition & 1 deletion recipes/gamenetworkingsockets/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
versions:
"1.3.0":
"1.4.1":
folder: all

0 comments on commit 2448a2d

Please sign in to comment.