diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..74ce56d --- /dev/null +++ b/.cargo/config.toml @@ -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"] diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1532f32 --- /dev/null +++ b/.clang-format @@ -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 +--- + diff --git a/.gitignore b/.gitignore index d5805d3..7c0da21 100644 --- a/.gitignore +++ b/.gitignore @@ -1,40 +1,17 @@ -__pycache__/ -.gradle/ -.idea/ -.metals/ -.next/ -.parcel-cache/ .vs/ .vscode/ bin/ -binaries/ -build/ -node_modules/ -obj/ -saved/ target/ .DS_Store .env -*.crt -*.csproj *.filters -*.fsproj -*.key -*.log *.make -*.mwb.bak -*.pem *.sln *.user *.vcxproj -*.vpp.* -*.wasm *.xcodeproj *.xcworkspace Cargo.lock desktop.ini -keystore.jks -local.properties Makefile -next-env.d.ts diff --git a/README.md b/README.md index d2b11c0..d1d5378 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Tribufu Rust -Rust SDK to access Tribufu APIs and services. +Native SDK to access Tribufu APIs and services. [![Crates.io][crates-badge]][crates-url] [![MIT License][mit-badge]][mit-url] diff --git a/config/cbindgen.toml b/config/cbindgen.toml new file mode 100644 index 0000000..a71366a --- /dev/null +++ b/config/cbindgen.toml @@ -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 = [] diff --git a/examples/c/main.c b/examples/c/main.c new file mode 100644 index 0000000..8ed17df --- /dev/null +++ b/examples/c/main.c @@ -0,0 +1,14 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +#include +#include + +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; +} diff --git a/examples/c/premake5.lua b/examples/c/premake5.lua new file mode 100644 index 0000000..d66728c --- /dev/null +++ b/examples/c/premake5.lua @@ -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", + } diff --git a/examples/cpp/main.cpp b/examples/cpp/main.cpp new file mode 100644 index 0000000..4fabc92 --- /dev/null +++ b/examples/cpp/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +#include +#include + +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; +} diff --git a/examples/cpp/premake5.lua b/examples/cpp/premake5.lua new file mode 100644 index 0000000..02dcddd --- /dev/null +++ b/examples/cpp/premake5.lua @@ -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", + } diff --git a/include/tribufu.h b/include/tribufu.h new file mode 100644 index 0000000..fa4aae6 --- /dev/null +++ b/include/tribufu.h @@ -0,0 +1,6 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +#pragma once + +#include diff --git a/include/tribufu/api.h b/include/tribufu/api.h new file mode 100644 index 0000000..d31c339 --- /dev/null +++ b/include/tribufu/api.h @@ -0,0 +1,6 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +#pragma once + +#include diff --git a/include/tribufu/macros.h b/include/tribufu/macros.h new file mode 100644 index 0000000..9e73b9b --- /dev/null +++ b/include/tribufu/macros.h @@ -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 +*/ diff --git a/include/tribufu/native.h b/include/tribufu/native.h new file mode 100644 index 0000000..cd63a1c --- /dev/null +++ b/include/tribufu/native.h @@ -0,0 +1,38 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +#pragma once + +#include + +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); diff --git a/include/tribufu/platform.h b/include/tribufu/platform.h new file mode 100644 index 0000000..7aeeccc --- /dev/null +++ b/include/tribufu/platform.h @@ -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 diff --git a/include/tribufu/prelude.h b/include/tribufu/prelude.h new file mode 100644 index 0000000..b141b38 --- /dev/null +++ b/include/tribufu/prelude.h @@ -0,0 +1,7 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +#pragma once + +#include +#include +#include diff --git a/include/tribufu/std.h b/include/tribufu/std.h new file mode 100644 index 0000000..1da1e53 --- /dev/null +++ b/include/tribufu/std.h @@ -0,0 +1,26 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +#pragma once + +#include +#include +#include +#include +#include + +#ifdef TRIBUFU_CPP +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif diff --git a/premake5.lua b/premake5.lua new file mode 100644 index 0000000..b986cda --- /dev/null +++ b/premake5.lua @@ -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" diff --git a/scripts/cbindgen.ps1 b/scripts/cbindgen.ps1 new file mode 100644 index 0000000..360bbe5 --- /dev/null +++ b/scripts/cbindgen.ps1 @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +cbindgen --config ./config/cbindgen.toml --crate tribufu-sdk --output ./include/tribufu/native.h diff --git a/scripts/unix/premake.sh b/scripts/unix/premake.sh new file mode 100644 index 0000000..3fb5e5e --- /dev/null +++ b/scripts/unix/premake.sh @@ -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 diff --git a/scripts/windows/build.ps1 b/scripts/windows/build.ps1 new file mode 100644 index 0000000..bec6721 --- /dev/null +++ b/scripts/windows/build.ps1 @@ -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" diff --git a/scripts/generate.ps1 b/scripts/windows/generate.ps1 similarity index 100% rename from scripts/generate.ps1 rename to scripts/windows/generate.ps1 diff --git a/scripts/windows/premake.bat b/scripts/windows/premake.bat new file mode 100644 index 0000000..5488708 --- /dev/null +++ b/scripts/windows/premake.bat @@ -0,0 +1,3 @@ +@echo off + +call .\vendor\premake-core\windows\premake5.exe vs2022 diff --git a/scripts/publish.ps1 b/scripts/windows/publish.ps1 similarity index 100% rename from scripts/publish.ps1 rename to scripts/windows/publish.ps1 diff --git a/src/native/Cargo.toml b/src/native/Cargo.toml new file mode 100644 index 0000000..2a2ac70 --- /dev/null +++ b/src/native/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "tribufu-sdk" +version = "0.0.0" +description = "Tribufu Native" +repository = "https://github.com/tribufu/tribufu-rust" +authors = ["Tribufu "] +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"] } diff --git a/src/native/api.rs b/src/native/api.rs new file mode 100644 index 0000000..7f55162 --- /dev/null +++ b/src/native/api.rs @@ -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>> = Lazy::new(|| Mutex::new(None)); +static RUNTIME: Lazy = 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()); + }); +} diff --git a/src/native/lib.rs b/src/native/lib.rs new file mode 100644 index 0000000..aa68739 --- /dev/null +++ b/src/native/lib.rs @@ -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; diff --git a/src/native/mem.rs b/src/native/mem.rs new file mode 100644 index 0000000..945f5e5 --- /dev/null +++ b/src/native/mem.rs @@ -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); + } +} diff --git a/src/native/models.rs b/src/native/models.rs new file mode 100644 index 0000000..b1076c2 --- /dev/null +++ b/src/native/models.rs @@ -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); diff --git a/vendor/premake-core/LICENSE.txt b/vendor/premake-core/LICENSE.txt new file mode 100644 index 0000000..63962d7 --- /dev/null +++ b/vendor/premake-core/LICENSE.txt @@ -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. diff --git a/vendor/premake-core/linux/libluasocket.so b/vendor/premake-core/linux/libluasocket.so new file mode 100644 index 0000000..92ced02 Binary files /dev/null and b/vendor/premake-core/linux/libluasocket.so differ diff --git a/vendor/premake-core/linux/premake5 b/vendor/premake-core/linux/premake5 new file mode 100644 index 0000000..bcb51ce Binary files /dev/null and b/vendor/premake-core/linux/premake5 differ diff --git a/vendor/premake-core/mac/libluasocket.dylib b/vendor/premake-core/mac/libluasocket.dylib new file mode 100644 index 0000000..337fa41 Binary files /dev/null and b/vendor/premake-core/mac/libluasocket.dylib differ diff --git a/vendor/premake-core/mac/premake5 b/vendor/premake-core/mac/premake5 new file mode 100644 index 0000000..48daa61 Binary files /dev/null and b/vendor/premake-core/mac/premake5 differ diff --git a/vendor/premake-core/windows/luasocket.dll b/vendor/premake-core/windows/luasocket.dll new file mode 100644 index 0000000..5768400 Binary files /dev/null and b/vendor/premake-core/windows/luasocket.dll differ diff --git a/vendor/premake-core/windows/luasocket.exp b/vendor/premake-core/windows/luasocket.exp new file mode 100644 index 0000000..a7f2ae7 Binary files /dev/null and b/vendor/premake-core/windows/luasocket.exp differ diff --git a/vendor/premake-core/windows/luasocket.lib b/vendor/premake-core/windows/luasocket.lib new file mode 100644 index 0000000..5110174 Binary files /dev/null and b/vendor/premake-core/windows/luasocket.lib differ diff --git a/vendor/premake-core/windows/luasocket.pdb b/vendor/premake-core/windows/luasocket.pdb new file mode 100644 index 0000000..9db9e2f Binary files /dev/null and b/vendor/premake-core/windows/luasocket.pdb differ diff --git a/vendor/premake-core/windows/premake5.exe b/vendor/premake-core/windows/premake5.exe new file mode 100644 index 0000000..1a637aa Binary files /dev/null and b/vendor/premake-core/windows/premake5.exe differ diff --git a/vendor/premake-core/windows/premake5.pdb b/vendor/premake-core/windows/premake5.pdb new file mode 100644 index 0000000..ca0aaee Binary files /dev/null and b/vendor/premake-core/windows/premake5.pdb differ