Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
ColumnLimit: 0
ColumnLimit: 120
AlignEscapedNewlines: Left
11 changes: 11 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Checks: >
clang-analyzer-*,
bugprone-*,
misc-*,
readability-*,
-readability-magic-numbers,
-readability-identifier-length,
-misc-unused-parameters,
-misc-include-cleaner

HeaderFilterRegex: 'src/.*'
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
.DS_Store
*.o
.so
*.dylib
build
*log*.txt

/tmp
/build

# VSCode
.vscode/*
!.vscode/c_cpp_properties.json
!.vscode/extensions.json
!.vscode/launch.json
!.vscode/settings.json
!.vscode/tasks.json

12 changes: 12 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"configurations": [
{
"name": "Mac",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"ms-vscode.cpptools",
"ms-vscode.cmake-tools"
]
}
18 changes: 14 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-vscode.cpptools"
},
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[markdown]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true
},
"files.associations": {
"*.jsonl": "json",
".clang-format": "yaml",
},
"cSpell.words": [
"armv",
"CMSIS",
"cppcheck",
"ctest",
"Dryrun",
"eabi",
"libnewlib",
"noninteractive",
"tinyclib",
"trunc"
"trunc",
"xcrun"
],
"files.associations": {
".clang-format": "yaml",
}
}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

See https://common-changelog.org for commit guidelines.
See https://common-changelog.org for commit guidelines and https://semver.org
for versioning.

## v0.1.1 - 2025-04-27

Expand Down
19 changes: 11 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
cmake_minimum_required(VERSION 3.25)

project(tinyclib VERSION 0.1.0 LANGUAGES C)
project(tinyclib VERSION 0.1.1 LANGUAGES C)

# Set the output directories
# Settings
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)

# Dependencies
include(FetchContent)
# Sources
file(GLOB SOURCES CONFIGURE_DEPENDS src/*.c)

# Add the library (BUILD_SHARED_LIBS is handled by CMake)
add_library(tinyclib ${SOURCES})

# Set the C standard to C11 for now
# Set the C standard
set_property(TARGET tinyclib PROPERTY C_STANDARD 11)
set_property(TARGET tinyclib PROPERTY C_STANDARD_REQUIRED ON)
set_property(TARGET tinyclib PROPERTY C_EXTENSIONS OFF)
Expand Down Expand Up @@ -81,22 +84,22 @@ if(BUILD_TESTS)

# FetchContent for Unity testing framework
FetchContent_Declare(
Unity
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
GIT_TAG v2.6.1
Unity
GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git
GIT_TAG v2.6.1
)
FetchContent_MakeAvailable(Unity)
FetchContent_GetProperties(Unity)

if(NOT TARGET Unity)
add_library(Unity STATIC ${unity_SOURCE_DIR}/src/unity.c)
add_library(Unity STATIC ${unity_SOURCE_DIR}/src/unity.c)
endif()
target_include_directories(Unity PUBLIC ${unity_SOURCE_DIR}/src)

enable_testing()
foreach(t app config debug error test)
add_executable(tl_${t}_test tests/arch/generic/tl_${t}_test.c)
target_link_libraries(tl_${t}_test Unity tinyclib)
target_link_libraries(tl_${t}_test unity tinyclib)
add_test(NAME tl_${t}_test COMMAND tl_${t}_test)
endforeach()
endif()
1 change: 1 addition & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"BUILD_TESTS": "ON"
}
}
Expand Down
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.PHONY: help install build clean test format lint check check-all fix

BUILD_DIR := build
CLANG_FORMAT := $(shell if command -v clang-format >/dev/null 2>&1; then echo clang-format; fi)
CLANG_TIDY := $(shell if command -v clang-tidy >/dev/null 2>&1; then echo clang-tidy; fi)
CPPCHECK := $(shell if command -v cppcheck >/dev/null 2>&1; then echo cppcheck; fi)
CLANG_TIDY_EXTRA_ARGS := $(shell if [ "$$(uname)" = "Darwin" ]; then echo "--extra-arg=--sysroot=$$(xcrun --show-sdk-path)"; fi)

SRC_FILES := src/*.c include/*.h
TEST_FILES := tests/arch/generic/*.c
ALL_FILES := $(SRC_FILES) $(TEST_FILES)

help: ## Show available make targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf " %-15s %s\n", $$1, $$2}'

configure: ## Configure cmake
cmake --preset default

build: ## Build the project
cmake --build --preset default

clean: ## Remove build directory
@test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ]
rm -rf "$(CURDIR)/$(BUILD_DIR)"

test: ## Build and run tests
cmake --workflow --preset default

format: ## Check code formatting
@test -n "$(CLANG_FORMAT)" || { echo "error: clang-format not found"; exit 1; }
$(CLANG_FORMAT) --dry-run --Werror $(ALL_FILES) --verbose

lint: ## Check code linting
@test -n "$(CLANG_TIDY)" || { echo "error: clang-tidy not found"; exit 1; }
$(CLANG_TIDY) -p $(BUILD_DIR) $(CLANG_TIDY_EXTRA_ARGS) \
--header-filter="^$(CURDIR)/(src|include|tests)/" src/*.c tests/arch/generic/*.c

check: ## Static analysis
@test -n "$(CPPCHECK)" || { echo "error: cppcheck not found"; exit 1; }
$(CPPCHECK) --enable=warning,style,performance,portability --error-exitcode=1 \
--project=$(BUILD_DIR)/compile_commands.json --suppress=missingIncludeSystem \
-i$(BUILD_DIR)

check-all: format lint check ## Run all checks

fix: ## Fix code formatting and linting issues
@test -n "$(CLANG_FORMAT)" || { echo "error: clang-format not found"; exit 1; }
$(CLANG_FORMAT) -i $(ALL_FILES)
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# tinyclib - A tiny C library

tinyclib is a tiny C library for building applications across various platforms,
tinyclib is a tiny C library for building applications across various platforms,
from server-grade hardware to desktops, mobile, and embedded devices.

## Requirements

- [Clang](https://clang.llvm.org/) or similar C compiler
- [CMake](https://cmake.org/)
- [Ninja](https://github.com/ninja-build/ninja)

See [mise.toml](mise.toml) for exact versions used.

### Recommended tools

Expand All @@ -17,17 +20,16 @@ from server-grade hardware to desktops, mobile, and embedded devices.
## Installation

```shell
git clone https://github.com/devfacet/tinyclib.git
cd tinyclib/
cmake --workflow --preset default
make configure
make build
```

## Usage

## Test

```shell
ctest --preset default
make test
```

## Contributing
Expand All @@ -36,5 +38,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md)

## License

Licensed under The MIT License (MIT)
For the full copyright and license information, please view the LICENSE.txt file.
Licensed under The MIT License (MIT)\
For the full copyright and license information, please view the LICENSE.txt
file.
2 changes: 1 addition & 1 deletion include/tl_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @note Available for all ARM architectures.
*/
#if defined(__ARM_ARCH)
#ifdef __ARM_ARCH
#define TL_CMSIS_DSP_AVAILABLE 1
#else
#define TL_CMSIS_DSP_AVAILABLE 0
Expand Down
2 changes: 1 addition & 1 deletion include/tl_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @param level The debug level.
* @param fmt The format string.
* @param ... The arguments.
*
*
* @return void
*/
#define TL_DEBUG_PRINT(level, fmt, ...) \
Expand Down
44 changes: 22 additions & 22 deletions include/tl_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
typedef enum {
TL_ERROR_NONE = 0, // no error

TL_ERROR_INTERNAL = 10, // internal error
TL_ERROR_NOT_FOUND, // not found
TL_ERROR_NOT_READY, // not ready
TL_ERROR_NOT_IMPLEMENTED, // not implemented
TL_ERROR_NOT_SUPPORTED, // not supported
TL_ERROR_NOT_AVAILABLE, // not available

TL_ERROR_TIMEOUT = 20, // timeout error
TL_ERROR_BUSY, // busy
TL_ERROR_IO, // I/O error
TL_ERROR_OUT_OF_RANGE, // out of range error
TL_ERROR_MEMORY_ALLOCATION, // memory allocation error

TL_ERROR_INIT_FAILED = 30, // initialization error
TL_ERROR_ALREADY_INITIALIZED, // already initialized
TL_ERROR_NOT_INITIALIZED, // not initialized
TL_ERROR_INVALID_ARGUMENT, // invalid argument
TL_ERROR_INVALID_FUNCTION, // invalid function
TL_ERROR_INVALID_INSTANCE, // invalid instance
TL_ERROR_INVALID_SIZE, // invalid size
TL_ERROR_INVALID_TYPE, // invalid type
TL_ERROR_INVALID_VALUE, // invalid value
TL_ERROR_INTERNAL = 10, // internal error
TL_ERROR_NOT_FOUND = 11, // not found
TL_ERROR_NOT_READY = 12, // not ready
TL_ERROR_NOT_IMPLEMENTED = 13, // not implemented
TL_ERROR_NOT_SUPPORTED = 14, // not supported
TL_ERROR_NOT_AVAILABLE = 15, // not available

TL_ERROR_TIMEOUT = 20, // timeout error
TL_ERROR_BUSY = 21, // busy
TL_ERROR_IO = 22, // I/O error
TL_ERROR_OUT_OF_RANGE = 23, // out of range error
TL_ERROR_MEMORY_ALLOCATION = 24, // memory allocation error

TL_ERROR_INIT_FAILED = 30, // initialization error
TL_ERROR_ALREADY_INITIALIZED = 31, // already initialized
TL_ERROR_NOT_INITIALIZED = 32, // not initialized
TL_ERROR_INVALID_ARGUMENT = 33, // invalid argument
TL_ERROR_INVALID_FUNCTION = 34, // invalid function
TL_ERROR_INVALID_INSTANCE = 35, // invalid instance
TL_ERROR_INVALID_SIZE = 36, // invalid size
TL_ERROR_INVALID_TYPE = 37, // invalid type
TL_ERROR_INVALID_VALUE = 38, // invalid value
} TLErrorCode;

