Merge Rust, C and C++ SDKs (#4)

* Generate native bindings from rust crate

* Add native instance and runtime statics

* Update README.md
This commit is contained in:
Guilherme Werner
2025-06-11 12:49:31 -03:00
committed by GitHub
parent 738f9c947b
commit a392dbb119
39 changed files with 1213 additions and 24 deletions

14
.cargo/config.toml Normal file
View File

@ -0,0 +1,14 @@
[build]
rustflags = ["--cfg", "uuid_unstable"]
[profile.dev]
lto = "off"
[profile.release]
lto = "thin"
[target.'cfg(target_os = "windows")']
rustflags = ["-C", "target-feature=+crt-static"]
[target.'cfg(target_family = "wasm")']
rustflags = ["--cfg", "web_sys_unstable_apis"]

221
.clang-format Normal file
View File

@ -0,0 +1,221 @@
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 0
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterExternBlock: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakAfterJavaFieldAnnotations: false
BreakArrays: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Custom
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: "^ IWYU pragma:"
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: ".*"
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: "(Test)?$"
IncludeIsMainSourceRegex: ""
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertNewlineAtEOF: false
InsertTrailingCommas: None
IntegerLiteralSeparator:
Binary: 0
Decimal: 0
Hex: 0
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
LineEnding: DeriveLF
MacroBlockBegin: ""
MacroBlockEnd: ""
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PackConstructorInitializers: BinPack
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Right
PPIndentWidth: -1
QualifierAlignment: Leave
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseTab: Never
WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
---

23
.gitignore vendored
View File

@ -1,40 +1,17 @@
__pycache__/
.gradle/
.idea/
.metals/
.next/
.parcel-cache/
.vs/ .vs/
.vscode/ .vscode/
bin/ bin/
binaries/
build/
node_modules/
obj/
saved/
target/ target/
.DS_Store .DS_Store
.env .env
*.crt
*.csproj
*.filters *.filters
*.fsproj
*.key
*.log
*.make *.make
*.mwb.bak
*.pem
*.sln *.sln
*.user *.user
*.vcxproj *.vcxproj
*.vpp.*
*.wasm
*.xcodeproj *.xcodeproj
*.xcworkspace *.xcworkspace
Cargo.lock Cargo.lock
desktop.ini desktop.ini
keystore.jks
local.properties
Makefile Makefile
next-env.d.ts

View File

@ -1,6 +1,6 @@
# Tribufu Rust # Tribufu Rust
Rust SDK to access Tribufu APIs and services. Native SDK to access Tribufu APIs and services.
[![Crates.io][crates-badge]][crates-url] [![Crates.io][crates-badge]][crates-url]
[![MIT License][mit-badge]][mit-url] [![MIT License][mit-badge]][mit-url]

157
config/cbindgen.toml Normal file
View File

@ -0,0 +1,157 @@
language = "C"
header = "// Copyright (c) Tribufu. All Rights Reserved.\n// SPDX-License-Identifier: MIT"
pragma_once = true
include_version = false
namespaces = []
using_namespaces = []
sys_includes = ["tribufu/prelude.h"]
includes = []
no_includes = true
cpp_compat = false
braces = "NextLine"
line_length = 100
tab_width = 4
documentation = true
documentation_style = "doxy"
style = "tag"
[defines]
"debug_assertions" = "TRIBUFU_DEBUG"
"not(debug_assertions)" = "TRIBUFU_RELEASE"
"panic = abort" = "TRIBUFU_PANIC_ABORT"
"panic = unwind" = "TRIBUFU_PANIC_UNWIND"
"proc_macro" = "TRIBUFU_PROC_MACRO"
"target_arch = aarch64" = "TRIBUFU_AARCH64"
"target_arch = arm" = "TRIBUFU_ARM"
"target_arch = armV7" = "TRIBUFU_ARMV7"
"target_arch = armv4t" = "TRIBUFU_ARMV4T"
"target_arch = armv6" = "TRIBUFU_ARMV6"
"target_arch = armv7a" = "TRIBUFU_ARMV7A"
"target_arch = armv7s" = "TRIBUFU_ARMV7S"
"target_arch = avr" = "TRIBUFU_AVR"
"target_arch = bpfeb" = "TRIBUFU_BPFEB"
"target_arch = bpfel" = "TRIBUFU_BPFEL"
"target_arch = hexagon" = "TRIBUFU_HEXAGON"
"target_arch = i386" = "TRIBUFU_I386"
"target_arch = i586" = "TRIBUFU_I586"
"target_arch = i686" = "TRIBUFU_I686"
"target_arch = m68k" = "TRIBUFU_M68K"
"target_arch = mips" = "TRIBUFU_MIPS"
"target_arch = mipsel" = "TRIBUFU_MIPSEL"
"target_arch = msp430" = "TRIBUFU_MSP430"
"target_arch = powerpc" = "TRIBUFU_POWERPC"
"target_arch = powerpc64" = "TRIBUFU_POWERPC64"
"target_arch = powerpc64l" = "TRIBUFU_POWERPC64L"
"target_arch = riscv32gc" = "TRIBUFU_RISCV32GC"
"target_arch = s390x" = "TRIBUFU_S390X"
"target_arch = sparc" = "TRIBUFU_SPARC"
"target_arch = sparc64" = "TRIBUFU_SPARC64"
"target_arch = thumbv4t" = "TRIBUFU_THUMBV4T"
"target_arch = thumbv7a" = "TRIBUFU_THUMBV7A"
"target_arch = thumbv7neon" = "TRIBUFU_THUMBV7NEON"
"target_arch = wasm32" = "TRIBUFU_WASM32"
"target_arch = wasm64" = "TRIBUFU_WASM64"
"target_arch = x86" = "TRIBUFU_X86"
"target_arch = x86_64" = "TRIBUFU_X86_64"
"target_endian = big" = "TRIBUFU_BIG_ENDIAN"
"target_endian = little" = "TRIBUFU_LITTLE_ENDIAN"
"target_env = gnu" = "TRIBUFU_GNU"
"target_env = msvc" = "TRIBUFU_MSVC"
"target_env = musl" = "TRIBUFU_MUSL"
"target_env = sgx" = "TRIBUFU_SGX"
"target_family = unix" = "TRIBUFU_UNIX"
"target_family = wasm" = "TRIBUFU_WASM"
"target_family = windows" = "TRIBUFU_MICROSOFT"
"target_feature = avx" = "TRIBUFU_AVX"
"target_feature = avx2" = "TRIBUFU_AVX2"
"target_feature = crt-static" = "TRIBUFU_CRTSTATIC"
"target_feature = rdrand" = "TRIBUFU_RDRAND"
"target_feature = sse" = "TRIBUFU_SSE"
"target_feature = sse2" = "TRIBUFU_SSE2"
"target_feature = sse4.1" = "TRIBUFU_SSE41"
"target_has_atomic = 128" = "TRIBUFU_ATOMIC_128"
"target_has_atomic = 16" = "TRIBUFU_ATOMIC_16"
"target_has_atomic = 32" = "TRIBUFU_ATOMIC_32"
"target_has_atomic = 64" = "TRIBUFU_ATOMIC_64"
"target_has_atomic = 8" = "TRIBUFU_ATOMIC_8"
"target_has_atomic = ptr" = "TRIBUFU_ATOMIC_PTR"
"target_os = android" = "TRIBUFU_ANDROID"
"target_os = dragonfly" = "TRIBUFU_DRAGONFLY"
"target_os = freebsd" = "TRIBUFU_FREEBSD"
"target_os = ios" = "TRIBUFU_IOS"
"target_os = linux" = "TRIBUFU_LINUX"
"target_os = macos" = "TRIBUFU_MACOS"
"target_os = netbsd" = "TRIBUFU_NETBSD"
"target_os = openbsd" = "TRIBUFU_OPENBSD"
"target_os = windows" = "TRIBUFU_WINDOWS"
"target_pointer_width = 16" = "TRIBUFU_POINTER_16"
"target_pointer_width = 32" = "TRIBUFU_POINTER_32"
"target_pointer_width = 64" = "TRIBUFU_POINTER_64"
"target_vendor = apple" = "TRIBUFU_APPLE"
"target_vendor = fortanix" = "TRIBUFU_FORTANIX"
"target_vendor = pc" = "TRIBUFU_MICROSOFT"
"target_vendor = unknown" = "TRIBUFU_UNKNOWN"
[export]
include = []
exclude = []
item_types = ["enums", "structs", "typedefs", "functions"]
renaming_overrides_prefixing = false
[layout]
packed = "PACKED"
aligned_n = "ALIGNED"
[fn]
prefix = "TRIBUFU_API"
args = "horizontal"
must_use = "MUST_USE_FUNC"
no_return = "NO_RETURN"
rename_args = "None"
sort_by = "Name"
[struct]
rename_fields = "None"
must_use = "MUST_USE_STRUCT"
derive_constructor = true
derive_eq = false
derive_neq = false
derive_lt = false
derive_lte = false
derive_gt = false
derive_gte = false
[enum]
rename_variants = "None"
add_sentinel = false
prefix_with_name = false
enum_class = false
derive_helper_methods = false
derive_const_casts = false
derive_mut_casts = false
must_use = "MUST_USE_ENUM"
derive_tagged_enum_destructor = false
derive_tagged_enum_copy_constructor = false
derive_tagged_enum_copy_assignment = false
private_default_tagged_enum_constructor = false
[const]
allow_static_const = true
allow_constexpr = false
sort_by = "Name"
[macro_expansion]
bitflags = true
[parse]
parse_deps = true
include = []
exclude = []
clean = false
extra_bindings = []
[parse.expand]
crates = []
all_features = true
default_features = true
features = []

14
examples/c/main.c Normal file
View File

@ -0,0 +1,14 @@
// Copyright (c) Tribufu. All Rights Reserved.
#include <stdio.h>
#include <tribufu/api.h>
int main(int argc, char **argv)
{
tribufu_api_initialize();
char *version = tribufu_api_get_user_agent();
printf("%s\n", version);
tribufu_free_string(version);
tribufu_api_shutdown();
return 0;
}

212
examples/c/premake5.lua Normal file
View File

@ -0,0 +1,212 @@
--- @diagnostic disable: undefined-global
project "example_c"
location "."
kind "ConsoleApp"
language "C"
targetdir("../../bin/%{cfg.platform}")
objdir("../../target/%{cfg.buildcfg}/obj/%{prj.name}/%{cfg.platform}")
files
{
"./**.c",
"./**.h",
"../../include/**.h",
}
vpaths
{
["Sources"] = { "./**.c" },
["Headers"] = { "./**.h", "../../include/**.h" },
}
includedirs
{
"../../include",
}
libdirs
{
"../../bin/%{cfg.platform}",
}
-- Profile
filter { "configurations:debug" }
runtime "Debug"
symbols "On"
defines
{
"DEBUG",
"TRACE",
"TRIBUFU_DEBUG",
"TRIBUFU_TRACE",
}
filter { "configurations:release" }
runtime "Release"
optimize "On"
defines
{
"RELEASE",
"TRIBUFU_RELEASE",
}
-- Platform
filter { "platforms:win-*" }
system "windows"
systemversion "latest"
staticruntime "On"
toolset "msc"
defines
{
"TRIBUFU_DESKTOP",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_WINDOWS",
}
links
{
"tribufu_sdk.lib",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:osx-*" }
system "macosx"
systemversion "10.15"
toolset "clang"
defines
{
"TRIBUFU_APPLE",
"TRIBUFU_DESKTOP",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_MAC",
}
links
{
"tribufu_sdk",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:linux-*" }
system "linux"
toolset "gcc"
defines
{
"TRIBUFU_DESKTOP",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_LINUX",
"TRIBUFU_UNIX",
}
links
{
"tribufu_sdk",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:android-*" }
system "android"
toolset "clang"
defines
{
"TRIBUFU_ANDROID",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_MOBILE",
"TRIBUFU_UNIX",
}
links
{
"tribufu_sdk",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:ios-*" }
system "ios"
systemversion "13.0"
toolset "clang"
defines
{
"TRIBUFU_APPLE",
"TRIBUFU_IOS",
"TRIBUFU_LIBRARY",
"TRIBUFU_MOBILE",
"TRIBUFU_MONOLITHIC",
}
links
{
"tribufu_sdk",
}
-- Architecture
filter { "platforms:*-x86" }
architecture "x86"
defines
{
"TRIBUFU_32BITS",
"TRIBUFU_X86",
}
filter { "platforms:*-x64" }
architecture "x64"
defines
{
"TRIBUFU_64BITS",
"TRIBUFU_X64",
}
filter { "platforms:*-arm64" }
architecture "ARM64"
defines
{
"TRIBUFU_64BITS",
"TRIBUFU_ARM64",
}

14
examples/cpp/main.cpp Normal file
View File

@ -0,0 +1,14 @@
// Copyright (c) Tribufu. All Rights Reserved.
#include <iostream>
#include <tribufu/api.h>
int main(int argc, char **argv)
{
tribufu_api_initialize();
const char *version = tribufu_api_get_user_agent();
std::cout << version << std::endl;
tribufu_free_string((char *)version);
tribufu_api_shutdown();
return 0;
}

213
examples/cpp/premake5.lua Normal file
View File

@ -0,0 +1,213 @@
--- @diagnostic disable: undefined-global
project "example_cpp"
location "."
kind "ConsoleApp"
language "C++"
cppdialect "C++20"
targetdir("../../bin/%{cfg.platform}")
objdir("../../target/%{cfg.buildcfg}/obj/%{prj.name}/%{cfg.platform}")
files
{
"./**.cpp",
"./**.h",
"../../include/**.h",
}
vpaths
{
["Sources"] = { "./**.cpp" },
["Headers"] = { "./**.h", "../../include/**.h" },
}
includedirs
{
"../../include",
}
libdirs
{
"../../bin/%{cfg.platform}",
}
-- Profile
filter { "configurations:debug" }
runtime "Debug"
symbols "On"
defines
{
"DEBUG",
"TRACE",
"TRIBUFU_DEBUG",
"TRIBUFU_TRACE",
}
filter { "configurations:release" }
runtime "Release"
optimize "On"
defines
{
"RELEASE",
"TRIBUFU_RELEASE",
}
-- Platform
filter { "platforms:win-*" }
system "windows"
systemversion "latest"
staticruntime "On"
toolset "msc"
defines
{
"TRIBUFU_DESKTOP",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_WINDOWS",
}
links
{
"tribufu_sdk.lib",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:osx-*" }
system "macosx"
systemversion "10.15"
toolset "clang"
defines
{
"TRIBUFU_APPLE",
"TRIBUFU_DESKTOP",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_MAC",
}
links
{
"tribufu_sdk",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:linux-*" }
system "linux"
toolset "gcc"
defines
{
"TRIBUFU_DESKTOP",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_LINUX",
"TRIBUFU_UNIX",
}
links
{
"tribufu_sdk",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:android-*" }
system "android"
toolset "clang"
defines
{
"TRIBUFU_ANDROID",
"TRIBUFU_DYNAMIC",
"TRIBUFU_LIBRARY",
"TRIBUFU_MOBILE",
"TRIBUFU_UNIX",
}
links
{
"tribufu_sdk",
}
prelinkcommands
{
}
postbuildcommands
{
}
filter { "platforms:ios-*" }
system "ios"
systemversion "13.0"
toolset "clang"
defines
{
"TRIBUFU_APPLE",
"TRIBUFU_IOS",
"TRIBUFU_LIBRARY",
"TRIBUFU_MOBILE",
"TRIBUFU_MONOLITHIC",
}
links
{
"tribufu_sdk",
}
-- Architecture
filter { "platforms:*-x86" }
architecture "x86"
defines
{
"TRIBUFU_32BITS",
"TRIBUFU_X86",
}
filter { "platforms:*-x64" }
architecture "x64"
defines
{
"TRIBUFU_64BITS",
"TRIBUFU_X64",
}
filter { "platforms:*-arm64" }
architecture "ARM64"
defines
{
"TRIBUFU_64BITS",
"TRIBUFU_ARM64",
}

6
include/tribufu.h Normal file
View File

@ -0,0 +1,6 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
#pragma once
#include <tribufu/api.h>

6
include/tribufu/api.h Normal file
View File

@ -0,0 +1,6 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
#pragma once
#include <tribufu/native.h>

25
include/tribufu/macros.h Normal file
View File

@ -0,0 +1,25 @@
// Copyright (c) Tribufu. All Rights Reserved.
#pragma once
#ifdef __cplusplus
#define TRIBUFU_CPP
#endif
#ifndef EXTERN_C
#ifdef TRIBUFU_CPP
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#endif
#define TRIBUFU_API EXTERN_C
/*
#ifdef TRIBUFU_LIBRARY
#define TRIBUFU_API DLLEXPORT
#else
#define TRIBUFU_API DLLIMPORT
#endif
*/

38
include/tribufu/native.h Normal file
View File

@ -0,0 +1,38 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
#pragma once
#include <tribufu/prelude.h>
typedef void TribufuApiGetUserInfoCallbackData;
typedef void (*TribufuApiGetUserInfoCallback)(void*, const TribufuApiGetUserInfoCallbackData*);
/**
* Gets the user agent string for the Tribufu API.
*/
TRIBUFU_API const char *tribufu_api_get_user_agent(void);
TRIBUFU_API void tribufu_api_get_user_info(void *context, TribufuApiGetUserInfoCallback callback);
/**
* Gets the version of the Tribufu API.
*/
TRIBUFU_API const char *tribufu_api_get_version(void);
/**
* Initialize the Tribufu API instance.
*
* This must be called before any other API functions.
*/
TRIBUFU_API bool tribufu_api_initialize(void);
/**
* Shutdown the Tribufu API instance.
*
* This must be called when the API is no longer needed.
*/
TRIBUFU_API void tribufu_api_shutdown(void);
TRIBUFU_API void tribufu_free_string(char *ptr);

View File

@ -0,0 +1,21 @@
// Copyright (c) Tribufu. All Rights Reserved.
#pragma once
#ifdef _WIN32
#ifndef DLLEXPORT
#define DLLEXPORT __declspec(dllexport)
#endif
#ifndef DLLIMPORT
#define DLLIMPORT __declspec(dllimport)
#endif
#endif
#ifdef __MACH__ || __APPLE__ || __linux__ || __FreeBSD__ || __ANDROID__
#ifndef DLLEXPORT
#define DLLEXPORT __attribute__((visibility("default")))
#endif
#ifndef DLLIMPORT
#define DLLIMPORT __attribute__((visibility("default")))
#endif
#endif

View File

@ -0,0 +1,7 @@
// Copyright (c) Tribufu. All Rights Reserved.
#pragma once
#include <tribufu/macros.h>
#include <tribufu/platform.h>
#include <tribufu/std.h>

26
include/tribufu/std.h Normal file
View File

@ -0,0 +1,26 @@
// Copyright (c) Tribufu. All Rights Reserved.
#pragma once
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef TRIBUFU_CPP
#include <algorithm>
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <future>
#include <iostream>
#include <memory>
#include <new>
#include <ostream>
#include <sstream>
#include <string>
#include <thread>
#include <utility>
#endif

31
premake5.lua Normal file
View File

@ -0,0 +1,31 @@
--- @diagnostic disable: undefined-global
workspace "tribufu"
location "."
configurations { "debug", "release" }
if _ACTION == "vs2022" then
platforms {
"win-arm64",
"win-x64",
"win-x86",
}
end
if _ACTION == "gmake2" then
platforms {
"linux-arm64",
"linux-x64",
"linux-x86",
}
end
if _ACTION == "xcode4" then
platforms {
"osx-arm64",
"osx-x64",
}
end
include "examples/c"
include "examples/cpp"

3
scripts/cbindgen.ps1 Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
cbindgen --config ./config/cbindgen.toml --crate tribufu-sdk --output ./include/tribufu/native.h

10
scripts/unix/premake.sh Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env sh
if [ "$(expr substr $(uname -s) 1 5)" = "Linux" ]
then
./vendor/premake-core/linux/premake5 gmake2
elif [ "$(uname)" = "Darwin" ]
then
./vendor/premake-core/mac/premake5 xcode4
fi

12
scripts/windows/build.ps1 Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env pwsh
echo "Building for win-x64"
cargo build --workspace
New-Item "bin/win-x64" -ItemType Directory -Force
Remove-Item -Path "bin/win-x64/*" -Force -Recurse
Copy-Item -Path "target/debug/tribufu_sdk.dll.lib" -Destination "bin/win-x64/tribufu_sdk.lib"
Copy-Item -Path "target/debug/tribufu_sdk.dll" -Destination "bin/win-x64/tribufu_sdk.dll"
msbuild /p:Configuration="debug" /p:Platform="win-x64"

View File

@ -0,0 +1,3 @@
@echo off
call .\vendor\premake-core\windows\premake5.exe vs2022

21
src/native/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
name = "tribufu-sdk"
version = "0.0.0"
description = "Tribufu Native"
repository = "https://github.com/tribufu/tribufu-rust"
authors = ["Tribufu <contact@tribufu.com>"]
license = "UNLICENSED"
edition = "2021"
publish = false
[lib]
name = "tribufu_sdk"
crate-type = ["rlib", "cdylib", "staticlib"]
path = "lib.rs"
[dependencies]
tribufu = { path = "../../" }
futures = "0.3.31"
libc = "0.2.172"
once_cell = "1.19.0"
tokio = { version = "1.45.1", features = ["full"] }

88
src/native/api.rs Normal file
View File

@ -0,0 +1,88 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: UNLICENSED
use crate::models::{TribufuApiCallbackContext, TribufuApiGetUserInfoCallback};
use futures::lock::Mutex;
use libc::{c_char, c_void};
use once_cell::sync::Lazy;
use std::ffi::CString;
use std::ptr;
use tokio::runtime::Runtime;
use tribufu::apis::tribufu_generated_api::TribufuGeneratedApiClient;
use tribufu::TribufuApi;
static INSTANCE: Lazy<Mutex<Option<TribufuGeneratedApiClient>>> = Lazy::new(|| Mutex::new(None));
static RUNTIME: Lazy<Runtime> = Lazy::new(|| Runtime::new().unwrap());
/// Gets the version of the Tribufu API.
#[no_mangle]
pub extern "C" fn tribufu_api_get_version() -> *const c_char {
CString::new(TribufuApi::get_version()).unwrap().into_raw()
}
/// Gets the user agent string for the Tribufu API.
#[no_mangle]
pub extern "C" fn tribufu_api_get_user_agent() -> *const c_char {
CString::new(TribufuApi::get_user_agent())
.unwrap()
.into_raw()
}
/// Initialize the Tribufu API instance.
///
/// This must be called before any other API functions.
#[no_mangle]
pub extern "C" fn tribufu_api_initialize() -> bool {
let api = TribufuApi::from_env_or_default(None);
if INSTANCE.try_lock().is_none() {
return false;
}
let mut instance = INSTANCE.try_lock().unwrap();
*instance = Some(api);
true
}
/// Shutdown the Tribufu API instance.
///
/// This must be called when the API is no longer needed.
#[no_mangle]
pub extern "C" fn tribufu_api_shutdown() {
if INSTANCE.try_lock().is_none() {
return;
}
let mut instance = INSTANCE.try_lock().unwrap();
*instance = None;
}
#[no_mangle]
#[allow(unused_variables)]
pub extern "C" fn tribufu_api_get_user_info(
context: *mut c_void,
callback: TribufuApiGetUserInfoCallback,
) {
let context = TribufuApiCallbackContext(context);
RUNTIME.spawn(async move {
let mut instance = INSTANCE.lock().await;
if instance.is_none() {
callback(context.as_ptr(), ptr::null());
return;
}
let api = instance.as_mut();
if api.is_none() {
callback(context.as_ptr(), ptr::null());
return;
}
let api = api.unwrap();
// TODO: Implement logic here
callback(context.as_ptr(), ptr::null());
});
}

8
src/native/lib.rs Normal file
View File

@ -0,0 +1,8 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: UNLICENSED
#![allow(dead_code)]
pub mod api;
pub mod mem;
pub mod models;

16
src/native/mem.rs Normal file
View File

@ -0,0 +1,16 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: UNLICENSED
use std::ffi::CString;
use std::os::raw::c_char;
#[no_mangle]
pub extern "C" fn tribufu_free_string(ptr: *mut c_char) {
if ptr.is_null() {
return;
}
unsafe {
let _ = CString::from_raw(ptr);
}
}

19
src/native/models.rs Normal file
View File

@ -0,0 +1,19 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
use libc::c_void;
pub struct TribufuApiCallbackContext(pub(crate) *mut c_void);
impl TribufuApiCallbackContext {
pub(crate) fn as_ptr(&self) -> *mut c_void {
self.0
}
}
unsafe impl Send for TribufuApiCallbackContext {}
unsafe impl Sync for TribufuApiCallbackContext {}
pub type TribufuApiGetUserInfoCallbackData = c_void;
pub type TribufuApiGetUserInfoCallback =
extern "C" fn(*mut c_void, *const TribufuApiGetUserInfoCallbackData);

27
vendor/premake-core/LICENSE.txt vendored Normal file
View File

@ -0,0 +1,27 @@
Copyright (c) 2003-2022 Jason Perkins and individual contributors.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of Premake nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Binary file not shown.

BIN
vendor/premake-core/linux/premake5 vendored Normal file

Binary file not shown.

Binary file not shown.

BIN
vendor/premake-core/mac/premake5 vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
vendor/premake-core/windows/premake5.exe vendored Normal file

Binary file not shown.

BIN
vendor/premake-core/windows/premake5.pdb vendored Normal file

Binary file not shown.