Skip to content

Commit

Permalink
refs #120 @2h #17
Browse files Browse the repository at this point in the history
- created a Cmake package that handles SonarCloud setup and defines needed make targets
  • Loading branch information
HerbertKoelman committed Jun 23, 2019
1 parent 40652e1 commit 196599c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ build/**
cmake-build*/
delivery/**

# SONAR Qube related working directories
.scannerwork

# extension
*.swp
*.o
Expand Down
31 changes: 14 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ project(
DESCRIPTION "Simple C++ logger (${GIT_LOG})")

option(GCOV "Activate GCOV options")
option(SONAR "Generate sonar setup" ON)

if ( GCOV )
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
Expand All @@ -23,6 +22,8 @@ endif()

#find_package(Git CONFIG)
#find_package(Conan)
find_package(GTestExt PATHS cmake)
find_package(SonarCloud PATHS cmake)

# main targets --------------------------------------------------
#
Expand All @@ -40,9 +41,6 @@ else()
endif()
endif()

message(STATUS "Generating SONAR properties file")
configure_file(sonar-project.properties.in sonar-project.properties)

# project's public headers
include_directories(include src)

Expand All @@ -62,18 +60,6 @@ set(USED_COMPILER_FEATURES
cxx_std_11
)

# For future use
#include(WriteCompilerDetectionHeader)
#write_compiler_detection_header(
# FILE "${CMAKE_CURRENT_BINARY_DIR}/logger_compiler_detection.h"
# PREFIX LOGGER
# COMPILERS GNU AppleClang Clang
# PROLOG "// Requested features: ${USED_COMPILER_FEATURES}"
# FEATURES
# ${USED_COMPILER_FEATURES}
# cxx_variadic_templates
#)

add_library (cpp-logger-static STATIC ${LOGGER_SOURCE})
target_link_libraries (cpp-logger-static pthread)
target_compile_features(cpp-logger-static PUBLIC ${USED_COMPILER_FEATURES})
Expand All @@ -88,7 +74,6 @@ set_target_properties (cpp-logger-shared PROPERTIES OUTPUT_NAME logger)
#
# Load and compile GTest
# Aliases: GTest::GTest, GTest::gtest_main, GMock::GMock
find_package(GTestExt PATHS cmake)
if (GTestExt_FOUND AND BUILD_TESTS)
enable_testing()
message(STATUS "Adding project's unit tests (in ./tests)...")
Expand Down Expand Up @@ -133,3 +118,15 @@ cpack_add_install_type(develop DESCRIPTION "includes & libs (for developpers)")
# misc ------------------------------------------------------------
#
find_program(RM rm)

# For future use
#include(WriteCompilerDetectionHeader)
#write_compiler_detection_header(
# FILE "${CMAKE_CURRENT_BINARY_DIR}/logger_compiler_detection.h"
# PREFIX LOGGER
# COMPILERS GNU AppleClang Clang
# PROLOG "// Requested features: ${USED_COMPILER_FEATURES}"
# FEATURES
# ${USED_COMPILER_FEATURES}
# cxx_variadic_templates
#)
39 changes: 39 additions & 0 deletions cmake/SonarCloudConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
message(STATUS "Loading SonarCloud module found here: ${CMAKE_CURRENT_LIST_DIR}")

option(SONAR "Generate sonar setup" ON)
set(SONAR_CLOUD_HOME https://sonarcloud.io CACHE STRING "Sonar cloud home URL(default is https://sonarcloud.io)")
set(SONAR_PROJECT_KEY ${PROJECT_NAME} CACHE STRING "Sonar project key property (default is ${PROJECT_NAME})")
set(SONAR_ORGANIZATION CACHE STRING "Organization (default is empty)")
set(SONAR_ACCESS_TOKEN CACHE STRING "Access/login token (default is empty)")
set(SONAR_WRAPPER_OUTPUT_DIR bw-output CACHE STRING "Sonar C/C++ wrapper output directory (default is empty)")

set(SONAR_PROPERTIES_FILE CACHE STRING "If file exists, it will used as is (default is empty)")

if ( SONAR )

find_program(SONAR_SCANNER sonar-scanner)

if ( SONAR_SCANNER )
if(NOT EXISTS ${SONAR_PROPERTIES_FILE} )
message(STATUS "Generating SONAR properties file (${CMAKE_CURRENT_BINARY_DIR}/sonar-project.properties)")
configure_file(${CMAKE_CURRENT_LIST_DIR}/sonar-project.properties.in sonar-project.properties)
set(SONAR_PROPERTIES_FILE ./sonar-project.properties)
else()
message(STATUS "Sonar scanner property file was provided (${SONAR_PROPERTIES_FILE})")
endif()

add_custom_target( code-quality
COMMAND ${SONAR_SCANNER} -Dproject.settings=${SONAR_PROPERTIES_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "run ${SONAR_SCANNER} -Dproject.settings=${SONAR_PROPERTIES_FILE} "
)
message(STATUS "Added custom target [code-quality]...")

else()
message(SEND_ERROR "Failed to find the program [sonar_scanner], make sure sonar tools are installed.")
endif()

else()
message(STATUS "SONAR setup skipped, option was FALSE")
endif()

12 changes: 12 additions & 0 deletions cmake/sonar-project.properties.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Project Configuration @PROJECT_NAME@ version @PROJECT_VERSION@
sonar.verbose=false
sonar.projectBaseDir=@CMAKE_SOURCE_DIR@
sonar.projectKey=@SONAR_PROJECT_KEY@
sonar.organization=@SONAR_ORGANIZATION@
sonar.links.homepage=@SONAR_PROJECT_HOME@
sonar.host.url=@SONAR_CLOUD_HOME@
sonar.login=@SONAR_ACCESS_TOKEN@
sonar.cfamily.build-wrapper-output=@SONAR_WRAPPER_OUTPUT_DIR@
sonar.projectName=@PROJECT_NAME@
sonar.projectVersion=@PROJECT_VERSION@
sonar.sources=src,include
31 changes: 0 additions & 31 deletions sonar-project.properties.in

This file was deleted.

0 comments on commit 196599c

Please sign in to comment.