ADTF  3.18.5
cCompileTimeTest

Implements basic functionality to test whether compile process fails (or succeeds) More...

Classes

struct  platform_specific
 platform specific settings More...
 
struct  platform_specific< 0 >
 
struct  platform_specific< 1 >
 
struct  to_generator
 "stringify" the cmake generator More...
 
struct  to_generator< cCompileTimeTest::CCT_GENERATOR_DEFAULT >
 
struct  to_generator< cCompileTimeTest::CCT_GENERATOR_PLATFORM_DEFAULT >
 
struct  to_generator< cCompileTimeTest::CCT_GENERATOR_UNIX_MAKEFILES >
 
struct  to_generator< cCompileTimeTest::CCT_GENERATOR_VS14_WIN64 >
 
struct  to_generator< cCompileTimeTest::CCT_GENERATOR_VS15_WIN64 >
 

Public Types

enum  eCCTCMakeGenerators {
  CCT_GENERATOR_DEFAULT = 0 , CCT_GENERATOR_PLATFORM_DEFAULT = 1 , CCT_GENERATOR_UNIX_MAKEFILES = 2 , CCT_GENERATOR_VS14_WIN64 = 3 ,
  CCT_GENERATOR_VS15_WIN64 = 4
}
 All currently supported CMake generators. Usable in tBool cCompileTimeTest::SetUp() More...
 

Public Member Functions

 cCompileTimeTest (A_UTILS_NS::cFilename &&i_strProjectPath, A_UTILS_NS::cFilename &&i_strCmakeCommand=CMAKE_DEFAULT_COMMAND_SET)
 Constructor. More...
 
 ~cCompileTimeTest ()
 Destructor.
 
template<tInt Generator = CCT_GENERATOR_DEFAULT>
tBool SetUp (A_UTILS_NS::cString &&i_strTargetName)
 Generate build files and initially build a target. More...
 
tBool BuildTarget (A_UTILS_NS::cString &&i_strTargetName)
 Build a target from the cmake project. More...
 
const A_UTILS_NS::cStringGetOutput () const
 Get the console output of the last executed command. More...
 

Private Member Functions

tBool SetUp (A_UTILS_NS::cString &&i_strTargetName, A_UTILS_NS::cString &&i_strGenerator)
 Generate build files and initially build a target. More...
 
tBool TearDown ()
 Deletes the entire build directory (automatically called on destruction). More...
 
A_UTILS_NS::cString GetBuildDir () const
 Get the current build directory. More...
 
A_UTILS_NS::cString GetAdtfDir () const
 Get directory to ADTF installation which is under test in the current test bench. More...
 

Private Attributes

const A_UTILS_NS::cFilename m_strProjectPath
 path to project containing targets to build
 
const A_UTILS_NS::cFilename m_strCmakeCommand
 path to cmake executable to use
 
A_UTILS_NS::cString m_strConsoleOut
 Contains output of the last executed command.
 

Detailed Description

Implements basic functionality to test whether compile process fails (or succeeds)

This class may be used inside the ordinary ADTF test functions to check whether a (cmake) project can be compiled. This is especially useful if for a test to succeed, the compilation process of a target must fail. A "project under test" must be set up the following way:

  • Create a small project directory containing the header and source files
  • The directory must include one CMakeLists.txt file which is usable by cmake
  • The project under test MUST NOT be part of your test solution, otherwise the entire test won't compile
  • One project directory may have multiple targets to build

As an example, we have two targets to build (one compiles successfully, one fails to compile). In this example, the project directory contains three files:

  • compile_success.cpp
  • compile_fail.cpp
  • CMakeLists.txt

With CMakeLists.txt containing:

#CMakeLists.txt
cmake_minimum_required(VERSION 3.1) #to prevent cmake warning
include_directories(${ADTF_DIR}/include) #ADTF_DIR is resolved inside the test suite
add_executable(compile_success compile_success.cpp) #first build target
add_executable(compile_fail compile_fail.cpp) #second build target
#add_executable... #add as many targets as needed
constexpr const tChar *const ADTF_DIR
The ADTF_DIR points to your ADTF installation directory.

