mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
fix adoc change
This commit is contained in:
@@ -16,9 +16,10 @@ Requirements
|
|||||||
To run this example you must have the CppCheck utility installed. On
|
To run this example you must have the CppCheck utility installed. On
|
||||||
Ubuntu you can install it as
|
Ubuntu you can install it as
|
||||||
|
|
||||||
code,cmake------------------------------- code,cmake
|
[source,cmake]
|
||||||
|
----
|
||||||
$ sudo apt-get install cppcheck
|
$ sudo apt-get install cppcheck
|
||||||
-------------------------------
|
----
|
||||||
|
|
||||||
[[concepts]]
|
[[concepts]]
|
||||||
Concepts
|
Concepts
|
||||||
@@ -36,23 +37,25 @@ 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
|
add variables for a custom package module. Custom modules can be used to
|
||||||
find programs, libraries and header files to include in your program.
|
find programs, libraries and header files to include in your program.
|
||||||
|
|
||||||
code,cmake----------------------------------------- code,cmake
|
[source,cmake]
|
||||||
|
----
|
||||||
find_program(CPPCHECK_BIN NAMES cppcheck)
|
find_program(CPPCHECK_BIN NAMES cppcheck)
|
||||||
-----------------------------------------
|
---
|
||||||
|
|
||||||
Search the path for the program "cppcheck" and store the result in the
|
Search the path for the program "cppcheck" and store the result in the
|
||||||
CPPCHECK_BIN variable
|
CPPCHECK_BIN variable
|
||||||
|
|
||||||
code,cmake---------------------------------------------------------------------------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
set (CPPCHECK_THREADS "-j 4" CACHE STRING "The -j argument to have cppcheck use multiple threads / cores")
|
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 (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.
|
Set some custom arguments that can be later passed to cppcheck.
|
||||||
|
|
||||||
code,cmake-------------------------------------- code,cmake
|
[source,cmake]
|
||||||
|
----
|
||||||
include(FindPackageHandleStandardArgs)
|
include(FindPackageHandleStandardArgs)
|
||||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
|
FIND_PACKAGE_HANDLE_STANDARD_ARGS(
|
||||||
CPPCHECK
|
CPPCHECK
|
||||||
@@ -65,7 +68,7 @@ mark_as_advanced(
|
|||||||
CPPCHECK_BIN
|
CPPCHECK_BIN
|
||||||
CPPCHECK_THREADS
|
CPPCHECK_THREADS
|
||||||
CPPCHECK_ARG)
|
CPPCHECK_ARG)
|
||||||
--------------------------------------
|
---
|
||||||
|
|
||||||
Export the variables so that they can be seen from ccmake / cmake-gui
|
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
|
and set in the cache. By default these will not be visible unless the
|
||||||
@@ -75,20 +78,21 @@ view advanced flag is set.
|
|||||||
Setting path to custom modules
|
Setting path to custom modules
|
||||||
++++++++++++++++++++++++++++++
|
++++++++++++++++++++++++++++++
|
||||||
|
|
||||||
code,cmake-------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules
|
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules
|
||||||
${CMAKE_MODULE_PATH})
|
${CMAKE_MODULE_PATH})
|
||||||
-------------------------------------------------------
|
---
|
||||||
|
|
||||||
The $\{CMAKE_MODULE_PATH} points towards a folder which contains custom
|
The $\{CMAKE_MODULE_PATH} points towards a folder which contains custom
|
||||||
cmake modules.
|
cmake modules.
|
||||||
|
|
||||||
To then add the package module you can call
|
To then add the package module you can call
|
||||||
|
|
||||||
code,cmake---------------------- code,cmake
|
[source,cmake]
|
||||||
|
----
|
||||||
find_package(CppCheck)
|
find_package(CppCheck)
|
||||||
----------------------
|
---
|
||||||
|
|
||||||
[[parent-scope-variables]]
|
[[parent-scope-variables]]
|
||||||
Parent Scope Variables
|
Parent Scope Variables
|
||||||
@@ -98,10 +102,10 @@ 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
|
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:
|
which is the caller of your scope, you should call it as follows:
|
||||||
|
|
||||||
code,cmake----------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
||||||
----------------------------------------------------------------
|
---
|
||||||
|
|
||||||
[[add_analysis-macro]]
|
[[add_analysis-macro]]
|
||||||
add_analysis macro
|
add_analysis macro
|
||||||
@@ -112,57 +116,57 @@ 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
|
and added to a custom command that calls cppcheck on the sources. These
|
||||||
are then added to a custom target.
|
are then added to a custom target.
|
||||||
|
|
||||||
code,cmake-------------------------------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
|
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
|
||||||
foreach(dir ${dirs})
|
foreach(dir ${dirs})
|
||||||
LIST(APPEND cppcheck_includes "-I${dir}")
|
LIST(APPEND cppcheck_includes "-I${dir}")
|
||||||
endforeach()
|
endforeach()
|
||||||
-------------------------------------------------------------------------------------
|
---
|
||||||
|
|
||||||
Find the include files from and calls to include_directories() in the
|
Find the include files from and calls to include_directories() in the
|
||||||
same project.
|
same project.
|
||||||
|
|
||||||
code,cmake----------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
LIST(APPEND ALL_ANALYSIS_TARGETS "${_target}_analysis")
|
LIST(APPEND ALL_ANALYSIS_TARGETS "${_target}_analysis")
|
||||||
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
set(ALL_ANALYSIS_TARGETS "${ALL_ANALYSIS_TARGETS}" PARENT_SCOPE)
|
||||||
----------------------------------------------------------------
|
---
|
||||||
|
|
||||||
Export the target name into a variable that can later be used to add a
|
Export the target name into a variable that can later be used to add a
|
||||||
global "make analysis" target.
|
global "make analysis" target.
|
||||||
|
|
||||||
code,cmake-------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VESION} GREATER 2.7)
|
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VESION} GREATER 2.7)
|
||||||
separate_arguments(tmp_args UNIX_COMMAND ${CPPCHECK_ARG})
|
separate_arguments(tmp_args UNIX_COMMAND ${CPPCHECK_ARG})
|
||||||
else ()
|
else ()
|
||||||
# cmake 2.6 has different arguments
|
# cmake 2.6 has different arguments
|
||||||
string(REPLACE " " ";" tmp_args ${CPPCHECK_ARG})
|
string(REPLACE " " ";" tmp_args ${CPPCHECK_ARG})
|
||||||
endif ()
|
endif ()
|
||||||
-------------------------------------------------------------
|
---
|
||||||
|
|
||||||
Change the CPPCHECK_ARG so that the can be added to command correctly in
|
Change the CPPCHECK_ARG so that the can be added to command correctly in
|
||||||
the custom command.
|
the custom command.
|
||||||
|
|
||||||
code,cmake---------------------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
add_custom_target(${_target}_analysis)
|
add_custom_target(${_target}_analysis)
|
||||||
set_target_properties(${_target}_analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
set_target_properties(${_target}_analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||||
---------------------------------------------------------------------------
|
---
|
||||||
|
|
||||||
Add a custom target with a name you have passed in followed by
|
Add a custom target with a name you have passed in followed by
|
||||||
_analysis. Do not include this in the all target.
|
_analysis. Do not include this in the all target.
|
||||||
|
|
||||||
code,cmake-----------------------------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
add_custom_command(TARGET ${_target}_analysis PRE_BUILD
|
add_custom_command(TARGET ${_target}_analysis PRE_BUILD
|
||||||
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
COMMAND ${CPPCHECK_BIN} ${tmp_args} ${cppcheck_includes} ${${_sources}}
|
COMMAND ${CPPCHECK_BIN} ${tmp_args} ${cppcheck_includes} ${${_sources}}
|
||||||
DEPENDS ${${_sources}}
|
DEPENDS ${${_sources}}
|
||||||
COMMENT "Running cppcheck: ${_target}"
|
COMMENT "Running cppcheck: ${_target}"
|
||||||
VERBATIM)
|
VERBATIM)
|
||||||
-----------------------------------------------------------------------------------
|
---
|
||||||
|
|
||||||
Add a custom command which is called from the custom target added above.
|
Add a custom command which is called from the custom target added above.
|
||||||
This will call cppcheck with any includes, arguments and sources that
|
This will call cppcheck with any includes, arguments and sources that
|
||||||
@@ -173,10 +177,11 @@ of source files.
|
|||||||
To call the add_analysis marco add the following to your projects
|
To call the add_analysis marco add the following to your projects
|
||||||
CMakeLists.txt file:
|
CMakeLists.txt file:
|
||||||
|
|
||||||
code,cmake------------------------------------------------- code,cmake
|
[source,cmake]
|
||||||
|
----
|
||||||
include(${CMAKE_SOURCE_DIR}/cmake/analysis.cmake)
|
include(${CMAKE_SOURCE_DIR}/cmake/analysis.cmake)
|
||||||
add_analysis(${PROJECT_NAME} SOURCES)
|
add_analysis(${PROJECT_NAME} SOURCES)
|
||||||
-------------------------------------------------
|
---
|
||||||
|
|
||||||
[[creating-a-target-to-call-other-targets]]
|
[[creating-a-target-to-call-other-targets]]
|
||||||
Creating a target to call other targets
|
Creating a target to call other targets
|
||||||
@@ -191,21 +196,22 @@ To achieve this 2 things should be added to the root CMakeLists.txt
|
|||||||
First add an empty variable ALL_ANALYSIS_TARGETS before calling your
|
First add an empty variable ALL_ANALYSIS_TARGETS before calling your
|
||||||
add_subdirectories() function.
|
add_subdirectories() function.
|
||||||
|
|
||||||
code,cmake-------------------------- code,cmake
|
[source,cmake]
|
||||||
|
----
|
||||||
set (ALL_ANALYSIS_TARGETS)
|
set (ALL_ANALYSIS_TARGETS)
|
||||||
--------------------------
|
---
|
||||||
|
|
||||||
Second add the following after your add_subdirectories() call.
|
Second add the following after your add_subdirectories() call.
|
||||||
|
|
||||||
code,cmake--------------------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
if( CPPCHECK_FOUND )
|
if( CPPCHECK_FOUND )
|
||||||
add_custom_target(analysis)
|
add_custom_target(analysis)
|
||||||
ADD_DEPENDENCIES(analysis ${ALL_ANALYSIS_TARGETS})
|
ADD_DEPENDENCIES(analysis ${ALL_ANALYSIS_TARGETS})
|
||||||
set_target_properties(analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
set_target_properties(analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
||||||
message("analysis analysis targets are ${ALL_ANALYSIS_TARGETS}")
|
message("analysis analysis targets are ${ALL_ANALYSIS_TARGETS}")
|
||||||
endif()
|
endif()
|
||||||
--------------------------------------------------------------------
|
---
|
||||||
|
|
||||||
This adds the "make analysis" target which calls all the sub-targets.
|
This adds the "make analysis" target which calls all the sub-targets.
|
||||||
|
|
||||||
@@ -213,8 +219,8 @@ This adds the "make analysis" target which calls all the sub-targets.
|
|||||||
Building the example
|
Building the example
|
||||||
~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
code,bash------------------------------------------------------------------------------------------------------------
|
[source,bash]
|
||||||
code,bash
|
----
|
||||||
$ mkdir build
|
$ mkdir build
|
||||||
|
|
||||||
$ cd build/
|
$ cd build/
|
||||||
@@ -250,18 +256,18 @@ Checking main2.cpp...
|
|||||||
Built target subproject2_analysis
|
Built target subproject2_analysis
|
||||||
Scanning dependencies of target analysis
|
Scanning dependencies of target analysis
|
||||||
Built target analysis
|
Built target analysis
|
||||||
------------------------------------------------------------------------------------------------------------
|
---
|
||||||
|
|
||||||
The above calls cppcheck in both subproject folders as
|
The above calls cppcheck in both subproject folders as
|
||||||
|
|
||||||
code,bash-----------------------------------------------------------
|
[source,bash]
|
||||||
code,bash
|
----
|
||||||
...
|
...
|
||||||
cd /path/to/subproject1 && /usr/bin/cppcheck -j 4 main1.cpp
|
cd /path/to/subproject1 && /usr/bin/cppcheck -j 4 main1.cpp
|
||||||
...
|
...
|
||||||
cd /path/to/subproject2 && /usr/bin/cppcheck -j 4 main2.cpp
|
cd /path/to/subproject2 && /usr/bin/cppcheck -j 4 main2.cpp
|
||||||
...
|
...
|
||||||
-----------------------------------------------------------
|
---
|
||||||
|
|
||||||
The main1.cpp has no errors so will complete correctly, however the
|
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 which shows the error.
|
||||||
@@ -274,7 +280,8 @@ By default cppcheck only prints warnings and exits with a successful
|
|||||||
exit code. If the $\{CPPCHECK_ARG} variable is set with
|
exit code. If the $\{CPPCHECK_ARG} variable is set with
|
||||||
"--error-exitcode=1", the make analysis will finish early as follows.
|
"--error-exitcode=1", the make analysis will finish early as follows.
|
||||||
|
|
||||||
------------------------------------------------------------------------------------
|
[source,bash]
|
||||||
|
----
|
||||||
$ make analysis
|
$ make analysis
|
||||||
Running cppcheck: subproject2
|
Running cppcheck: subproject2
|
||||||
Checking main2.cpp...
|
Checking main2.cpp...
|
||||||
@@ -283,7 +290,7 @@ make[3]: *** [subproject2_analysis] Error 1
|
|||||||
make[2]: *** [subproject2/CMakeFiles/subproject2_analysis.dir/all] Error 2
|
make[2]: *** [subproject2/CMakeFiles/subproject2_analysis.dir/all] Error 2
|
||||||
make[1]: *** [CMakeFiles/analysis.dir/rule] Error 2
|
make[1]: *** [CMakeFiles/analysis.dir/rule] Error 2
|
||||||
make: *** [analysis] Error 2
|
make: *** [analysis] Error 2
|
||||||
------------------------------------------------------------------------------------
|
---
|
||||||
|
|
||||||
[[extra-notes]]
|
[[extra-notes]]
|
||||||
Extra Notes
|
Extra Notes
|
||||||
@@ -308,7 +315,7 @@ sub folders, such as below:
|
|||||||
You must add the following to the src/CMakeLists.txt file to correctly
|
You must add the following to the src/CMakeLists.txt file to correctly
|
||||||
generate the "make analysis" target
|
generate the "make analysis" target
|
||||||
|
|
||||||
code,cmake--------------------------------------------------------
|
[source,cmake]
|
||||||
code,cmake
|
----
|
||||||
set(analysis_TARGETS "${analysis_TARGETS}" PARENT_SCOPE)
|
set(analysis_TARGETS "${analysis_TARGETS}" PARENT_SCOPE)
|
||||||
--------------------------------------------------------
|
---
|
||||||
|
|||||||
@@ -20,10 +20,11 @@ The basic requirements for most examples are:
|
|||||||
|
|
||||||
The easiest way to install the above on Ubuntu is as follows
|
The easiest way to install the above on Ubuntu is as follows
|
||||||
|
|
||||||
--------------------------------------
|
[source,bash]
|
||||||
|
----
|
||||||
$ sudo apt-get install cmake
|
$ sudo apt-get install cmake
|
||||||
$ sudo apt-get install build-essential
|
$ sudo apt-get install build-essential
|
||||||
--------------------------------------
|
---
|
||||||
|
|
||||||
Some specific examples may require other tools including:
|
Some specific examples may require other tools including:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user