From 7ae112a7ceff1fe5c0088ac4fbb2e9e53d845d13 Mon Sep 17 00:00:00 2001 From: Jake Goodwin Date: Wed, 30 Aug 2023 05:43:48 +0000 Subject: [PATCH] Initial commit --- .gitignore | 9 ++ .gitmodules | 3 + CMakeLists.txt | 56 +++++++ LICENSE | 0 README.md | 103 +++++++++++++ build/compile_commands.json | 22 +++ cmake/cmocka/AddCCompilerFlag.cmake | 21 +++ cmake/cmocka/AddCMockaTest.cmake | 143 ++++++++++++++++++ cmake/cmocka/COPYING-CMAKE-SCRIPTS | 22 +++ cmake/cmocka/CheckCCompilerFlagSSP.cmake | 48 ++++++ cmake/cmocka/DefineCMakeDefaults.cmake | 25 +++ cmake/cmocka/DefineCompilerFlags.cmake | 49 ++++++ cmake/cmocka/DefinePlatformDefaults.cmake | 21 +++ cmake/cmocka/FindNSIS.cmake | 54 +++++++ .../cmocka/MacroEnsureOutOfSourceBuild.cmake | 17 +++ compile_commands.json | 1 + src/CMakeLists.txt | 10 ++ src/led_driver/CMakeLists.txt | 7 + src/led_driver/led_driver.c | 21 +++ src/led_driver/led_driver.h | 12 ++ src/main.c | 17 +++ src/main.h | 4 + tests/CMakeLists.txt | 4 + tests/led_driver/CMakeLists.txt | 13 ++ tests/led_driver/test_led_driver.c | 18 +++ tests/simple_test/CMakeLists.txt | 7 + tests/simple_test/simple_test.c | 19 +++ 27 files changed, 726 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build/compile_commands.json create mode 100644 cmake/cmocka/AddCCompilerFlag.cmake create mode 100644 cmake/cmocka/AddCMockaTest.cmake create mode 100644 cmake/cmocka/COPYING-CMAKE-SCRIPTS create mode 100644 cmake/cmocka/CheckCCompilerFlagSSP.cmake create mode 100644 cmake/cmocka/DefineCMakeDefaults.cmake create mode 100644 cmake/cmocka/DefineCompilerFlags.cmake create mode 100644 cmake/cmocka/DefinePlatformDefaults.cmake create mode 100644 cmake/cmocka/FindNSIS.cmake create mode 100644 cmake/cmocka/MacroEnsureOutOfSourceBuild.cmake create mode 120000 compile_commands.json create mode 100644 src/CMakeLists.txt create mode 100644 src/led_driver/CMakeLists.txt create mode 100644 src/led_driver/led_driver.c create mode 100644 src/led_driver/led_driver.h create mode 100644 src/main.c create mode 100644 src/main.h create mode 100644 tests/CMakeLists.txt create mode 100644 tests/led_driver/CMakeLists.txt create mode 100644 tests/led_driver/test_led_driver.c create mode 100644 tests/simple_test/CMakeLists.txt create mode 100644 tests/simple_test/simple_test.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..454936a --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +build/CMakeFiles +build/Testing/Temporary +build/src +build/tests +build/CMakeCache.txt +build/CTestTestfile.cmake +build/Makefile +build/cmake_install.cmake +build/DartConfiguration.tcl diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e8fc423 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "git_modules/cmocka"] + path = git_modules/cmocka + url = https://gitlab.com/cmocka/cmocka.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..08d3c48 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,56 @@ +cmake_minimum_required(VERSION 3.20) + +# Use the fancy version substitution +project(template + VERSION 1.0 + DESCRIPTION "C TDD template using cmocka" + LANGUAGES C +) +enable_testing() + +set(TARGET_GROUP production CACHE STRING "Group to build") + +# For being able to used LSP +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Request C 17 standard features +set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD_REQUIRED True) +set(CMAKE_C_FLAGS "-Wall -Werror -Wpedantic") + +# Add CMocka CMake modules +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmocka) + +set (CMOCKA_PATH "" CACHE STRING "/usr/local/include") + +# Search for the CMocka include directory +find_path(CMOCKA_INCLUDE_DIR + NAMES cmocka.h + PATHS ${CMOCKA_PATH}/include + DOC "Where the CMocka header can be found" +) +set(CMOCKA_INCLUDE_DIRS "${CMOCKA_INCLUDE_DIR}") + +# Search for the CMocka library directory +find_library(CMOCKA_LIBRARY + NAMES cmocka + PATHS ${CMOCKA_PATH}/lib + DOC "Where the CMocka library can be found" +) +set(CMOCKA_LIBRARIES "${CMOCKA_LIBRARY}") + +# Set CMOCKA_FOUND (if all required vars are found). +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_INCLUDE_DIRS CMOCKA_LIBRARIES) + +# Hide variables from cmake GUIs. +mark_as_advanced(CMOCKA_PATH CMOCKA_INCLUDE_DIR CMOCKA_INCLUDE_DIRS CMOCKA_LIBRARY CMOCKA_LIBRARIES) + +add_subdirectory(src) + +if (UNIT_TESTING) + find_package(cmocka 1.1.5 REQUIRED) + include(AddCMockaTest) + add_subdirectory(tests) +endif() + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..201ae0b --- /dev/null +++ b/README.md @@ -0,0 +1,103 @@ +# CMOCKA/CMAKE template + +## Purporse + +- Streamline the setup of new C projects +- Make it easy to setup a develoment enviroment +- Allow TDD for embedded systems +- Provide easy LSP usage via compile_commands.json +- correctly use the CTEST command. +- Avoid having to setup ruby/ceedling with it's dependency hell + +## Organization + +``` +. +├── CMakeLists.txt +├── LICENSE +├── README.md +├── build +├── cmake +│   └── cmocka +│   ├── AddCCompilerFlag.cmake +│   ├── AddCMockaTest.cmake +│   ├── COPYING-CMAKE-SCRIPTS +│   ├── CheckCCompilerFlagSSP.cmake +│   ├── DefineCMakeDefaults.cmake +│   ├── DefineCompilerFlags.cmake +│   ├── DefinePlatformDefaults.cmake +│   ├── FindNSIS.cmake +│   └── MacroEnsureOutOfSourceBuild.cmake +├── compile_commands.json -> ./build/compile_commands.json +├── git_modules +├── src +│   ├── CMakeLists.txt +│   ├── led_driver +│   │   ├── CMakeLists.txt +│   │   ├── led_driver.c +│   │   └── led_driver.h +│   ├── main.c +│   └── main.h +└── tests + ├── CMakeLists.txt + ├── led_driver + │   ├── CMakeLists.txt + │   └── test_led_driver.c + └── simple_test + ├── CMakeLists.txt + └── simple_test.c + +10 directories, 24 files +``` + +The actual code is all stored inside the src direcotry with any libs/submodules +stored inside their own subdirectories. + +The code we use to test stuff gets stored inside the dir "tests" in the +projects root. + +The compile_commands.json file in the project root is actaully a symlink to +the generated compile_commands.json file inside the build directory. So if +you notice your LSP isn't working correctly it probably means that you haven't +ran cmake yet. + + +## Running and building the project + +```sh +# change into the build dir +cd ./build + +# generate the make files and make everything. +cmake ../ -DUNIT_TESTING=ON; make all; + +# Run the tests +ctest + +``` + +# Adding tests + +To actually add tests to the project create a new directory inside the +"tests" dir. From there it will need a source.c and CMAKELists.txt file. + +You will also need to include it inside the tests/CMakeLists.txt file as well. + +```cmake +add_subdirectory(new_test) +``` + +## RoadMap + +Things I want to add in the future: + +- Automatic module creation +- template generation +- header --> mocking +- report generation +- embedded specific scripts for common hardware items + + + + + diff --git a/build/compile_commands.json b/build/compile_commands.json new file mode 100644 index 0000000..4641eb4 --- /dev/null +++ b/build/compile_commands.json @@ -0,0 +1,22 @@ +[ +{ + "directory": "/home/ronin/Documents/Projects/C/cmocka_template/build/src", + "command": "/usr/bin/cc -I/home/ronin/Documents/Projects/C/cmocka_template/src/led_driver -Wall -Werror -Wpedantic -std=gnu17 -o CMakeFiles/main.dir/main.c.o -c /home/ronin/Documents/Projects/C/cmocka_template/src/main.c", + "file": "/home/ronin/Documents/Projects/C/cmocka_template/src/main.c" +}, +{ + "directory": "/home/ronin/Documents/Projects/C/cmocka_template/build/src/led_driver", + "command": "/usr/bin/cc -I/home/ronin/Documents/Projects/C/cmocka_template/src/led_driver -Wall -Werror -Wpedantic -std=gnu17 -o CMakeFiles/led_driver.dir/led_driver.c.o -c /home/ronin/Documents/Projects/C/cmocka_template/src/led_driver/led_driver.c", + "file": "/home/ronin/Documents/Projects/C/cmocka_template/src/led_driver/led_driver.c" +}, +{ + "directory": "/home/ronin/Documents/Projects/C/cmocka_template/build/tests/led_driver", + "command": "/usr/bin/cc -I/usr/local/include -I/src -I/home/ronin/Documents/Projects/C/cmocka_template/src/led_driver -Wall -Werror -Wpedantic -std=gnu17 -o CMakeFiles/test_led_driver.dir/test_led_driver.c.o -c /home/ronin/Documents/Projects/C/cmocka_template/tests/led_driver/test_led_driver.c", + "file": "/home/ronin/Documents/Projects/C/cmocka_template/tests/led_driver/test_led_driver.c" +}, +{ + "directory": "/home/ronin/Documents/Projects/C/cmocka_template/build/tests/simple_test", + "command": "/usr/bin/cc -I/usr/local/include -Wall -Werror -Wpedantic -std=gnu17 -o CMakeFiles/simple_test.dir/simple_test.c.o -c /home/ronin/Documents/Projects/C/cmocka_template/tests/simple_test/simple_test.c", + "file": "/home/ronin/Documents/Projects/C/cmocka_template/tests/simple_test/simple_test.c" +} +] \ No newline at end of file diff --git a/cmake/cmocka/AddCCompilerFlag.cmake b/cmake/cmocka/AddCCompilerFlag.cmake new file mode 100644 index 0000000..c24c215 --- /dev/null +++ b/cmake/cmocka/AddCCompilerFlag.cmake @@ -0,0 +1,21 @@ +# +# add_c_compiler_flag("-Werror" SUPPORTED_CFLAGS) +# +# Copyright (c) 2018 Andreas Schneider +# +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + +include(CheckCCompilerFlag) + +macro(add_c_compiler_flag _COMPILER_FLAG _OUTPUT_VARIABLE) + string(TOUPPER ${_COMPILER_FLAG} _COMPILER_FLAG_NAME) + string(REGEX REPLACE "^-" "" _COMPILER_FLAG_NAME "${_COMPILER_FLAG_NAME}") + string(REGEX REPLACE "(-|=|\ )" "_" _COMPILER_FLAG_NAME "${_COMPILER_FLAG_NAME}") + + check_c_compiler_flag("${_COMPILER_FLAG}" WITH_${_COMPILER_FLAG_NAME}_FLAG) + if (WITH_${_COMPILER_FLAG_NAME}_FLAG) + #string(APPEND ${_OUTPUT_VARIABLE} "${_COMPILER_FLAG} ") + list(APPEND ${_OUTPUT_VARIABLE} ${_COMPILER_FLAG}) + endif() +endmacro() diff --git a/cmake/cmocka/AddCMockaTest.cmake b/cmake/cmocka/AddCMockaTest.cmake new file mode 100644 index 0000000..12fbd79 --- /dev/null +++ b/cmake/cmocka/AddCMockaTest.cmake @@ -0,0 +1,143 @@ +# +# Copyright (c) 2007 Daniel Gollub +# Copyright (c) 2007-2018 Andreas Schneider +# Copyright (c) 2018 Anderson Toshiyuki Sasaki +# +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + +#.rst: +# AddCMockaTest +# ------------- +# +# This file provides a function to add a test +# +# Functions provided +# ------------------ +# +# :: +# +# add_cmocka_test(target_name +# SOURCES src1 src2 ... srcN +# [COMPILE_OPTIONS opt1 opt2 ... optN] +# [LINK_LIBRARIES lib1 lib2 ... libN] +# [LINK_OPTIONS lopt1 lop2 .. loptN] +# ) +# +# ``target_name``: +# Required, expects the name of the test which will be used to define a target +# +# ``SOURCES``: +# Required, expects one or more source files names +# +# ``COMPILE_OPTIONS``: +# Optional, expects one or more options to be passed to the compiler +# +# ``LINK_LIBRARIES``: +# Optional, expects one or more libraries to be linked with the test +# executable. +# +# ``LINK_OPTIONS``: +# Optional, expects one or more options to be passed to the linker +# +# +# Example: +# +# .. code-block:: cmake +# +# add_cmocka_test(my_test +# SOURCES my_test.c other_source.c +# COMPILE_OPTIONS -g -Wall +# LINK_LIBRARIES mylib +# LINK_OPTIONS -Wl,--enable-syscall-fixup +# ) +# +# Where ``my_test`` is the name of the test, ``my_test.c`` and +# ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler +# options to be used, ``mylib`` is a target of a library to be linked, and +# ``-Wl,--enable-syscall-fixup`` is an option passed to the linker. +# + +enable_testing() +include(CTest) + +if (CMAKE_CROSSCOMPILING) + if (WIN32) + find_program(WINE_EXECUTABLE + NAMES wine) + set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "") + endif() +endif() + +function(ADD_CMOCKA_TEST _TARGET_NAME) + + set(one_value_arguments + ) + + set(multi_value_arguments + SOURCES + COMPILE_OPTIONS + LINK_LIBRARIES + LINK_OPTIONS + ) + + cmake_parse_arguments(_add_cmocka_test + "" + "${one_value_arguments}" + "${multi_value_arguments}" + ${ARGN} + ) + + if (NOT DEFINED _add_cmocka_test_SOURCES) + message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}") + endif() + + add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES}) + + if (DEFINED _add_cmocka_test_COMPILE_OPTIONS) + target_compile_options(${_TARGET_NAME} + PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS} + ) + endif() + + if (DEFINED _add_cmocka_test_LINK_LIBRARIES) + target_link_libraries(${_TARGET_NAME} + PRIVATE ${_add_cmocka_test_LINK_LIBRARIES} + ) + endif() + + if (DEFINED _add_cmocka_test_LINK_OPTIONS) + set_target_properties(${_TARGET_NAME} + PROPERTIES LINK_FLAGS + ${_add_cmocka_test_LINK_OPTIONS} + ) + endif() + + add_test(${_TARGET_NAME} + ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME} + ) + +endfunction (ADD_CMOCKA_TEST) + +function(ADD_CMOCKA_TEST_ENVIRONMENT _TARGET_NAME) + if (WIN32 OR CYGWIN OR MINGW OR MSVC) + file(TO_NATIVE_PATH "${cmocka-library_BINARY_DIR}" CMOCKA_DLL_PATH) + + if (TARGET_SYSTEM_EMULATOR) + set(DLL_PATH_ENV "WINEPATH=${CMOCKA_DLL_PATH};$ENV{WINEPATH}") + else() + set(DLL_PATH_ENV "PATH=${CMOCKA_DLL_PATH}\\${CMAKE_BUILD_TYPE};$ENV{PATH}") + endif() + # + # IMPORTANT NOTE: The set_tests_properties(), below, internally + # stores its name/value pairs with a semicolon delimiter. + # because of this we must protect the semicolons in the path + # + string(REPLACE ";" "\\;" DLL_PATH_ENV "${DLL_PATH_ENV}") + + set_tests_properties(${_TARGET_NAME} + PROPERTIES + ENVIRONMENT + "${DLL_PATH_ENV}") + endif() +endfunction() diff --git a/cmake/cmocka/COPYING-CMAKE-SCRIPTS b/cmake/cmocka/COPYING-CMAKE-SCRIPTS new file mode 100644 index 0000000..4b41776 --- /dev/null +++ b/cmake/cmocka/COPYING-CMAKE-SCRIPTS @@ -0,0 +1,22 @@ +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/cmake/cmocka/CheckCCompilerFlagSSP.cmake b/cmake/cmocka/CheckCCompilerFlagSSP.cmake new file mode 100644 index 0000000..eeaf4fc --- /dev/null +++ b/cmake/cmocka/CheckCCompilerFlagSSP.cmake @@ -0,0 +1,48 @@ +# - Check whether the C compiler supports a given flag in the +# context of a stack checking compiler option. + +# CHECK_C_COMPILER_FLAG_SSP(FLAG VARIABLE) +# +# FLAG - the compiler flag +# VARIABLE - variable to store the result +# +# This actually calls check_c_source_compiles. +# See help for CheckCSourceCompiles for a listing of variables +# that can modify the build. + +# Copyright (c) 2006, Alexander Neundorf, +# +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + +# Requires cmake 3.10 +#include_guard(GLOBAL) +include(CheckCSourceCompiles) +include(CMakeCheckCompilerFlagCommonPatterns) + +macro(CHECK_C_COMPILER_FLAG_SSP _FLAG _RESULT) + set(SAFE_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + set(CMAKE_REQUIRED_FLAGS "${_FLAG}") + + # Normalize locale during test compilation. + set(_CheckCCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG) + foreach(v ${_CheckCCompilerFlag_LOCALE_VARS}) + set(_CheckCCompilerFlag_SAVED_${v} "$ENV{${v}}") + set(ENV{${v}} C) + endforeach() + + CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckCCompilerFlag_COMMON_PATTERNS) + check_c_source_compiles("int main(int argc, char **argv) { char buffer[256]; return buffer[argc]=0;}" + ${_RESULT} + # Some compilers do not fail with a bad flag + FAIL_REGEX "command line option .* is valid for .* but not for C" # GNU + ${_CheckCCompilerFlag_COMMON_PATTERNS}) + foreach(v ${_CheckCCompilerFlag_LOCALE_VARS}) + set(ENV{${v}} ${_CheckCCompilerFlag_SAVED_${v}}) + unset(_CheckCCompilerFlag_SAVED_${v}) + endforeach() + unset(_CheckCCompilerFlag_LOCALE_VARS) + unset(_CheckCCompilerFlag_COMMON_PATTERNS) + + set(CMAKE_REQUIRED_FLAGS "${SAFE_CMAKE_REQUIRED_FLAGS}") +endmacro(CHECK_C_COMPILER_FLAG_SSP) diff --git a/cmake/cmocka/DefineCMakeDefaults.cmake b/cmake/cmocka/DefineCMakeDefaults.cmake new file mode 100644 index 0000000..9fb034f --- /dev/null +++ b/cmake/cmocka/DefineCMakeDefaults.cmake @@ -0,0 +1,25 @@ +# Always include srcdir and builddir in include path +# This saves typing ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY} in +# about every subdir +# since cmake 2.4.0 +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +# Put the include dirs which are in the source or build tree +# before all other include dirs, so the headers in the sources +# are prefered over the already installed ones +# since cmake 2.4.1 +set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON) + +# Use colored output +# since cmake 2.4.0 +set(CMAKE_COLOR_MAKEFILE ON) + +# Create the compile command database for clang by default +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Always build with -fPIC +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# Avoid source tree pollution +set(CMAKE_DISABLE_SOURCE_CHANGES ON) +set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) diff --git a/cmake/cmocka/DefineCompilerFlags.cmake b/cmake/cmocka/DefineCompilerFlags.cmake new file mode 100644 index 0000000..c9aea58 --- /dev/null +++ b/cmake/cmocka/DefineCompilerFlags.cmake @@ -0,0 +1,49 @@ +if (UNIX AND NOT WIN32) + # Activate with: -DCMAKE_BUILD_TYPE=Profiling + set(CMAKE_C_FLAGS_PROFILING "-O0 -g -fprofile-arcs -ftest-coverage" + CACHE STRING "Flags used by the C compiler during PROFILING builds.") + set(CMAKE_CXX_FLAGS_PROFILING "-O0 -g -fprofile-arcs -ftest-coverage" + CACHE STRING "Flags used by the CXX compiler during PROFILING builds.") + set(CMAKE_SHARED_LINKER_FLAGS_PROFILING "-fprofile-arcs -ftest-coverage" + CACHE STRING "Flags used by the linker during the creation of shared libraries during PROFILING builds.") + set(CMAKE_MODULE_LINKER_FLAGS_PROFILING "-fprofile-arcs -ftest-coverage" + CACHE STRING "Flags used by the linker during the creation of shared libraries during PROFILING builds.") + set(CMAKE_EXEC_LINKER_FLAGS_PROFILING "-fprofile-arcs -ftest-coverage" + CACHE STRING "Flags used by the linker during PROFILING builds.") + + # Activate with: -DCMAKE_BUILD_TYPE=AddressSanitizer + set(CMAKE_C_FLAGS_ADDRESSSANITIZER "-g -O1 -fsanitize=address -fno-omit-frame-pointer" + CACHE STRING "Flags used by the C compiler during ADDRESSSANITIZER builds.") + set(CMAKE_CXX_FLAGS_ADDRESSSANITIZER "-g -O1 -fsanitize=address -fno-omit-frame-pointer" + CACHE STRING "Flags used by the CXX compiler during ADDRESSSANITIZER builds.") + set(CMAKE_SHARED_LINKER_FLAGS_ADDRESSSANITIZER "-fsanitize=address" + CACHE STRING "Flags used by the linker during the creation of shared libraries during ADDRESSSANITIZER builds.") + set(CMAKE_MODULE_LINKER_FLAGS_ADDRESSSANITIZER "-fsanitize=address" + CACHE STRING "Flags used by the linker during the creation of shared libraries during ADDRESSSANITIZER builds.") + set(CMAKE_EXEC_LINKER_FLAGS_ADDRESSSANITIZER "-fsanitize=address" + CACHE STRING "Flags used by the linker during ADDRESSSANITIZER builds.") + + # Activate with: -DCMAKE_BUILD_TYPE=MemorySanitizer + set(CMAKE_C_FLAGS_MEMORYSANITIZER "-g -O2 -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer" + CACHE STRING "Flags used by the C compiler during MEMORYSANITIZER builds.") + set(CMAKE_CXX_FLAGS_MEMORYSANITIZER "-g -O2 -fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer" + CACHE STRING "Flags used by the CXX compiler during MEMORYSANITIZER builds.") + set(CMAKE_SHARED_LINKER_FLAGS_MEMORYSANITIZER "-fsanitize=memory" + CACHE STRING "Flags used by the linker during the creation of shared libraries during MEMORYSANITIZER builds.") + set(CMAKE_MODULE_LINKER_FLAGS_MEMORYSANITIZER "-fsanitize=memory" + CACHE STRING "Flags used by the linker during the creation of shared libraries during MEMORYSANITIZER builds.") + set(CMAKE_EXEC_LINKER_FLAGS_MEMORYSANITIZER "-fsanitize=memory" + CACHE STRING "Flags used by the linker during MEMORYSANITIZER builds.") + + # Activate with: -DCMAKE_BUILD_TYPE=UndefinedSanitizer + set(CMAKE_C_FLAGS_UNDEFINEDSANITIZER "-g -O1 -fsanitize=undefined -fsanitize=null -fsanitize=alignment -fno-sanitize-recover" + CACHE STRING "Flags used by the C compiler during UNDEFINEDSANITIZER builds.") + set(CMAKE_CXX_FLAGS_UNDEFINEDSANITIZER "-g -O1 -fsanitize=undefined -fsanitize=null -fsanitize=alignment -fno-sanitize-recover" + CACHE STRING "Flags used by the CXX compiler during UNDEFINEDSANITIZER builds.") + set(CMAKE_SHARED_LINKER_FLAGS_UNDEFINEDSANITIZER "-fsanitize=undefined" + CACHE STRING "Flags used by the linker during the creation of shared libraries during UNDEFINEDSANITIZER builds.") + set(CMAKE_MODULE_LINKER_FLAGS_UNDEFINEDSANITIZER "-fsanitize=undefined" + CACHE STRING "Flags used by the linker during the creation of shared libraries during UNDEFINEDSANITIZER builds.") + set(CMAKE_EXEC_LINKER_FLAGS_UNDEFINEDSANITIZER "-fsanitize=undefined" + CACHE STRING "Flags used by the linker during UNDEFINEDSANITIZER builds.") +endif() diff --git a/cmake/cmocka/DefinePlatformDefaults.cmake b/cmake/cmocka/DefinePlatformDefaults.cmake new file mode 100644 index 0000000..46c3185 --- /dev/null +++ b/cmake/cmocka/DefinePlatformDefaults.cmake @@ -0,0 +1,21 @@ +# Set system vars + +if (CMAKE_SYSTEM_NAME MATCHES "Linux") + set(LINUX TRUE) +endif(CMAKE_SYSTEM_NAME MATCHES "Linux") + +if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + set(FREEBSD TRUE) +endif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") + +if (CMAKE_SYSTEM_NAME MATCHES "OpenBSD") + set(OPENBSD TRUE) +endif (CMAKE_SYSTEM_NAME MATCHES "OpenBSD") + +if (CMAKE_SYSTEM_NAME MATCHES "NetBSD") + set(NETBSD TRUE) +endif (CMAKE_SYSTEM_NAME MATCHES "NetBSD") + +if (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") + set(SOLARIS TRUE) +endif (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") diff --git a/cmake/cmocka/FindNSIS.cmake b/cmake/cmocka/FindNSIS.cmake new file mode 100644 index 0000000..9f1ab17 --- /dev/null +++ b/cmake/cmocka/FindNSIS.cmake @@ -0,0 +1,54 @@ +# - Try to find NSIS +# Once done this will define +# +# NSIS_ROOT_PATH - Set this variable to the root installation of NSIS +# +# Read-Only variables: +# +# NSIS_FOUND - system has NSIS +# NSIS_MAKE - NSIS creator executable +# +#============================================================================= +# Copyright (c) 2010-2013 Andreas Schneider +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# + +if (WIN32) + set(_x86 "(x86)") + + set(_NSIS_ROOT_PATHS + "$ENV{ProgramFiles}/NSIS" + "$ENV{ProgramFiles${_x86}}/NSIS" + "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\NSIS;Default]") + + find_path(NSIS_ROOT_PATH + NAMES + Include/Library.nsh + PATHS + ${_NSIS_ROOT_PATHS} + ) + mark_as_advanced(NSIS_ROOT_PATH) +endif (WIN32) + +find_program(NSIS_MAKE + NAMES + makensis + PATHS + ${NSIS_ROOT_PATH} +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(NSIS DEFAULT_MSG NSIS_MAKE) + +if (NSIS_MAKE) + set(NSIS_FOUND TRUE) +endif (NSIS_MAKE) + +mark_as_advanced(NSIS_MAKE) diff --git a/cmake/cmocka/MacroEnsureOutOfSourceBuild.cmake b/cmake/cmocka/MacroEnsureOutOfSourceBuild.cmake new file mode 100644 index 0000000..a2e9480 --- /dev/null +++ b/cmake/cmocka/MacroEnsureOutOfSourceBuild.cmake @@ -0,0 +1,17 @@ +# - MACRO_ENSURE_OUT_OF_SOURCE_BUILD() +# MACRO_ENSURE_OUT_OF_SOURCE_BUILD() + +# Copyright (c) 2006, Alexander Neundorf, +# +# Redistribution and use is allowed according to the terms of the BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + +macro (MACRO_ENSURE_OUT_OF_SOURCE_BUILD _errorMessage) + + string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" _insource) + if (_insource) + message(SEND_ERROR "${_errorMessage}") + message(FATAL_ERROR "Remove the file CMakeCache.txt in ${CMAKE_SOURCE_DIR} first.") + endif (_insource) + +endmacro (MACRO_ENSURE_OUT_OF_SOURCE_BUILD) diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..affbd32 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +./build/compile_commands.json \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e89dd68 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,10 @@ +add_subdirectory(led_driver) + + +add_executable(main + main.c +) + +target_link_libraries(main + led_driver +) diff --git a/src/led_driver/CMakeLists.txt b/src/led_driver/CMakeLists.txt new file mode 100644 index 0000000..49da348 --- /dev/null +++ b/src/led_driver/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library(led_driver STATIC + led_driver.c +) + +target_include_directories(led_driver PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) diff --git a/src/led_driver/led_driver.c b/src/led_driver/led_driver.c new file mode 100644 index 0000000..f845ab6 --- /dev/null +++ b/src/led_driver/led_driver.c @@ -0,0 +1,21 @@ +#include "led_driver.h" + +uint8_t internal_led_state = 0x00; +uint8_t *led_state = &internal_led_state; + +void init_led_driver(void) +{ + led_state = &internal_led_state; +} + +uint8_t get_led_state(void) +{ + return *led_state; +} + +void set_led_state(uint8_t state) +{ + *led_state = state; +} + + diff --git a/src/led_driver/led_driver.h b/src/led_driver/led_driver.h new file mode 100644 index 0000000..7001114 --- /dev/null +++ b/src/led_driver/led_driver.h @@ -0,0 +1,12 @@ +#ifndef LED_DRIVER +#define LED_DRIVER + +#include +//extern volatile uint8_t *led_state; + + +uint8_t get_led_state(void); +void set_led_state(uint8_t state); +void init_led_driver(void); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..84d2b3f --- /dev/null +++ b/src/main.c @@ -0,0 +1,17 @@ +/* + * Author: Jake Goodwin + * Date: 2023 + * Description: + */ + +#include "main.h" + + + +int main(void) +{ + printf("Hello World\n"); + uint8_t state = get_led_state(); + printf("led state: %d", state); + return 0; +} diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..8d7e17e --- /dev/null +++ b/src/main.h @@ -0,0 +1,4 @@ +#include +//#include +#include +#include "led_driver/led_driver.h" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..35b495e --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,4 @@ +# FILE: /tests/CMakeLists.txt +# +add_subdirectory(led_driver) +add_subdirectory(simple_test) diff --git a/tests/led_driver/CMakeLists.txt b/tests/led_driver/CMakeLists.txt new file mode 100644 index 0000000..1442371 --- /dev/null +++ b/tests/led_driver/CMakeLists.txt @@ -0,0 +1,13 @@ +# The led_driver module tests +list(APPEND TEST_LIBS "${CMOCKA_LIBRARIES}") +list(APPEND TEST_LIBS led_driver) + +list(APPEND TEST_DIRS "${CMOCKA_INCLUDE_DIRS}") +list(APPEND TEST_DIRS "${MyProject_SOURCE_DIR}/src") + +add_cmocka_test(test_led_driver + SOURCES test_led_driver.c + COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} + LINK_LIBRARIES "${TEST_LIBS}") +add_cmocka_test_environment(test_led_driver) +target_include_directories(test_led_driver PUBLIC "${TEST_DIRS}") diff --git a/tests/led_driver/test_led_driver.c b/tests/led_driver/test_led_driver.c new file mode 100644 index 0000000..7d47c40 --- /dev/null +++ b/tests/led_driver/test_led_driver.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include + +/* A test case that does nothing and succeeds. */ +static void null_test_success(void **state) { + (void) state; /* unused */ +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(null_test_success), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/tests/simple_test/CMakeLists.txt b/tests/simple_test/CMakeLists.txt new file mode 100644 index 0000000..0fadc14 --- /dev/null +++ b/tests/simple_test/CMakeLists.txt @@ -0,0 +1,7 @@ +# The example one. +add_cmocka_test(simple_test + SOURCES simple_test.c + COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} + LINK_LIBRARIES "${CMOCKA_LIBRARIES}") +add_cmocka_test_environment(simple_test) +target_include_directories(simple_test PUBLIC "${CMOCKA_INCLUDE_DIRS}") diff --git a/tests/simple_test/simple_test.c b/tests/simple_test/simple_test.c new file mode 100644 index 0000000..c8c52ff --- /dev/null +++ b/tests/simple_test/simple_test.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include + +/* A test case that does nothing and succeeds. */ +static void null_test_success(void **state) { + (void) state; /* unused */ +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(null_test_success), + }; + +return cmocka_run_group_tests(tests, NULL, NULL); +}