Skip to content

Commit

Permalink
feat: basic CLI program (#16)
Browse files Browse the repository at this point in the history
* feat: basic CLI program

* chore: makefile cli targets

* chore: add fmt targets for CLI

* ci: add workflow to check fmt and build cli

* style: cli fmt

* refactor: fmt receive root_dir via param

* docs: cli makefile targets

* ci: rename cli workflow

* ci: run workflows when related files are modified only

* ci: update names
  • Loading branch information
MarcosNicolau authored Jan 7, 2025
1 parent eed4315 commit 2bee5ec
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
name: "Compile and test"
name: "LIBS"

on:
push:
branches: [main]
pull_request:
branches: ["*"]
paths:
- "libs/**"

jobs:
compile_and_test:
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/cli_build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "CLI"

on:
push:
branches: [main]
pull_request:
branches: ["*"]
paths:
- "cli/**"

jobs:
compile_and_test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: "Check format"
run: make cli_check_fmt
- name: "Compile"
run: make cli_build
26 changes: 24 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ PREFIX = /usr/local
INSTALL_LIB_DIR = $(PREFIX)/lib
INSTALL_INCLUDE_DIR = $(PREFIX)/include/$(PROJECT_NAME)

# CLI
CLI_SRC = cli/almunecar.c
CLI_OUTPUT = $(BUILD_DIR)/$(PROJECT_NAME)
CLI_OUTPUT_INSTALL = $(HOME)/.local/bin/$(PROJECT_NAME)

# Compiler settings
CC = gcc
CFLAGS = -fPIC -Wall -Wextra -std=c99
Expand Down Expand Up @@ -82,10 +87,27 @@ test_%: build $(TESTS_BUILD_DIR)
done

check_fmt: ## Checks formatting and outputs the diff
@./scripts/fmt.sh
@./scripts/fmt.sh libs

fmt: ## Formats the code
@./scripts/fmt.sh --fix
@./scripts/fmt.sh libs --fix

clean: ## Clean build file
rm -rf $(BUILD_DIR)

__CLI__:
cli_check_fmt: ## Checks formatting for CLI code
@./scripts/fmt.sh cli

cli_fmt: ## Formats the CLI code
@./scripts/fmt.sh cli --fix

cli_build: build ## Build cli program
@$(CC) $(CFLAGS) -I$(INCLUDE_BUILD_DIR) -L$(LIB_BUILD_DIR) \
$(CLI_SRC) $(patsubst %, -l%, $(LIBS)) -o $(CLI_OUTPUT)

cli_install: cli_build ## Build cli program and install it globally in your system
cp $(CLI_OUTPUT) $(CLI_OUTPUT_INSTALL)

cli_uninstall: ## Uninstall cli program from your system
rm $(CLI_OUTPUT_INSTALL)
18 changes: 18 additions & 0 deletions cli/almunecar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <primitive-types/u256.h>

int main(int argc, char **argv) {
if (argc < 3) {
printf("You must provide two decimal strings.");
return 1;
}

u256 first = u256_from_dec_string(argv[1]);
u256 second = u256_from_dec_string(argv[2]);

u256_overflow_op res = u256_overflow_add(first, second);

printf("RESULT:\n");
u256_println(res.res);

return 0;
}
4 changes: 2 additions & 2 deletions scripts/fmt.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

FORMATTER="clang-format"
ROOT_DIR="libs"
ROOT_DIR=$1
EXIT_CODE=0

if [ "$1" == "--fix" ]; then
if [ "$2" == "--fix" ]; then
echo "Auto-fixing formatting issues..."
for file in $(find "$ROOT_DIR" -type f \( -name '*.c' -o -name '*.h' \)); do
echo "Formatting $file..."
Expand Down

0 comments on commit 2bee5ec

Please sign in to comment.