In this section:
Table of Contents | ||
---|---|---|
|
Introduction
You can use the cpptestscan or cpptesttrace utility to create a C++test project that you would normally build using tools such as GNU make, CMake, and QMake. The utilities collect information from the build process of an existing code base that C++test can use to automatically create a project. You can also build a project and manually configure it using the information collected by the utilities.
...
Build Data File
...
(.bdf)
Build information, such as the working directory, command line options for the compilation, and link processes of the original build, are stored in a file called the build data file. The following example is a fragment from a build data file:
...
- Manually setting up 'Use options from the build data file' as the options source for the project and selecting appropriate build data file (see Creating a Project from the GUI).
- Using the GUI to automatically import a project. See Importing project using Build Data File with the GUI wizard.
Using the command line to automatically import a project. See Importing a Project from the Command Line.
Note title Note Required environment variables can also be stored in the build data file if the following apply:
- Your build system sets up the required environment variables for the compiler / linker to work correctly
- These variables are not available in the environment when run C++tests.
See description of the
'--cpptestscanEnvInOutput'
option below.
Using cpptestscan
or cpptesttrace
to Create a Build Data File
Anchor | ||||
---|---|---|---|---|
|
...
Option | Environment Variable | Description | Default |
---|---|---|---|
-- cpptestscanOutputFile= <OUTPUT_FILE> -- cpptesttraceOutputFile= <OUTPUT_FILE> | CPPTEST_SCAN_OUTPUT_FILE) | Defines file to append build information to. | cpptestscan.bdf |
| CPPTEST_SCAN_PROJECT_NAME | Defines suggested name of the C++test project. | name of the current working directory |
| CPPTEST_SCAN_RUN_ORIG_CMD | If set to "yes", original command line will be executed. | yes |
| CPPTEST_SCAN_QUOTE_CMD_LINE_MODE | Determines the way C++test quotes parameters when preparing cmd line to run.
cpptestscanQuoteCm dLineMode is not supported on Linux
| all |
| CPPTEST_SCAN_CMD_LINE_PREFIX | If non-empty and running original executable is turned on, the specified command will be prefixed to the original command line. | [empty] |
| CPPTEST_SCAN_ENV_IN_OUTPUT | Enabling dumps the selected environment variables and the command line arguments that outputs the file. For advanced settings use – cpptestscanEnvFile and – cpptestscanEnvars options | no |
| CPPTEST_SCAN_ENV_FILE | If enabled, the specified file keeps common environment variables for all build commands; the main output file will only keep differences. Use this option to reduce the size of the main output file. Use this option with – cpptestscanEnvInOut put enabled | [empty] |
| CPPTEST_SCAN_ENVARS | Selects the names of environment variables to be dumped or '*' to select them all. Use this option with – cpptestscanEnvInOut put enabled. | * |
| CPPTEST_SCAN_USE_VARIABLE | Replaces each occurence of "VALUE" string in the scanned build information with the "${VAR_NAME}" variable usage. | [empty] |
-- cpptesttraceTraceComm and | CPPTEST_SCAN_TRACE_COMMAND | Defines the command names that will be traced when collecting build process information. These names, specified as regular expressions, should match original compiler / linker commands used in the build process. |
Example: Modifying GNU Make Build Command to Using cpptestscan
Assuming that a make-based build in which the compiler variable is CXX and the original compiler is g++:
...
Note | ||
---|---|---|
| ||
When the build runs in multiple directories:
|
Example: Modifying GNU Make Build Command Using cpptesttrace
Assume that a regular make-based build is executed with:
...
Note | ||
---|---|---|
| ||
If the compiler and/or linker executable names do not match default |
Example: Modifying GNU Makefile to use cpptestscan
If your Makefile uses CXX as a variable for the compiler executable and is normally defined as CXX=g++, you can redefine the variable:
...
Note | ||
---|---|---|
| ||
The |
Example: Using cpptesttrace with CMake build
Assuming that you have a CMake-based build, you can produce a build data file using cpptesttrace:
...
A build data file will be generated in the location defined by the CPPTEST_SCAN_OUTPUT_FILE variable. If the varialbe is isn’t set, the build data file(s) will be generated in the Makefiles’ locations.
Example: Using cpptestscan with CMake build
All scripts and commands are bash-based – adapt them as needed for different shells.
...
- Use CMake to re-generate make files with the 'cpptestscan' used as a compiler prefix. Make sure the ‘cpptestscan’ executable is available on the PATH.
If original CMake command is
cmake -G “Unix Makefiles” ../project_root
, then you need to get rid of existing CMake cache and run cmake overriding compiler variables. The following example assumes 'gcc' is used as a C compiler and 'g++' as a C++ compiler executable:Code Block rm CMakeCache.txt CC="cpptestscan gcc" CXX="cpptestscan g++" cmake -G "Unix Makefiles" ../project_root
- Look in the CMakeCache.txt file to see if CMAKE_*_COMPILER variables point to cpptestscan.
- If the make files are re-generated, jump to step 5. Continue if
cmake
failed in the boot-strap phase because the compiler wasn’t recognized.
Prepare the
cpptestscan
wrapper scripts that will behave like a CMake compiler by creating the following BASH scripts. In this example, we assume 'gcc' is used as a C compiler and 'g++' as a C++ compiler executable:Code Block >cat cpptest_gcc.sh #!/bin/bash cpptestscan gcc --cpptestscanRunOrigCmd=no $* > /dev/null 2>&1 gcc $* exit $? >cat cpptest_g++.sh #!/bin/bash cpptestscan g++ --cpptestscanRunOrigCmd=no $* > /dev/null 2>&1 g++ $* exit $?
- The first script invokes
cpptestscan
to extract options without running the compiler. The second script runs the actual compiler so that the entire script looks and acts like a compiler in order to be “accepted” by CMake.
- The first script invokes
- Give the scripts executable attributes and place them in a common location so they are acces-sible to everyone who needs to scan make files. Make sure the
cpptestscan
and the scripts are available on the PATH. - Use CMake to re-generate make files with the scripts used as compilers by extending the original CMake command with options that re-generate make files from the prepared scripts.
Original CMake command is
cmake -G “Unix Makefiles” ../project_root
then you need to get rid of existing CMake cache and run cmake overriding compiler vari-ables. In the following example, we assume 'gcc' is used as a C compiler and 'g++' as a C++ compiler executable:Code Block rm CMakeCache.txt cmake -G "Unix Makefiles" -D CMAKE_C_COMPILER=cpptest_gcc.sh -D CMAKE_CXX_COMPILER=cpptest_g++.sh ../project_root
- Look in the CMakeCache.txt file to see if CMAKE_*_COMPILER variables point to prepared wrappers.
Setup environment variables for
cpptestscan
; make sure to use an absolute path for the out-put BDF file:Code Block export CPPTEST_SCAN_PROJECT_NAME=my_project export CPPTEST_SCAN_OUTPUT_FILE=$PROJ_ROOT/cpptestscan.bdf
- Run the project build normally without overwriting any make variables. Build data files(s) will be generated in location defined by the CPPTEST_SCAN_OUTPUT_FILE variable or - if not set -in location of Makefiles.
Note | ||
---|---|---|
| ||
By default CMake-generated make files only print information about performed actions without actual compiler/linker command lines. Add “VERBOSE=1” to the make command line to see executed compiler/linker command lines. |
Using cpptestscan or cpptestrace with other Build Systems
For non-make based build systems, usage of cpptestscan and cpptesttrace is very similar to the examples shown above. Typically, a compiler is defined as a variable somewhere in the build scripts. To create a Build Data File from that build system using cpptestscan, prefix the original compiler executable with cpptestscan. To create a Build Data File from that build system using cpptesttrace, prefix whole build command line with cpptesttrace.
Info |
---|
When should I use cpptestscan?It is highly recommended that the procedures to prepare a build data file are integrated with the build system. In this way, generating the build data file can be done when the normal build is performed without additional actions. To achieve this, prefix your compiler and linker executables with the cpptestscan utility in your Makefiles / build scripts. When should I use cpptesttrace?Use cpptesttrace as the prefix for the whole build command when modifying your Makefiles / build scripts isn’t possible or when prefixing your compiler / linker executables from the build command line is too complex. |
Importing
...
Project Using Build Data File with the GUI
...
Wizard
Anchor | ||||
---|---|---|---|---|
|
Once you have used cpptestscan
or cpptesttrace
to generate a build data file for code you want to test in C++test, you can use the Project Creation wizard to create a C++test project.
Info | ||
---|---|---|
| ||
If you are using a custom compiler, add it as described in Configuring Testing with the Cross Compiler before starting the wizard. |
...
You can define custom project settings in a plain text options file, which is passed to cpptestcli
using the -localsettings
switch. Settings can be specified in the options file as described in Local Settings (Options) Files.
Examples
The following examples demonstrate how to create a C++test project from the command line using cpptestscan
. They use C++test’s ATM example, which is available in the <INSTALL_DIR>/Examplesexamples/ATM
directory
The examples use a make-based build; however, a .bdf file can be produced from any build system.
Assumptions and Prerequisites
- The
cpptestscan
executable should be included on $PATH (it is located in C++test's installa-tion directory). - The
cpptestcli
executable should be included on $PATH (it is located in C++test's installa-tion directory). g++
is assumed to be the original compiler executable.- The workspace and .bdf file locations have to be entered in a format supported by the given shell/command prompt. For example:
/home/MyWorkspace on UNIX/Cygwin
c:\home\MyWorkspace on Windows
c:/home/MyWorkspace on Cygwin
- To create a project without performing any testing, omit
-config
- To create and test the new project, use the appropriate test configuration (e.g.
-config Must-HaveRules
). - A full project rebuild will be performed ("clean all") to ensure that all objects are built in the make run
Example 1 - Creating a C++test project in the workspace location with default settings
- Create a build data file (.bdf) based on the original Makefile as follows:
- Go to the
<INSTALL_DIR>/Examplesexamples/ATM
directory. - Build the ATM project while prefixing the original compiler executable with the
cpptestscan
executable:> make CC="cpptestscan g++" clean all
- Note that a new data file
(cpptestscan.bdf)
was created in the<INSTALL_DIR>/Examplesexamples/ATM
directory.
- Go to the
- Create a C++test project based on the build data file (.bdf) as follows:
- Use C++test's CLI mode to create a new project in the
/home/MyWorkspace
workspace:> cpptestcli -data /home/MyWorkspace -bdf cpptestscan.bdf
- Note that a new C++test project (ATM) was created in MyWorkspace location. It con-tains all the source files and build options of the original
<INSTALL_DIR>/Examplesexamples/ATM
project.
- Use C++test's CLI mode to create a new project in the
Example 2 - Creating a C++test project in original project's location with "Visual C++ 7.1" set as the compiler and "myProject" set as the project name
- Create a build data file (.bdf) based on the original Makefile as follows:
- Go to the
<INSTALL_DIR>/Examplesexamples/ATM
directory. - Build the ATM project while prefixing the original compiler executable with the
cpptestscan
executable:> make CC="cpptestscan --cpptestscanProjectName=myProject g++" clean all
- Note that a new data file
(cpptestscan.bdf)
was created in the<INSTALL_DIR>/Examplesexamples/ATM
directory. Notice thatmyProject
was set as project name.
- Go to the
- Create a C++test project based on the build data file (.bdf) as follows:
- First, override the default settings:
- Create a plain text options file named
opts.properties
in<INSTALL_DIR>/Examplesexamples/ATM
. - Set the compiler family to Visual C++ 7.1 by entering
bdf.import.compiler.family=vc_7_1
into theopts.properties
file. - Change the destination project location to the location of the
cpptestscan.bdf
file (which is located in the original project's directory) by entering:bdf.import.location=BDF_LOC into opts.properties file
- Create a plain text options file named
- Next, use C++test's CLI mode to create a new project in the
/home/MyWorkspace
workspace:cpptestcli -data /home/MyWorkspace -bdf cpptestscan.bdf -localsettings opts.properties
- Finally, note that a New C++test project (myProject) was created in <INSTALL_DIR>/Examplesexamples/ATM location, containing all the source files and build options of the original <INSTALL_DIR>/Examplesexamples/ATM project, having Visual C++ 7.1 set as compiler family.
- First, override the default settings:
...