Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmake support #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
test
a.out
*.asm
.vscode/settings.json
build/
Uninstall.cmake
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ctl>)

# 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}
)
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Target> PRIVATE ctl)
```

Include the header using `#include <ctl/...>`, for example:
```c
#include <stdio.h>

#define P
#define T int
#include <ctl/vec.h>

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
Expand Down
56 changes: 56 additions & 0 deletions common.cmake
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion ctl/deq.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#error "Template type T undefined for <deq.h>"
#endif

#include <ctl.h>
#include "ctl.h"

#define A JOIN(deq, T)
#define B JOIN(A, bucket)
Expand Down
2 changes: 1 addition & 1 deletion ctl/lst.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#error "Template type T undefined for <lst.h>"
#endif

#include <ctl.h>
#include "ctl.h"

#define A JOIN(lst, T)
#define B JOIN(A, node)
Expand Down
2 changes: 1 addition & 1 deletion ctl/pqu.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define HOLD
#define COMPARE
#define init __INIT
#include <vec.h>
#include "vec.h"
#undef init
#undef vec

Expand Down
2 changes: 1 addition & 1 deletion ctl/que.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define remove_if __REMOVE_IF

#define deq que
#include <deq.h>
#include "deq.h"
#undef deq

#undef push_back
Expand Down
2 changes: 1 addition & 1 deletion ctl/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#error "Template type T undefined for <set.h>"
#endif

#include <ctl.h>
#include "ctl.h"

#define A JOIN(set, T)
#define B JOIN(A, node)
Expand Down
2 changes: 1 addition & 1 deletion ctl/stk.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define remove_if __REMOVE_IF

#define deq stk
#include <deq.h>
#include "deq.h"
#undef deq

#undef push_back
Expand Down
2 changes: 1 addition & 1 deletion ctl/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define str_equal str___EQUAL
#define str_find str___FIND
#define str_copy str___COPY
#include <vec.h>
#include "vec.h"
#undef str_init
#undef str_copy
#undef str_equal
Expand Down
2 changes: 1 addition & 1 deletion ctl/ust.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#error "Template type T undefined for <ust.h>"
#endif

#include <ctl.h>
#include "ctl.h"

#define A JOIN(ust, T)
#define B JOIN(A, node)
Expand Down
2 changes: 1 addition & 1 deletion ctl/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#error "Template type T undefined for <vec.h>"
#endif

#include <ctl.h>
#include "ctl.h"

#define A JOIN(vec, T)
#define I JOIN(A, it)
Expand Down
19 changes: 19 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)