blob: 6e807c012f61e5ccc21b2b8701d66953677a95dd [file] [log] [blame]
Imre Kised98e9c2019-10-15 16:17:55 +02001#
Imre Kisfdaaf8c2019-12-16 23:53:41 +01002# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
Imre Kised98e9c2019-10-15 16:17:55 +02003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
Imre Kis1d2fbdd2019-12-13 11:42:08 +01007#[===[.rst:
8Processing flow
9---------------
10
11The following steps show how the CMake based build system works. The first
12couple steps are responsible for detecting and checking the environment. The
13following steps are the fetching and building phase of CppUTest. Finally it
14collects the test cases.
15
161. Checking if :cmake:variable:`TF_A_PATH` is specified
17
182. Checking git
19
203. Fetching CppUTest
21
224. Building CppUTest
23
245. Configuring coverage targets if they are enabled
25
266. Collecting unit test suites
27
28Note that the included modules can also execute further checks and commands on
29init.
30
31
32Variables
33---------
34
35The following cache entries can be set with the ``-D`` command line option of
36CMake when the configuration phase is first run. This allows applying
37environment specific configuration to the build. The current values of these
38variables can be checked by opening ``CMakeCache.txt`` in the build directory or
39by using ``cmake-gui``.
40
41.. cmake:variable:: TF_A_PATH
42
43Path of the Trusted Firmware-A source directory. **This needs to be specified
44by the developer** to point to a suitable working copy of TF-A.
45
46.. cmake:variable:: TF_A_UNIT_TESTS_PATH
47
48Root directory of unit test repository. It can be used to reference source
49files from the unit test repository relative to its root directory.
50
51.. cmake:variable:: CPPUTEST_URL
52
53URL of the CppUTest git repository. By default it points to the official Github
54repository of CppUTest. It can be used to specify a different CppUTest mirror.
55
56.. cmake:variable:: CPPUTEST_REFSPEC
57
58CppUTest git refspec. The default value selects the latest release.
59
60.. cmake:variable:: CPPUTEST_INSTALL_PATH
61
62Temporary directory used during CppUTest build
63
64.. cmake:variable:: CPPUTEST_PACKAGE_PATH
65
66Path of the CppUTest CMake package directory
67
68.. cmake:variable:: CPICKER_CACHE_PATH
69
70Directory of c-picker generated files. Subdirectories are added according to
71the path of the original source file's path.
72
73.. cmake:variable:: CLANG_LIBRARY_PATH
74
75c-picker uses libclang to parse the source files. If defined this variable
76specifies the path of the library.
77
78.. cmake:variable:: COVERAGE
79
80Adds compiler flags for coverage measurement and enables ``coverage`` and
81``coverage_report`` targets.
82
83#]===]
84
Imre Kised98e9c2019-10-15 16:17:55 +020085cmake_minimum_required(VERSION 3.11...3.15) # TODO: test with ubuntu
86project(tf-a-unit-tests)
87
88list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
89
90include(CTest)
91include(ExternalProject)
92include(FetchContent)
Imre Kisc625d492020-02-10 16:36:22 +010093include(UnitTest)
Imre Kised98e9c2019-10-15 16:17:55 +020094
95# Configuration variables
96set(TF_A_PATH "" CACHE PATH "Path of the Trusted Firmware A directory")
97set(TF_A_UNIT_TESTS_PATH ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path of root directory of the unit test repository")
98set(CPPUTEST_URL "https://github.com/cpputest/cpputest.git" CACHE STRING "CppUTest repository URL")
99set(CPPUTEST_REFSPEC "v3.8" CACHE STRING "CppUTest git refspec")
100set(CPPUTEST_INSTALL_PATH ${CMAKE_CURRENT_BINARY_DIR}/CppUTest_install CACHE PATH "CppUTest installation directory")
101set(CPPUTEST_PACKAGE_PATH ${CPPUTEST_INSTALL_PATH}/lib/CppUTest/cmake CACHE PATH "CppUTest CMake package directory")
102set(CPICKER_CACHE_PATH ${CMAKE_CURRENT_BINARY_DIR}/cpicker_cache CACHE PATH "Directory of c-picker generated file")
103set(UNIT_TEST_COMMON_SOURCES ${CMAKE_CURRENT_LIST_DIR}/common/main.cpp)
104set(CMAKE_CXX_STANDARD 11)
Imre Kise6bf9e62020-01-20 11:34:24 +0100105option(COVERAGE "Enable code coverage measurement" OFF)
Imre Kised98e9c2019-10-15 16:17:55 +0200106
107# Checking TF-A
108if (NOT TF_A_PATH)
109 message(FATAL_ERROR "TF_A_PATH is not set")
110endif()
111
Imre Kised98e9c2019-10-15 16:17:55 +0200112# Checking git
113find_program(GIT_COMMAND "git")
114if (NOT GIT_COMMAND)
115 message(FATAL_ERROR "Please install git")
116endif()
117
118# Fetching CppUTest
119FetchContent_Declare(
120 cpputest
121 GIT_REPOSITORY ${CPPUTEST_URL}
122 GIT_TAG ${CPPUTEST_REFSPEC}
123 GIT_SHALLOW TRUE
124 PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/common/cpputest-cmake-fix.patch
125)
126
127# FetchContent_GetProperties exports cpputest_SOURCE_DIR and cpputest_BINARY_DIR variables
128FetchContent_GetProperties(cpputest)
129if(NOT cpputest_POPULATED)
130 message(STATUS "Fetching CppUTest")
131 FetchContent_Populate(cpputest)
132endif()
133
134# Build and install CppUTest in CMake time. This makes us able to use CppUTest as a CMake package later.
135# Memory leak detection is turned off to avoid conflict with memcheck.
136execute_process(COMMAND
137 ${CMAKE_COMMAND}
138 -DMEMORY_LEAK_DETECTION=OFF
139 -DLONGLONG=ON
140 -DC++11=ON
141 -DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
142 -GUnix\ Makefiles
143 ${cpputest_SOURCE_DIR}
144 WORKING_DIRECTORY
145 ${cpputest_BINARY_DIR}
146)
147execute_process(COMMAND ${CMAKE_COMMAND} --build ${cpputest_BINARY_DIR} -- install -j)
148
149# Finding CppUTest package. CMake will check [package name]_DIR variable.
150set(CppUTest_DIR ${CPPUTEST_PACKAGE_PATH} CACHE PATH "Path of CppUTestConfig.cmake")
151find_package(CppUTest CONFIG REQUIRED)
152
153# find_package sets the CppUTest_INCLUDE_DIRS and CppUTest_LIBRARIES variables
154include_directories(${CppUTest_INCLUDE_DIRS})
155link_libraries(${CppUTest_LIBRARIES})
156
Imre Kise6bf9e62020-01-20 11:34:24 +0100157# Coverage
158if (COVERAGE)
159 include(Coverage)
160
161 set(COVERAGE_FILE "coverage.info")
162 set(TF_A_COVERAGE_FILE "trusted-firmware-a-coverage.info")
163 set(TF_A_UT_COVERAGE_FILE "tf-a-unit-tests-coverage.info")
164 set(TF_A_COVERAGE_REPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/trusted-firmware-a-coverage")
165 set(TF_A_UT_COVERAGE_REPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/tf-a-unit-tests-coverage")
166
167 # Collecting coverage
Imre Kis335834e2020-02-07 15:44:38 +0100168 coverage_generate(
169 NAME "Unit test"
170 SOURCE_DIR ${TF_A_PATH}
171 BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}
172 CPICKER_MAPPING_PATH ${CPICKER_CACHE_PATH}
173 OUTPUT_FILE ${COVERAGE_FILE}
174 )
Imre Kise6bf9e62020-01-20 11:34:24 +0100175
176 # Filtering TF-A and unit test coverage
177 coverage_filter(
178 INPUT_FILE ${COVERAGE_FILE}
179 OUTPUT_FILE ${TF_A_COVERAGE_FILE}
180 INCLUDE_DIRECTORY ${TF_A_PATH}
181 )
182 coverage_filter(
183 INPUT_FILE ${COVERAGE_FILE}
184 OUTPUT_FILE ${TF_A_UT_COVERAGE_FILE}
185 INCLUDE_DIRECTORY ${TF_A_UNIT_TESTS_PATH}
186 )
187
188 # Coverage reports
189 coverage_generate_report(
190 INPUT_FILE trusted-firmware-a-coverage.info
191 OUTPUT_DIRECTORY ${TF_A_COVERAGE_REPORT_DIR}
192 )
193 coverage_generate_report(
194 INPUT_FILE tf-a-unit-tests-coverage.info
195 OUTPUT_DIRECTORY ${TF_A_UT_COVERAGE_REPORT_DIR}
196 )
197endif()
198
Imre Kised98e9c2019-10-15 16:17:55 +0200199# Project level include directory
200include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
201
Imre Kisa71757b2019-12-17 00:19:40 +0100202include(tests/bl1/test_bl1_fwu.cmake)
Imre Kised98e9c2019-10-15 16:17:55 +0200203include(tests/lib/libc/test_libc.cmake)
Imre Kisfdaaf8c2019-12-16 23:53:41 +0100204include(tests/lib/object_pool/test_object_pool.cmake)
johpow0173007872020-07-15 20:01:05 -0500205include(tests/lib/fdt/test_fdt.cmake)