blob: c18f3e3b51764fab59397aff93d4afe1f944e1ae [file] [log] [blame]
Julian Hall3d844c22020-11-23 18:22:43 +01001#-------------------------------------------------------------------------------
2# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8set(CPPUTEST_URL "https://github.com/cpputest/cpputest.git" CACHE STRING "CppUTest repository URL")
9set(CPPUTEST_REFSPEC "v3.8" CACHE STRING "CppUTest git refspec")
10set(CPPUTEST_INSTALL_PATH ${CMAKE_CURRENT_BINARY_DIR}/CppUTest_install CACHE PATH "CppUTest installation directory")
11
12include(FetchContent)
13
14# Checking git
15find_program(GIT_COMMAND "git")
16if (NOT GIT_COMMAND)
17 message(FATAL_ERROR "Please install git")
18endif()
19
20# Fetching CppUTest
21FetchContent_Declare(
22 cpputest
23 GIT_REPOSITORY ${CPPUTEST_URL}
24 GIT_TAG ${CPPUTEST_REFSPEC}
25 GIT_SHALLOW TRUE
26 PATCH_COMMAND git stash
27 COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/cpputest-cmake-fix.patch
28)
29
30# FetchContent_GetProperties exports cpputest_SOURCE_DIR and cpputest_BINARY_DIR variables
31FetchContent_GetProperties(cpputest)
32if(NOT cpputest_POPULATED)
33 message(STATUS "Fetching CppUTest")
34 FetchContent_Populate(cpputest)
35endif()
36
37# Build and install CppUTest configuration time. This makes us able to use CppUTest as a CMake package.
38# Memory leak detection is turned off to avoid conflict with memcheck.
39if(NOT CMAKE_CROSSCOMPILING)
40 execute_process(COMMAND
41 ${CMAKE_COMMAND}
42 -DMEMORY_LEAK_DETECTION=OFF
43 -DLONGLONG=ON
44 -DC++11=ON
45 -DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
46 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
47 -G${CMAKE_GENERATOR}
48 ${cpputest_SOURCE_DIR}
49 WORKING_DIRECTORY
50 ${cpputest_BINARY_DIR}
51 RESULT_VARIABLE
52 _exec_error
53 )
54else()
55 execute_process(COMMAND
56 ${CMAKE_COMMAND}
57 -DMEMORY_LEAK_DETECTION=OFF
58 -DLONGLONG=ON
59 -DC++11=ON
60 -DCMAKE_INSTALL_PREFIX=${CPPUTEST_INSTALL_PATH}
61 -DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
62 -DTESTS=OFF
63 -DEXTENSIONS=OFF
64 -DHAVE_FORK=OFF
65 -DCPP_PLATFORM=armcc
66 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
67 -G${CMAKE_GENERATOR}
68 ${cpputest_SOURCE_DIR}
69 WORKING_DIRECTORY
70 ${cpputest_BINARY_DIR}
71 RESULT_VARIABLE
72 _exec_error
73 )
74endif()
75if (NOT _exec_error EQUAL 0)
76 message(FATAL_ERROR "Configuriong CppUTest build failed.")
77endif()
78execute_process(COMMAND
79 ${CMAKE_COMMAND}
80 --build ${cpputest_BINARY_DIR}
81 -- install -j8
82 RESULT_VARIABLE
83 _exec_error
84 )
85if (NOT _exec_error EQUAL 0)
86 message(FATAL_ERROR "Building CppUTest failed.")
87endif()
88
89# Finding CppUTest package. CMake will check [package name]_DIR variable.
90set(CppUTest_DIR ${CPPUTEST_INSTALL_PATH}/lib/CppUTest/cmake CACHE PATH "CppUTest package location" FORCE)
91find_package(CppUTest CONFIG REQUIRED NO_DEFAULT_PATH PATHS ${CppUTest_DIR})
92# CppUTest package files do not set include path properties on the targets.
93# Fix this here.
94foreach(_cpputest_target IN LISTS CppUTest_LIBRARIES)
95 if (TARGET ${_cpputest_target})
96 target_include_directories(${_cpputest_target} INTERFACE ${CppUTest_INCLUDE_DIRS})
97 target_compile_features(${_cpputest_target} INTERFACE cxx_std_11)
98 endif()
99endforeach()