diff --git a/.gitignore b/.gitignore index d6bbe5ca..13e12f5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ test a.out *.asm +.vscode/settings.json +build/ +Uninstall.cmake \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..0a0cf484 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,55 @@ +cmake_minimum_required(VERSION 3.10) + +project( + ctl + VERSION 3.0 + HOMEPAGE_URL "https://github.com/glouw/ctl" + DESCRIPTION "CTL is a fast compiling, type safe, header only, template-like library for ISO C99/C11." +) + + +# Package to a library +add_library(ctl INTERFACE) +set(ctlIncludeDir ${PROJECT_SOURCE_DIR}/ctl) +target_include_directories(ctl INTERFACE $) + +# Install +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + + +install( + TARGETS ctl + EXPORT ctlTargets + INCLUDES DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR} +) + +set(versionFile ctlConfigVersion.cmake) +write_basic_package_version_file( + ${versionFile} + VERSION ${CMAKE_PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion + ARCH_INDEPENDENT +) +install( + EXPORT ctlTargets + FILE ctlConfig.cmake + DESTINATION ${CMAKE_INSTALL_PREFIX} +) +install( + FILES ${PROJECT_BINARY_DIR}/${versionFile} + DESTINATION ${CMAKE_INSTALL_PREFIX} +) + +install( + DIRECTORY ${ctlIncludeDir} + DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR} +) + +# Uninstall +add_custom_target( + Uninstall + COMMAND cmake -E rm -rf ${CMAKE_INSTALL_PREFIX}/ctl + ${CMAKE_INSTALL_PREFIX}/ctlConfig.cmake + ${CMAKE_INSTALL_PREFIX}/${versionFile} +) diff --git a/README.md b/README.md index c297ab7e..1ed3c90f 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,73 @@ gcc main.c -I ctl For a much more thorough getting started guide, see the wiki: https://github.com/glouw/ctl/wiki +## CMake Support +### Install +1. Clone this repo by +``` +git clone https://github.com/glouw/ctl +``` +2. Configure +``` +cd ctl +mkdir build +cd build +cmake .. +``` +3. Install +- Linux: +``` +sudo cmake --install . +``` + +- Windows: Open a shell with administrator +``` +cmake --install . +``` + +### Use ctl CMake packages +Add the following line to your `CMakeLists.txt` +```cmake +find_package(ctl REQUIRED CONFIG) +#... Add your executable or target +target_link_libraries( PRIVATE ctl) +``` + +Include the header using `#include `, for example: +```c +#include + +#define P +#define T int +#include + +int compare(int* a, int* b) { return *b < *a; } + +int main(void) +{ + vec_int a = vec_int_init(); + vec_int_push_back(&a, 9); + vec_int_push_back(&a, 1); + vec_int_push_back(&a, 8); + vec_int_push_back(&a, 3); + vec_int_push_back(&a, 4); + vec_int_sort(&a, compare); + foreach(vec_int, &a, it) + printf("%d\n", *it.ref); + vec_int_free(&a); +} +``` + +### Uninstall +`cd` into `ctl/build` directory +- Linux: +``` +sudo cmake --build . --target Uninstall +``` +- Windows: Open a shell with administrator +``` +cmake --build . --target Uninstall +``` ## Memory Ownership Types with memory ownership require definition `P` be omitted, and require diff --git a/common.cmake b/common.cmake new file mode 100644 index 00000000..d1e8903b --- /dev/null +++ b/common.cmake @@ -0,0 +1,56 @@ +# Global settings for Tests and Examples + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +option(LONG OFF) +option(SANITIZE OFF) +option(SRAND ON) + +set(OptimizationLevel Debug 0 1 2 3 Fast Size) +set(Optimization Debug CACHE STRING "Level of optimization") +set_property(CACHE Optimization PROPERTY STRINGS ${OptimizationLevel}) + +macro(AddFlag MSVCFlag GCCFlag) + if(MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MSVCFlag}") + else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCCFlag}") + endif() +endmacro() + +AddFlag("${CMAKE_C_FLAGS} /W3 /sdl-" "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic -Wfatal-errors -Wshadow -march=native -g") + +if(${Optimization} STREQUAL 0) + AddFlag(/Od -O0) +elseif(${Optimization} STREQUAL 1) + AddFlag(/Ox -O1) +elseif(${Optimization} STREQUAL 2) + AddFlag(/O1 -O2) +elseif(${Optimization} STREQUAL 3) + AddFlag(/O2 -O3) +elseif(${Optimization} STREQUAL Debug) + AddFlag("" -Og) +elseif(${Optimization} STREQUAL Fast) + AddFlag("/O2 /Ot" -Ofast) +elseif(${Optimization} STREQUAL Size) + AddFlag("/O1 /Os" -Os) +endif() + +if(${LONG}) + add_compile_definitions(LONG) +endif() + +if(${SANITIZE}) + AddFlag("/fsanitize=address" "-fsanitize=address -fsanitize=undefined") +endif() + +if(${SRAND}) + add_compile_definitions(SRAND) +endif() + +message("Using flag: ${CMAKE_C_FLAGS}") + +include_directories(../ctl) # A dirty solution, but works for tests and examples diff --git a/ctl/deq.h b/ctl/deq.h index abe45086..c21f9892 100644 --- a/ctl/deq.h +++ b/ctl/deq.h @@ -6,7 +6,7 @@ #error "Template type T undefined for " #endif -#include +#include "ctl.h" #define A JOIN(deq, T) #define B JOIN(A, bucket) diff --git a/ctl/lst.h b/ctl/lst.h index 24e92db3..010982cd 100644 --- a/ctl/lst.h +++ b/ctl/lst.h @@ -6,7 +6,7 @@ #error "Template type T undefined for " #endif -#include +#include "ctl.h" #define A JOIN(lst, T) #define B JOIN(A, node) diff --git a/ctl/pqu.h b/ctl/pqu.h index c5f6ca12..eaa92aa4 100644 --- a/ctl/pqu.h +++ b/ctl/pqu.h @@ -34,7 +34,7 @@ #define HOLD #define COMPARE #define init __INIT -#include +#include "vec.h" #undef init #undef vec diff --git a/ctl/que.h b/ctl/que.h index c013c066..0fdedffc 100644 --- a/ctl/que.h +++ b/ctl/que.h @@ -26,7 +26,7 @@ #define remove_if __REMOVE_IF #define deq que -#include +#include "deq.h" #undef deq #undef push_back diff --git a/ctl/set.h b/ctl/set.h index 19244c9c..3d7b2931 100644 --- a/ctl/set.h +++ b/ctl/set.h @@ -6,7 +6,7 @@ #error "Template type T undefined for " #endif -#include +#include "ctl.h" #define A JOIN(set, T) #define B JOIN(A, node) diff --git a/ctl/stk.h b/ctl/stk.h index aaf285fb..6f51ac50 100644 --- a/ctl/stk.h +++ b/ctl/stk.h @@ -28,7 +28,7 @@ #define remove_if __REMOVE_IF #define deq stk -#include +#include "deq.h" #undef deq #undef push_back diff --git a/ctl/str.h b/ctl/str.h index cdb3eabc..2d5150ab 100644 --- a/ctl/str.h +++ b/ctl/str.h @@ -16,7 +16,7 @@ #define str_equal str___EQUAL #define str_find str___FIND #define str_copy str___COPY -#include +#include "vec.h" #undef str_init #undef str_copy #undef str_equal diff --git a/ctl/ust.h b/ctl/ust.h index b59989c5..afa260db 100644 --- a/ctl/ust.h +++ b/ctl/ust.h @@ -6,7 +6,7 @@ #error "Template type T undefined for " #endif -#include +#include "ctl.h" #define A JOIN(ust, T) #define B JOIN(A, node) diff --git a/ctl/vec.h b/ctl/vec.h index 97d0db1a..2caaffda 100644 --- a/ctl/vec.h +++ b/ctl/vec.h @@ -6,7 +6,7 @@ #error "Template type T undefined for " #endif -#include +#include "ctl.h" #define A JOIN(vec, T) #define I JOIN(A, it) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..c7007f3c --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.10) + +project(ctlExamples) + +# Set up the global common compiler settings +include(../common.cmake) + +# Add examples +add_custom_target(examples) +function(AddExample sourceFileNoExtension) + add_executable(${sourceFileNoExtension} "${sourceFileNoExtension}.c") + add_dependencies(examples ${sourceFileNoExtension}) +endfunction() + +AddExample(astar) +AddExample(postfix) +AddExample(json) +AddExample(snow) +AddExample(6502) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..a7b39eb8 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 3.10) + +project(ctlTest) + +# Set up the global common compiler settings +include(../common.cmake) + +# Add tests +add_custom_target(tests) +set(testFileDir func) + +function(AddTest sourceFileNoExtension) + find_file(${sourceFileNoExtension}.c ${sourceFileNoExtension}.c ${testFileDir}) + if(${sourceFileNoExtension}.c) + message("Found ${sourceFileNoExtension}.c") + add_executable(${sourceFileNoExtension} "${testFileDir}/${sourceFileNoExtension}.c") + else() + message("Found ${sourceFileNoExtension}.cc") + add_executable(${sourceFileNoExtension} "${testFileDir}/${sourceFileNoExtension}.cc") + endif() + add_dependencies(tests ${sourceFileNoExtension}) +endfunction() + +AddTest(test_c11) +AddTest(test_container_composing) +AddTest(test_deq) +AddTest(test_lst) +AddTest(test_str) +AddTest(test_pqu) +AddTest(test_que) +AddTest(test_set) +AddTest(test_ust) +AddTest(test_stk) +AddTest(test_vec_capacity) +AddTest(test_vec)