Skip to content

Commit

Permalink
Fix package config (#60)
Browse files Browse the repository at this point in the history
* Fix package config

The install config has been simplified.

The definitions are placed in the `dsplib/defs.h` file
generated by cmake. This solves the problem with
installing and linking the `float32` version of
the package via `find_package()'.

* Fix tests. Add `OnePlan` class for nfft=1.
  • Loading branch information
vitalsong authored Jun 19, 2024
1 parent 35839bd commit a1726de
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 87 deletions.
30 changes: 13 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(dsplib LANGUAGES CXX VERSION 0.50.0)
project(dsplib LANGUAGES CXX VERSION 0.50.1)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -25,8 +25,9 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE
"DSPLIB_FFT_CACHE_SIZE=${DSPLIB_FFT_CACHE_SIZE}")

target_include_directories(${PROJECT_NAME}
PUBLIC
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
PRIVATE lib
)
Expand All @@ -38,20 +39,19 @@ endif()

# base type select
if (DSPLIB_USE_FLOAT32)
message(STATUS "dsplib: base type of real_t: float32")
target_compile_definitions(${PROJECT_NAME} PUBLIC "DSPLIB_USE_FLOAT32")
message(STATUS "dsplib: base type of real_t = float32")
else()
message(STATUS "dsplib: base type of real_t: float64")
message(STATUS "dsplib: base type of real_t = float64")
endif()

if (DSPLIB_NO_EXCEPTIONS)
message(STATUS "dsplib: disable exceptions")
target_compile_options(${PROJECT_NAME} PRIVATE -fno-exceptions)
target_compile_definitions(${PROJECT_NAME} PUBLIC "DSPLIB_NO_EXCEPTIONS")
else()
message(STATUS "dsplib: enable exceptions")
endif()

# configure <dsplib/defs.h> file
configure_file(cmake/defs.h.in ${CMAKE_BINARY_DIR}/dsplib/defs.h)

# check warnings
if (DSPLIB_IS_ROOT AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(STATUS "dsplib: warnings guard enabled")
Expand All @@ -76,21 +76,17 @@ if (DSPLIB_IS_ROOT AND DSPLIB_BUILD_BENCHS)
add_subdirectory(benchs)
endif()

target_compile_definitions(${PROJECT_NAME} PUBLIC
DSPLIB_VERSION="${CMAKE_PROJECT_VERSION}"
DSPLIB_MAJOR_VERSION=${CMAKE_PROJECT_VERSION_MAJOR}
DSPLIB_MINOR_VERSION=${CMAKE_PROJECT_VERSION_MINOR}
DSPLIB_PATCH_VERSION=${CMAKE_PROJECT_VERSION_PATCH}
)

# install package
include(cmake/InstallTools.cmake)
include(GNUInstallDirs)

ConfigInstallTarget(${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME})

install(FILES "include/dsplib.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(FILES "${CMAKE_BINARY_DIR}/dsplib/defs.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})

install(DIRECTORY include/${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
Expand Down
5 changes: 0 additions & 5 deletions cmake/Config.cmake.in

This file was deleted.

35 changes: 0 additions & 35 deletions cmake/InstallTools.cmake

This file was deleted.

9 changes: 9 additions & 0 deletions cmake/defs.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#cmakedefine DSPLIB_NO_EXCEPTIONS
#cmakedefine DSPLIB_USE_FLOAT32

#define DSPLIB_VERSION "@CMAKE_PROJECT_VERSION@"
#define DSPLIB_MAJOR_VERSION @CMAKE_PROJECT_VERSION_MAJOR@
#define DSPLIB_MINOR_VERSION @CMAKE_PROJECT_VERSION_MINOR@
#define DSPLIB_PATCH_VERSION @CMAKE_PROJECT_VERSION_PATCH@
32 changes: 23 additions & 9 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from conan import ConanFile
from conans.tools import load
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
import re


def get_version():
try:
content = load("CMakeLists.txt")
with open("CMakeLists.txt", "r", encoding="utf-8", newline="") as handle:
content = handle.read()
version = re.search("project\(dsplib .* VERSION (.*)\)", content).group(1)
return version.strip()
except Exception as e:
return None


class DsplibConan(ConanFile):
name = "dsplib"
version = get_version()
Expand All @@ -19,11 +21,22 @@ class DsplibConan(ConanFile):
author = "Vitaly Yulis ([email protected])"
url = "https://github.com/vitalsong/dsplib"
description = "C++ DSP library for MATLAB/Octave similar programming"
topics = ("dsp", "matlab", "c++17", "sound", "radio")
topics = ("dsp", "matlab", "c++17", "audio")

settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False], "float32": [True, False]}
default_options = {"shared": False, "fPIC": True, "float32": False}
options = {
"shared": [True, False],
"fPIC": [True, False],
"float32": [True, False],
"noexcept": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"float32": False,
"noexcept": False,
}
generators = "CMakeDeps"

exports_sources = "cmake/*", "CMakeLists.txt", "lib/*", "include/*"

Expand All @@ -36,14 +49,15 @@ def layout(self):

def generate(self):
tc = CMakeToolchain(self)
if self.options.float32:
tc.variables["DSPLIB_USE_FLOAT32"] = "ON"
if self.options.noexcept:
tc.variables["DSPLIB_NO_EXCEPTIONS"] = "ON"
tc.generate()

def build(self):
cmake = CMake(self)
cmake.configure(variables = {
"BUILD_SHARED_LIBS": "ON" if self.options.shared else "OFF",
"DSPLIB_USE_FLOAT32": "ON" if self.options.float32 else "OFF",
"CMAKE_BUILD_TYPE": self.settings.build_type})
cmake.configure()
cmake.build()

def package(self):
Expand Down
2 changes: 2 additions & 0 deletions include/dsplib/throw.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <dsplib/defs.h>

#ifdef DSPLIB_NO_EXCEPTIONS

#include <iostream>
Expand Down
4 changes: 4 additions & 0 deletions include/dsplib/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <limits>
#include <iosfwd>

#include <dsplib/defs.h>

#ifndef restrict
#ifdef _MSC_VER
#define restrict __restrict
Expand Down Expand Up @@ -45,8 +47,10 @@ namespace dsplib {
//base scalar type
#ifdef DSPLIB_USE_FLOAT32
using real_t = float;
static_assert(sizeof(real_t) == 4);
#else
using real_t = double;
static_assert(sizeof(real_t) == 8);
#endif

constexpr real_t pi = 3.141592653589793238463;
Expand Down
2 changes: 1 addition & 1 deletion lib/detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PreambleDetectorImpl

std::optional<PreambleDetector::Result> process(const arr_cmplx& sig) {
if (sig.size() % frame_len() != 0) {
throw std::runtime_error("Frame len not supported");
DSPLIB_THROW("Frame len not supported");
}

const auto cx = _corr_flt.process(sig);
Expand Down
4 changes: 1 addition & 3 deletions lib/fft/fact-fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class PlanTree
public:
explicit PlanTree(int n)
: _n{n} {
if (n < 2) {
throw std::runtime_error("Plan size must be greater than 2");
}
assert(n >= 2);

const auto fac = factor(n);
if (fac.size() == 1) {
Expand Down
32 changes: 32 additions & 0 deletions lib/fft/fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,36 @@ constexpr int FFT_CACHE_SIZE = DSPLIB_FFT_CACHE_SIZE;

static_assert(FFT_CACHE_SIZE > 0);

//dumb implementation for n==1
template<typename T>
class OnePlan : public std::conditional_t<std::is_same_v<T, real_t>, BaseFftPlanR, BaseFftPlanC>
{
public:
OnePlan() = default;
virtual ~OnePlan() = default;

[[nodiscard]] virtual arr_cmplx solve(const base_array<T>& x) const final {
assert(x.size() == 1);
return x;
}

void solve(const T* x, cmplx_t* y, int n) const final {
assert(n == 1);
*y = *x;
}

[[nodiscard]] int size() const noexcept final {
return 1;
}
};

using OnePlanR = OnePlan<real_t>;
using OnePlanC = OnePlan<cmplx_t>;

std::shared_ptr<BaseFftPlanC> _get_fft_plan(int n) {
if (n == 1) {
return std::make_shared<OnePlanC>();
}
if (isprime(n)) {
return std::make_shared<PrimesFftC>(n);
}
Expand All @@ -31,6 +60,9 @@ std::shared_ptr<BaseFftPlanC> _get_fft_plan(int n) {
}

std::shared_ptr<BaseFftPlanR> _get_rfft_plan(int n) {
if (n == 1) {
return std::make_shared<OnePlanR>();
}
if (isprime(n)) {
return std::make_shared<PrimesFftR>(n);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/detector_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "tests_common.h"
#include <gtest/gtest.h>
#include <vector>

using namespace dsplib;

Expand All @@ -22,6 +21,11 @@ TEST(PreambleDetector, SingleDetect) {
ASSERT_TRUE(result.has_value());
ASSERT_EQ(result->preamble.size(), ref.size());
ASSERT_EQ(finddelay(ref, result->preamble), 0);

ASSERT_ANY_THROW({
auto x = zeros(dtc.frame_len() + 1);
auto r = dtc.process(x);
});
}

//-------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit a1726de

Please sign in to comment.