Skip to content
Snippets Groups Projects
Verified Commit 37da2d30 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

setup repository stub (formatting, build, test, coverage)

parents
No related branches found
No related tags found
No related merge requests found
# General
build/
include/libcaosdbConfig.h
# CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
# C
## Prerequisites
*.d
## Object files
*.o
*.ko
*.obj
*.elf
## Linker output
*.ilk
*.map
*.exp
## Precompiled Headers
*.gch
*.pch
## Libraries
*.lib
*.a
*.la
*.lo
## Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
## Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
## Debug files
*.dSYM/
*.su
*.idb
*.pdb
## Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# VIM
## Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
## Session
Session.vim
Sessionx.vim
## Temporary
.netrwhist
*~
## Auto-generated tag files
tags
## Persistent undo
[._]*.un~
# Emacs
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
## Org-mode
.org-id-locations
*_archive
## flymake-mode
*_flymake.*
## eshell files
/eshell/history
/eshell/lastdir
## elpa packages
/elpa/
## reftex files
*.rel
## AUCTeX auto folder
/auto/
## cask packages
.cask/
dist/
## Flycheck
flycheck_*.el
## server auth directory
/server/
## projectiles files
.projectile
## directory configuration
.dir-locals.el
## network security
/network-security.data
cmake_minimum_required(VERSION 3.14)
set(libcaosdb_VERSION 0.0.1)
project(libcaosdb
VERSION ${libcaosdb_VERSION}
DESCRIPTION "Plain C client libraries for CaosDB"
LANGUAGES C)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
add_subdirectory(src)
add_subdirectory(include)
add_library(libcaosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC})
add_executable(caosdbcli src/caosdbcli.c)
target_link_libraries(caosdbcli libcaosdb)
if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
target_link_libraries(libcaosdb gcov)
enable_testing()
add_subdirectory(test)
append_coverage_compiler_flags()
endif()
# These variables slightly modify the install location to allow for version
# specific installations.
set(libcaosdb_INCLUDE_DEST "include/libcaosdb-${libcaosdb_VERSION}")
set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}")
# generator expressions are needed for the include directories, since
# installing headers changes the include path specify that libcaosdb requires
# the files located in the include/ directory at compile time. This would
# normally look like
# target_include_directories(libcaosdb PUBLIC include/)
# PUBLIC means that other libraries including libcaosdb should also include
# the directory include/.
# However, there is a catch. If we are installing the project in
# CMAKE_INSTALL_PREFIX, we can't specify include/ in the build directory: we
# have copied the contents of include to CMAKE_INSTALL_PREFIX/include and we
# would like other projects to include this directory instead of include/.
# The following CMake command handles this. $<BUILD_INTERFACE:...> and
# $<INSTALL_INTERFACE:...> are macros whose values change depending on if we
# are simply building the code or if we are installing it.
target_include_directories(libcaosdb PUBLIC
# headers to include when building from source
$<BUILD_INTERFACE:${libcaosdb_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${libcaosdb_BINARY_DIR}/include>
# headers to include when installing (implicitly prefixes with ${CMAKE_INSTALL_PREFIX}).
$<INSTALL_INTERFACE:include>
)
# Install libcaosdb in CMAKE_INSTALL_PREFIX (defaults to /usr/local on linux).
# To change the install location, run
# cmake -DCMAKE_INSTALL_PREFIX=<desired-install-path> ..
# install(...) specifies installation rules for the project. It can specify
# location of installed files on the system, user permissions, build
# configurations, etc. Here, we are only copying files.
# install(TARGETS ...) specifies rules for installing targets.
# Here, we are taking a target or list of targets (libcosdb) and telling
# CMake the following:
# - put shared libraries associated with libcaosdb in ${libcaosdb_LIB_DEST}
# - put static libraries associated with libcaosdb in ${libcaosdb_LIB_DEST}
# - put include files associated with libcaosdb in ${libcaosdb_INCLUDE_DEST}
# We also need to specify the export that is associated with libcaosdb; an
# export is just a list of targets to be installed. So we are associating
# libcaosdb with libcaosdbTargets.
install(
# targets to install
TARGETS libcaosdb
# name of the CMake "export group" containing the targets we want to install
EXPORT libcaosdbTargets
# Dynamic, static library and include destination locations after running
# "make install"
LIBRARY DESTINATION ${libcaosdb_LIB_DEST}
ARCHIVE DESTINATION ${libcaosdb_LIB_DEST}
INCLUDES DESTINATION ${libcaosdb_INCLUDE_DEST}
)
# We now need to install the export libcaosdbTargets that we defined above.
# This is needed in order for another project to import libcaosdb using
# find_package(libcaosdb)
# find_package(libcaosdb) will look for libcaosdb-config.cmake to provide
# information about the targets contained in the project libcaosdb.
# Fortunately, this is specified in the export libcaosdbTargets, so we will
# install this too.
# install(EXPORT ...) will install the information about an export. Here, we
# save it to a file {$libcaosdb_LIB_DEST}/libcaosdbTargets.cmake and prepend
# everything inside libcaosdbTargets with the namespace libcaosdb::.
install(
# The export we want to save (matches name defined above containing the
# install targets)
EXPORT libcaosdbTargets
# CMake file in which to store the export's information
FILE libcaosdbTargets.cmake
# Namespace prepends all targets in the export (when we import later, we
# will use libcaosdb::libcaosdb)
NAMESPACE libcaosdb::
# where to place the resulting file (here, we're putting it with the library)
DESTINATION ${libcaosdb_LIB_DEST}
)
# install(FILES ...) simply puts files in a certain place with certain
# properties. We're just copying them to the desired place here.
install(FILES ${libcaosdb_INC} DESTINATION ${libcaosdb_INCLUDE_DEST})
# libcaosdb
This is libcaosdb - the CaosDB client library.
## Build
We use [cmake](https://cmake.org) as build tool.
1. `cmake -B build`
2. `cd build`
3. `cmake --build .`
## Unit Tests
### Run
Build tests as described above. In the build directory, run `ctest`
### Framework
We use [cmocka](https://cmocka.org) for unit testing
### Coverage
* We use gcov and lcov for generating test coverage reports.
## Code Formatting
* `clang-format -i --verbose **/*.c **/*.h`
This diff is collapsed.
# Copyright 2020 OLIVIER LE DOEUFF
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
include(FetchContent)
# Declare our target. We want the lastest stable version, not the master.
# Also specify GIT_SHALLOW to avoid cloning branch we don't care about
FetchContent_Declare(
cmocka
GIT_REPOSITORY https://git.cryptomilk.org/projects/cmocka.git
GIT_TAG cmocka-1.1.5
GIT_SHALLOW 1
)
# We want to link to cmocka-static, so we need to set this option before calling the FetchContent_MakeAvailable
# We also don't care about example and tests
set(WITH_STATIC_LIB ON CACHE BOOL "CMocka: Build with a static library" FORCE)
set(WITH_CMOCKERY_SUPPORT OFF CACHE BOOL "CMocka: Install a cmockery header" FORCE)
set(WITH_EXAMPLES OFF CACHE BOOL "CMocka: Build examples" FORCE)
set(UNIT_TESTING OFF CACHE BOOL "CMocka: Build with unit testing" FORCE)
set(PICKY_DEVELOPER OFF CACHE BOOL "CMocka: Build with picky developer flags" FORCE)
# Download cmocka, and execute its cmakelists.txt
FetchContent_MakeAvailable(cmocka)
set(libcaosdb_INCL
include/libcaosdbConfig.h
include/connection.h
)
set(libcaosdb_INCL ${libcaosdb_INCL} PARENT_SCOPE)
configure_file(libcaosdbConfig.h.in libcaosdbConfig.h)
int configure_connection(char host[], int port);
#define LIBCAOSDB_VERSION_MAJOR @libcaosdb_VERSION_MAJOR@
#define LIBCAOSDB_VERSION_MINOR @libcaosdb_VERSION_MINOR@
#define LIBCAOSDB_VERSION_PATCH @libcaosdb_VERSION_PATCH@
# Make an explicit list of all source files in `libcaosdb_SRC`. This is important
# because CMake is not a build system: it is a build system generator. Suppose
# you add a file foo.cpp to src/ after running cmake .. . If you set
# `libcaosdb_SRC` with `file(GLOB ... )`, this is not passed to the makefile;
# the makefile doesn't know that foo.cpp exists and will not re-run cmake. Your
# collaborator's builds will fail and it will be unclear why. Whether you use
# file(GLOB ...) or not, you will need to re-run cmake, but with an explicit
# file list, you know beforehand why your code isn't compiling.
set(libcaosdb_SRC
src/connection.c
)
# ... and pass the variable to the parent scope.
set(libcaosdb_SRC ${libcaosdb_SRC} PARENT_SCOPE)
// A simple caosdb client
#include "connection.h"
#include "libcaosdbConfig.h"
#include <stdio.h>
int main(void) {
printf("CaosDB (libcaosdb %d.%d.%d)\n", LIBCAOSDB_VERSION_MAJOR,
LIBCAOSDB_VERSION_MINOR, LIBCAOSDB_VERSION_PATCH);
printf("We don't miss the H of caos.\n");
configure_connection("localhost", 8080);
return 0;
}
#include "connection.h"
#include <stdio.h>
int configure_connection(char host[], int port) {
if (port > 666 && port < 668) {
printf("667 - One step ahead of the Devil!");
}
printf("Configure connection: %s:%d\n", host, port);
return 0;
}
include(FetchCMocka)
# append all the test cases here (file name without the ".c" suffix)
set(test_cases
test_connection
)
list(LENGTH test_cases len_test_cases)
math(EXPR len_test_cases "${len_test_cases} - 1")
foreach(i RANGE "${len_test_cases}")
list(GET test_cases ${i} test_case_name)
add_executable(${test_case_name} ${test_case_name}.c)
target_compile_features(${test_case_name} PRIVATE c_std_99)
target_link_libraries(${test_case_name} PRIVATE cmocka-static libcaosdb)
add_test(${test_case_name} ${test_case_name})
endforeach()
#add_executable(test_connection test_connection.c)
#target_compile_features(test_connection PRIVATE c_std_99)
#target_link_libraries(test_connection PRIVATE cmocka-static libcaosdb)
#add_test(test_connection test_connection)
# code coverage report
include(CodeCoverage)
if(LCOV_PATH)
setup_target_for_coverage_lcov(
NAME unit_test_coverage
EXECUTABLE ctest
DEPEDENCIES libcaosdb
LCOV_ARGS --rc lcov_branch_coverage=1
GENHTML_ARGS --rc lcov_branch_coverage=1
)
else()
message(WARNING "Could not generate code coverage report. Please install lcov.")
endif()
#include "connection.h"
#include <cmocka.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
static void test_configure_connection(void **state) {
assert_int_equal(0, configure_connection("localhost", 8080));
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_configure_connection),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment