-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,155 @@ | ||
import os | ||
from conans import ConanFile, MSBuild, AutoToolsBuildEnvironment, tools | ||
from conans.errors import ConanInvalidConfiguration | ||
|
||
import yaml | ||
from conan import ConanFile | ||
from conan.errors import ConanInvalidConfiguration | ||
from conan.tools.files import chdir, collect_libs, copy, get, replace_in_file, rmdir | ||
from conan.tools.gnu import Autotools, AutotoolsDeps, AutotoolsToolchain | ||
from conan.tools.layout import basic_layout | ||
from conan.tools.microsoft import MSBuild, MSBuildDeps, MSBuildToolchain, is_msvc | ||
from conan.tools.microsoft.visual import msvc_version_to_vs_ide_version | ||
|
||
required_conan_version = ">=1.53.0" | ||
|
||
|
||
class YojimboConan(ConanFile): | ||
name = "yojimbo" | ||
url = "https://github.com/conan-io/conan-center-index" | ||
homepage = "https://github.com/networkprotocol/yojimbo" | ||
topics = ("conan", "yojimbo", "game", "udp", "protocol", "client-server", "multiplayer-game-server") | ||
description = "A network library for client/server games written in C++" | ||
license = "BSD-3-Clause" | ||
exports = "submoduledata.yml" | ||
build_requires = "premake/5.0.0-alpha15" | ||
url = "https://github.com/conan-io/conan-center-index" | ||
homepage = "https://github.com/networkprotocol/yojimbo" | ||
topics = ("game", "udp", "protocol", "client-server", "multiplayer-game-server") | ||
|
||
package_type = "library" | ||
settings = "os", "arch", "compiler", "build_type" | ||
options = {"fPIC": [True, False]} | ||
default_options = {"fPIC": True} | ||
options = { | ||
"shared": [True, False], | ||
"fPIC": [True, False], | ||
} | ||
default_options = { | ||
"shared": False, | ||
"fPIC": True, | ||
} | ||
|
||
def export_sources(self): | ||
copy(self, "submoduledata.yml", src=self.recipe_folder, dst=self.export_sources_folder) | ||
|
||
_source_subfolder = "source_subfolder" | ||
_build_subfolder = "build_subfolder" | ||
def config_options(self): | ||
if self.settings.os == "Windows": | ||
del self.options.fPIC | ||
|
||
def configure(self): | ||
if self.settings.arch != "x86_64": | ||
raise ConanInvalidConfiguration("Only 64-bit architecture supported") | ||
|
||
def config_options(self): | ||
if self.settings.os == "Windows": | ||
del self.options.fPIC | ||
def layout(self): | ||
basic_layout(self, src_folder="src") | ||
|
||
def requirements(self): | ||
self.requires("libsodium/1.0.18") | ||
self.requires("mbedtls/2.25.0") | ||
|
||
|
||
def build_requirements(self): | ||
self.tool_requires("premake/5.0.0-alpha15") | ||
|
||
def source(self): | ||
tools.get(**self.conan_data["sources"][self.version], strip_root=True, destination=self._source_subfolder) | ||
submodule_filename = os.path.join(self.recipe_folder, 'submoduledata.yml') | ||
with open(submodule_filename, 'r') as submodule_stream: | ||
submodules_data = yaml.load(submodule_stream) | ||
get(self, **self.conan_data["sources"][self.version], strip_root=True) | ||
|
||
submodule_filename = os.path.join(self.export_sources_folder, "submoduledata.yml") | ||
with open(submodule_filename, "r") as submodule_stream: | ||
submodules_data = yaml.load(submodule_stream, Loader=yaml.Loader) | ||
for path, submodule in submodules_data["submodules"][self.version].items(): | ||
submodule_data = { | ||
"url": submodule["url"], | ||
"sha256": submodule["sha256"], | ||
"destination": os.path.join(self._source_subfolder, submodule["destination"]), | ||
"strip_root": True | ||
"destination": os.path.join(self.source_folder, submodule["destination"]), | ||
"strip_root": True, | ||
} | ||
|
||
tools.get(**submodule_data) | ||
submodule_source = os.path.join(self._source_subfolder, path) | ||
tools.rmdir(submodule_source) | ||
get(self, **submodule_data) | ||
submodule_source = os.path.join(self.source_folder, path) | ||
rmdir(self, submodule_source) | ||
|
||
def build(self): | ||
def generate(self): | ||
if is_msvc(self): | ||
tc = MSBuildToolchain(self) | ||
tc.generate() | ||
tc = MSBuildDeps(self) | ||
tc.generate() | ||
else: | ||
tc = AutotoolsToolchain(self) | ||
tc.generate() | ||
tc = AutotoolsDeps(self) | ||
tc.generate() | ||
|
||
def build(self): | ||
# Before building we need to make some edits to the premake file to build using conan dependencies rather than local/bundled | ||
|
||
# Generate the list of dependency include and library paths as strings | ||
include_path_str = ', '.join('"{0}"'.format(p) for p in self.deps_cpp_info["libsodium"].include_paths + self.deps_cpp_info["mbedtls"].include_paths) | ||
lib_path_str = ', '.join('"{0}"'.format(p) for p in self.deps_cpp_info["libsodium"].lib_paths + self.deps_cpp_info["mbedtls"].lib_paths) | ||
|
||
premake_path = os.path.join(self._source_subfolder, "premake5.lua") | ||
premake_path = os.path.join(self.source_folder, "premake5.lua") | ||
|
||
if self.settings.os == "Windows": | ||
|
||
# Replace Windows directory seperator | ||
include_path_str = include_path_str.replace("\\", "/") | ||
lib_path_str = lib_path_str.replace("\\", "/") | ||
|
||
# Edit the premake script to use conan rather than bundled dependencies | ||
tools.replace_in_file(premake_path, "includedirs { \".\", \"./windows\"", "includedirs { \".\", %s" % include_path_str, strict=True) | ||
tools.replace_in_file(premake_path, "libdirs { \"./windows\" }", "libdirs { %s }" % lib_path_str, strict=True) | ||
|
||
replace_in_file( | ||
self, | ||
premake_path, | ||
'includedirs { ".", "./windows"', | ||
'includedirs { ".", ', | ||
strict=True, | ||
) | ||
replace_in_file( | ||
self, premake_path, 'libdirs { "./windows" }', "libdirs { }", strict=True | ||
) | ||
|
||
# Edit the premake script to change the name of libsodium | ||
tools.replace_in_file(premake_path, "\"sodium\"", "\"libsodium\"", strict=True) | ||
replace_in_file(self, premake_path, '"sodium"', '"libsodium"', strict=True) | ||
|
||
else: | ||
|
||
# Edit the premake script to use conan rather than local dependencies | ||
tools.replace_in_file(premake_path, "\"/usr/local/include\"", include_path_str, strict=True) | ||
|
||
|
||
# Edit the premake script to use conan rather than local dependencies | ||
replace_in_file(self, premake_path, '"/usr/local/include"', "", strict=True) | ||
|
||
# Build using premake | ||
|
||
if self.settings.compiler == "Visual Studio": | ||
generator = "vs" + {"16": "2019", | ||
"15": "2017", | ||
"14": "2015", | ||
"12": "2013", | ||
"11": "2012", | ||
"10": "2010", | ||
"9": "2008", | ||
"8": "2005"}.get(str(self.settings.compiler.version)) | ||
|
||
if is_msvc(self): | ||
generator = "vs" + { | ||
"17": "2022", | ||
"16": "2019", | ||
"15": "2017", | ||
"14": "2015", | ||
"12": "2013", | ||
"11": "2012", | ||
"10": "2010", | ||
"9": "2008", | ||
"8": "2005", | ||
}.get(msvc_version_to_vs_ide_version(str(self.settings.compiler.version))) | ||
else: | ||
generator = "gmake2" | ||
|
||
with tools.chdir(self._source_subfolder): | ||
self.run("premake5 %s" % generator) | ||
|
||
if self.settings.compiler == "Visual Studio": | ||
with chdir(self, self.source_folder): | ||
self.run(f"premake5 {generator}") | ||
if is_msvc(self): | ||
msbuild = MSBuild(self) | ||
msbuild.build("Yojimbo.sln") | ||
else: | ||
config = "debug" if self.settings.build_type == "Debug" else "release" | ||
config += "_x64" | ||
env_build = AutoToolsBuildEnvironment(self) | ||
env_build.make(args=["config=%s" % config]) | ||
|
||
autotools = Autotools(self) | ||
autotools.make(args=[f"config={config}"]) | ||
|
||
def package(self): | ||
self.copy(pattern="LICENCE", dst="licenses", src=self._source_subfolder) | ||
self.copy(pattern="yojimbo.h", dst="include", src=self._source_subfolder) | ||
|
||
self.copy(pattern="*/yojimbo.lib", dst="lib", keep_path=False) | ||
self.copy(pattern="*/libyojimbo.a", dst="lib", keep_path=False) | ||
copy(self, "LICENCE", | ||
dst=os.path.join(self.package_folder, "licenses"), | ||
src=self.source_folder) | ||
copy(self, "yojimbo.h", | ||
dst=os.path.join(self.package_folder, "include"), | ||
src=self.source_folder) | ||
copy(self, "*/yojimbo.lib", | ||
dst=os.path.join(self.package_folder, "lib"), | ||
src=self.source_folder, | ||
keep_path=False) | ||
copy(self, "*/libyojimbo.a", | ||
dst=os.path.join(self.package_folder, "lib"), | ||
src=self.source_folder, | ||
keep_path=False) | ||
|
||
def package_info(self): | ||
self.cpp_info.libs = tools.collect_libs(self) | ||
self.cpp_info.libs = collect_libs(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.1.0) | ||
project(test_package) | ||
cmake_minimum_required(VERSION 3.15) | ||
project(test_package LANGUAGES CXX) | ||
|
||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) | ||
conan_basic_setup() | ||
find_package(yojimbo REQUIRED CONFIG) | ||
|
||
add_executable(${PROJECT_NAME} test_package.cpp) | ||
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS}) | ||
target_link_libraries(${PROJECT_NAME} PRIVATE yojimbo::yojimbo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "compiler", "build_type", "arch" | ||
generators = "cmake" | ||
settings = "os", "arch", "compiler", "build_type" | ||
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.settings): | ||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "compiler", "build_type", "arch" | ||
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.settings): | ||
bin_path = os.path.join("bin", "test_package") | ||
self.run(bin_path, run_environment=True) |