Skip to content

Commit

Permalink
(#25269) fixed_math: add version 2.0.0, fix install path
Browse files Browse the repository at this point in the history
* fixed_math: add version 2.0.0

* fix trailing spaces

* fix install path

* Update recipes/fixed_math/all/conanfile.py

---------

Co-authored-by: Abril Rincón Blanco <[email protected]>
  • Loading branch information
toge and AbrilRBS authored Sep 20, 2024
1 parent ec1db34 commit f909134
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 118 deletions.
4 changes: 4 additions & 0 deletions recipes/fixed_math/1.x.x/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources:
"1.0.2":
url: "https://github.com/arturbac/fixed_math/archive/refs/tags/v1.0.2.tar.gz"
sha256: "6c87217286e3b93ce5e1fff0bbbb08a9751d04f416cd76ddd5ddd37a8c6772cf"
143 changes: 143 additions & 0 deletions recipes/fixed_math/1.x.x/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir, replace_in_file
from conan.tools.scm import Version
from conan.tools.layout import basic_layout
from conan.tools.env import VirtualBuildEnv
from conan.tools.microsoft import is_msvc
import os

required_conan_version = ">=1.53.0"

class FixedMathConan(ConanFile):
name = "fixed_math"
description = "A High-Performance C++17 Library for Fixed-Point 48.16 Arithmetic"
license = "MIT"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/arturbac/fixed_math/"
topics = ("mathematics", "fixed-point")
package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"header_only": [True, False],
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"header_only": False,
"shared": False,
"fPIC": True,
}

@property
def _min_cppstd(self):
return 17

@property
def _compilers_minimum_version(self):
return {
"gcc": "10", # fixed_math requires __has_builtin
"clang": "7",
"apple-clang": "12",
"Visual Studio": "16",
"msvc": "192",
}

def config_options(self):
if self.settings.os == "Windows":
self.package_type = "static-library"
del self.options.fPIC

def configure(self):
if self.settings.os == "Windows":
self.options.rm_safe("shared")
if self.options.header_only:
self.options.rm_safe("shared")
self.options.rm_safe("fPIC")
self.package_type = "header-library"
elif self.options.get_safe("shared"):
self.options.rm_safe("fPIC")

def layout(self):
if self.options.header_only:
basic_layout(self, src_folder="src")
else:
cmake_layout(self, src_folder="src")

def package_id(self):
if self.info.options.header_only:
self.info.clear()

def validate(self):
if self.settings.compiler.cppstd:
check_min_cppstd(self, self._min_cppstd)
minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
)

def build_requirements(self):
if not self.options.header_only:
self.tool_requires("cmake/[>=3.21 <4]")

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

def generate(self):
if self.options.header_only:
return
tc = CMakeToolchain(self)
if is_msvc(self):
tc.variables["CMAKE_CXX_FLAGS"] = "/Zc:__cplusplus"
tc.generate()
venv = VirtualBuildEnv(self)
venv.generate(scope="build")

def build(self):
if not self.options.header_only:
# fix install path (https://github.com/arturbac/fixed_math/issues/8)
replace_in_file(self, os.path.join(self.source_folder, "fixed_lib", "CMakeLists.txt"),
"${CMAKE_INSTALL_INCLUDEDIR}/fixed_math",
"${CMAKE_INSTALL_INCLUDEDIR}/fixedmath")
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
copy(self, "LICENCE", self.source_folder, os.path.join(self.package_folder, "licenses"))
if not self.options.header_only:
cmake = CMake(self)
cmake.install()
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
else:
# fix install path (https://github.com/arturbac/fixed_math/issues/8)
copy(
self,
"*.h",
os.path.join(self.source_folder, "fixed_lib", "include", "fixedmath"),
os.path.join(self.package_folder, "include", "fixedmath"),
)
copy(
self,
"*.hpp",
os.path.join(self.source_folder, "fixed_lib", "include", "fixedmath"),
os.path.join(self.package_folder, "include", "fixedmath"),
)

def package_info(self):
if self.options.header_only:
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []
else:
self.cpp_info.libs = ["fixed_math"]
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")

self.cpp_info.set_property("cmake_file_name", "fixed_math")
self.cpp_info.set_property("cmake_target_name", "fixed_math::fixed_math")

if is_msvc(self):
self.cpp_info.cxxflags.append("/Zc:__cplusplus")
8 changes: 8 additions & 0 deletions recipes/fixed_math/1.x.x/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES CXX)

find_package(fixed_math REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE fixed_math::fixed_math)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
26 changes: 26 additions & 0 deletions recipes/fixed_math/1.x.x/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 = "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 can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
39 changes: 39 additions & 0 deletions recipes/fixed_math/1.x.x/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>

#include "fixedmath/fixed_math.hpp"
#include "fixedmath/iostream.h"

using fixedmath::fixed_t;
using fixedmath::operator ""_fix;

//fixed and all functionality is constexpr so You can declare constants see features [1]
inline constexpr fixed_t foo_constant{ fixedmath::tan( 15 * fixedmath::phi/180) };

constexpr fixed_t my_function( fixed_t value ) {
using namespace fixedmath;
return foo_constant + sin(value) / (1.41_fix - 2*cos(value) / 4);
}