The path to the ADTF installation dir is evaluated using ${ADTF_DIR} which is perfectly fine to use, as it is passed to cmake from inside the test call (as command line argument ADTF_DIR:PATH = "path/to/adtf/". As a result it is also perfectly fine to add include directives to ADTF includes in the source files to test.

After the project is created and ready to be tested, cCompileTimeTest comes into play. An object of cCompileTimeTest is created with the path to the cmake project, which is the path to the directory containing the CMakeLists.txt file. To initially generate the project, cCompileTimeTest::SetUp() is called which does two things:

It generates the project files using the given cmake generator

It tells cmake to build a target which must succeed

The latter case is to prevent further compile executions to fail for the wrong reasons. For example, the target compile_fail should fail as a result to some invalid code snippet, but might unintentionally fail due to changes in the include hierarchy which would also yield a compilation error (which is not the intended error we hoped for).

To finally check whether compilation succeeds or fails, call cCompileTimeTest::BuildTarget() on as many targets as you like.

An example on how to use this inside a functional tester is given below.

// file: tester_compile.cpp
//setup the functional tester the usual way and create the compile test
{ //put into braces, the build directory gets cleaned up automatically when leaving scope
//1. Instantiate using path to the directory containing the CMakeLists.txt
cCompileTimeTest oMyCompileTimeTest("files/compile_test");
//2. Generate target (here with default generator) and try to compile target "compile_success"
__adtf_test(oMyCompileTimeTest.SetUp("compile_success"));
//3. To have this test succeed, building target "compile_fail" must fail
__adtf_test_err(oMyCompileTimeTest.BuildTarget("compile_fail"));
//[...] Build as many targets as you want
}
Implements basic functionality to test whether compile process fails (or succeeds)
Definition: compile_test.h:96

Definition at line 95 of file compile_test.h.

Member Enumeration Documentation

◆ eCCTCMakeGenerators

All currently supported CMake generators. Usable in tBool cCompileTimeTest::SetUp()

Enumerator
CCT_GENERATOR_DEFAULT 

The same generator this project was generated with.

CCT_GENERATOR_PLATFORM_DEFAULT 

Default generator (Linux: "Unix Makefiles", Win64: "Visual Studio 12 2013 Win64")

CCT_GENERATOR_UNIX_MAKEFILES 

"Unix Makefiles" (Linux only)

CCT_GENERATOR_VS14_WIN64 

"Visual Studio 14 2015 Win64" (Win64 only)

CCT_GENERATOR_VS15_WIN64 

"Visual Studio 15 2017 Win64" (Win64 only)

Definition at line 110 of file compile_test.h.

Constructor & Destructor Documentation

◆ cCompileTimeTest()

cCompileTimeTest ( A_UTILS_NS::cFilename &&  i_strProjectPath,
A_UTILS_NS::cFilename &&  i_strCmakeCommand = CMAKE_DEFAULT_COMMAND_SET 
)

Constructor.

Parameters
[in]i_strProjectPathPath to the cmake project
[in]i_strCmakeCommandPath to the CMake executable to use.

Member Function Documentation

◆ BuildTarget()

tBool BuildTarget ( A_UTILS_NS::cString &&  i_strTargetName)

Build a target from the cmake project.

Parameters
[in]i_strTargetNameName of the target to compile.
Returns
tTrue if compilation went successful, tFalse otherwise.

◆ GetAdtfDir()

A_UTILS_NS::cString GetAdtfDir ( ) const
private

Get directory to ADTF installation which is under test in the current test bench.

Returns
The ADTF installation directory as string.

◆ GetBuildDir()

A_UTILS_NS::cString GetBuildDir ( ) const
private

Get the current build directory.

Returns
The current build directory as string.

◆ GetOutput()

const A_UTILS_NS::cString& GetOutput ( ) const

Get the console output of the last executed command.

Returns
String containing the last console output.

◆ SetUp() [1/2]

tBool SetUp ( A_UTILS_NS::cString &&  i_strTargetName)
inline

Generate build files and initially build a target.

Currently the build directory is created in the systems $TEMPDIR$.

Parameters
[in]i_strTargetNameName of the target to test the initial build on.
Template Parameters
GeneratorSee eCCTCMakeGenerators for current supported generator types.
Returns
tTrue if generating and building went successful, tFalse otherwise.

Definition at line 140 of file compile_test.h.

◆ SetUp() [2/2]

tBool SetUp ( A_UTILS_NS::cString &&  i_strTargetName,
A_UTILS_NS::cString &&  i_strGenerator 
)
private

Generate build files and initially build a target.

Parameters
[in]i_strTargetNameName of the target to test the initial build on.
[in]i_strGeneratorThe generator name, see eCCTCMakeGenerators.
Returns
tTrue if generating and initial building worked correctly, tFalse otherwise

◆ TearDown()

tBool TearDown ( )
private

Deletes the entire build directory (automatically called on destruction).

Returns
tTrue if deletion was successful, tFalse otherwise.