Skip to content

Commit

Permalink
(conan-io#16688) ruy: update to support Conan 2.0
Browse files Browse the repository at this point in the history
* ruy: update to support Conan 2.0

* ruy: fixes

* ruy: remove cmake generated files

* ruy: remove old version

* ruy: fix test_package when cppstd is not defined in the profile (conan 1.x)

* ruy: fix order of cmake_minimum_required invocation

* ruy: export all symbols on windows to support shared

* Update recipes/ruy/all/conanfile.py

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>

* Update recipes/ruy/all/test_package/conanfile.py

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>

* Update recipes/ruy/all/test_package/conanfile.py

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>

* Update recipes/ruy/all/test_package/CMakeLists.txt

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>

* Update recipes/ruy/all/test_package/CMakeLists.txt

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>

---------

Co-authored-by: SpaceIm <30052553+SpaceIm@users.noreply.github.com>
  • Loading branch information
2 people authored and MartinDelille committed Apr 12, 2023
1 parent 5eb5c0d commit ca0d539
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 81 deletions.
9 changes: 0 additions & 9 deletions recipes/ruy/all/CMakeLists.txt

This file was deleted.

3 changes: 0 additions & 3 deletions recipes/ruy/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
sources:
"cci.20210622":
url: "https://github.com/google/ruy/archive/9c56af3fce210a8a103eda19bd6f47c08a9e3d90.tar.gz"
sha256: "8895874cfbf7263b028a6ec55b4b1737af0ea2b7bfedac90fd14d0b0a654f2dc"
"cci.20220628":
url: "https://github.com/google/ruy/archive/841ea4172ba904fe3536789497f9565f2ef64129.tar.gz"
sha256: "fbd20e6c8cd5cf8ef159f69600ea0c7ef0f1401a4c16a9cd04e5a12ffa9c6e14"
105 changes: 52 additions & 53 deletions recipes/ruy/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import os
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
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, replace_in_file, rmdir
from conan.tools.scm import Version
from conan.errors import ConanInvalidConfiguration

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.57.0"


class RuyConan(ConanFile):
Expand All @@ -13,8 +17,6 @@ class RuyConan(ConanFile):
homepage = "https://github.com/google/ruy"
license = "Apache-2.0"
topics = ("matrix", "multiplication", "neural", "network", "AI", "tensorflow")
exports_sources = "CMakeLists.txt"
generators = "cmake"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -25,91 +27,88 @@ class RuyConan(ConanFile):
"fPIC": True,
}

_cmake = None

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

@property
def _minimum_compilers_version(self):
return {
"Visual Studio": "15",
"msvc": "191",
"gcc": "5",
"clang": "3.4",
"apple-clang": "5.1",
}

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

minimum_version = self._minimum_compilers_version.get(str(self.settings.compiler), False)
if not minimum_version:
self.output.warn("Compiler is unknown. Assuming it supports C++14.")
elif tools.Version(self.settings.compiler.version) < minimum_version:
self.output.warning("Compiler is unknown. Assuming it supports C++14.")
elif Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration("Build requires support for C++14. Minimum version for {} is {}"
.format(str(self.settings.compiler), minimum_version))

if str(self.settings.compiler) == "clang" and tools.Version(self.settings.compiler.version) <= 5 and self.settings.build_type == "Debug":
if str(self.settings.compiler) == "clang" and Version(self.settings.compiler.version) <= 5 and self.settings.build_type == "Debug":
raise ConanInvalidConfiguration("Debug builds are not supported on older versions of Clang (<=5)")

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
self.options.rm_safe("fPIC")

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

def requirements(self):
self.requires("cpuinfo/cci.20201217")
self.requires("cpuinfo/cci.20220228")

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

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["RUY_MINIMAL_BUILD"] = True
self._cmake.configure()
return self._cmake
def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.cache_variables["RUY_MINIMAL_BUILD"] = True
tc.cache_variables["RUY_FIND_CPUINFO"] = True
# Ruy public headers don't have API decorators,
# export everything to support shared libraries on Windows
tc.variables["CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS"] = True
tc.generate()

deps = CMakeDeps(self)
deps.generate()

