Browse Source

Initial cmake / tasks setup

Kajetan Johannes Hammerle 1 month ago
commit
0a4106d151
7 changed files with 498 additions and 0 deletions
  1. 3 0
      .clangd
  2. 10 0
      .gitignore
  3. 220 0
      CMakeLists.txt
  4. 16 0
      src/Logger.c
  5. 176 0
      tasks
  6. 71 0
      test/Main.c
  7. 2 0
      test/modules/resources/test

+ 3 - 0
.clangd

@@ -0,0 +1,3 @@
+CompileFlags:
+  Add: [-ferror-limit=0, -std=c17, -DERROR_SIMULATOR=true]
+  CompilationDatabase: ./build_debug/

+ 10 - 0
.gitignore

@@ -0,0 +1,10 @@
+build_debug
+build_release
+install
+profile
+compiler
+.cache
+*.swp
+*.swo
+*.cpp
+*.hpp

+ 220 - 0
CMakeLists.txt

@@ -0,0 +1,220 @@
+cmake_minimum_required(VERSION 3.25)
+project(core)
+
+set(CMAKE_CXX_STANDARD 20)
+
+set(SRC
+    "src/Logger.c"
+    #"src/Utility.cpp"
+    #"src/Error.cpp"
+    #"src/New.cpp"
+    #"src/Buffer.cpp"
+    #"src/Clock.cpp"
+    #"src/Random.cpp"
+    #"src/BitArray.cpp"
+    #"src/Vector.cpp"
+    #"src/Quaternion.cpp"
+    #"src/Matrix.cpp"
+    #"src/Box.cpp"
+    #"src/Plane.cpp"
+    #"src/Frustum.cpp"
+    #"src/View.cpp"
+    #"src/Thread.cpp"
+    #"src/Mutex.cpp"
+    #"src/SpinLock.cpp"
+    #"src/FileReader.cpp"
+    #"src/ErrorSimulator.cpp"
+    #"src/ArrayString.cpp"
+)
+
+set(SRC_TESTS
+    "test/Main.c"
+    #"test/Test.cpp"
+    #"test/modules/ArrayTests.cpp"
+    #"test/modules/ArrayStringTests.cpp"
+    #"test/modules/UtilityTests.cpp"
+    #"test/modules/ArrayListTests.cpp"
+    #"test/modules/BitArrayTests.cpp"
+    #"test/modules/MathTests.cpp"
+    #"test/modules/ListTests.cpp"
+    #"test/modules/LinkedListTests.cpp"
+    #"test/modules/UniquePointerTests.cpp"
+    #"test/modules/HashMapTests.cpp"
+    #"test/modules/StackTests.cpp"
+    #"test/modules/RingBufferTests.cpp"
+    #"test/modules/ComponentsTests.cpp"
+    #"test/modules/VectorTests.cpp"
+    #"test/modules/QuaternionTests.cpp"
+    #"test/modules/MatrixTests.cpp"
+    #"test/modules/BoxTests.cpp"
+    #"test/modules/BufferedValueTests.cpp"
+    #"test/modules/PlaneTests.cpp"
+    #"test/modules/FrustumTests.cpp"
+    #"test/modules/ViewTests.cpp"
+    #"test/modules/MatrixStackTests.cpp"
+    #"test/modules/ColorTests.cpp"
+    #"test/modules/BufferTests.cpp"
+    #"test/modules/ClockTests.cpp"
+    #"test/modules/RandomTests.cpp"
+    #"test/modules/ThreadTests.cpp"
+    #"test/modules/FileReaderTests.cpp"
+    #"test/modules/ErrorTests.cpp"
+    #"test/modules/NewTests.cpp"
+    #"test/modules/HashedStringTests.cpp"
+)
+
+set(SRC_PERFORMANCE
+    #"performance/Main.cpp"
+    #"test/Test.cpp"
+)
+
+if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
+    set(COMPILE_OPTIONS -flto)
+    set(LINK_OPTIONS -flto)
+    set(LOG_LEVEL 2)
+    set(ERROR_SIMULATOR "")
+else()
+    if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+        set(COMPILE_OPTIONS --coverage)
+    endif()
+    set(LINK_OPTIONS gcov)
+    set(LOG_LEVEL 4)
+    set(ERROR_SIMULATOR "ERROR_SIMULATOR")
+endif()
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+    set(MORE_WARNINGS
+        -Walloc-zero
+        -Wanalyzer-too-complex
+        -Warith-conversion
+        -Warray-bounds=2
+        -Wattribute-alias=2
+        -Wbidi-chars=any
+        -Wcast-align=strict
+        -Wduplicated-branches
+        -Wduplicated-cond
+        -Wformat-overflow=2
+        -Wformat-signedness
+        -Wformat-truncation=2
+        -Wimplicit-fallthrough=5
+        -Wjump-misses-init
+        -Wlogical-op
+        -Wnormalized=nfkc
+        -Wshift-overflow=2
+        -Wstack-usage=8388608
+        -Wstringop-overflow=4
+        -Wtrampolines
+        -Wtrivial-auto-var-init
+        -Wunused-const-variable=2
+        -Wuse-after-free=3
+    )
+endif()
+
+add_library(core STATIC ${SRC})
+target_compile_options(core PUBLIC
+    ${COMPILE_OPTIONS}
+    -Wall
+    -Walloca
+    -Warray-parameter
+    -Wbad-function-cast
+    -Wcast-qual
+    -Wconversion
+    -Wdate-time
+    -Wdisabled-optimization
+    -Wdouble-promotion
+    -Wenum-compare
+    -Wenum-conversion
+    -Werror
+    -Wextra
+    -Wfloat-equal
+    -Wformat=2
+    -Wframe-larger-than=8388608
+    -Winfinite-recursion
+    -Winit-self
+    -Winvalid-pch
+    -Wlarger-than=1073741824
+    -Wmissing-braces
+    -Wmissing-declarations
+    -Wmissing-include-dirs
+    -Wmissing-prototypes
+    -Wmultichar
+    -Wnarrowing
+    -Wnested-externs
+    -Wnull-dereference
+    -Wold-style-definition
+    -Woverlength-strings
+    -Wredundant-decls
+    -Wshadow
+    -Wsign-conversion
+    -Wstack-protector
+    -Wstrict-overflow=2
+    -Wstrict-prototypes
+    -Wswitch-enum
+    -Wundef
+    -Wunreachable-code
+    -Wvla
+    -Wwrite-strings
+    -fdiagnostics-color=always
+    -pedantic
+    -pedantic-errors
+    ${MORE_WARNINGS}
+)
+target_compile_definitions(core 
+    PUBLIC CORE_LOG_LEVEL=${LOG_LEVEL}
+    PRIVATE ${ERROR_SIMULATOR}
+)
+target_link_libraries(core 
+    PUBLIC -nodefaultlibs c m
+    PRIVATE ${LINK_OPTIONS}
+)
+#target_sources(core PUBLIC 
+#    FILE_SET HEADERS
+#    BASE_DIRS include
+#    FILES 
+#        ./include/core/data/Stack.hpp
+#        ./include/core/data/HashMap.hpp
+#        ./include/core/data/Components.hpp
+#        ./include/core/data/ArrayList.hpp
+#        ./include/core/data/ProbingHashMap.hpp
+#        ./include/core/data/List.hpp
+#        ./include/core/data/LinkedList.hpp
+#        ./include/core/data/BitArray.hpp
+#        ./include/core/data/Array.hpp
+#        ./include/core/data/RingBuffer.hpp
+#        ./include/core/thread/Thread.hpp
+#        ./include/core/utils/HashCode.hpp
+#        ./include/core/utils/New.hpp
+#        ./include/core/utils/Check.hpp
+#        ./include/core/utils/Buffer.hpp
+#        ./include/core/utils/Random.hpp
+#        ./include/core/utils/UniquePointer.hpp
+#        ./include/core/utils/Types.hpp
+#        ./include/core/utils/Color.hpp
+#        ./include/core/utils/Logger.hpp
+#        ./include/core/utils/ArrayString.hpp
+#        ./include/core/utils/Utility.hpp
+#        ./include/core/utils/Meta.hpp
+#        ./include/core/utils/AlignedData.hpp
+#        ./include/core/utils/Clock.hpp
+#        ./include/core/utils/Error.hpp
+#        ./include/core/math/Quaternion.hpp
+#        ./include/core/math/Box.hpp
+#        ./include/core/math/Frustum.hpp
+#        ./include/core/math/Vector.hpp
+#        ./include/core/math/Matrix.hpp
+#        ./include/core/math/View.hpp
+#        ./include/core/math/BufferedValue.hpp
+#        ./include/core/math/Plane.hpp
+#        ./include/core/math/MatrixStack.hpp
+#        ./include/core/math/Math.hpp
+#        ./include/core/io/File.hpp
+#        ./include/core/io/FileReader.hpp
+#)
+#install(TARGETS core FILE_SET HEADERS)
+
+add_executable(test ${SRC_TESTS})
+target_link_libraries(test PRIVATE core)
+target_compile_definitions(test PRIVATE ${ERROR_SIMULATOR})
+
+#add_executable(performance ${SRC_PERFORMANCE})
+#target_link_libraries(performance PRIVATE core)

