Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zakaria-fadli-netatmo committed Nov 10, 2024
0 parents commit cdd5c9a
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build & Check

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
install:
runs-on: ubuntu-latest

strategy:
matrix:
build_type: [Debug, Release]

steps:
- uses: actions/checkout@v3

- name: Install pre-requisits
run: sudo ./first_setup.sh


- name: Configure CMake
# Configure CMake in a 'build' subdirectory.
run: cmake -B ${{github.workspace}}/build -G 'Ninja Multi-Config'

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.build_type }}

- name: clang-tidy
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.build_type }} -t clang-tidy

- name: cppcheck
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.build_type }} -t cppcheck

54 changes: 54 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Compiled Object files
**/.DS_Store
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

**/cmake-build-debug
**/CMakeCache.txt
**/cmake_install.cmake
**/install_manifest.txt
**/CMakeFiles/
**/CTestTestfile.cmake
**/*.cbp
**/CMakeScripts
**/compile_commands.json


## Local

.idea/*.xml

build/**/*

lib/*
bin/*
test/test_runner

# clangd cache
.cache/
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "googletest"]
path = test/lib/googletest
url = https://github.com/google/googletest.git
branch = master
239 changes: 239 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
############################################################
# Build types definitions
############################################################
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

if(isMultiConfig)
# Necessary for multi-configuration generators such as Ninja Multi-Config
set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
set(CMAKE_DEFAULT_BUILD_TYPE "Debug")
message(STATUS "Multi-configuration generator used, append --config <Debug|Release> "
"to build the desired configuration.")

else()
# Only relevant for Makefile/Ninja generators (And other single-configuration generators)
# Manually set the default build type to Debug
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
message(STATUS "Setting build type to 'Debug' as none was specified.")
endif()
# Print the build type and flags
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
endif()

############################################################
# Set compiler, language and flags
# Keep before project() call
############################################################
# Set the C++ standard to use
set(CMAKE_CXX_STANDARD 23)

# Set the clang++ compiler
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_SIZE "/usr/bin/size")

# Colorize output
set(CMAKE_COLOR_DIAGNOSTICS ON)

############################################################
# CMake options
############################################################
cmake_minimum_required(VERSION 3.9)

project(my_template VERSION 1.0.0)

# Compile_commands.json generation
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

# Set to ON to enable verbose output when building
set(CMAKE_VERBOSE_MAKEFILE OFF)

set(PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR})


############################################################
# Create a library
############################################################
set(LIBRARY_SOURCES
src/lib/lib.cpp
)

#Generate the static library from the library sources
add_library(custom_library STATIC
${LIBRARY_SOURCES}
)

target_include_directories(custom_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)

# Compiler flags
target_compile_options(custom_library PRIVATE
-Wall
-Wextra
-Wno-unused-parameter
-Wno-unused-private-field
$<$<CONFIG:Debug>:
-ggdb3
-fdebug-macro
-fparse-all-comments
-fno-omit-frame-pointer
-fno-optimize-sibling-calls
-fsanitize=address,undefined
>
$<$<CONFIG:Release>:
-O2
>
)

############################################################
# Create an executable
############################################################
set(EXECUTABLE_SOURCES
src/main.cpp
)

# Add an executable with the above sources
add_executable(my_template_binary
${EXECUTABLE_SOURCES}
)

# link the new hello_library target with the hello_binary target
target_link_libraries(my_template_binary
PRIVATE
custom_library
)

# Linker flags
target_link_options(my_template_binary PRIVATE
-Wl,-Map=output.map
$<$<CONFIG:Debug>:
-fsanitize=address,undefined
>
$<$<CONFIG:Release>:
-flto
>
)

target_compile_options(my_template_binary PRIVATE $<TARGET_PROPERTY:custom_library,COMPILE_OPTIONS>)

############################################################
# Post build commands
############################################################

# Generate the .bin
add_custom_command(TARGET my_template_binary POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:my_template_binary> $<TARGET_FILE:my_template_binary>.bin
COMMENT "Generating .bin"
)

# Print the size
add_custom_command(TARGET my_template_binary POST_BUILD
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:my_template_binary>
COMMENT "Size of the executable"
)

############################################################
# Gen vars for include files
############################################################

file(GLOB_RECURSE
PRIVATE_INCLUDE_FILES
"${CMAKE_SOURCE_DIR}/src/include/*"
)

file(GLOB_RECURSE
PUBLIC_INCLUDE_FILES
"${CMAKE_SOURCE_DIR}/include/*"
)

############################################################
# Add clang tidy
############################################################

# Add clang-tidy to the project
find_program(CLANG_TIDY_EXE NAMES "clang-tidy-17")

if(CLANG_TIDY_EXE)
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(DO_CLANG_TIDY
"${CLANG_TIDY_EXE}"
--use-color # Colorize output
-p ${CMAKE_BINARY_DIR}/compile_commands.json
)
add_custom_target(
clang-tidy
COMMAND ${DO_CLANG_TIDY} ${EXECUTABLE_SOURCES} ${LIBRARY_SOURCES}
WORKING_DIRECTORY ${PROJECT_DIR}
COMMENT "Running clang-tidy"
VERBATIM
)
add_custom_target(
clang-tidy-fix
COMMAND ${DO_CLANG_TIDY} -fix ${EXECUTABLE_SOURCES} ${LIBRARY_SOURCES}
WORKING_DIRECTORY ${PROJECT_DIR}
COMMENT "Running clang-tidy fix"
VERBATIM
)
else()
message(STATUS "clang-tidy not found.")
endif()

############################################################
# Add clang format
############################################################

# Add clang-format to the project only if version >= 17
# using --version
find_program(CLANG_FORMAT "clang-format-17")

if(CLANG_FORMAT)
message(STATUS "clang-format found: ${CLANG_FORMAT}")
# Add a custom target to run Clang-Format on all relevant files
file(GLOB_RECURSE FORMAT_FILES
${PUBLIC_INCLUDE_FILES}
${PRIVATE_INCLUDE_FILES}
${EXECUTABLE_SOURCES}
${LIBRARY_SOURCES})

set(CLANG_FORMAT_CONFIG_FILE "${CMAKE_SOURCE_DIR}/.clang-format")
add_custom_target(clang-format
COMMAND ${CLANG_FORMAT}
-style=file # Look for the .clang-format file in the WORKING_DIRECTORY
-i # In-place editing of the files
${FORMAT_FILES} # Files to format
COMMENT "Running Clang-Format"
WORKING_DIRECTORY ${PROJECT_DIR}
VERBATIM
)
else()
message(STATUS "clang-format not found.")
endif()

############################################################
# Add cppcheck
############################################################

find_program(CPPCHECK "cppcheck")

if(CPPCHECK)
message(STATUS "cppcheck found: ${CPPCHECK}")
# Add a custom target to run Clang-Format on all relevant files
file(GLOB_RECURSE CPPCHECK_FILES
${PUBLIC_INCLUDE_FILES}
${PRIVATE_INCLUDE_FILES}
${EXECUTABLE_SOURCES}
${LIBRARY_SOURCES})

add_custom_target(cppcheck
COMMAND ${CPPCHECK}
--enable=all
${CPPCHECK_FILES} # Files to format
COMMENT "Running cppcheck"
WORKING_DIRECTORY ${PROJECT_DIR}
)
else()
message(STATUS "cppcheck not found.")
endif()
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
BUILD_DIR :=./build
GENERATOR :='Ninja Multi-Config'

configure:
cmake --fresh -B ${BUILD_DIR} -G ${GENERATOR}

RELEASE ?= n
ifeq (${RELEASE},y)
CONFIG=Release
else
CONFIG=Debug
endif

DRY_RUN ?= n
ifeq (${DRY_RUN},y)
DRY_RUN_FLAG=-- -n
$(info Dry run)
else
DRY_RUN_FLAG=
endif

VERBOSE ?= n
ifeq (${VERBOSE},y)
VERBOSE_FLAG=--verbose
$(info Verbose)
else
VERBOSE_FLAG=
endif

$(info verbose = ${VERBOSE})

build_$(CONFIG):
$(info Build Config = ${CONFIG})
cmake --build ${BUILD_DIR} --config ${CONFIG} ${VERBOSE_FLAG} ${DRY_RUN_FLAG}

build: build_$(CONFIG)

%:
cmake --build ${BUILD_DIR} -t $@ ${VERBOSE_FLAG} ${DRY_RUN_FLAG}

OUTPUT_BIN := ${BUILD_DIR}/$(CONFIG)/mockbee_test_bin

run: build_$(CONFIG)
${OUTPUT_BIN}

gdb: build_$(CONFIG)
gdb-multiarch ${OUTPUT_BIN} -ex "run"

.PHONY: build build_$(CONFIG) configure run
Loading

0 comments on commit cdd5c9a

Please sign in to comment.