Remove old md files and update asciidoc headers. Fix some cppcheck docs
This commit is contained in:
parent
91d05021d5
commit
96e8ae921c
@ -1,11 +1,13 @@
|
||||
= Hello CMake
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
[[hello-cmake]]
|
||||
Hello CMake
|
||||
-----------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a very basic hello world example.
|
||||
|
||||
|
@ -1,73 +0,0 @@
|
||||
# Hello CMake
|
||||
|
||||
Shows a very basic hello world example.
|
||||
|
||||
## New Concepts
|
||||
|
||||
### Minimum CMake version
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
```
|
||||
|
||||
When creating a project using CMake, you can specify the minimum version of CMake that is supported.
|
||||
|
||||
|
||||
### Projects
|
||||
|
||||
```cmake
|
||||
project (hello_cmake)
|
||||
```
|
||||
|
||||
A CMake build can include a project name to make referencing certain variables when using multiple projects.
|
||||
|
||||
### Creating an executable
|
||||
|
||||
```cmake
|
||||
add_executable(hello_cmake main.cpp)
|
||||
```
|
||||
|
||||
The add_executable() command specifies that an executable should be build from the specified source files, in this example main.cpp. The first argument to the add_executable() function is the name of the executable to be built, and the second argument is the source file from which to build the executable.
|
||||
|
||||
HINT: A shorthand that some people use is to have the project name and executable name the same. This allows you to specify the CMakeLists.txt as follows,
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
project (hello_cmake)
|
||||
add_executable(${PROJECT_NAME} main.cpp)
|
||||
```
|
||||
|
||||
In this example, the project() function, will create a variable ${PROJECT_NAME} with the value hello_cmake. This can then be passed to the add_executable() function to output a 'hello_cmake' executable.
|
||||
|
||||
## Building the example
|
||||
|
||||
```bash
|
||||
$ mkdir build
|
||||
|
||||
$ cd build
|
||||
|
||||
$ cmake ..
|
||||
-- The C compiler identification is GNU 4.8.4
|
||||
-- The CXX compiler identification is GNU 4.8.4
|
||||
-- Check for working C compiler: /usr/bin/cc
|
||||
-- Check for working C compiler: /usr/bin/cc -- works
|
||||
-- Detecting C compiler ABI info
|
||||
-- Detecting C compiler ABI info - done
|
||||
-- Check for working CXX compiler: /usr/bin/c++
|
||||
-- Check for working CXX compiler: /usr/bin/c++ -- works
|
||||
-- Detecting CXX compiler ABI info
|
||||
-- Detecting CXX compiler ABI info - done
|
||||
-- Configuring done
|
||||
-- Generating done
|
||||
-- Build files have been written to: /workspace/cmake-examples/01-basic/hello_cmake/build
|
||||
|
||||
$ make
|
||||
Scanning dependencies of target hello_cmake
|
||||
[100%] Building CXX object CMakeFiles/hello_cmake.dir/hello_cmake.cpp.o
|
||||
Linking CXX executable hello_cmake
|
||||
[100%] Built target hello_cmake
|
||||
|
||||
$ ./hello_cmake
|
||||
Hello CMake!
|
||||
```
|
||||
|
@ -1,12 +1,14 @@
|
||||
= Hello Headers
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
|
||||
[[hello-headers]]
|
||||
Hello Headers
|
||||
-------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a hello world example which uses a different folder for source and include
|
||||
files.
|
||||
|
@ -1,117 +0,0 @@
|
||||
# Hello CMake
|
||||
|
||||
Shows a hello world example, which uses a seperate cpp file and include directory.
|
||||
|
||||
## New Concepts
|
||||
|
||||
### Directory Paths
|
||||
|
||||
CMake syntax specifies a number of variables which can be used to help find useful directories in your project or source tree. Some of these include
|
||||
|
||||
Variable | Info
|
||||
-------- | ------
|
||||
CMAKE_SOURCE_DIR | The root source directory
|
||||
CMAKE_CURRENT_SOURCE_DIR | The current source directory if using sub-projects and directories
|
||||
PROJECT_SOURCE_DIR | The source director of the current cmake project.
|
||||
CMAKE_BINARY_DIR | The root binary / build directory. This is the directory you run the cmake command from
|
||||
CMAKE_CURRENT_BINARY_DIR | The build directory you are currently in.
|
||||
PROJECT_BINARY_DIR | The build directory for the current project.
|
||||
|
||||
### Including Directories
|
||||
|
||||
```
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
```
|
||||
|
||||
When you have a seperate include folders, you can include them using the 'include_directories' function.
|
||||
This will add these directories to the compiler with the -I flag, e.g. -I/directory/path
|
||||
|
||||
### Setting Source files
|
||||
|
||||
```
|
||||
# Create a sources variable with a link to all cpp files to compile
|
||||
set(SOURCES
|
||||
src/Hello.cpp
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
```
|
||||
|
||||
Creating a variable which includes the source files allows you to be clearer about these files and add them to various commands, for example, the 'add_executable()' function.
|
||||
|
||||
An alternative to setting specific file names in the SOURCES variable is to use a GLOB command to find files using wildcard pattern matching.
|
||||
|
||||
```
|
||||
file(GLOB SOURCES "src/*.cpp")
|
||||
```
|
||||
|
||||
## Building the example
|
||||
|
||||
```
|
||||
$ mkdir build
|
||||
|
||||
$ cd build
|
||||
|
||||
$ cmake ..
|
||||
-- The C compiler identification is GNU 4.8.4
|
||||
-- The CXX compiler identification is GNU 4.8.4
|
||||
-- Check for working C compiler: /usr/bin/cc
|
||||
-- Check for working C compiler: /usr/bin/cc -- works
|
||||
-- Detecting C compiler ABI info
|
||||
-- Detecting C compiler ABI info - done
|
||||
-- Check for working CXX compiler: /usr/bin/c++
|
||||
-- Check for working CXX compiler: /usr/bin/c++ -- works
|
||||
-- Detecting CXX compiler ABI info
|
||||
-- Detecting CXX compiler ABI info - done
|
||||
-- Configuring done
|
||||
-- Generating done
|
||||
-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build
|
||||
|
||||
$ make
|
||||
Scanning dependencies of target hello_headers
|
||||
[ 50%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.o
|
||||
[100%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.o
|
||||
Linking CXX executable hello_headers
|
||||
[100%] Built target hello_headers
|
||||
|
||||
$ ./hello_headers
|
||||
Hello Headers!
|
||||
```
|
||||
|
||||
### Verbose Output
|
||||
|
||||
In the previous examples, when running the make command the output only shows the status of the build. To see the full output for debugging purposes you can add 'VERBOSE=1' to the command to show all commands.
|
||||
|
||||
The VERBOSE output is show below, and looking at the output lets you see the include directories being added to the g++ command.
|
||||
|
||||
```
|
||||
$ make clean
|
||||
$ make VERBOSE=1
|
||||
/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/hello_headers -B/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build --check-build-system CMakeFiles/Makefile.cmake 0
|
||||
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles/progress.marks
|
||||
make -f CMakeFiles/Makefile2 all
|
||||
make[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
make -f CMakeFiles/hello_headers.dir/build.make CMakeFiles/hello_headers.dir/depend
|
||||
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
cd /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/hello_headers /home/matrim/workspace/cmake-examples/01-basic/hello_headers /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles/hello_headers.dir/DependInfo.cmake --color=
|
||||
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
make -f CMakeFiles/hello_headers.dir/build.make CMakeFiles/hello_headers.dir/build
|
||||
make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 1
|
||||
[ 50%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.o
|
||||
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/01-basic/hello_headers/inc -o CMakeFiles/hello_headers.dir/src/Hello.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/hello_headers/src/Hello.cpp
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 2
|
||||
[100%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.o
|
||||
/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/01-basic/hello_headers/inc -o CMakeFiles/hello_headers.dir/src/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/hello_headers/src/main.cpp
|
||||
Linking CXX executable hello_headers
|
||||
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_headers.dir/link.txt --verbose=1
|
||||
/usr/bin/c++ CMakeFiles/hello_headers.dir/src/Hello.cpp.o CMakeFiles/hello_headers.dir/src/main.cpp.o -o hello_headers -rdynamic
|
||||
make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 1 2
|
||||
[100%] Built target hello_headers
|
||||
make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'
|
||||
/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 0
|
||||
```
|
@ -1,12 +1,14 @@
|
||||
= Static Library
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
|
||||
[[static-lib]]
|
||||
Static Library
|
||||
--------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a hello world example which first creates and links a static library
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
= Shared Library
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
@ -5,9 +6,9 @@
|
||||
toc::[]
|
||||
|
||||
|
||||
[[shared-lib]]
|
||||
Shared Library
|
||||
--------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Shows a hello world example which first creates and links a shared library
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
= Static Analysis
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
[[static-analysis]]
|
||||
Static Analysis
|
||||
---------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Static analysis is the analysis of code without executing it. It can be
|
||||
used to find common programming errors and enforce coding guidelines.
|
||||
|
@ -1,14 +0,0 @@
|
||||
# Static Analysis
|
||||
|
||||
Static analysis is the analysis of code without executing it. It can be used to find common programming errors and enforce coding guidelines. Examples of errors that can be found using static analysis tools include:
|
||||
|
||||
* Out of bounds errors
|
||||
* Memory leaks
|
||||
* Usage of uninitialized variables
|
||||
* Use of unsafe functions
|
||||
|
||||
Analysis tools can detect errors early and are becoming a standard tool in most build chains. Some build tools such as [Clang](http://clang-analyzer.llvm.org/) include a build in static analysis tool. However standalone tools also exist.
|
||||
|
||||
The examples here include using the following tools:
|
||||
|
||||
* [CppCheck](http://cppcheck.sourceforge.net/)
|
@ -1,12 +1,13 @@
|
||||
= CppCheck Static Analysis
|
||||
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
[[cppcheck-static-analysis]]
|
||||
CppCheck Static Analysis
|
||||
------------------------
|
||||
[[intro]]
|
||||
Introduction
|
||||
------------
|
||||
|
||||
This example shows how to call the
|
||||
http://cppcheck.sourceforge.net/[CppCheck] tool to do static analysis.
|
||||
@ -18,6 +19,32 @@ It includes code to
|
||||
* Generate an overall `make analysis` target to do static
|
||||
analysis on all sub-projects.
|
||||
|
||||
The files included in this example are:
|
||||
|
||||
```
|
||||
cppcheck$ tree
|
||||
.
|
||||
├── cmake
|
||||
│ ├── analysis.cmake
|
||||
│ └── modules
|
||||
│ └── FindCppCheck.cmake
|
||||
├── CMakeLists.txt
|
||||
├── subproject1
|
||||
│ ├── CMakeLists.txt
|
||||
│ └── main1.cpp
|
||||
└── subproject2
|
||||
├── CMakeLists.txt
|
||||
└── main2.cpp
|
||||
```
|
||||
|
||||
* CMakeLists.txt - Top level CMakeLists.txt
|
||||
* cmake/analysis.cmake - Includes functions to add analysis targets
|
||||
* cmake/modules/FindCppCheck.cmake - A custom package module to find CppCheck
|
||||
* subproject1/CMakeLists.txt - CMake commands for subproject 1
|
||||
* subproject1/main.cpp - source for a subproject with no errors
|
||||
* subproject2/CMakeLists.txt - CMake commands for subproject 2
|
||||
* subproject2/main2.cpp - source for a subproject that includes errors
|
||||
|
||||
[[requirements]]
|
||||
Requirements
|
||||
~~~~~~~~~~~~
|
||||
@ -25,7 +52,7 @@ Requirements
|
||||
To run this example you must have the CppCheck utility installed. On
|
||||
Ubuntu you can install it as
|
||||
|
||||
[source,cmake]
|
||||
[source,bash]
|
||||
----
|
||||
$ sudo apt-get install cppcheck
|
||||
----
|
||||
@ -38,15 +65,15 @@ Concepts
|
||||
Adding Custom Package Modules
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Custom modules can be used to find programs, libraries and header files
|
||||
to include in your program.
|
||||
|
||||
[[adding-a-custom-module]]
|
||||
Adding a custom module
|
||||
++++++++++++++++++++++
|
||||
|
||||
include::cmake/modules/FindCppCheck.cmake
|
||||
|
||||
The `cmake/modules/FindCppCheck.cmake` file contains the code to initialise a
|
||||
custom package module. Custom modules can be used to
|
||||
find programs, libraries and header files to include in your program.
|
||||
custom package module.
|
||||
|
||||
The following is a breakdown of the file:
|
||||
|
||||
@ -91,16 +118,19 @@ view advanced flag is set.
|
||||
Setting path to custom modules
|
||||
++++++++++++++++++++++++++++++
|
||||
|
||||
The default path that CMake will search for modules is `/usr/share/cmake/Modules`.
|
||||
To include custom modules you must tell CMake where to search for them.
|
||||
This can be done using the variable +${CMAKE_MODULE_PATH}+ which
|
||||
contains the paths that CMake will search for modules.
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules
|
||||
${CMAKE_MODULE_PATH})
|
||||
----
|
||||
|
||||
The variable +${CMAKE_MODULE_PATH}+ points towards a folder which contains custom
|
||||
cmake modules.
|
||||
|
||||
To then add the package module you can call
|
||||
To then add the package module to your CMakeLists.txt you can call
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
@ -129,6 +159,8 @@ example. If cppcheck is available then a list of arguments are compiled
|
||||
and added to a custom command that calls cppcheck on the sources. These
|
||||
are then added to a custom target.
|
||||
|
||||
A breakdown of this macro is below:
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
|
||||
@ -200,11 +232,11 @@ add_analysis(${PROJECT_NAME} SOURCES)
|
||||
Creating a target to call other targets
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
In the root CMakeLists.txt a custom target is created, which will call
|
||||
In the top level CMakeLists.txt a custom target is created, which will call
|
||||
all other analysis targets. This allows you to call `make analysis` and
|
||||
scan all sub projects.
|
||||
|
||||
To achieve this 2 things should be added to the root CMakeLists.txt
|
||||
To achieve this 2 things should be added to the top level CMakeLists.txt
|
||||
|
||||
First add an empty variable +ALL_ANALYSIS_TARGETS+ before calling your
|
||||
+add_subdirectories()+ function.
|
||||
@ -283,7 +315,7 @@ cd /path/to/subproject2 && /usr/bin/cppcheck -j 4 main2.cpp
|
||||
----
|
||||
|
||||
The main1.cpp has no errors so will complete correctly, however the
|
||||
main2.cpp includes an out-of-bounds error which shows the error.
|
||||
main2.cpp includes an out-of-bounds error. This is show with a warning as
|
||||
|
||||
------------------------------------------------------------------------------------
|
||||
[main2.cpp:7]: (error) Array 'tmp[10]' accessed at index 11, which is out of bounds.
|
||||
@ -317,18 +349,20 @@ sub folders, such as below:
|
||||
│ ├── CMakeLists.txt
|
||||
│ ├── src
|
||||
│ │ ├── CMakeLists.txt
|
||||
│ │ ├── project
|
||||
│ │ ├── project1
|
||||
│ │ │ ├── CMakeLists.txt
|
||||
│ │ │ ├── main.cpp
|
||||
│ │ ├── project
|
||||
│ │ ├── project2
|
||||
│ │ │ ├── CMakeLists.txt
|
||||
│ │ │ ├── main.cpp
|
||||
------------------------------
|
||||
|
||||
You must add the following to the `src/CMakeLists.txt` file to correctly
|
||||
generate the `make analysis` target
|
||||
To correctly generate the root `make analysis` target you must export the +ALL_ANALYSIS_TARGET+
|
||||
variable to the parent scope in `src/CMakeLists.txt`.
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
set(analysis_TARGETS "${analysis_TARGETS}" PARENT_SCOPE)
|
||||
add_subdirectory(project1)
|
||||
add_subdirectory(project2)
|
||||
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
||||
----
|
||||
|
@ -1,251 +0,0 @@
|
||||
# CppCheck Static Analysis
|
||||
|
||||
This example is for calling the [CppCheck](http://cppcheck.sourceforge.net/) tool to do static analysis.
|
||||
|
||||
It shows how to add cppcheck with a target for each sub-projects and also how to generate an overall "make analysis" target to do static analysis on all sub-projects.
|
||||
|
||||
## Requirements
|
||||
|
||||
To run this example you must have the CppCheck utility installed. On Ubuntu you can install it as
|
||||
|
||||
```cmake
|
||||
$ sudo apt-get install cppcheck
|
||||
```
|
||||
|
||||
## Concepts
|
||||
|
||||
### Adding Custom Package Modules
|
||||
|
||||
#### Adding a custom module
|
||||
|
||||
The cmake/modules/FindCppCheck.cmake file contains the code to find and add variables for a custom package module. Custom modules can be used to find programs, libraries and header files to include in your program.
|
||||
|
||||
```cmake
|
||||
find_program(CPPCHECK_BIN NAMES cppcheck)
|
||||
```
|
||||
Search the path for the program "cppcheck" and store the result in the CPPCHECK_BIN variable
|
||||
|
||||
|
||||
```cmake
|
||||
set (CPPCHECK_THREADS "-j 4" CACHE STRING "The -j argument to have cppcheck use multiple threads / cores")
|
||||
|
||||
set (CPPCHECK_ARG "${CPPCHECK_THREADS}" CACHE STRING "The arguments to pass to cppcheck. If set will overwrite CPPCHECK_THREADS")
|
||||
```
|
||||
Set some custom arguments that can be later passed to cppcheck.
|
||||
|
||||
|
||||
```cmake
|
||||
include(FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
|
||||
CPPCHECK
|
||||
DEFAULT_MSG
|
||||
CPPCHECK_BIN
|
||||
CPPCHECK_THREADS
|
||||
CPPCHECK_ARG)
|
||||
|
||||
mark_as_advanced(
|
||||
CPPCHECK_BIN
|
||||
CPPCHECK_THREADS
|
||||
CPPCHECK_ARG)
|
||||
```
|
||||
|
||||
Export the variables so that they can be seen from ccmake / cmake-gui and set in the cache. By default these will not be visible unless the view advanced flag is set.
|
||||
|
||||
|
||||
#### Setting path to custom modules
|
||||
|
||||
```cmake
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules
|
||||
${CMAKE_MODULE_PATH})
|
||||
```
|
||||
|
||||
The ${CMAKE_MODULE_PATH} points towards a folder which contains custom cmake modules.
|
||||
|
||||
To then add the package module you can call
|
||||
|
||||
```cmake
|
||||
find_package(CppCheck)
|
||||
```
|
||||
|
||||
### Parent Scope Variables
|
||||
|
||||
The scope of variables when they are declared / changed is typically in the function of file the are called. To make a change to a variable which is the caller of your scope, you should call it as follows:
|
||||
|
||||
```cmake
|
||||
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
||||
```
|
||||
|
||||
### add_analysis macro
|
||||
|
||||
The add_analysis macro in cmake/analysis.cmake is the core idea for this example. If cppcheck is available then a list of arguments are compiled and added to a custom command that calls cppcheck on the sources. These are then added to a custom target.
|
||||
|
||||
```cmake
|
||||
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
|
||||
foreach(dir ${dirs})
|
||||
LIST(APPEND cppcheck_includes "-I${dir}")
|
||||
endforeach()
|
||||
```
|
||||
Find the include files from and calls to include_directories() in the same project.
|
||||
|
||||
|
||||
```cmake
|
||||
LIST(APPEND ALL_ANALYSIS_TARGETS "${_target}_analysis")
|
||||
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
||||
```
|
||||
Export the target name into a variable that can later be used to add a global "make analysis" target.
|
||||
|
||||
|
||||
```cmake
|
||||
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VESION} GREATER 2.7)
|
||||
separate_arguments(tmp_args UNIX_COMMAND ${CPPCHECK_ARG})
|
||||
else ()
|
||||
# cmake 2.6 has different arguments
|
||||
string(REPLACE " " ";" tmp_args ${CPPCHECK_ARG})
|
||||
endif ()
|
||||
```
|
||||
Change the CPPCHECK_ARG so that the can be added to command correctly in the custom command.
|
||||
|
||||
|
||||
```cmake
|
||||
add_custom_target(${_target}_analysis)
|
||||
set_target_properties(${_target}_analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
```
|
||||
Add a custom target with a name you have passed in followed by _analysis. Do not include this in the all target.
|
||||
|
||||
|
||||
```cmake
|
||||
add_custom_command(TARGET ${_target}_analysis PRE_BUILD
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
COMMAND ${CPPCHECK_BIN} ${tmp_args} ${cppcheck_includes} ${${_sources}}
|
||||
DEPENDS ${${_sources}}
|
||||
COMMENT "Running cppcheck: ${_target}"
|
||||
VERBATIM)
|
||||
```
|
||||
Add a custom command which is called from the custom target added above. This will call cppcheck with any includes, arguments and sources that you have provided. The sources that are analysed come from a _sources variable. This should be the name of the variable which holds your list of source files.
|
||||
|
||||
|
||||
To call the add_analysis marco add the following to your projects CMakeLists.txt file:
|
||||
|
||||
```cmake
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/analysis.cmake)
|
||||
add_analysis(${PROJECT_NAME} SOURCES)
|
||||
```
|
||||
|
||||
### Creating a target to call other targets
|
||||
|
||||
In the root CMakeLists.txt a custom target is created, which will call all other analysis targets. This allows you to call "make analysis" and scan all sub projects.
|
||||
|
||||
To achieve this 2 things should be added to the root CMakeLists.txt
|
||||
|
||||
First add an empty variable ALL_ANALYSIS_TARGETS before calling your add_subdirectories() function.
|
||||
|
||||
```cmake
|
||||
set (ALL_ANALYSIS_TARGETS)
|
||||
```
|
||||
|
||||
Second add the following after your add_subdirectories() call.
|
||||
|
||||
```cmake
|
||||
if( CPPCHECK_FOUND )
|
||||
add_custom_target(analysis)
|
||||
ADD_DEPENDENCIES(analysis ${ALL_ANALYSIS_TARGETS})
|
||||
set_target_properties(analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||
message("analysis analysis targets are ${ALL_ANALYSIS_TARGETS}")
|
||||
endif()
|
||||
```
|
||||
This adds the "make analysis" target which calls all the sub-targets.
|
||||
|
||||
## Building the example
|
||||
|
||||
```bash
|
||||
$ mkdir build
|
||||
|
||||
$ cd build/
|
||||
|
||||
$ cmake ..
|
||||
-- The C compiler identification is GNU 4.8.4
|
||||
-- The CXX compiler identification is GNU 4.8.4
|
||||
-- Check for working C compiler: /usr/bin/cc
|
||||
-- Check for working C compiler: /usr/bin/cc -- works
|
||||
-- Detecting C compiler ABI info
|
||||
-- Detecting C compiler ABI info - done
|
||||
-- Check for working CXX compiler: /usr/bin/c++
|
||||
-- Check for working CXX compiler: /usr/bin/c++ -- works
|
||||
-- Detecting CXX compiler ABI info
|
||||
-- Detecting CXX compiler ABI info - done
|
||||
-- Found CPPCHECK: /usr/bin/cppcheck
|
||||
adding cppcheck analysys target for subproject1
|
||||
adding cppcheck analysys target for subproject2
|
||||
analysis analysis targets are subproject1_analysis;subproject2_analysis
|
||||
-- Configuring done
|
||||
-- Generating done
|
||||
-- Build files have been written to: /home/matrim/workspace/cmake-examples/04-static-analysis/cppcheck/build
|
||||
|
||||
$ make analysis
|
||||
Scanning dependencies of target subproject1_analysis
|
||||
Running cppcheck: subproject1
|
||||
Checking main1.cpp...
|
||||
Built target subproject1_analysis
|
||||
Scanning dependencies of target subproject2_analysis
|
||||
Running cppcheck: subproject2
|
||||
Checking main2.cpp...
|
||||
[main2.cpp:7]: (error) Array 'tmp[10]' accessed at index 11, which is out of bounds.
|
||||
Built target subproject2_analysis
|
||||
Scanning dependencies of target analysis
|
||||
Built target analysis
|
||||
|
||||
```
|
||||
|
||||
The above calls cppcheck in both subproject folders as
|
||||
|
||||
```bash
|
||||
...
|
||||
cd /path/to/subproject1 && /usr/bin/cppcheck -j 4 main1.cpp
|
||||
...
|
||||
cd /path/to/subproject2 && /usr/bin/cppcheck -j 4 main2.cpp
|
||||
...
|
||||
```
|
||||
|
||||
The main1.cpp has no errors so will complete correctly, however the main2.cpp includes an out-of-bounds error which shows the error.
|
||||
|
||||
```
|
||||
[main2.cpp:7]: (error) Array 'tmp[10]' accessed at index 11, which is out of bounds.
|
||||
```
|
||||
|
||||
By default cppcheck only prints warnings and exits with a successful exit code. If the ${CPPCHECK_ARG} variable is set with "--error-exitcode=1", the make analysis will finish early as follows.
|
||||
|
||||
```
|
||||
$ make analysis
|
||||
Running cppcheck: subproject2
|
||||
Checking main2.cpp...
|
||||
[main2.cpp:7]: (error) Array 'tmp[10]' accessed at index 11, which is out of bounds.
|
||||
make[3]: *** [subproject2_analysis] Error 1
|
||||
make[2]: *** [subproject2/CMakeFiles/subproject2_analysis.dir/all] Error 2
|
||||
make[1]: *** [CMakeFiles/analysis.dir/rule] Error 2
|
||||
make: *** [analysis] Error 2
|
||||
|
||||
```
|
||||
|
||||
## Extra Notes
|
||||
|
||||
If you have a multiple folders levels, where one folder just points to sub folders, such as below:
|
||||
|
||||
```
|
||||
├── root
|
||||
│ ├── CMakeLists.txt
|
||||
│ ├── src
|
||||
│ │ ├── CMakeLists.txt
|
||||
│ │ ├── project
|
||||
│ │ │ ├── CMakeLists.txt
|
||||
│ │ │ ├── main.cpp
|
||||
│ │ ├── project
|
||||
│ │ │ ├── CMakeLists.txt
|
||||
│ │ │ ├── main.cpp
|
||||
|
||||
```
|
||||
|
||||
You must add the following to the src/CMakeLists.txt file to correctly generate the "make analysis" target
|
||||
|
||||
```cmake
|
||||
set(analysis_TARGETS "${analysis_TARGETS}" PARENT_SCOPE)
|
||||
```
|
35
README.md
35
README.md
@ -1,35 +0,0 @@
|
||||
# cmake-examples
|
||||
This includes some example cmake configurations which I have picked up when exploring it's usage for various projects.
|
||||
|
||||
These examples have been tested on Ubuntu 14.04 but should work under any Linux system that supports cmake.
|
||||
|
||||
## Requirements
|
||||
|
||||
The basic requirements for most examples are:
|
||||
|
||||
* CMake
|
||||
* A c++ compiler (defaults to gcc)
|
||||
* make
|
||||
|
||||
The easiest way to install the above on Ubuntu is as follows
|
||||
|
||||
```
|
||||
$ sudo apt-get install cmake
|
||||
$ sudo apt-get install build-essential
|
||||
```
|
||||
|
||||
Some specific examples may require other tools including:
|
||||
|
||||
* boost
|
||||
```
|
||||
$ sudo apt-get libboost-all-dev
|
||||
```
|
||||
* protobuf
|
||||
```
|
||||
$ sudo apt-get install libprotobuf-dev
|
||||
```
|
||||
* cppcheck
|
||||
```
|
||||
$ sudo apt-get install cppcheck
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user