Skip to content

Commit

Permalink
(#10584) magnum-extras: modernize
Browse files Browse the repository at this point in the history
* modernize

- CMakeDeps support
- relocatable shared lib on macOS
- patch in build() instead of source()
- cache CMake configuration with functools.lru_cache

* add short_paths
  • Loading branch information
SpaceIm authored May 4, 2022
1 parent 31e2674 commit 4aabc2b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion recipes/magnum-extras/all/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1)
project(cmake_wrapper)

include(conanbuildinfo.cmake)
conan_basic_setup()
conan_basic_setup(KEEP_RPATHS)

set(MAGNUM_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/Magnum)
#set(MAGNUM_EXTERNAL_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/MagnumExternal)
Expand Down
70 changes: 38 additions & 32 deletions recipes/magnum-extras/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import functools
import os

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.43.0"


class MagnumExtrasConan(ConanFile):
Expand All @@ -13,8 +14,7 @@ class MagnumExtrasConan(ConanFile):
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://magnum.graphics"

settings = "os", "compiler", "build_type", "arch"

settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
Expand All @@ -31,20 +31,18 @@ class MagnumExtrasConan(ConanFile):
"ui_gallery": True,
"application": "sdl2",
}
generators = "cmake", "cmake_find_package"
exports_sources = ["CMakeLists.txt", "patches/*"]

_cmake = None
short_paths = True
generators = "cmake", "cmake_find_package"

@property
def _source_subfolder(self):
return "source_subfolder"

def source(self):
tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"),
'set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/modules/" ${CMAKE_MODULE_PATH})',
"")
def export_sources(self):
self.copy("CMakeLists.txt")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])

def config_options(self):
if self.settings.os == "Windows":
Expand All @@ -54,53 +52,59 @@ def config_options(self):
self.options.application = "android"
if self.settings.os == "Emscripten":
self.options.application = "emscripten"
# FIXME: Requires 'magnum:basis_importer=True'
# FIXME: Requires 'magnum:basis_importer=True'
self.options.player = False
# FIXME: Fails to compile
self.options.ui_gallery = False

def configure(self):
if self.options.shared:
del self.options.fPIC

def requirements(self):
self.requires("magnum/{}".format(self.version))
self.requires("corrade/{}".format(self.version))
if self.settings.os in ["iOS", "Emscripten", "Android"] and self.options.ui_gallery:
self.requires("magnum-plugins/{}".format(self.version))

def build_requirements(self):
self.build_requires("corrade/{}".format(self.version))

def validate(self):
opt_name = "{}_application".format(self.options.application)
if not getattr(self.options["magnum"], opt_name):
raise ConanInvalidConfiguration("Magnum needs option '{opt}=True'".format(opt=opt_name))
if self.settings.os == "Emscripten" and self.options["magnum"].target_gl == "gles2":
raise ConanInvalidConfiguration("OpenGL ES 3 required, use option 'magnum:target_gl=gles3'")

def build_requirements(self):
self.build_requires("corrade/{}".format(self.version))

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

@functools.lru_cache(1)
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_STATIC_PIC"] = self.options.get_safe("fPIC", False)
self._cmake.definitions["BUILD_TESTS"] = False
self._cmake.definitions["BUILD_GL_TESTS"] = False

self._cmake.definitions["WITH_PLAYER"] = self.options.player
self._cmake.definitions["WITH_UI"] = self.options.ui
self._cmake.definitions["WITH_UI_GALLERY"] = self.options.ui_gallery

self._cmake.configure()
return self._cmake
cmake = CMake(self)
cmake.definitions["BUILD_STATIC"] = not self.options.shared
cmake.definitions["BUILD_STATIC_PIC"] = self.options.get_safe("fPIC", False)
cmake.definitions["BUILD_TESTS"] = False
cmake.definitions["BUILD_GL_TESTS"] = False

cmake.definitions["WITH_PLAYER"] = self.options.player
cmake.definitions["WITH_UI"] = self.options.ui
cmake.definitions["WITH_UI_GALLERY"] = self.options.ui_gallery

cmake.configure()
return cmake

def _patch_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)

cmakelists = [os.path.join("src", "Magnum", "Ui", "CMakeLists.txt"),
tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"),
'set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/modules/" ${CMAKE_MODULE_PATH})',
"")

cmakelists = [os.path.join("src", "Magnum", "Ui", "CMakeLists.txt"),
os.path.join("src", "player","CMakeLists.txt")]
app_name = "{}Application".format("XEgl" if self.options.application == "xegl" else str(self.options.application).capitalize())
for cmakelist in cmakelists:
Expand All @@ -122,11 +126,13 @@ def package(self):
self.copy("COPYING", src=self._source_subfolder, dst="licenses")

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "MagnumExtras")
self.cpp_info.names["cmake_find_package"] = "MagnumExtras"
self.cpp_info.names["cmake_find_package_multi"] = "MagnumExtras"

lib_suffix = "-d" if self.settings.build_type == "Debug" else ""
if self.options.ui:
self.cpp_info.components["ui"].set_property("cmake_target_name", "MagnumExtras::Ui")
self.cpp_info.components["ui"].names["cmake_find_package"] = "Ui"
self.cpp_info.components["ui"].names["cmake_find_package_multi"] = "Ui"
self.cpp_info.components["ui"].libs = ["MagnumUi{}".format(lib_suffix)]
Expand Down
2 changes: 1 addition & 1 deletion recipes/magnum-extras/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(test_package CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(MagnumExtras REQUIRED)
find_package(MagnumExtras REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE MagnumExtras::Ui)
Expand Down
5 changes: 2 additions & 3 deletions recipes/magnum-extras/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanException
import os


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

def build(self):
if self.options["magnum-extras"].ui:
Expand Down

0 comments on commit 4aabc2b

Please sign in to comment.