+ 16 - 0
src/Logger.c

@@ -0,0 +1,16 @@
+/*#include "core/utils/Logger.hpp"
+
+Core::Logger::Level Core::Logger::level = Core::Logger::Level::DEBUG;
+
+const char* Core::Logger::getFileName(const char* path) {
+    int end = 0;
+    while(path[end] != '\0') {
+        end++;
+    }
+    while(end > 0 && path[end - 1] != '/') {
+        end--;
+    }
+    return path + end;
+}*/
+
+int wusi = 0;

+ 176 - 0
tasks

@@ -0,0 +1,176 @@
+#!/bin/bash
+set -e
+clear
+cd $(dirname $0)
+
+compiler="gcc"
+if [ -e compiler ]; then
+    compiler=$(cat compiler)
+    echo "compiling with $compiler"
+fi
+
+printHelpExit() {
+    echo "$0 clean           | remove build results"
+    echo "$0 build <type>    | build everything"
+    echo "$0 install         | move build results into the install folder"
+    echo "$0 test <type>     | run the tests"
+    echo "$0 valgrind <type> | run the tests with valgrind"
+    echo "$0 coverage        | generate code coverage"
+    echo "$0 performance     | run the performance tests"
+    echo "$0 macro           | find macros without CORE" 
+    echo "$0 time            | check build time"
+    exit 0
+}
+
+task=$1
+if [ -z "$task" ]; then
+    printHelpExit
+fi
+
+# task vars
+build_debug=false
+build_release=false
+
+test_debug=false
+test_release=false
+
+valgrind=""
+performance=false
+time=false
+install=false
+coverage=false
+
+export CMAKE_EXPORT_COMPILE_COMMANDS=true
+
+# parsing
+if [ "$task" = "clean" ]; then
+    rm -rf build_debug build_release install
+elif [ "$task" = "build" ]; then
+    type=$2
+    if [ "$type" = "debug" ]; then
+        build_debug=true
+    elif [ "$type" = "release" ]; then
+        build_release=true
+    elif [ "$type" = "all" ]; then
+        build_debug=true
+        build_release=true
+    else
+        echo "Valid build types are: debug, release, all"
+        printHelpExit
+    fi
+elif [ "$task" = "install" ]; then
+    build_release=true
+    install=true
+elif [ "$task" = "coverage" ]; then
+    build_debug=true
+    test_debug=true
+    coverage=true
+elif [ "$task" = "test" ]; then
+    type=$2
+    if [ "$type" = "debug" ]; then
+        build_debug=true
+        test_debug=true
+    elif [ "$type" = "release" ]; then
+        build_release=true
+        test_release=true
+    elif [ "$type" = "all" ]; then
+        build_debug=true
+        test_debug=true
+        build_release=true
+        test_release=true
+    else
+        echo "Valid test types are: debug, release, all"
+        printHelpExit
+    fi
+elif [ "$task" = "valgrind" ]; then
+    type=$2
+    if [ "$type" = "debug" ]; then
+        build_debug=true
+        test_debug=true
+    elif [ "$type" = "release" ]; then
+        build_release=true
+        test_release=true
+    elif [ "$type" = "all" ]; then
+        build_debug=true
+        test_debug=true
+        build_release=true
+        test_release=true
+    else
+        echo "Valid valgrind types are: debug, release, all"
+        printHelpExit
+    fi
+    valgrind="valgrind"
+elif [ "$task" = "performance" ]; then
+    build_release=true
+    performance=true
+elif [ "$task" = "time" ]; then
+    build_release=true
+    time=true
+elif [ "$task" = "macro" ]; then
+    grep -r "#define" src include | grep -v " CORE" || true
+    exit 0
+else
+    echo "unknown task"
+    printHelpExit
+fi
+
+# task execution
+buildProfile() {
+    folder=$1
+    shift 1
+    if [ ! -e "$folder" ]; then 
+        cmake -B "$folder" -S . -G Ninja -DCMAKE_C_COMPILER=${compiler} -DCMAKE_INSTALL_PREFIX=./install $@
+    fi
+    ninja -C "$folder"
+}
+
+if $build_debug; then
+    buildProfile build_debug -DCMAKE_BUILD_TYPE=Debug
+fi
+if $build_release; then
+    buildProfile build_release -DCMAKE_BUILD_TYPE=Release
+fi
+if $install; then
+    ninja -C build_release install
+fi
+if $test_debug; then
+    cd build_debug
+    $valgrind ./test light $valgrind || true
+    cd ..
+fi
+if $test_release; then
+    cd build_release
+    $valgrind ./test light $valgrind || true
+    cd ..
+fi
+if $performance; then
+    cd build_release
+    ./performance
+    cd ..
+fi
+if $time; then
+    lines=$(cat build_release/.ninja_log | grep "^[0-9]")
+
+    startMillis=0
+    endMillis=0
+    name=""
+    i=0
+    output=""
+    for arg in $lines; do
+        if [ $i == 0 ]; then
+            startMillis=$arg
+        elif [ $i == 1 ]; then
+            endMillis=$arg
+        elif [ $i == 3 ]; then
+            name=$arg
+            diff=$(expr $endMillis - $startMillis)
+            output="${output}\n$diff $name"
+        fi
+        i=$(expr $(expr $i + 1) % 5) && true
+    done
+    printf "$output" | sort -n
+fi
+if $coverage; then
+    gcovr -r . build_debug -e test -e performance \
+        --exclude-lines-by-pattern ".*CoverageIgnore.*"
+fi