int main() {
// converting to/from fixed_t
// construction from other arithmetic types is explicit
fixed_t val { 3.14 };

//- there is no implicit assignment from other types
float some_float{val};
fixed_t some_fixed{some_float};

some_fixed = fixed_t{some_float};

//- converting to other arithmetic types coud be done with static cast and is explicit
double some_double { static_cast<double>(some_fixed) };

// for constant values postfix operator _fix may be used
some_fixed = some_float * 2.45_fix; //operation with float is promoted to fixed_t
some_double = 4.15 * some_fixed; //operation with double is promoted to double

std::cout << some_fixed << '\n';
std::cout << some_double << '\n';

return 0;
}
6 changes: 3 additions & 3 deletions recipes/fixed_math/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sources:
"1.0.2":
url: "https://github.com/arturbac/fixed_math/archive/refs/tags/v1.0.2.tar.gz"
sha256: "6c87217286e3b93ce5e1fff0bbbb08a9751d04f416cd76ddd5ddd37a8c6772cf"
"2.0.0":
url: "https://github.com/arturbac/fixed_math/archive/refs/tags/v2.0.0.tar.gz"
sha256: "a02efd417592f9cb3d21fc39877aba76fe5c37b3e68b11769065202f71242f66"
112 changes: 26 additions & 86 deletions recipes/fixed_math/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir
from conan.tools.files import copy, get
from conan.tools.scm import Version
from conan.tools.layout import basic_layout
from conan.tools.env import VirtualBuildEnv
from conan.tools.microsoft import is_msvc
import os

Expand All @@ -17,58 +15,30 @@ class FixedMathConan(ConanFile):
license = "MIT"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/arturbac/fixed_math/"
topics = ("mathematics", "fixed-point")
package_type = "library"
topics = ("mathematics", "fixed-point", "header-only")
package_type = "header-library"
settings = "os", "arch", "compiler", "build_type"
options = {
"header_only": [True, False],
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"header_only": False,
"shared": False,
"fPIC": True,
}
no_copy_source = True

@property
def _min_cppstd(self):
return 17
return 23

@property
def _compilers_minimum_version(self):
return {
"gcc": "10", # fixed_math requires __has_builtin
"clang": "7",
"apple-clang": "12",
"Visual Studio": "16",
"msvc": "192",
"Visual Studio": "17",
"msvc": "193",
"gcc": "12",
"clang": "15",
"apple-clang": "16",
}

def config_options(self):
if self.settings.os == "Windows":
self.package_type = "static-library"
del self.options.fPIC

def configure(self):
if self.settings.os == "Windows":
self.options.rm_safe("shared")
if self.options.header_only:
self.options.rm_safe("shared")
self.options.rm_safe("fPIC")
self.package_type = "header-library"
elif self.options.get_safe("shared"):
self.options.rm_safe("fPIC")

def layout(self):
if self.options.header_only:
basic_layout(self, src_folder="src")
else:
cmake_layout(self, src_folder="src")
basic_layout(self, src_folder="src")

def package_id(self):
if self.info.options.header_only:
self.info.clear()
self.info.clear()

def validate(self):
if self.settings.compiler.cppstd:
Expand All @@ -79,57 +49,27 @@ def validate(self):
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
)

def build_requirements(self):
if not self.options.header_only:
self.tool_requires("cmake/[>=3.21 <4]")

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

def generate(self):
if self.options.header_only:
return
tc = CMakeToolchain(self)
if is_msvc(self):
tc.variables["CMAKE_CXX_FLAGS"] = "/Zc:__cplusplus"
tc.generate()
venv = VirtualBuildEnv(self)
venv.generate(scope="build")

def build(self):
if not self.options.header_only:
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
copy(self, "LICENCE", self.source_folder, os.path.join(self.package_folder, "licenses"))
if not self.options.header_only:
cmake = CMake(self)
cmake.install()
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
else:
copy(
self,
"*.h",
os.path.join(self.source_folder, "fixed_lib", "include", "fixedmath"),
os.path.join(self.package_folder, "include", "fixed_math"),
)
copy(
self,
"*.hpp",
os.path.join(self.source_folder, "fixed_lib", "include", "fixedmath"),
os.path.join(self.package_folder, "include", "fixed_math"),
)
copy(
self,
"*.h",
os.path.join(self.source_folder, "fixed_lib", "include"),
os.path.join(self.package_folder, "include"),
)
copy(
self,
"*.hpp",
os.path.join(self.source_folder, "fixed_lib", "include"),
os.path.join(self.package_folder, "include"),
)

def package_info(self):
if self.options.header_only:
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []
else:
self.cpp_info.libs = ["fixed_math"]
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []

self.cpp_info.set_property("cmake_file_name", "fixed_math")
self.cpp_info.set_property("cmake_target_name", "fixed_math::fixed_math")
Expand Down
4 changes: 2 additions & 2 deletions recipes/fixed_math/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.20)
project(test_package LANGUAGES CXX)

find_package(fixed_math REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE fixed_math::fixed_math)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_23)
Loading

0 comments on commit f909134

Please sign in to comment.