From 858a6a3c4e304bea365d461e7052a8d09cf51727 Mon Sep 17 00:00:00 2001 From: servostar Date: Sat, 18 May 2024 12:25:13 +0200 Subject: [PATCH] transioned to glib --- .gitmodules | 3 --- CMakeLists.txt | 21 +++++++-------------- Dockerfile | 1 - dep/klib | 1 - run-check-test.sh | 2 ++ sdk/Dockerfile | 2 +- tests/CMakeLists.txt | 2 +- tests/glib/CMakeLists.txt | 22 ++++++++++++++++++++++ tests/glib/glib_hashmap.c | 27 +++++++++++++++++++++++++++ tests/klib/CMakeLists.txt | 20 -------------------- tests/klib/hashmap.c | 36 ------------------------------------ 11 files changed, 60 insertions(+), 77 deletions(-) delete mode 100644 .gitmodules delete mode 160000 dep/klib create mode 100644 tests/glib/CMakeLists.txt create mode 100644 tests/glib/glib_hashmap.c delete mode 100644 tests/klib/CMakeLists.txt delete mode 100644 tests/klib/hashmap.c diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 48d9717..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "dep/klib"] - path = dep/klib - url = https://github.com/attractivechaos/klib.git diff --git a/CMakeLists.txt b/CMakeLists.txt index a0cbd38..b304447 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,25 +71,18 @@ add_custom_command(OUTPUT ${YACC_GENERATED_SOURCE_FILE} VERBATIM) # ------------------------------------------------ # -# Klib # +# Setup Glib 2.0 # # ------------------------------------------------ # -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) +find_package(PkgConfig REQUIRED) +pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0) # ------------------------------------------------ # # Source # # ------------------------------------------------ # include_directories(${PROJECT_SOURCE_DIR}/src) -include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/dep) +include_directories(PRIVATE ${GLIB_INCLUDE_DIRS}) file(GLOB_RECURSE SOURCE_FILES src/*.c) @@ -114,7 +107,7 @@ set_target_properties(release OUTPUT_NAME "gsc" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/release) -target_link_libraries(release klib) +target_link_libraries(release PkgConfig::GLIB) # FIXME: cannot compile with /O2 because of /RTC1 flag if (MSVC) @@ -148,7 +141,7 @@ set_target_properties(debug OUTPUT_NAME "gsc" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/debug) -target_link_libraries(debug klib) +target_link_libraries(debug PkgConfig::GLIB) if (MSVC) set(DEBUG_FLAGS /DEBUG) @@ -179,7 +172,7 @@ set_target_properties(check OUTPUT_NAME "gsc" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/check) -target_link_libraries(check klib) +target_link_libraries(check PkgConfig::GLIB) if (MSVC) set(CHECK_FLAGS /DEBUG /WX) diff --git a/Dockerfile b/Dockerfile index dd774f4..68265d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,6 @@ 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 deleted file mode 160000 index 4988b65..0000000 --- a/dep/klib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4988b65b9c90473d155268c8ee15f3aad546406b diff --git a/run-check-test.sh b/run-check-test.sh index a2a3ab4..bd7e5cc 100644 --- a/run-check-test.sh +++ b/run-check-test.sh @@ -9,6 +9,8 @@ echo "+--------------------------------------+" echo "| BUILDING all TARGETS |" echo "+--------------------------------------+" +cmake . + make -B if [ ! $? -eq 0 ]; then echo "===> failed to build targets" diff --git a/sdk/Dockerfile b/sdk/Dockerfile index fe3592b..b5523b3 100644 --- a/sdk/Dockerfile +++ b/sdk/Dockerfile @@ -5,7 +5,7 @@ LABEL description="base image for building the gemstone programming language com LABEL website="https://github.com/Servostar/gemstone" # install dependencies -RUN apk add build-base gcc make cmake bison flex git python3 graphviz zlib zlib-dev curl-dev +RUN apk add build-base gcc make cmake bison flex git python3 graphviz glib glib-dev # create user for build RUN adduser --disabled-password lorang diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d3447bd..1228d2b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,4 +9,4 @@ set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/tests) add_subdirectory(logging) add_subdirectory(input_file) add_subdirectory(ast) -add_subdirectory(klib) \ No newline at end of file +add_subdirectory(glib) \ No newline at end of file diff --git a/tests/glib/CMakeLists.txt b/tests/glib/CMakeLists.txt new file mode 100644 index 0000000..e3ede27 --- /dev/null +++ b/tests/glib/CMakeLists.txt @@ -0,0 +1,22 @@ +include(CTest) + +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(PRIVATE ${GLIB_INCLUDE_DIRS}) + +find_package(PkgConfig REQUIRED) +pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0) + +# ------------------------------------------------------- # +# CTEST 1 +# test Glib's hashmap + +add_executable(glib_hashmap + glib_hashmap.c) +set_target_properties(glib_hashmap + PROPERTIES + OUTPUT_NAME "glib_hashmap" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/glib) +target_link_libraries(glib_hashmap PkgConfig::GLIB) +add_test(NAME glib_hashmap + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND ${GEMSTONE_BINARY_DIR}/tests/glib/glib_hashmap) diff --git a/tests/glib/glib_hashmap.c b/tests/glib/glib_hashmap.c new file mode 100644 index 0000000..70c1e01 --- /dev/null +++ b/tests/glib/glib_hashmap.c @@ -0,0 +1,27 @@ + +#include + +int main(int argc, char* argv[]) { + + GHashTable* map = g_hash_table_new(g_str_hash, g_str_equal); + + for (int i = 0; i < argc; i++) { + int* index = malloc(sizeof(int)); + + *index = i; + + g_hash_table_insert(map, argv[i], &index); + } + + for (int i = 0; i < argc; i++) { + int* index = (int*) g_hash_table_lookup(map, argv[i]); + + g_hash_table_remove(map, argv[i]); + + free(index); + } + + g_hash_table_destroy(map); + + return 0; +} diff --git a/tests/klib/CMakeLists.txt b/tests/klib/CMakeLists.txt deleted file mode 100644 index 7542f70..0000000 --- a/tests/klib/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index abd7ab0..0000000 --- a/tests/klib/hashmap.c +++ /dev/null @@ -1,36 +0,0 @@ - -#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; -}