+ 71 - 0
test/Main.c

@@ -0,0 +1,71 @@
+#include <locale.h>
+#include <stdbool.h>
+#include <string.h>
+// #include "../src/ErrorSimulator.hpp"
+// #include "Test.hpp"
+// #include "Tests.hpp"
+// #include "core/utils/ArrayString.hpp"
+// #include "core/utils/Utility.hpp"
+
+/*static void onExit(int code, void* data) {
+    unsigned int i = *static_cast<unsigned int*>(data);
+    Core::ArrayString<1024> s;
+    s.append("Hello from exit #: #");
+    s.format(code, i);
+    s.printLine();
+    Core::print('A');
+    Core::Test::finalize();
+}*/
+
+int main(int argAmount, const char** args) {
+    setlocale(LC_ALL, "en_US.utf8");
+    bool light = false;
+    for(int i = 0; i < argAmount; i++) {
+        if(strcmp(args[i], "light") == 0) {
+            light = true;
+        }
+    }
+    (void)light;
+    /*
+    Core::testArrayList(light);
+    Core::testArrayString();
+    Core::testArray();
+    Core::testBitArray();
+    Core::testBox();
+    Core::testBuffer(light);
+    Core::testBufferedValue();
+    Core::testClock(light);
+    Core::testColor();
+    Core::testComponents();
+    Core::testError();
+    Core::testFileReader();
+    Core::testFrustum();
+    Core::testHashedString();
+    Core::testHashMap(light);
+    Core::testLinkedList(light);
+    Core::testList(light);
+    Core::testMath();
+    Core::testMatrixStack(light);
+    Core::testMatrix();
+    Core::testNew();
+    Core::testPlane();
+    Core::testQuaternion();
+    Core::testRandom(light);
+    Core::testRingBuffer();
+    Core::testStack(light);
+    Core::testThread();
+    Core::testUniquePointer();
+    Core::testUtility();
+    Core::testVector();
+    Core::testView();
+
+    Core::Logger::level = Core::Logger::Level::WARNING;
+    CORE_LOG_DEBUG("You won't see this!");
+    Core::Logger::level = Core::Logger::Level::DEBUG;
+
+    unsigned int data = 123456789;
+    Core::setExitHandler(onExit, &data);
+
+    CORE_EXIT(1);*/
+    return 0;
+}

+ 2 - 0
test/modules/resources/test

@@ -0,0 +1,2 @@
+abc
+Baum