Skip to content

Commit abb2a0e

Browse files
committed
Reformed CMake script for easier use
Using items from the cmake-scripts repository.
1 parent db51c6e commit abb2a0e

File tree

5 files changed

+403
-57
lines changed

5 files changed

+403
-57
lines changed

CMakeLists.txt

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,25 @@
11
cmake_minimum_required(VERSION 3.2)
22
project(UnicodeHpp)
33

4-
# GCC
5-
if(CMAKE_COMPILER_IS_GNUCXX AND NOT COMPILER_FLAGS_SET)
6-
set(COMPILER_FLAGS_SET ON)
7-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Weffc++")
8-
endif()
4+
include(cmake/c++-standards.cmake)
5+
include(cmake/clang-tools.cmake)
6+
include(cmake/code-coverage.cmake)
7+
include(cmake/sanitizer-builds.cmake)
98

10-
# C++17
11-
set(CMAKE_CXX_STANDARD 11)
12-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13-
set(CMAKE_CXX_EXTENSIONS OFF)
14-
15-
if (MSVC_VERSION GREATER_EQUAL "1900" AND CMAKE_VERSION LESS 3.10)
16-
include(CheckCXXCompilerFlag)
17-
CHECK_CXX_COMPILER_FLAG("/std:c++11" _cpp_latest_flag_supported)
18-
if (_cpp_latest_flag_supported)
19-
add_compile_options("/std:c++11")
20-
endif()
21-
endif()
22-
23-
# Clang Tools
249
file(GLOB_RECURSE ALL_CXX_SOURCE_FILES
2510
${PROJECT_SOURCE_DIR}/src/*.[ch]pp
2611
${PROJECT_SOURCE_DIR}/src/*.[ch]
2712
)
2813

29-
if(NOT CLANG_FORMAT)
30-
find_program(CLANG_FORMAT "clang-format")
31-
endif()
32-
if(CLANG_FORMAT)
33-
add_custom_target(
34-
${PROJECT_NAME}_format
35-
COMMAND clang-format
36-
-i
37-
-style=file
38-
${ALL_CXX_TEST_SOURCE_FILES}
39-
)
40-
41-
if(NOT TARGET format)
42-
add_custom_target(format)
43-
endif()
14+
_Cxx11()
15+
_ClangFormat(UnicodeHpp ${ALL_CXX_SOURCE_FILES})
16+
_ClangTidy(UnicodeHpp ${ALL_CXX_SOURCE_FILES})
4417

45-
add_dependencies(format ${PROJECT_NAME}_format)
46-
endif()
47-
48-
if(NOT CLANG_TIDY)
49-
find_program(CLANG_TIDY "clang-tidy")
50-
endif()
51-
if(CLANG_TIDY)
52-
add_custom_target(
53-
${PROJECT_NAME}_tidy
54-
COMMAND clang-tidy
55-
${ALL_CXX_SOURCE_FILES}
56-
-checks=*,-cert-err58-cpp,-readability-else-after-return,-readability-named-parameter,-llvm-header-guard,-readability-avoid-const-params-in-decls,-readability-implicit-bool-cast,-google-build-using-namespace,-cppcoreguidelines-pro-bounds-constant-array-index,-google-readability-namespace-comments,-llvm-namespace-comment,-google-explicit-constructor,-readability-redundant-member-init,-misc-unused-parameters,-google-runtime-int,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-modernize-use-auto,-hicpp-explicit-conversions
57-
--
58-
-std=c++17
59-
-I${CMAKE_CURRENT_SOURCE_DIR}/ext
60-
)
61-
62-
if(NOT TARGET tidy)
63-
add_custom_target(tidy)
64-
endif()
65-
66-
add_dependencies(tidy ${PROJECT_NAME}_tidy)
18+
# GCC
19+
if(CMAKE_COMPILER_IS_GNUCXX AND NOT COMPILER_FLAGS_SET)
20+
set(COMPILER_FLAGS_SET ON)
21+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Weffc++")
6722
endif()
6823

69-
add_executable(UnicodeHpp src/unicode_cpp_generator.cpp)
24+
_AddExecutable(UnicodeHpp src/unicode_cpp_generator.cpp)
7025
target_include_directories(UnicodeHpp PUBLIC ext)

cmake/c++-standards.cmake

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018 George Cave
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
# Set the compiler standard to C++11
24+
macro(_Cxx11)
25+
set(CMAKE_CXX_STANDARD 11)
26+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
27+
set(CMAKE_CXX_EXTENSIONS OFF)
28+
29+
if(MSVC_VERSION GREATER_EQUAL "1900" AND CMAKE_VERSION LESS 3.10)
30+
include(CheckCXXCompilerFlag)
31+
CHECK_CXX_COMPILER_FLAG("/std:c++11" _cpp_latest_flag_supported)
32+
if(_cpp_latest_flag_supported)
33+
add_compile_options("/std:c++11")
34+
endif()
35+
endif()
36+
endmacro()
37+
38+
# Set the compiler standard to C++14
39+
macro(_Cxx14)
40+
set(CMAKE_CXX_STANDARD 14)
41+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
42+
set(CMAKE_CXX_EXTENSIONS OFF)
43+
44+
if(MSVC_VERSION GREATER_EQUAL "1900" AND CMAKE_VERSION LESS 3.10)
45+
include(CheckCXXCompilerFlag)
46+
CHECK_CXX_COMPILER_FLAG("/std:c++14" _cpp_latest_flag_supported)
47+
if(_cpp_latest_flag_supported)
48+
add_compile_options("/std:c++14")
49+
endif()
50+
endif()
51+
endmacro()
52+
53+
# Set the compiler standard to C++17
54+
macro(_Cxx17)
55+
set(CMAKE_CXX_STANDARD 17)
56+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
57+
set(CMAKE_CXX_EXTENSIONS OFF)
58+
59+
if(MSVC_VERSION GREATER_EQUAL "1900" AND CMAKE_VERSION LESS 3.10)
60+
include(CheckCXXCompilerFlag)
61+
CHECK_CXX_COMPILER_FLAG("/std:c++17" _cpp_latest_flag_supported)
62+
if(_cpp_latest_flag_supported)
63+
add_compile_options("/std:c++17")
64+
endif()
65+
endif()
66+
endmacro()

cmake/clang-tools.cmake

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018 George Cave
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
#file(GLOB_RECURSE ALL_CODE_FILES
24+
# ${PROJECT_SOURCE_DIR}/src/*.[ch]pp
25+
# ${PROJECT_SOURCE_DIR}/src/*.[ch]
26+
# ${PROJECT_SOURCE_DIR}/include/*.[h]pp
27+
# ${PROJECT_SOURCE_DIR}/include/*.[h]
28+
#)
29+
30+
#get_target_property(_TARGET_TYPE ${PROJECT_NAME} TYPE)
31+
#if(NOT _TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
32+
# ...
33+
#endif()
34+
35+
# Generates a 'format' target using a custom name, files, and include directories all being parameters.
36+
#
37+
# TIDY_TARGET_NAME - The name of the target to create
38+
# ARGN - The list of files to format
39+
macro(_ClangFormat FORMAT_TARGET_NAME)
40+
if(NOT CLANG_FORMAT)
41+
find_program(CLANG_FORMAT "clang-format")
42+
endif()
43+
if(CLANG_FORMAT)
44+
add_custom_target(
45+
${FORMAT_TARGET_NAME}_format
46+
COMMAND clang-format
47+
-i
48+
-style=file
49+
${ARGN}
50+
)
51+
52+
if(NOT TARGET format)
53+
add_custom_target(format)
54+
endif()
55+
56+
add_dependencies(format ${FORMAT_TARGET_NAME}_format)
57+
endif()
58+
endmacro()
59+
60+
# Generates a 'tidy' target using a custom name, files, and include directories all being parameters.
61+
#
62+
# TIDY_TARGET_NAME - The name of the target to create
63+
# ARGN - The list of files to process, and any items prefixed by '-I' will become an include directory instead.
64+
macro(_ClangTidy TIDY_TARGET_NAME)
65+
if(NOT CLANG_TIDY)
66+
find_program(CLANG_TIDY "clang-tidy")
67+
endif()
68+
if(CLANG_TIDY)
69+
# Clear the vars
70+
set(TIDY_CODE_FILES "")
71+
set(TIDY_INCLUDE_DIRS "")
72+
# Go through the parameters and figure out which are code files and which are include directories
73+
set(params "${ARGN}")
74+
foreach(param IN LISTS params)
75+
string(SUBSTRING param 0 2 TIDY_TEMP_STRING)
76+
if(TIDY_TEMP_STRING STREQUAL "-I")
77+
set(TIDY_INCLUDE_DIRS "${TIDY_INCLUDE_DIRS}" "${param}")
78+
else()
79+
set(TIDY_CODE_FILES "${TIDY_CODE_FILES}" "${param}")
80+
endif()
81+
endforeach()
82+
83+
if(NOT TIDY_CODE_FILES STREQUAL "")
84+
add_custom_target(
85+
${TIDY_TARGET_NAME}_tidy
86+
COMMAND clang-tidy
87+
${TIDY_CODE_FILES}
88+
-format-style=file
89+
--
90+
-std=c++${CMAKE_CXX_STANDARD}
91+
${TIDY_INCLUDE_DIRS}
92+
)
93+
94+
if(NOT TARGET tidy)
95+
add_custom_target(tidy)
96+
endif()
97+
98+
add_dependencies(tidy ${TIDY_TARGET_NAME}_tidy)
99+
endif()
100+
endif()
101+
endmacro()

cmake/code-coverage.cmake

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018 George Cave
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
# Code Coverage
24+
option(CODE_COVERAGE "Builds targets with code coverage tools (Requires llvm and Clang)." OFF)
25+
26+
if(CODE_COVERAGE)
27+
message("Building with Code Coverage Tools")
28+
if (NOT UNIX OR NOT (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
29+
message(FATAL_ERROR "Error: CODE_COVERAGE option requires a unix environment and clang compiler.")
30+
endif()
31+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
32+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
33+
endif()
34+
35+
# Replacement for add_library that adds code coverage targets automatically.
36+
#
37+
# Takes in the same options as a regular add_library command, however adds two targets
38+
# named '${ARGV0}-ccov-show' and '${ARGV0}-ccov-report', aswell as the pooled end target of
39+
# 'ccov-report', except for non-compilable interface libraries.
40+
macro(_AddLibrary)
41+
add_library(${ARGN})
42+
43+
# Make sure not an interface library
44+
get_target_property(_TARGET_TYPE ${PROJECT_NAME} TYPE)
45+
if(CODE_COVERAGE AND NOT _TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
46+
add_custom_target(${ARGV0}-ccov-preprocessing
47+
COMMAND LLVM_PROFILE_FILE=${ARGV0}.profraw $<TARGET_FILE:${ARGV0}>
48+
COMMAND llvm-profdata merge -sparse ${ARGV0}.profraw -o ${ARGV0}.profdata
49+
DEPENDS ${ARGV0})
50+
51+
add_custom_target(${ARGV0}-ccov-show
52+
COMMAND llvm-cov show $<TARGET_FILE:${ARGV0}> -instr-profile=${ARGV0}.profdata -show-line-counts-or-regions
53+
DEPENDS ${ARGV0}-ccov-preprocessing)
54+
55+
add_custom_target(${ARGV0}-ccov-report
56+
COMMAND llvm-cov report $<TARGET_FILE:${ARGV0}> -instr-profile=${ARGV0}.profdata
57+
DEPENDS ${ARGV0}-ccov-preprocessing)
58+
59+
if(NOT TARGET ccov-report)
60+
add_custom_target(ccov-report)
61+
endif()
62+
63+
add_dependencies(ccov-report ${ARGV0}-ccov-report)
64+
endif()
65+
endmacro()
66+
67+
# Replacement for add_executable that adds code coverage targets automatically.
68+
#
69+
# Takes in the same options as a regular add_library command, however adds two targets
70+
# named '${ARGV0}-ccov-show' and '${ARGV0}-ccov-report', aswell as the pooled end target of
71+
# 'ccov-report'.
72+
macro(_AddExecutable)
73+
add_executable(${ARGN})
74+
75+
if(CODE_COVERAGE)
76+
add_custom_target(${ARGV0}-ccov-preprocessing
77+
COMMAND LLVM_PROFILE_FILE=${ARGV0}.profraw $<TARGET_FILE:${ARGV0}>
78+
COMMAND llvm-profdata merge -sparse ${ARGV0}.profraw -o ${ARGV0}.profdata
79+
DEPENDS ${ARGV0})
80+
81+
add_custom_target(${ARGV0}-ccov-show
82+
COMMAND llvm-cov show $<TARGET_FILE:${ARGV0}> -instr-profile=${ARGV0}.profdata -show-line-counts-or-regions
83+
DEPENDS ${ARGV0}-ccov-preprocessing)
84+
85+
add_custom_target(${ARGV0}_ccov-report
86+
COMMAND llvm-cov report $<TARGET_FILE:${ARGV0}> -instr-profile=${ARGV0}.profdata
87+
DEPENDS ${ARGV0}-ccov-preprocessing)
88+
89+
if(NOT TARGET ccov-report)
90+
add_custom_target(ccov-report)
91+
endif()
92+
93+
add_dependencies(ccov-report ${ARGV0}_ccov-report)
94+
endif()
95+
endmacro()

0 commit comments

Comments
 (0)