Contenuto principale

Create Project from Build System Using Python API for Polyspace

You can manage Polyspace® Test™ workflows entirely at the command line by using the Python® API for Polyspace. In particular, you can dynamically generate a Polyspace Test configuration based on your build command and build your tests using this configuration.

In this example, you create a Polyspace Platform project from a makefile by using a Python script. The script dynamically generates the source and build information from the makefile and then attaches them to the project.

Prerequisites

Make sure you are using a supported Python version and you are able to import the polyspace.project and polyspace.test Python modules without errors.

import polyspace.project
import polyspace.test
For more information, see Set Up Python API for Polyspace.

Example Files

Source Files

The source and header files used in this example are available in the folder polyspaceroot/polyspace/examples/doc_pstest/python_api_test_authoring/src. Here, polyspaceroot is the Polyspace installation folder.

Build System

The sources in this example can be built outside a project using this makefile, which is available in the polyspaceroot/polyspace/examples/doc_pstest/python_api_test_authoring folder. This makefile uses the POLYSPACEROOT variable that you specify when you invoke the make command.

# Compiler and flags
CC = gcc
SOURCES = $(wildcard $(POLYSPACEROOT)/polyspace/examples/doc_pstest/python_api_test_authoring/src/*.c) 
TARGET = test_program

# Default target
all: $(TARGET)

# Compilation target
$(TARGET): $(SOURCES)
	$(CC) -c $(SOURCES)
    
# Clean target
clean:
	rm -f $(TARGET)

Dynamically Generate Project with Source and Build Information

To execute the instructions in the makefile to dynamically create a project that contains the source and build information, do the following:

  1. Navigate to a writable folder that will contain the build artifacts and the generated Polyspace Platform project.

  2. Write a Python script to trace your build system (Makefile) and generate a Polyspace Platform project myProject.psprjx. If this project already exists, sync the project with the latest sources and build information by retracing your build system. See Information Gathered From Build Systems for Polyspace Analysis and Testing.

    This script:

    • Uses the polyspace.__install_path__ function to supply the POLYSPACEROOT variable to the make command.

    • Uses the -f option to specify the location of the makefile to the make command.

    • Invokes the polyspace-configure system command to trace the build.

    import os
    import subprocess
    makefileArgument = f"POLYSPACEROOT={polyspace.__install_path__}"
    makefilePath = os.path.join(polyspace.__install_path__, "polyspace", "examples", "doc_pstest", "python_api_test_authoring", "makefile")
    polyspaceConfigurePath = os.path.join(polyspace.__install_path__, "polyspace", "bin", "polyspace-configure")
    if os.path.isfile("myProject.psprjx"):
        subprocess.run([polyspaceConfigurePath, "-update-platform-project", "myProject.psprjx", "make", "-f", makefilePath, makefileArgument, "-B"])
    else:   
        subprocess.run([polyspaceConfigurePath, "-output-platform-project", "myProject.psprjx", "make", "-f", makefilePath, makefileArgument, "-B"])

  3. Load the project myProject.psprjx in a polyspace.project.Project object.

    proj = polyspace.project.Project("myProject.psprjx")

Append Additional Options

If your system has special build requirements, you can append additional options to the generated build configuration.

Suppose that, for conditional compilation of your code, you want to mark the preprocessor macro VARIANT_1 as defined. Use the Defines.append method to add this definition to your active build configuration. To update the myProject.psprjx file, save the project.

proj.ActiveBuildConfiguration.Defines.append("VARIANT_1")
proj.save()

Add Tests

To test your source code, add reusable graphical tests to the dynamically generated project:

  • You can add graphical tests to the project object proj using the Python API for Polyspace. See Author Graphical Tests Using Python API for Polyspace.

    Once you add new graphical tests, call the save method to update the myProject.psprjx file. Then export the tests to .pstestd files so that they are available even after you discard the dynamically generated project file.

  • If you already have graphical tests in existing .pstestd files that your source code must pass, add references to those .pstestd files in myProject.psprjx.

Build and Run Tests

Build and run the tests in myProject.psprjx in the Polyspace Platform user interface, or by using the Python API as follows:

res = polyspace.test.build(proj)

See Also

| | |

Topics