Main Content

buildtool

Invoke build tool

Since R2022b

Description

Run Tasks

example

buildtool invokes the build tool on the build file in your project. It runs the default tasks in the plan returned by the build file as well as all the tasks on which they depend.

The command searches for a build file named buildfile.m in your current folder or its parent folders. If the build file does not exist in your current folder, the command searches through the parent folders until it finds buildfile.m.

example

buildtool tasks runs the specified tasks as well as all the tasks on which they depend. The command runs the tasks in the order specified. It runs each task at most one time and ignores a task in the list that has already run.

buildtool ___ option1 ... optionN specifies build options in addition to any of the input argument combinations in previous syntaxes. For example, buildtool -continueOnFailure continues running the build upon a failure.

List Tasks

example

buildtool -tasks lists all the tasks in the build plan. The list includes task names and descriptions.

Examples

collapse all

Invoke the build tool to list and run the tasks in the plan returned by a build file.

Open the example and then navigate to the buildtool_example folder, which contains a build file.

cd buildtool_example

This code shows the contents of the build file.

function plan = buildfile
import matlab.buildtool.tasks.CodeIssuesTask
import matlab.buildtool.tasks.TestTask

% Create a plan from task functions
plan = buildplan(localfunctions);

% Add the "check" task to identify code issues
plan("check") = CodeIssuesTask;

% Add the "test" task to run tests
plan("test") = TestTask;

% Make the "archive" task the default task in the plan
plan.DefaultTasks = "archive";

% Make the "archive" task dependent on the "check" and "test" tasks
plan("archive").Dependencies = ["check" "test"];
end

function archiveTask(~)
% Create ZIP file
filename = "source_" + ...
    string(datetime("now",Format="yyyyMMdd'T'HHmmss"));
zip(filename,"*")
end

List the tasks in the plan returned by the main function of the build file.

buildtool -tasks
archive - Create ZIP file
check   - Identify code issues
test    - Run tests

Run the default task in the plan. The build tool runs the "archive" task as well as both the tasks on which it depends. In this example, the tasks run successfully.

buildtool
** Starting check
Analysis Summary:
    Total Files: 3
         Errors: 0 (Threshold: 0)
       Warnings: 0 (Threshold: Inf)
** Finished check

** Starting test
...

Test Summary:
    Total Tests: 3
         Passed: 3
         Failed: 0
     Incomplete: 0
       Duration: 1.1387 seconds testing time.
                 
** Finished test

** Starting archive
** Finished archive

Now, run only the "check" and "test" tasks.

buildtool check test
** Starting check
Analysis Summary:
    Total Files: 3
         Errors: 0 (Threshold: 0)
       Warnings: 0 (Threshold: Inf)
** Finished check

** Starting test
...

Test Summary:
    Total Tests: 3
         Passed: 3
         Failed: 0
     Incomplete: 0
       Duration: 0.061273 seconds testing time.
                 
** Finished test

Run a task that accepts arguments to customize its actions.

Open the example and then navigate to the buildtool_example1 folder, which contains a build file.

cd buildtool_example1

This code shows the contents of the build file. The build file specifies a "test" task that accepts an optional argument, tests, as well as a name-value argument, OutputDetail.

function plan = buildfile
% Create a plan from task functions
plan = buildplan(localfunctions);
end

function testTask(context,tests,options)
% Run unit tests

arguments
    context
    tests string = context.Plan.RootFolder
    options.OutputDetail (1,1) string = "terse"
end

results = runtests(tests, ...
    IncludeSubfolders=true, ...
    OutputDetail=options.OutputDetail);
assertSuccess(results);
end

Use the "test" task to run the tests in the tests subfolder of your current folder. In this example, the tests pass and the task runs successfully.

buildtool test("tests")
** Starting test
...
** Finished test

Run the tests again and display test run progress at the "concise" level.

buildtool test("tests",OutputDetail="concise")
** Starting test
Running SolverTest
...
Done SolverTest
__________

** Finished test

Run tasks that specify inputs and outputs to support incremental builds.

Open the example and then navigate to the incremental_build_example folder, which contains a build file.

cd incremental_build_example

This code shows the contents of the build file:

  • The "pcode" task obfuscates its inputs and creates the P-code files in the same folders as the inputs.

  • The "archive" task creates an archive of its inputs.

function plan = buildfile
% Create a plan from the task functions
plan = buildplan(localfunctions);

% Specify the inputs and outputs of the "pcode" task
plan("pcode").Inputs = "source/**/*.m";
plan("pcode").Outputs = plan("pcode").Inputs.replace(".m",".p");

% Specify the inputs and outputs of the "archive" task
plan("archive").Inputs = plan("pcode").Outputs;
plan("archive").Outputs = "source.zip";
end

function pcodeTask(context)
% Create P-code files
filePaths = context.Task.Inputs.paths;
pcode(filePaths{:},"-inplace")
end

function archiveTask(context)
% Create ZIP file
task = context.Task;
zip(task.Outputs.paths,task.Inputs.paths)
end

Run the "archive" task. Because the inputs of the "archive" task are the outputs of the "pcode" task, the build tool runs the "pcode" task before running the "archive" task.

buildtool archive
** Starting pcode
** Finished pcode

** Starting archive
** Finished archive

Run the "archive" task again. The build tool skips both of the tasks because none of the inputs or outputs of the tasks have changed.

buildtool archive
** Skipped pcode: up-to-date

** Skipped archive: up-to-date

Add a file to the source folder, and then rerun the "archive" task. The build tool runs the "pcode" task because its inputs have changed. The build tool also runs the "archive" task because its inputs have changed.

fclose(fopen(fullfile("source","newFile.m"),"w"));
buildtool archive
** Starting pcode
** Finished pcode

** Starting archive
** Finished archive

Delete the ZIP file created by the "archive" task, and then run the task. The build tool skips the "pcode" task because none of its inputs or outputs have changed. However, the build tool runs the "archive" task because its output has changed.

delete("source.zip") 
buildtool archive
** Skipped pcode: up-to-date

** Starting archive
** Finished archive

Input Arguments

collapse all

Tasks to run, specified as one or more strings. If a task accepts arguments, enclose them in parentheses.

Example: test

Example: compile test

Example: check test("myFolder",OutputDetail="concise") archive("source.zip")

Build options, specified as one or more values in this table. Options can appear in any order.

OptionDescription
-continueOnFailure

Continue running the build upon a build environment setup or task failure. By default, the build runner terminates the build if a failure occurs. Use -continueOnFailure to run the subsequent tasks upon a failure.

-skip task

Skip the task named task. To skip multiple tasks, specify the -skip option for each task.

Example: -skip test

Example: -skip check -skip test

Example: -continueOnFailure

Example: -continueOnFailure -skip check -skip test

Tips

  • To maintain valid relative paths in tasks at run time, the build tool changes the current folder to the plan root folder before running the build and restores the current folder to its original state after running the build. To prevent a task from affecting subsequent tasks by changing the current folder, the build tool also restores the current folder after running each task.

Version History

Introduced in R2022b

expand all