/**
Expand Down
2 changes: 1 addition & 1 deletion include/tl_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
*
* @return The difference between the two timespec structures in nanoseconds.
*/
long long tl_timespec_diff_ns(struct timespec *start, struct timespec *end);
long long tl_timespec_diff_ns(const struct timespec *start, const struct timespec *end);

#endif // TL_TEST_H
10 changes: 10 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# mise settings set experimental true
# mise install

[tools]
cmake = "4.3.0"
"conda:make" = "4.4.1"
ninja = "1.13.2"
"pipx:clang-format" = "latest"
"pipx:clang-tidy" = "latest"
"pipx:cppcheck" = "latest"
5 changes: 3 additions & 2 deletions src/tl_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static char **args = NULL;
void tl_init_app(int argc, char *argv[]) {
tl_parse_args(argc, argv);
if (tl_get_flag("--debug-level")) {
tl_set_debug_level(atoi(tl_get_flag("--debug-level")));
tl_set_debug_level((int)strtol(tl_get_flag("--debug-level"), NULL, 10));
}
}

Expand All @@ -25,7 +25,8 @@ void tl_parse_args(int argc, char *argv[]) {
bool tl_lookup_flag(const char *flag) {
for (int i = 1; i < arg_count; i++) {
// If the argument starts with the flag and is followed by either '\0' or '=' then
if (strncmp(args[i], flag, strlen(flag)) == 0 && (args[i][strlen(flag)] == '\0' || args[i][strlen(flag)] == '=')) {
if (strncmp(args[i], flag, strlen(flag)) == 0 &&
(args[i][strlen(flag)] == '\0' || args[i][strlen(flag)] == '=')) {
return true;
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/tl_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
#include <string.h>

void tl_error_set(TLError *error, TLErrorCode code, const char *message, ...) {
// Check and initialize error
// Ignore if error is NULL
if (!error) {
error = malloc(sizeof(TLError));
if (!error) {
return;
}
error->message = NULL;
error->message_size = 0;
return;
}

// Set error code and message
Expand Down
6 changes: 2 additions & 4 deletions src/tl_test.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// See LICENSE.txt and CONTRIBUTING.md for details.

#define _POSIX_C_SOURCE 199309L

#include "tl_test.h"
#include <time.h>

// TODO: Add tests

long long tl_timespec_diff_ns(struct timespec *start, struct timespec *end) {
return (end->tv_sec - start->tv_sec) * 1000000000LL + (end->tv_nsec - start->tv_nsec);
long long tl_timespec_diff_ns(const struct timespec *start, const struct timespec *end) {
return ((end->tv_sec - start->tv_sec) * 1000000000LL) + (end->tv_nsec - start->tv_nsec);
}
Loading
Loading