From 6d03b97b9c5da64ea8d47ef412007692581defda Mon Sep 17 00:00:00 2001 From: servostar Date: Fri, 17 May 2024 15:39:07 +0200 Subject: [PATCH] added klib as external library --- .env | 1 + .github/workflows/build-check-sdk.yaml | 6 ++-- CMakeLists.txt | 27 +++++++++++++- Dockerfile | 5 +-- dep/klib | 1 + run-docker-build.sh | 50 ++++++++++++++++++++++++++ sdk/Dockerfile | 4 +-- tests/CMakeLists.txt | 3 +- tests/klib/CMakeLists.txt | 20 +++++++++++ tests/klib/hashmap.c | 36 +++++++++++++++++++ 10 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 .env create mode 160000 dep/klib create mode 100755 run-docker-build.sh create mode 100644 tests/klib/CMakeLists.txt create mode 100644 tests/klib/hashmap.c diff --git a/.env b/.env new file mode 100644 index 0000000..e09c958 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +SDK=0.2.4-alpine-3.19.1 \ No newline at end of file diff --git a/.github/workflows/build-check-sdk.yaml b/.github/workflows/build-check-sdk.yaml index b17b734..e7a9561 100644 --- a/.github/workflows/build-check-sdk.yaml +++ b/.github/workflows/build-check-sdk.yaml @@ -1,14 +1,12 @@ name: "Build check gemstone in SDK" run-name: SDK build check to ${{ inputs.deploy_target }} by @${{ github.actor }} on: [push, pull_request] -env: - SDK: 0.2.3-alpine-3.19.1 jobs: build-check-sdk: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup SDK - run: docker pull servostar/gemstone:sdk-"$SDK" && docker build --tag gemstone:devkit-"$SDK" . + run: docker pull servostar/gemstone:sdk-"$SDK" - name: Compile - run: docker run gemstone:devkit-"$SDK" sh run-check-test.sh + run: run-check-test.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 213143c..a0cbd38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 3.15...3.25) # Header must be included this way: #include # # ├─ res +# ├─ dep +# │ └─ klib # ├─ src # │ ├─ lex # │ │ └─ lexer.l @@ -68,17 +70,34 @@ add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} COMMENT "generate C source file for parser" VERBATIM) +# ------------------------------------------------ # +# Klib # +# ------------------------------------------------ # + +file(GLOB KLIB_SOURCE_FILES ${PROJECT_SOURCE_DIR}/dep/klib/*.c) + +add_library(klib + STATIC + ${KLIB_SOURCE_FILES}) + +set_target_properties(klib + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/dep) + # ------------------------------------------------ # # Source # # ------------------------------------------------ # +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/dep) + file(GLOB_RECURSE SOURCE_FILES src/*.c) # define default compile flags if (MSVC) set(FLAGS /Wall /W3 /permissive) else() - set(FLAGS -Wall -Wextra -Wconversion -Wpedantic) + set(FLAGS -Wall -Wextra -Wpedantic) endif() # ------------------------------------------------ # @@ -95,6 +114,8 @@ set_target_properties(release OUTPUT_NAME "gsc" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/release) +target_link_libraries(release klib) + # FIXME: cannot compile with /O2 because of /RTC1 flag if (MSVC) set(RELEASE_FLAGS) @@ -127,6 +148,8 @@ set_target_properties(debug OUTPUT_NAME "gsc" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/debug) +target_link_libraries(debug klib) + if (MSVC) set(DEBUG_FLAGS /DEBUG) else() @@ -156,6 +179,8 @@ set_target_properties(check OUTPUT_NAME "gsc" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/check) +target_link_libraries(check klib) + if (MSVC) set(CHECK_FLAGS /DEBUG /WX) else() diff --git a/Dockerfile b/Dockerfile index a8c2f7f..0693935 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,11 @@ -FROM servostar/gemstone:sdk-0.2.3-alpine-3.19.1 +FROM servostar/gemstone:sdk-0.2.4-alpine-3.19.1 LABEL authors="servostar" -LABEL version="0.2.3" +LABEL version="0.2.4" LABEL description="docker image for setting up the build pipeline on SDK" LABEL website="https://github.com/Servostar/gemstone" COPY --chown=lorang src /home/lorang/src +COPY --chown=lorang dep /home/lorang/dep COPY --chown=lorang tests /home/lorang/tests COPY --chown=lorang CMakeLists.txt /home/lorang/ COPY --chown=lorang run-check-test.sh /home/lorang/ diff --git a/dep/klib b/dep/klib new file mode 160000 index 0000000..4988b65 --- /dev/null +++ b/dep/klib @@ -0,0 +1 @@ +Subproject commit 4988b65b9c90473d155268c8ee15f3aad546406b diff --git a/run-docker-build.sh b/run-docker-build.sh new file mode 100755 index 0000000..9a6635a --- /dev/null +++ b/run-docker-build.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env sh + +# Author: Sven Vogel +# Created: 17.05.2024 +# Description: Builds the Dockerfile for SDK and DEVKIT + +echo "+--------------------------------------+" +echo "| CHECKING prelude |" +echo "+--------------------------------------+" + +source ./.env + +if [ -z "$SDK" ]; then + echo "no SDK specified" + exit 1 +fi + +echo "+--------------------------------------+" +echo "| BUILDING SDK |" +echo "+--------------------------------------+" + +docker build --tag servostar/gemstone:sdk-"$SDK" sdk/. +if [ ! $? -eq 0 ]; then + echo "===> failed to build sdk" + exit 1 +fi + +echo "+--------------------------------------+" +echo "| BUILDING DEVKIT |" +echo "+--------------------------------------+" + +docker build --tag servostar/gemstone:devkit-"$SDK" . +if [ ! $? -eq 0 ]; then + echo "===> failed to build devkit" + exit 1 +fi + +echo "+--------------------------------------+" +echo "| RUNNING check test |" +echo "+--------------------------------------+" + +docker run -it servostar/gemstone:devkit-"$SDK" sh run-check-test.sh +if [ ! $? -eq 0 ]; then + echo "===> failed to build devkit" + exit 1 +fi + +echo "+--------------------------------------+" +echo "| DONE |" +echo "+--------------------------------------+" diff --git a/sdk/Dockerfile b/sdk/Dockerfile index aae7438..fe3592b 100644 --- a/sdk/Dockerfile +++ b/sdk/Dockerfile @@ -1,11 +1,11 @@ FROM alpine:3.19.1 LABEL authors="servostar" -LABEL version="0.2.3" +LABEL version="0.2.4" LABEL description="base image for building the gemstone programming language compiler" LABEL website="https://github.com/Servostar/gemstone" # install dependencies -RUN apk add build-base gcc make cmake bison flex git python3 graphviz +RUN apk add build-base gcc make cmake bison flex git python3 graphviz zlib zlib-dev curl-dev # create user for build RUN adduser --disabled-password lorang diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b5c1111..d3447bd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,4 +8,5 @@ set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/tests) add_subdirectory(logging) add_subdirectory(input_file) -add_subdirectory(ast) \ No newline at end of file +add_subdirectory(ast) +add_subdirectory(klib) \ No newline at end of file diff --git a/tests/klib/CMakeLists.txt b/tests/klib/CMakeLists.txt new file mode 100644 index 0000000..7542f70 --- /dev/null +++ b/tests/klib/CMakeLists.txt @@ -0,0 +1,20 @@ +include(CTest) + +include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/dep) +include_directories(${PROJECT_SOURCE_DIR}/src) + +# ------------------------------------------------------- # +# CTEST 1 +# test build configuration and dependency of Klib + +add_executable(klib_hashmap + hashmap.c) +set_target_properties(klib_hashmap + PROPERTIES + OUTPUT_NAME "klib_hashmap" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/klib) +target_link_libraries(klib_hashmap klib) +target_compile_options(klib_hashmap PUBLIC -Wall -Wextra -Wpedantic -Werror) + add_test(NAME klib_hashmap + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND ${GEMSTONE_BINARY_DIR}/tests/klib/klib_hashmap) diff --git a/tests/klib/hashmap.c b/tests/klib/hashmap.c new file mode 100644 index 0000000..abd7ab0 --- /dev/null +++ b/tests/klib/hashmap.c @@ -0,0 +1,36 @@ + +#include + +struct Book { + const char* author; + size_t pages; +}; + +KHASH_MAP_INIT_STR(books, struct Book) + +void put(const char* key, const struct Book book, khash_t(books)* books) { + khint_t idx; + int ret; + + idx = kh_put(books, books, key, &ret); + + if (!ret) + kh_del(books, books, idx); + + kh_value(books, idx) = book; +} + +int main(void) { + + khash_t(books) *map = kh_init(books); + + struct Book book; + book.author = "Bob"; + book.pages = 45; + + put("Pharao of Egypt", book, map); + + kh_destroy(books, map); + + return 0; +}