def _patch_sources(self):
cmakelists = os.path.join(self.source_folder, "CMakeLists.txt")
patches = {
#Remove the invocation after project(), see https://github.com/google/ruy/issues/328
"cmake_minimum_required(VERSION 3.13)": "",
# Ensure `cmake_minimum_required` is called first
"# Copyright 2021 Google LLC": "# Copyright 2021 Google LLC\ncmake_minimum_required(VERSION 3.13)",
}

for pattern, patch in patches.items():
replace_in_file(self, cmakelists, pattern, patch)

def build(self):
# 1. Allow Shared builds
tools.replace_in_file(os.path.join(self._source_subfolder, "cmake", "ruy_cc_library.cmake"),
replace_in_file(self, os.path.join(self.source_folder, "cmake", "ruy_cc_library.cmake"),
"add_library(${_NAME} STATIC",
"add_library(${_NAME}"
)

# 2. Shared builds fail with undefined symbols without this fix.
# This is because ruy only links to 'cpuinfo' but it also needs 'clog' (from the same package)
cpuinfoLibs = self.deps_cpp_info["cpuinfo"].libs + self.deps_cpp_info["cpuinfo"].system_libs
libsListAsString = ";".join(cpuinfoLibs)
if int(self.version.strip('cci.')) < 20220628:
tools.replace_in_file(os.path.join(self._source_subfolder, "ruy", "CMakeLists.txt"),
"set(ruy_6_cpuinfo \"cpuinfo\")",
f"set(ruy_6_cpuinfo \"{libsListAsString}\")"
)
else:
tools.replace_in_file(os.path.join(self._source_subfolder, "ruy", "CMakeLists.txt"),
"set(ruy_6_cpuinfo_cpuinfo \"cpuinfo::cpuinfo\")",
f"set(ruy_6_cpuinfo_cpuinfo \"{libsListAsString}\")"
)

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

def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
self.copy(pattern="*.h", dst=os.path.join("include", "ruy"), src=os.path.join(self._source_subfolder, "ruy"))
self.copy(pattern="*", dst="lib", src="lib")
self.copy(pattern="*", dst="bin", src="bin")

tools.remove_files_by_mask(self.package_folder, "*.pdb")
cmake = CMake(self)
cmake.install()
copy(self, "LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.libs = ["ruy_frontend",
Expand Down
13 changes: 6 additions & 7 deletions recipes/ruy/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)
cmake_minimum_required(VERSION 3.15)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
project(test_package LANGUAGES CXX)

find_package(ruy REQUIRED)
find_package(ruy REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} ruy::ruy)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)

target_link_libraries(${PROJECT_NAME} PRIVATE ruy::ruy)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
22 changes: 15 additions & 7 deletions recipes/ruy/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake, CMakeToolchain
import os
from conans import ConanFile, CMake, tools


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package"
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):
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.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
11 changes: 11 additions & 0 deletions recipes/ruy/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

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

find_package(ruy REQUIRED)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} ruy::ruy)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
17 changes: 17 additions & 0 deletions recipes/ruy/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
from conans import ConanFile, CMake, tools


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

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)
27 changes: 27 additions & 0 deletions recipes/ruy/all/test_v1_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <cstdint>
#include <iostream>

#include "ruy/ruy.h"

int main() {

ruy::Context context;
const float lhs_data[] = {1, 2, 3, 4};
const float rhs_data[] = {1, 2, 3, 4};
float dst_data[4];

ruy::Matrix<float> lhs;
ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, lhs.mutable_layout());
lhs.set_data(lhs_data);
ruy::Matrix<float> rhs;
ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, rhs.mutable_layout());
rhs.set_data(rhs_data);
ruy::Matrix<float> dst;
ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, dst.mutable_layout());
dst.set_data(dst_data);

ruy::MulParams<float, float> mul_params;
ruy::Mul(lhs, rhs, mul_params, &context, &dst);

std::cout << "ruy's test_package ran successfully \n";
}
2 changes: 0 additions & 2 deletions recipes/ruy/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
versions:
"cci.20210622":
folder: all
"cci.20220628":
folder: all

0 comments on commit ca0d539

Please sign in to comment.