blob: 3ad62d241116ddd77bb2782702508af983745a72 [file] [log] [blame]
#
# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
cmake_minimum_required(VERSION 3.11...3.15) # TODO: test with ubuntu
project(tf-a-unit-tests)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
include(CTest)
include(ExternalProject)
include(FetchContent)
# Configuration variables
set(TF_A_PATH "" CACHE PATH "Path of the Trusted Firmware A directory")
set(TF_A_UNIT_TESTS_PATH ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path of root directory of the unit test repository")
set(CPPUTEST_URL "https://github.com/cpputest/cpputest.git" CACHE STRING "CppUTest repository URL")
set(CPPUTEST_REFSPEC "v3.8" CACHE STRING "CppUTest git refspec")
set(CPPUTEST_INSTALL_PATH ${CMAKE_CURRENT_BINARY_DIR}/CppUTest_install CACHE PATH "CppUTest installation directory")
set(CPPUTEST_PACKAGE_PATH ${CPPUTEST_INSTALL_PATH}/lib/CppUTest/cmake CACHE PATH "CppUTest CMake package directory")
set(CPICKER_CACHE_PATH ${CMAKE_CURRENT_BINARY_DIR}/cpicker_cache CACHE PATH "Directory of c-picker generated file")
set(UNIT_TEST_COMMON_SOURCES ${CMAKE_CURRENT_LIST_DIR}/common/main.cpp)
set(CMAKE_CXX_STANDARD 11)
set(CLANG_LIBRARY_PATH_HELP "libclang directory for c-picker")
# Checking TF-A
if (NOT TF_A_PATH)
message(FATAL_ERROR "TF_A_PATH is not set")
endif()
# Trying to set CLANG_LIBRARY_PATH using the following methods
# 1. Using cache or command line definition
# Show warning if environment variable is also set but has different value
# 2. Copying the value of CLANG_LIBRARY_PATH environment variable if set
# 3. find_package (llvm-config, common paths or Windows registry)
# If none of the above steps succeeded CMake emits a fatal error and stops
if (DEFINED CLANG_LIBRARY_PATH)
message(STATUS "Using CLANG_LIBRARY_PATH from CMake variable (command line or cache)")
if (DEFINED ENV{CLANG_LIBRARY_PATH})
if (NOT (${CLANG_LIBRARY_PATH} STREQUAL $ENV{CLANG_LIBRARY_PATH}))
message(WARNING "Both CLANG_LIBRARY_PATH CMake and environment variables are set but have different values")
endif()
endif()
else()
if (DEFINED ENV{CLANG_LIBRARY_PATH})
message(STATUS "Setting CLANG_LIBRARY_PATH based on environment variable")
set(CLANG_LIBRARY_PATH $ENV{CLANG_LIBRARY_PATH} CACHE PATH ${CLANG_LIBRARY_PATH_HELP})
else()
message(STATUS "Setting CLANG_LIBRARY_PATH based on find_package")
find_package(LibClang REQUIRED)
set(CLANG_LIBRARY_PATH ${LibClang_LIBRARY_DIRS} CACHE PATH ${CLANG_LIBRARY_PATH_HELP})
endif()
endif()
message(STATUS "CLANG_LIBRARY_PATH has been set to ${CLANG_LIBRARY_PATH}")
# Checking c-picker
find_program(CPICKER_COMMAND "c-picker")
if (NOT CPICKER_COMMAND)
message(FATAL_ERROR "Please install c-picker using pip")
endif()
# Checking git
find_program(GIT_COMMAND "git")
if (NOT GIT_COMMAND)
message(FATAL_ERROR "Please install git")
endif()
# Fetching CppUTest
FetchContent_Declare(
cpputest
GIT_REPOSITORY ${CPPUTEST_URL}
GIT_TAG ${CPPUTEST_REFSPEC}
GIT_SHALLOW TRUE
PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/common/cpputest-cmake-fix.patch
)
# FetchContent_GetProperties exports cpputest_SOURCE_DIR and cpputest_BINARY_DIR variables
FetchContent_GetProperties(cpputest)
if(NOT cpputest_POPULATED)
message(STATUS "Fetching CppUTest")
FetchContent_Populate(cpputest)
endif()
# Build and install CppUTest in CMake time. This makes us able to use CppUTest as a CMake package later.
# Memory leak detection is turned off to avoid conflict with memcheck.
execute_process(COMMAND
${CMAKE_COMMAND}
-DMEMORY_LEAK_DETECTION=OFF
-DLONGLONG=ON
-DC++11=ON
-DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
-GUnix\ Makefiles
${cpputest_SOURCE_DIR}
WORKING_DIRECTORY
${cpputest_BINARY_DIR}
)
execute_process(COMMAND ${CMAKE_COMMAND} --build ${cpputest_BINARY_DIR} -- install -j)
# Finding CppUTest package. CMake will check [package name]_DIR variable.
set(CppUTest_DIR ${CPPUTEST_PACKAGE_PATH} CACHE PATH "Path of CppUTestConfig.cmake")
find_package(CppUTest CONFIG REQUIRED)
# find_package sets the CppUTest_INCLUDE_DIRS and CppUTest_LIBRARIES variables
include_directories(${CppUTest_INCLUDE_DIRS})
link_libraries(${CppUTest_LIBRARIES})
# Project level include directory
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
include(tests/lib/libc/test_libc.cmake)
include(tests/lib/object_pool/test_object_pool.cmake)