Skip to content

Commit

Permalink
build: split astl into public and private, closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Nov 4, 2024
1 parent 826c2a4 commit 6b42581
Show file tree
Hide file tree
Showing 39 changed files with 216 additions and 23 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ mark_as_advanced(AC_BUILD_TESTS AC_BUILD_EXAMPLES AC_BUILD_POC)
#################
# global packages

CPMAddPackage(gh:iboB/[email protected])
CPMAddPackage(gh:iboB/[email protected])
CPMAddPackage(gh:iboB/[email protected])

Expand Down
2 changes: 1 addition & 1 deletion acord/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
ac_dep(boost)
ac_dep(xec)

add_subdirectory(ahttp)
# add_subdirectory(ahttp)
# add_subdirectory(asset-mgr)
4 changes: 4 additions & 0 deletions cmake/deps/splat.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
CPMAddPackage(gh:iboB/[email protected])
3 changes: 2 additions & 1 deletion common/ac-audio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
ac_dep(splat)

CPMAddPackage(
NAME dr_libs
Expand All @@ -23,7 +24,7 @@ target_link_libraries(ac-audio
splat::splat
itlib::itlib
PRIVATE
ac::astl
ac::astl-private
dr_libs
)
target_sources(ac-audio
Expand Down
6 changes: 2 additions & 4 deletions common/astl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
add_library(astl INTERFACE)
add_library(ac::astl ALIAS astl)
target_include_directories(astl INTERFACE include)
target_link_libraries(astl INTERFACE itlib::itlib splat::splat)
add_subdirectory(public)
add_subdirectory(private)

ac_add_test_subdir()
7 changes: 7 additions & 0 deletions common/astl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ C++ header-only libraries which are mostly STL extensions.

Think of this as alpaca-core boost (though much more humble)

This directory defines two targets

* `astl-public`: A library which is suitable to be exposed to plugins. It has no external dependencies other than the standard library.
* `astl-private`: A library which is suitable to be used internally by the project (or plugins). It depends on `astl-public` and third party libraries.

> IMPORTANT!
> Do not publicly expose `astl-private` in the Language API. It is a recipe for target clashes and ODR violations.
9 changes: 9 additions & 0 deletions common/astl/private/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ac_dep(splat)
add_library(astl INTERFACE)
add_library(ac::astl-private ALIAS astl)
target_include_directories(astl INTERFACE .)
target_link_libraries(astl INTERFACE
splat::splat
itlib::itlib
ac::astl-public
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions common/astl/public/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library(astl-public INTERFACE)
add_library(ac::astl-public ALIAS astl-public)
target_include_directories(astl-public INTERFACE .)

# DO NOT USE THIS
# The library must remain without any dependencies
# Any required dependencies must be copied inside
# target_link_libraries(astl INTERFACE )
File renamed without changes.
19 changes: 19 additions & 0 deletions common/astl/public/astl/symbol_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Borislav Stanimirov
// SPDX-License-Identifier: MIT
//

// copied from https://github.com/iboB/splat/tree/master/splat

// Intentionally leave no include guard
// Thus one can define symbols before including

// Shared library interface
#if !defined(SYMBOL_IMPORT)
# if defined(_WIN32)
# define SYMBOL_EXPORT __declspec(dllexport)
# define SYMBOL_IMPORT __declspec(dllimport)
# else
# define SYMBOL_EXPORT __attribute__((__visibility__("default")))
# define SYMBOL_IMPORT
# endif
#endif
File renamed without changes.
File renamed without changes.
140 changes: 140 additions & 0 deletions common/astl/public/astl/ufunction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// itlib-ufunction v1.02
//
// Unique Function
// Non-copyable and noexcept move-constructible replacement for std::function
// Similar to C++23's move_only_function
//
// SPDX-License-Identifier: MIT
// MIT License:
// Copyright(c) 2020 Borislav Stanimirov
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files(the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and / or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions :
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// VERSION HISTORY
//
// 1.02 (2024-09-24) Allow binding to copies of source functions as per C++23
// 1.01 (2022-09-23) Allow ufunction from a free function
// 1.00 (2020-10-15) Initial release
//
//
// DOCUMENTATION
//
// Simply include this file wherever you need.
// It defines the class itlib::ufunction. It can serve as a replacement
// of std::function, but it differs in the following two ways:
//
// 1. itlib::ufunction is not copyable. Thus you can capture non-copyable
// values from the outside world, like std::unique_ptr, or wrap other
// non-copyable function objects.
// 2. itlib::ufunction is noexcept move-constructible, thus vectors of
// ufunction won't copy when expanded and structures with ufunction members
// will not be implicitly no-noexcept move-constructuble
//
// You can use itlib::ufunction in most places where you would use
// std::function as long as you don't copy it
//
// ufunction is essentially equivalent to std::move_only_function from C++23
//
// Example:
//
// std::unique_ptr<foo> fp;
// itlib::ufunction<void()> = [captured = std::move(fp)]() { ... }
//
//
// TESTS
//
// You can find unit tests in the official repo:
// https://github.com/iboB/itlib/blob/master/test/
//
#pragma once

#include <functional>
#include <type_traits>

namespace astl
{

template <typename F>
class ufunction : private std::function<F>
{
using function = std::function<F>;
public:
ufunction() noexcept = default;

ufunction(std::nullptr_t) noexcept : function(nullptr) {}
ufunction& operator=(std::nullptr_t) noexcept { function::operator=(nullptr); return *this; }

ufunction(const ufunction&) = delete;
ufunction operator=(const ufunction&) = delete;

ufunction(ufunction&&) noexcept = default;
ufunction& operator=(ufunction&&) noexcept = default;

template <typename FO>
ufunction(FO f) noexcept : function(copy_wrapper<FO>{std::move(f)}) {}

template <typename FO>
ufunction& operator=(FO f) noexcept
{
function::operator=(copy_wrapper<FO>{std::move(f)});
return *this;
}

// function pointer overloads (otherwise clang and gcc complain for const_cast of function pointers)
// noexcept since we're relying on small function opti
ufunction(F* fptr) noexcept : function(fptr) {}
ufunction& operator=(F* fptr) noexcept
{
function::operator=(fptr);
return *this;
}

using function::operator bool;
using function::operator();
private:
template <typename FO>
struct copy_wrapper
{
static_assert(!std::is_const<FO>::value, "Cannot bind to a const function");
FO func_object;
copy_wrapper(FO&& f) : func_object(std::move(f)) {}

// we need these copy ops in order for our parent std::function to compile, but they will never be called
copy_wrapper(const copy_wrapper& other) : func_object(const_cast<FO&&>(other.func_object)) { throw 0; } // should never get to here
copy_wrapper& operator=(const copy_wrapper&) { throw 0; } // should never get to here

// not `= default` so we can force noexcept
copy_wrapper(copy_wrapper&& other) noexcept : func_object(std::move(other.func_object)) {}
copy_wrapper& operator=(copy_wrapper&& other) noexcept
{
func_object = std::move(other.func_object);
return *this;
}

template <typename... Args>
auto operator()(Args&&... args) -> decltype(func_object(std::forward<Args>(args)...))
{
return func_object(std::forward<Args>(args)...);
}
};
};

}
2 changes: 2 additions & 0 deletions common/astl/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# SPDX-License-Identifier: MIT
#
macro(astl_test test)
# just test everything with astl-private
# we're not testing for api leakage here
add_doctest_lib_test(${test} astl t-${test}.cpp)
endmacro()

Expand Down
3 changes: 0 additions & 3 deletions dict/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@
add_library(ac-dict INTERFACE)
add_library(ac::dict ALIAS ac-dict)
target_include_directories(ac-dict INTERFACE .)
target_link_libraries(ac-dict INTERFACE
ac::astl
)
4 changes: 3 additions & 1 deletion dict/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
add_doctest_lib_test(core ac-dict t-Dict.cpp)
add_doctest_lib_test(core ac-dict t-Dict.cpp
LIBRARIES ac::astl-private
)
2 changes: 2 additions & 0 deletions inference/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# message(STATUS "${CMAKE_PROJECT_NAME}: system Vulkan found")
# endif()

ac_dep(splat)

find_package(CUDAToolkit)
if(CUDAToolkit_FOUND)
set(haveCuda YES)
Expand Down
2 changes: 1 addition & 1 deletion inference/dummy/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ target_link_libraries(ac-dummy
jalog::jalog
PUBLIC
splat::splat
ac::astl
ac::astl-private
)
target_sources(ac-dummy PRIVATE
ac/dummy/export.h
Expand Down
2 changes: 1 addition & 1 deletion inference/llama.cpp/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ target_link_libraries(ac-llama
jalog::jalog
PUBLIC
splat::splat
ac::astl
ac::astl-private
)
target_sources(ac-llama PRIVATE
ac/llama/export.h
Expand Down
2 changes: 1 addition & 1 deletion inference/whisper/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ target_link_libraries(ac-whisper
jalog::jalog
PUBLIC
splat::splat
ac::astl
ac::astl-private
)
target_sources(ac-whisper PRIVATE
ac/whisper/export.h
Expand Down
2 changes: 2 additions & 0 deletions local/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ target_include_directories(ac-local INTERFACE .)
target_link_libraries(ac-local
PRIVATE
jalog::jalog
ac::astl-private
PUBLIC
ac::astl-public
ac::dict
ac::schema
)
Expand Down
6 changes: 3 additions & 3 deletions local/code/ac/local/ProgressCb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT
//
#pragma once
#include <itlib/ufunction.hpp> // C++23: replace with std::move_only_function
#include <astl/ufunction.hpp> // C++23: replace with std::move_only_function
#include <string_view>

namespace ac::local {
Expand All @@ -11,14 +11,14 @@ namespace ac::local {
///
/// This function is called by the SDK to report progress during a synchronous local operation.
///
/// It uses `itlib::ufunction` which is a C++11 implementation of C++23's `std::move_only_function` (ie you can capture
/// It uses `astl::ufunction` which is a C++11 implementation of C++23's `std::move_only_function` (ie you can capture
/// move-only types).
///
/// @param tag A string view representing a tag or category for the progress update. Since a task may be composed of
/// multiple unrelated sub-tasks, each with its own progress, the tag can be used to differentiate them.
/// @param progress A float between 0 and 1.
/// @return A boolean value indicating whether the operation should continue. Return false to cancel the operation.
/// @ingroup cpp-local
using ProgressCb = itlib::ufunction<bool(std::string_view tag, float progress)>;
using ProgressCb = astl::ufunction<bool(std::string_view tag, float progress)>;

} // namespace ac::local
2 changes: 1 addition & 1 deletion local/code/ac/local/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT
//
#pragma once
#include <splat/symbol_export.h>
#include <astl/symbol_export.h>

#if AC_LOCAL_SHARED
# if BUILDING_AC_LOCAL
Expand Down
1 change: 1 addition & 0 deletions local/schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ac_dep(magic_enum)
add_library(ac-schema INTERFACE)
add_library(ac::schema ALIAS ac-schema)
target_link_libraries(ac-schema INTERFACE
ac::astl-private
ac::dict
magic_enum::magic_enum
)
Expand Down
8 changes: 4 additions & 4 deletions poc/sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
ac_dep(test-data-llama)

add_executable(ac-sandbox sandbox.cpp)
target_link_libraries(ac-sandbox
ac::http
)
# add_executable(ac-sandbox sandbox.cpp)
# target_link_libraries(ac-sandbox
# ac::http
# )
2 changes: 1 addition & 1 deletion poc/xec-coro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ ac_dep(xec)
add_executable(poc-xec-coro poc-xec-coro.cpp)
target_link_libraries(poc-xec-coro PRIVATE
xec::xec
ac::astl
ac::astl-private
)
set_target_properties(poc-xec-coro PROPERTIES FOLDER poc)
1 change: 1 addition & 0 deletions wrapper/c/dict/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_link_libraries(ac-c-dict
ac-c::util
PRIVATE
ac::dict
ac::astl-private
)
target_sources(ac-c-dict PRIVATE
ac/export.h
Expand Down
1 change: 1 addition & 0 deletions wrapper/java/ac-jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_link_libraries(ac-jni-dict PUBLIC
JNI::JNI
mapbox::jni.hpp
ac::dict
ac::astl-private
)

# main module
Expand Down

0 comments on commit 6b42581

Please sign in to comment.