Skip to content

Commit

Permalink
(#4499) raylib: use external glfw + fix CMake imported target
Browse files Browse the repository at this point in the history
* use external glfw

* explicitly set several CMake options

* explicit cpp_info.libs

* explicit pkg_config

* fix CMake imported target

* use C test_package

* define USE_LIBTYPE_SHARED if msvc and shared

* remove transitive system dependencies

removed system libs were coming from glfw and are not direct dependencies of raylib

* remove CUSTOMIZE_BUILD CMake option

not available in raylib 3.5.0

* fix windows and external glfw
  • Loading branch information
SpaceIm authored Feb 9, 2021
1 parent 50b4cbf commit 5266fe6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
52 changes: 46 additions & 6 deletions recipes/raylib/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RaylibConan(ConanFile):
default_options = { "shared": False, "fPIC": True }

exports_sources = ["CMakeLists.txt"]
generators = "cmake"
generators = "cmake", "cmake_find_package_multi"
_cmake = None

@property
Expand All @@ -36,6 +36,7 @@ def configure(self):
del self.settings.compiler.cppstd

def requirements(self):
self.requires("glfw/3.3.2")
self.requires("opengl/system")
if self.settings.os == "Linux":
self.requires("xorg/system")
Expand All @@ -44,14 +45,23 @@ def source(self):
tools.get(**self.conan_data["sources"][self.version])
os.rename(self.name + "-" + self.version, self._source_subfolder)

def _patch_sources(self):
# avoid symbols conflicts with Win SDK
tools.replace_in_file(os.path.join(self._source_subfolder, "src", "core.c"),
"#define GLFW_EXPOSE_NATIVE_WIN32", "")

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["BUILD_EXAMPLES"] = False
self._cmake.definitions["WITH_PIC"] = self.options.get_safe("fPIC", True)
self._cmake.definitions["USE_EXTERNAL_GLFW"] = "ON"
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake

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

Expand All @@ -61,14 +71,44 @@ def package(self):
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._module_subfolder, self._module_file),
{"raylib": "raylib::raylib"}
)

@staticmethod
def _create_cmake_module_alias_targets(module_file, targets):
content = ""
for alias, aliased in targets.items():
content += (
"if(TARGET {aliased} AND NOT TARGET {alias})\n"
" add_library({alias} INTERFACE IMPORTED)\n"
" set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})\n"
"endif()\n"
).format(alias=alias, aliased=aliased)
tools.save(module_file, content)

@property
def _module_subfolder(self):
return os.path.join("lib", "cmake")

@property
def _module_file(self):
return "conan-official-{}-targets.cmake".format(self.name)

def package_info(self):
self.cpp_info.names["cmake_find_package"] = "raylib"
self.cpp_info.names["cmake_find_package_multi"] = "raylib"
self.cpp_info.libs = tools.collect_libs(self)
self.cpp_info.names["pkg_config"] = "raylib"
self.cpp_info.builddirs = [self._module_subfolder]
self.cpp_info.build_modules = [os.path.join(self._module_subfolder, self._module_file)]
libname = "raylib"
if self.settings.compiler == "Visual Studio" and not self.options.shared:
libname += "_static"
self.cpp_info.libs = [libname]
if self.settings.compiler == "Visual Studio" and self.options.shared:
self.cpp_info.defines.append("USE_LIBTYPE_SHARED")
if self.settings.os == "Linux":
self.cpp_info.system_libs.extend(["m", "pthread", "dl", "rt"])
self.cpp_info.system_libs.extend(["m", "pthread"])
elif self.settings.os == "Windows":
self.cpp_info.system_libs.extend(["gdi32", "winmm"])
elif self.settings.os == "Macos":
self.cpp_info.frameworks.extend(["Cocoa", "IOKit", "CoreVideo"])
self.cpp_info.system_libs.append("winmm")
6 changes: 3 additions & 3 deletions recipes/raylib/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)
project(test_package C)

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

find_package(raylib REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} raylib::raylib)
add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} raylib)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <iostream>

#include "raylib.h"

#include <stdio.h>

int main(void) {
Vector3 center = {0, 0, 0};
float r = 1.0;
if (CheckCollisionSpheres(center, r, center, r)) {
std::cout << "unit sphere collides with itself!" << std::endl;
printf("unit sphere collides with itself!\n");
}
return 0;
}

0 comments on commit 5266fe6

Please sign in to comment.