cargo-make

crates.io Build Status Build status codecov
license Libraries.io for GitHub downloads Download
Built with cargo-make

Rust task runner and build tool.

Overview

The cargo-make task runner enables to define and configure sets of tasks and run them as a flow.
A task is a command, script, rust code or other sub tasks to execute.
Tasks can have dependencies which are also tasks that will be executed before the task itself.
With a simple toml based configuration file, you can define a multi platform build script that can run build, test, generate documentation,run bench tests, run security validations and more, executed by running a single command.

Installation

In order to install, just run the following command

sh cargo install --force cargo-make

This will install cargo-make in your ~/.cargo/bin.
Make sure to add ~/.cargo/bin directory to your PATH variable.

You will have two executables available: cargo-make and makers

Binary Release

Binary releases are available in the github releases page.
The following binaries are available for each release:

Linux builds for arm are available on bintray

Usage

When using cargo-make, all tasks are defined and configured via toml files.
Below are simple instructions to get you started off quickly.

Simple Example

In order to run a set of tasks, you first must define them in a toml file.
For example, if we would like to have a script which:

We will create a toml file as follows:

```toml [tasks.format] install_crate = "rustfmt" command = "cargo" args = ["fmt", "--", "--write-mode=overwrite"]

[tasks.clean] command = "cargo" args = ["clean"]

[tasks.build] command = "cargo" args = ["build"] dependencies = ["clean"]

[tasks.test] command = "cargo" args = ["test"] dependencies = ["clean"]

[tasks.my-flow] dependencies = [ "format", "build", "test" ] ```

We would execute the flow with the following command:

sh cargo make --makefile simple-example.toml my-flow

The output would look something like this:

```console [cargo-make] info - Using Build File: simple-example.toml [cargo-make] info - Task: my-flow [cargo-make] info - Setting Up Env. [cargo-make] info - Running Task: format [cargo-make] info - Execute Command: "cargo" "fmt" "--" "--write-mode=overwrite" [cargo-make] info - Running Task: clean [cargo-make] info - Execute Command: "cargo" "clean" [cargo-make] info - Running Task: build [cargo-make] info - Execute Command: "cargo" "build" Compiling bitflags v0.9.1 Compiling unicode-width v0.1.4 Compiling quote v0.3.15 Compiling unicode-segmentation v1.1.0 Compiling strsim v0.6.0 Compiling libc v0.2.24 Compiling serde v1.0.8 Compiling vecmap v0.8.0 Compiling ansiterm v0.9.0 Compiling unicode-xid v0.0.4 Compiling synom v0.11.3 Compiling rand v0.3.15 Compiling termsize v0.3.0 Compiling atty v0.2.2 Compiling syn v0.11.11 Compiling textwrap v0.6.0 Compiling clap v2.25.0 Compiling serdederiveinternals v0.15.1 Compiling toml v0.4.2 Compiling serdederive v1.0.8 Compiling cargo-make v0.1.2 (file:///home/ubuntu/workspace) Finished dev [unoptimized + debuginfo] target(s) in 79.75 secs [cargo-make] info - Running Task: test [cargo-make] info - Execute Command: "cargo" "test" Compiling cargo-make v0.1.2 (file:///home/ubuntu/workspace) Finished dev [unoptimized + debuginfo] target(s) in 5.1 secs Running target/debug/deps/cargo_make-d5f8d30d73043ede

running 10 tests test log::tests::createinfo ... ok test log::tests::getlevelerror ... ok test log::tests::createverbose ... ok test log::tests::getlevelinfo ... ok test log::tests::getlevelother ... ok test log::tests::getlevelverbose ... ok test installer::tests::iscrateinstalledfalse ... ok test installer::tests::iscrateinstalledtrue ... ok test command::tests::validateexitcodeerror ... ok test log::tests::createerror ... ok

test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

[cargo-make] info - Running Task: my-flow [cargo-make] info - Build done in 72 seconds. ```

We now created a build script that can run on any platform.

cargo-make can be invoked as a cargo plugin via 'cargo make' command or as a standalone executable via 'makers' command.

Tasks, Dependencies and Aliases

In many cases, certain tasks depend on other tasks.
For example you would like to format the code before running build and run the build before running tests.
Such flow can be defined as follows:

```toml [tasks.format] install_crate = "rustfmt" command = "cargo" args = ["fmt", "--", "--write-mode=overwrite"]

[tasks.build] command = "cargo" args = ["build"] dependencies = ["format"]

[tasks.test] command = "cargo" args = ["test"] dependencies = ["build"] ```

When you run:

sh cargo make --makefile ./my_build.toml test

It will try to run test, see that it has dependencies and those have other dependencies.
Therefore it will create an execution plan for the tasks based on the tasks and their dependencies.
In our case it will invoke format -> build -> test.

The same task will never be executed twice so if we have for example:

```toml [tasks.A] dependencies = ["B", "C"]

[tasks.B] dependencies = ["D"]

[tasks.C] dependencies = ["D"]

[tasks.D] script = [ "echo hello" ] ```

In this example, A depends on B and C, and both B and C are dependent on D.
Task D however will not be invoked twice.
The output of the execution will look something like this:

console [cargo-make] info - Task: A [cargo-make] info - Setting Up Env. [cargo-make] info - Running Task: D [cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/CNuU47tIix.sh" hello [cargo-make] info - Running Task: B [cargo-make] info - Running Task: C [cargo-make] info - Running Task: A

As you can see, 'hello' was printed once by task D as it was only invoked once.
But what if we want to run D twice?
Simple answer would be to duplicate task D and have B depend on D and C depend on D2 which is a copy of D.
But duplicating can lead to bugs and to huge makefiles, so we have aliases for that.
An alias task has its own name and points to another task.
All of the definitions of the alias task are ignored.
So now, if we want to have D execute twice we can do the following:

```toml [tasks.A] dependencies = ["B", "C"]

[tasks.B] dependencies = ["D"]

[tasks.C] dependencies = ["D2"]

[tasks.D] script = [ "echo hello" ]

[tasks.D2] alias="D" ```

Now C depends on D2 and D2 is an alias for D.
Execution output of such make file would like as follows:

console [cargo-make] info - Task: A [cargo-make] info - Setting Up Env. [cargo-make] info - Running Task: D [cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/HP0UD7pgoX.sh" hello [cargo-make] info - Running Task: B [cargo-make] info - Running Task: D2 [cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/TuuZJkqCE2.sh" hello [cargo-make] info - Running Task: C [cargo-make] info - Running Task: A

Now you can see that 'hello' was printed twice.

It is also possible to define platform specific aliases, for example:

```toml [tasks.mytask] linuxalias = "linuxmytask" windowsalias = "windowsmytask" macalias = "macmytask"

[tasks.linuxmytask]

[tasks.macmytask]

[tasks.windowsmytask] ```

If platform specific alias is found and matches current platform it will take precedence over the non platform alias definition.
For example:

```toml [tasks.mytask] linuxalias = "run" alias = "do_nothing"

[tasks.run] script = [ "echo hello" ]

[tasks.do_nothing] ```

If you run task mytask on windows or mac, it will invoke the donothing task.
However, if executed on a linux platform, it will invoke the run task.

As a side note, cargo-make will attempt to invoke the task dependencies in the order that they were defined unless they are defined also as sub dependencies.

Commands, Scripts and Sub Tasks

The actual operation that a task invokes can be defined in 3 ways.
The below explains each one:

Only one of the definitions will be used.
If multiple attributes are defined (for example both command and script), the task will fail during invocation.

The script attribute may hold non OS scripts, for example rust code to be compiled and executed.
In order to use non OS script runners, you must define the special script_runner with the @ prefix.
The following runners are currently supported:

Below are some basic examples of each action type.

Sub Task

In this example, if we execute the flow task, it will invoke the echo task defined in the run_task attribute.

```toml [tasks.echo] script = [ "echo hello world" ]

[tasks.flow] run_task = "echo" ```

Command

For running commands, you can also define the command line arguments as below example invokes cargo command with the plugin name as a command line argument:

toml [tasks.build-with-verbose] command = "cargo" args = ["build", "--verbose", "--all-features"]

It is possible to provide environment variables as part of the command and arguments to be replaced in runtime with actual values, for example:

```toml [env] SIMPLE = "SIMPLE VALUE" ECHO_CMD = "echo"

[tasks.expand] command = "${ECHO_CMD}" args = [ "VALUE: ${SIMPLE}" ] ```

cargo-make cli also supports additional arguments which will be available to all tasks.
Following example task, will print those additional arguments:

toml [tasks.varargs] command = "echo" args = [ "args are:", "${@}" ]

Invoking cargo-make with additional arguments would result in the following:

```console

cargo make varargs arg1 arg2 arg3

[cargo-make] INFO - cargo-make 0.16.0 [cargo-make] INFO - Using Build File: Makefile.toml [cargo-make] INFO - Task: varargs [cargo-make] INFO - Setting Up Env. [cargo-make] INFO - Running Task: init [cargo-make] INFO - Running Task: varargs [cargo-make] INFO - Execute Command: "echo" "args are:" "arg1" "arg2" "arg3" args are: arg1 arg2 arg3 [cargo-make] INFO - Running Task: end [cargo-make] INFO - Build Done in 0 seconds. ```

Invoking cargo-make without any additional arguments would result in the following:

```console

cargo make varargs

[cargo-make] INFO - cargo-make 0.16.0 [cargo-make] INFO - Using Build File: Makefile.toml [cargo-make] INFO - Task: varargs [cargo-make] INFO - Setting Up Env. [cargo-make] INFO - Running Task: init [cargo-make] INFO - Running Task: varargs [cargo-make] INFO - Execute Command: "echo" "args are:" args are: [cargo-make] INFO - Running Task: end [cargo-make] INFO - Build Done in 0 seconds. ```

This can also be used for templating, for example:

toml [tasks.varargs] command = "echo" args = [ "args are:", "-o=${@}" ]

Would output:

```console

cargo make varargs arg1 arg2 arg3

[cargo-make] INFO - cargo-make 0.16.0 [cargo-make] INFO - Using Build File: Makefile.toml [cargo-make] INFO - Task: varargs [cargo-make] INFO - Setting Up Env. [cargo-make] INFO - Running Task: init [cargo-make] INFO - Running Task: varargs [cargo-make] INFO - Execute Command: "echo" "args are:" "arg1" "arg2" "arg3" args are: -o=arg1 -o=arg2 -o=arg3 [cargo-make] INFO - Running Task: end [cargo-make] INFO - Build Done in 0 seconds. ```

Script

Below simple script which prints hello world.

toml [tasks.hello-world] script = [ "echo start...", "echo \"Hello World From Script\"", "echo end..." ]

You can use multi line toml string to make the script more readable as follows:

toml [tasks.hello-world] script = [ ''' echo start... echo "Hello World From Script" echo end... ''' ]

cargo-make cli also supports additional arguments which will be available to all tasks.
Following example task, will print those additional arguments:

toml [tasks.cli-args] script = [ "echo args are: ${@}" ]

Invoking cargo-make with additional arguments would result in the following:

```console

cargo make cli-args arg1 arg2 arg3

[cargo-make] INFO - cargo-make 0.16.0 [cargo-make] INFO - Using Build File: Makefile.toml [cargo-make] INFO - Task: cli-args [cargo-make] INFO - Setting Up Env. [cargo-make] INFO - Running Task: init [cargo-make] INFO - Running Task: cli-args + cd /media/devhdd/projects/rust/cargo-make/examples + echo args are: arg1 arg2 arg3 args are: arg1 arg2 arg3 [cargo-make] INFO - Running Task: end ```

Invoking cargo-make without any additional arguments would result in the following:

```console

cargo make cli-args

[cargo-make] INFO - cargo-make 0.16.0 [cargo-make] INFO - Using Build File: Makefile.toml [cargo-make] INFO - Task: cli-args [cargo-make] INFO - Setting Up Env. [cargo-make] INFO - Running Task: init [cargo-make] INFO - Running Task: cli-args + cd /media/devhdd/projects/rust/cargo-make/examples + echo args are: args are: [cargo-make] INFO - Running Task: end [cargo-make] INFO - Build Done in 0 seconds. ```

Rust Code

In this example, when the rust task is invoked, the script content will be compiled and executed. You can see how dependencies are defined in Cargo.toml format inside the code.

toml [tasks.rust] script_runner = "@rust" script = [ ''' //!cargo //! [dependencies] //! time = "*" //! extern crate time; fn main() { println!("{}", time::now().rfc822z()); } ''' ]

Same as OS scripts, the @rust runner also supports the cargo-make CLI arguments access.

Cross Platform Shell

In this example, when the shell task is invoked, the script content will be automatically converted to windows batch commands (in case we are on windows platform) and invoked.

toml [tasks.shell] script_runner = "@shell" script = [ ''' rm ./myfile.txt ''' ]

Same as OS scripts, the @shell runner also supports the cargo-make CLI arguments access.

Other Programming Languages

cargo-make can also run scripts written in various scripting languages such as python, perl, ruby, javascript and more...
Any runner which takes the form of command file (for example python ./program.py) is supported.

Below are few examples:

```toml [tasks.python] scriptrunner = "python" scriptextension = "py" script = [ ''' print("Hello, World!") ''' ]

[tasks.perl] scriptrunner = "perl" scriptextension = "pl" script = [ ''' print "Hello, World!\n"; ''' ]

[tasks.javascript] scriptrunner = "node" scriptextension = "js" script = [ ''' console.log('Hello, World!'); ''' ]

[tasks.powershell] scriptrunner = "powershell" scriptextension = "ps1" script = [ ''' Write-Host "Hello, World!" ''' ] ```

Default Tasks and Extending

There is no real need to define the tasks that were shown in the previous examples.
cargo-make comes with a built in toml file that will serve as a base for every execution.
The optional external toml file that is provided while running cargo-make will only extend and add or overwrite tasks that are defined in the default Makefile.toml.
Lets take the build task definition which comes already in the default toml:

toml [tasks.build] command = "cargo" args = ["build"]

If for example, you would like to add verbose output to it, you would just need to change the args and add the --verbose as follows:

toml [tasks.build] args = ["build", "--verbose"]

If you want to disable some existing task (will also disable its dependencies), you can do it as follows:

toml [tasks.build] disabled = true

There is no need to redefine existing properties of the task, only what needs to be added or overwritten.
The default toml file comes with many steps and flows already built in, so it is worth to check it first.

In case you do want to delete all of the original task attributes in your extended task, you can use the clear attribute as follows:

toml [tasks.sometask] clear = true command = "echo" args = [ "extended task" ]

You can also extend additional external files from your external file by using the extend attribute, for example:

toml extend = "my_common_makefile.toml"

The file path in the extend attribute is always relative to the current toml file you are in and not to the process working directory.

The extend attribute can be very useful when you have a workspace with a Makefile.toml that contains all of the common custom tasks and in each project you can have a simple Makefile.toml which just has the extend attribute pointing to the workspace makefile.

Automatically Extend Workspace Makefile

When running cargo make for modules which are part of a workspace, you can automatically have the member crates makefile (even if doesn't exist) extend the workspace level makefile.

The workspace level makefile env section must contain the following environment variable (can also be set via cli)

toml [env] CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = "true"

Load Scripts

In more complex scenarios, you may want multiple unrelated projects to share some common custom tasks, for example if you wish to notify some internal company server of the build status.
Instead of redefining those tasks in each project you can create a single toml file with those definitions and have all projects extend that file.
The extend however, only knows to find the extending files in the file system, so in order to pull some common toml from a remote server (using http or git clone and so on...), you can use the load scripts.

Load scripts are defined in the config section using the load_script attribute and are invoked before the extend attribute is evaluated.
This allows you to first pull the toml file from the remote server and put it in a location defined by the extend attribute.

Here is an example of a load script which downloads the common toml from a remote server using HTTP:

toml [config] load_script = ["wget -O /home/myuser/common.toml companyserver.com/common.toml"]

Here is an example of pulling the common toml file from some git repo:

toml [config] load_script = ["git clone git@mygitserver:user/project.git /home/myuser/common"]

You can run any command or set of commands you want, therefore you can build a more complex flow of how and from where to fetch the common toml file and where to put it.
If needed, you can override the loadscript per platform using the linuxloadscript, windowsloadscript and macload_script attributes.

Ignoring Errors

In some cases you want to run optional tasks as part of a bigger flow, but do not want to break your entire build in case of any error in those optional tasks.
For those tasks, you can add the force=true attribute.

toml [tasks.unstable_task] force = true

Platform Override

In case you want to override a task or specific attributes in a task for specific platforms, you can define an override task with the platform name (currently linux, windows and mac) under the specific task.
For example:

```toml [tasks.hello-world] script = [ "echo \"Hello World From Unknown\"" ]

[tasks.hello-world.linux] script = [ "echo \"Hello World From Linux\"" ] ```

If you run cargo make with task 'hello-world' on linux, it would redirect to hello-world.linux while on other platforms it will execute the original hello-world.
In linux the output would be:

console [cargo-make] info - Task: hello-world [cargo-make] info - Setting Up Env. [cargo-make] info - Running Task: hello-world [cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/kOUJfw8Vfc.sh" Hello World From Linux [cargo-make] info - Build done in 0 seconds.

While on other platforms

console [cargo-make] info - Task: hello-world [cargo-make] info - Setting Up Env. [cargo-make] info - Running Task: hello-world [cargo-make] info - Execute Command: "sh" "/tmp/cargo-make/2gYnulOJLP.sh" Hello World From Unknown [cargo-make] info - Build done in 0 seconds.

In the override task you can define any attribute that will override the attribute of the parent task, while undefined attributes will use the value from the parent task and will not be modified.
In case you need to delete attributes from the parent (for example you have a command defined in the parent task but you want to have a script defined in the override task), then you will have to clear the parent task in the override task using the clear attribute as follows:

toml [tasks.hello-world.linux] clear = true script = [ "echo \"Hello World From Linux\"" ]

This means, however, that you will have to redefine all attributes in the override task that you want to carry with you from the parent task.
Important - alias comes before checking override task so if parent task has an alias it will be redirected to that task instead of the override.
To have an alias redirect per platform, use the linuxalias, windowsalias, mac_alias attributes.
In addition, aliases can not be defined in platform override tasks, only in parent tasks.

Private Tasks

Private tasks are tasks that should only be invoked by other tasks and not directly from the cli.

In order to define a task as private, add the private attribute with value true as follows:

toml [tasks.internal-task] private = true

Environment Variables

cargo-make enables you to defined environment variables in several ways.

Global Configuration

You can define env vars to be set as part of the execution of the flow in the global env block for your makefile, for example:

toml [env] RUST_BACKTRACE = "1" EVALUATED_VAR = { script = ["echo SOME VALUE"] } TEST1 = "value1" TEST2 = "value2" COMPOSITE = "${TEST1} ${TEST2}"

Environment variables can be defined as a simple key/value pair or key and the output (second line) of the provided script. In addition, you can define environment variables values based on other environment variables using the ${} syntax.

All environment variables defined in the env block and in the default Makefile.toml will be set before running the tasks.

Task

Environment variables can be defined inside tasks using the env attribute, so when a task is invoked (after its dependencies), the environment variables will be set, for example:

```toml [tasks.test-flow] env = { "SOMEENVVAR" = "value" } run_task = "actual-task"

[tasks.actual-task] condition = { envset = [ "SOMEENVVAR" ] } script = [ "echo var: ${SOMEENV_VAR}" ] ```

In task level, environment variables can also be defined as key/value pair or key/script as in the global env block.

Command Line

Environment variables can be defined in the command line using the --env/-e argument as follows:

console cargo make --env ENV1=VALUE1 --env ENV2=VALUE2 -e ENV3=VALUE3

Env File

It is also possible to provide an env file path as part of the cli args as follows:

console cargo make --env-file=./env/production.env

This allows to use the same Makefile.toml but with different environment variables loaded from different env files.

The env file, is a simple key=value file.
In addition, you can define environment variables values based on other environment variables using the ${} syntax.
For example:

```properties

just a comment...

ENV1TEST=TEST1 ENV2TEST=TEST2 ENV3TEST=VALUE OF ENV2 IS: ${ENV2TEST} ```

Global

In addition to manually setting environment variables, cargo-make will also automatically add few environment variables on its own which can be helpful when running task scripts, commands, conditions, etc:

The following environment variables will be set by cargo-make if Cargo.toml file exists and the relevant value is defined:

The following environment variables will be set by cargo-make if the project is part of a git repo:

Conditions

Conditions allow you to evaluate at runtime if to run a specific task or not.
These conditions are evaluated before the task is running its installation and/or commands and if the condition is not fulfilled, the task will not be invoked.
The task dependencies however are not affected by parent task condition outcome.

There are two types of conditions:

The task runner will evaluate any condition defined and a task definition may contain both types at the same time.

Criteria

The condition attribute may define multiple parameters to validate.
All defined parameters must be valid for the condition as a whole to be true and enable the task to run.

Below is an example of a condition script that checks that we are running on windows or linux (but not mac) and that we are running on beta or nightly (but not stable):

toml [tasks.test-condition] condition = { platforms = ["windows", "linux"], channels = ["beta", "nightly"] } script = [ "echo \"condition was met\"" ]

The following condition types are available:

Few examples:

toml [tasks.test-condition] condition = { platforms = ["windows", "linux"], channels = ["beta", "nightly"], env_set = [ "KCOV_VERSION" ], env_not_set = [ "CARGO_MAKE_SKIP_CODECOV" ], env = { "TRAVIS" = "true", "CARGO_MAKE_RUN_CODECOV" = "true" }, rust_version = { min = "1.20.0", max = "1.30.0" } }

Scripts

These script are invoked before the task is running its installation and/or commands and if the exit code of the condition script is non zero, the task will not be invoked.

Below is an example of a condition script that always returns a non zero value, in which case the command is never executed:

toml [tasks.never] condition_script = [ "exit 1" ] command = "cargo" args = ["build"]

Condition scripts can be used to ensure that the task is only invoked if a specific condition is met, for example if a specific 3rd party is installed.

Combining Conditions and Sub Tasks

Conditions and runtask combined can enable you to define a conditional sub flow.
For example, if you have a coverage flow that should only be invoked on linux in a travis build, and only if the CARGO
MAKERUNCODECOV environment variable is defined as "true":

```toml [tasks.ci-coverage-flow] description = "Runs the coverage flow and uploads the results to codecov." condition = { platforms = ["linux"], env = { "TRAVIS" = "true", "CARGOMAKERUNCODECOV" = "true" } } runtask = "codecov-flow"

[tasks.codecov-flow] description = "Runs the full coverage flow and uploads the results to codecov." windows_alias = "empty" dependencies = [ "coverage-flow", "codecov" ] ```

The first task ci-coverage-flow defines the condition that checks we are on linux, running as part of a travis build and the CARGOMAKERUN_CODECOV environment variable is set to "true".
Only if all conditions are met, it will run the codecov-flow task.
We can't define the condition directly on the codecov-flow task, as it will invoke the task dependencies before checking the condition.

Installing Dependencies

Some tasks will require third party crates, rustup components or other native tools.
cargo-make provides multiple ways to setup those dependencies before running the task.

Cargo Plugins

When a task invokes a cargo plugin using the command attribute, for example:

toml [tasks.audit] command = "cargo" args = ["audit"]

cargo-make will first check the command is available.
Only if the command is not available, it will attempt to install it by running cargo install cargo-<first arg>
In case the cargo plugin has a different name, you can specify it manually via installcrate attribute.
You can specify additional installation arguments using the install
crate_args attribute (for example: version).

Crates

cargo-make can verify third party crates are installed if the relevant installation info is provided.
First it will check the crate is installed, and only if not available it will attempt to install it.
Installation of third party crates is first done via rustup if the component name is provided.
If rustup failed or component name is not provided, it will resort to using cargo install command.
For example:

toml [tasks.rustfmt] install_crate = { crate_name = "rustfmt-nightly", rustup_component_name = "rustfmt-preview", binary = "rustfmt", test_arg = "--help" } command = "rustfmt"

In this example, cargo will first test that the command rustfmt --help works well and only if fails, it will first attempt to install via rustup the component rustfmt-preview and if failed, it will try to run cargo install for the crate name rustfmt-nightly.

Rustup Components

Rustup components that are not deployed as crates or components which are pure sources (no executable binary), can also be installed via cargo-make.
The following example show how to install a rustup component with binaries:

toml [tasks.install-rls] install_crate = { rustup_component_name = "rls-preview", binary = "rls", test_arg = "--help" }

In this example, cargo-make will first check if rls binary is available and only if failed to execute it, it will install the rls component using rustup.

Some rustup components are pure sources and therefore in those cases, cargo-make cannot verify that they are already installed, and will attempt to install them every time.
Example:

toml [tasks.install-rust-src] install_crate = { rustup_component_name = "rust-src" }

Native Dependencies

Native dependencies can also be installed, however it is up to the Makefile author to write the script which checks the dependency exists and if not, to install it correctly.
This is done by setting up an installation script in the install_script attribute of the task.
It is possible to use platform overrides to specify different installation scripts for linux/mac/windows platforms.
For example:

```toml [tasks.coverage-kcov] windowsalias = "empty" installscript = [ ''' command -v kcov >/dev/null 2>&1 || { if [ "$(grep -Ei 'debian|buntu|mint' /etc/*release)" ]; then sudo apt-get update || true sudo apt-get install -y libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc binutils-dev

    wget https://github.com/SimonKagstrom/kcov/archive/v$KCOV_VERSION.zip
    unzip v$KCOV_VERSION.zip
    cd kcov-$KCOV_VERSION
    mkdir build
    cd ./build
    cmake ..
    make
    sudo make install
    cd ../..
    rm -rf kcov-$KCOV_VERSION
fi

} ''' ] ```

This task, checks if kcov is installed and if not, will install it and any other dependency it requires.

Installation Priorities

Only one type of installation will be invoked per task.
The following defines the installation types sorted by priority for which cargo-make uses to decide which installation flow to invoke:

In case multiple installation types are defined (for example both installcrate and installscript) only one installation type will be invoked based on the above priority list.

Multiple Installations

In some cases, tasks require multiple items installed in order to run properly.
For example, you might need rustup component rls and rust-src and cargo plugin cargo-xbuild at the same task.
In order to achieve this, you can split the task to invocation task and installation task and set the installation task as a dependency.
The following example defines a flow of two similar tasks that have the same dependencies: cargo-xbuild crate, rls rustup binary component and rust-src rustup sources only component.
You can have both rustup dependencies as an installation only tasks which are set as dependencies for the xbuild tasks.
Since dependencies are only invoked once, it will also ensure that those rustup components are not installed twice.

```toml [tasks.install-rls]

install rls-preview only if needed

installcrate = { rustupcomponentname = "rls-preview", binary = "rls", testarg = "--help" }

[tasks.install-rust-src]

always install rust-src via rustup component add

installcrate = { rustupcomponent_name = "rust-src" }

[tasks.xbuild1]

run cargo xbuild, if xbuild is not installed, it will be automatically installed for you

command = "cargo" args = [ "xbuild", "some arg" ] dependencies = [ "install-rls", "install-rust-src" ]

[tasks.xbuild2]

run cargo xbuild, if xbuild is not installed, it will be automatically installed for you

command = "cargo" args = [ "xbuild", "another arg" ] dependencies = [ "install-rls", "install-rust-src" ]

[tasks.myflow] dependencies = [ "xbuild1", "xbuild2" ] ```

Toolchain

cargo-make supports setting the toolchain to be used when invoking commands and installing rust dependencies by setting the toolchain attribute as part of the task definition.
The following example shows how to print both stable and nightly rustc versions currently installed:

```toml [tasks.rustc-version-stable] toolchain = "stable" command = "rustc" args = [ "--version" ]

[tasks.rustc-version-nightly] toolchain = "nightly" command = "rustc" args = [ "--version" ]

[tasks.rustc-version-flow] dependencies = [ "rustc-version-stable", "rustc-version-nightly" ] ```

An example output of the above rustc-version-flow is:

console [cargo-make] INFO - Task: rustc-version-flow [cargo-make] INFO - Setting Up Env. [cargo-make] INFO - Running Task: init [cargo-make] INFO - Running Task: rustc-version-stable [cargo-make] INFO - Execute Command: "rustup" "run" "stable" "rustc" "--version" rustc 1.30.1 (1433507eb 2018-11-07) [cargo-make] INFO - Running Task: rustc-version-nightly [cargo-make] INFO - Execute Command: "rustup" "run" "nightly" "rustc" "--version" rustc 1.32.0-nightly (451987d86 2018-11-01) [cargo-make] INFO - Running Task: rustc-version-flow [cargo-make] INFO - Running Task: end [cargo-make] INFO - Build Done in 2 seconds.

Continuous Integration

cargo-make comes with a predefined flow for continuous integration build executed by internal or online services such as travis-ci and appveyor.
It is recommended to install cargo-make with the debug flag for faster installation.

Travis

Add the following to .travis.yml file:

yaml script: - cargo install --debug cargo-make - cargo make ci-flow

If you want to run code coverage and upload it to codecov, also define the following environment variable:

yaml env: global: - CARGO_MAKE_RUN_CODECOV="true"

You can see full yaml file at: .travis.yml

When working with workspaces, in order to run the ci-flow for each member and package all coverage data, use the following command:

yaml script: - cargo install --debug cargo-make - cargo make --no-workspace workspace-ci-flow

AppVeyor

Add the following to appveyor.yml file:

```yaml build: false

test_script: - cargo install --debug cargo-make - cargo make ci-flow ```

You can see full yaml file at: appveyor.yml

When working with workspaces, in order to run the ci-flow for each member and package all coverage data, use the following command:

```yaml build: false

test_script: - cargo install --debug cargo-make - cargo make --no-workspace workspace-ci-flow ```

GitLab CI

Add the following to your gitlab-ci.yml file:

yaml test:cargo: script: - cargo install --debug cargo-make - cargo make ci-flow

When working with workspaces, in order to run the ci-flow for each member and package all coverage data, use the following command:

```yaml build: false

test:cargo: script: - cargo install --debug cargo-make - cargo make --no-workspace workspace-ci-flow ```

To upload your coverage information to codecov, you'll need to go to repo settings for your GitLab repo, and add a secret variable with your codecov token for that repository.

Then you can add the following in your gitlab-ci.yml to enable coverage support:

yaml variables: CARGO_MAKE_RUN_CODECOV: "true"

CircleCI

Add the following to your .circleci/config.yml file:

yaml - run: name: install cargo-make command: cargo install --debug cargo-make - run: name: ci flow command: cargo make ci-flow

When working with workspaces, in order to run the ci-flow for each member and package all coverage data, use the following command:

yaml - run: name: install cargo-make command: cargo install --debug cargo-make - run: name: ci flow command: cargo make --no-workspace workspace-ci-flow

Predefined Flows

The default Makefile.toml file comes with many predefined tasks and flows.
The following are some of the main flows that can be used without any need of an external Makefile.toml definition.

Coverage

cargo-make has built in support for multiple coverage tasks.
Switching between them without modifying the flows is done by changing the main coverage task alias.

Currently the main coverage task is defined as follows:

toml [tasks.coverage] alias = "coverage-kcov"

To switch to another provider simply change the alias to that specific task name, for example if we would like to use the already defined tarpaulin provider:

toml [tasks.coverage] alias = "coverage-tarpaulin"

You can run:

sh cargo make --list-all-steps | grep "coverage-"

To view all currently supported providers. Example output:

console ci-coverage-flow: No Description. coverage-tarpaulin: Runs coverage using tarpaulin rust crate (linux only) coverage-flow: Runs the full coverage flow. coverage-kcov: Installs (if missing) and runs coverage using kcov (not supported on windows)

All built in coverage providers are supported by their authors and not by cargo-make.

Based on the above explanation, to generate a coverage report for a simple project, run the following command:

sh cargo make coverage

In order to run coverage in a workspace project and package all member coverage reports in the workspace level, run the following command:

sh cargo make --no-workspace workspace-coverage

If you are using kcov, you may declare the following environment variables in your Makefile.toml to customize the coverage task:

Specify lines or regions of code to ignore:

toml [env] CARGO_MAKE_KCOV_EXCLUDE_LINE = "unreachable,kcov-ignore" # your choice of pattern(s) CARGO_MAKE_KCOV_EXCLUDE_REGION = "kcov-ignore-start:kcov-ignore-end" # your choice of markers

By default, the binaries executed to collect coverage are filtered by a regular expression. You may override the following in case it does not match the binaries generated on your system:

```toml [env]

for example: cargo make filter regex would be cargo_make-[a-z0-9]*$

CARGOMAKETESTCOVERAGEBINARYFILTER = "${CARGOMAKECRATEFS_NAME}-[a-z0-9]*$" ```

Cargo Commands and Plugins

Git Commands

Flows/Other

Full List

Full list of all predefined tasks (can be generated via cargo make --list-all-steps)

Build
CI
Cleanup
Development
Documentation
Git
Hooks
Publish
Test
Tools

Disabling Predefined Tasks/Flows

In order to prevent loading of internal core tasks and flows, simply add the following configuration property in your external Makefile.toml:

toml [config] skip_core_tasks = true

Workspace Support

In case cargo-make detects that the current working directory is a workspace crate (crate with Cargo.toml which defines a workspace and its members), it will not invoke the requested tasks in that directory.
Instead, it will generate a task definition in runtime which will go to each member directory and invoke the requested task on that member.
For example if we have the following directory structure:

console workspace ├── Cargo.toml ├── member1 │ └── Cargo.toml └── member2 └── Cargo.toml

And we ran cargo make mytask, it will go to each workspace member directory and execute: cargo make mytask at that directory, where mytask is the original task that was requested on the workspace level.
The order of the members is defined by the member attribute in the workspace Cargo.toml.

We can use this capability to run same functionality on all workspace member crates, for example if we want to format all crates, we can run in the workspace directory: cargo make format.

In case you wish to run the tasks on the workspace level and not on the members, use the --no-workspace cli flag when running cargo make, for example:

sh cargo make --no-workspace mytask

You can define a composite flow that runs both workspace level tasks and member level tasks using this flag.
This is an example of a workspace level Makefile.toml which enables to run such a flow:

```toml [tasks.composite] dependencies = ["memberflow", "workspaceflow"]

[tasks.memberflow] command = "cargo" args = ["make", "membertask"]

[tasks.workspace_flow]

run some workspace level command or flow

```

You can start this composite flow as follows:

sh cargo make --no-workspace composite

Another way to call a task on the workspace level and not for each member, is to define that task in the workspace Makefile.toml with workspace set to false as follows:

toml [tasks.ignore-members] workspace = false

Setting workspace=false for the task requested on the cargo-make command line is equivalent to calling it with the --no-workspace flag.
This flag is only checked for the task on the cargo-make command line and is completely ignored for all other tasks which are executed as part of the flow.

Skipping Specific Members

In most cases you will want to run a specific flow on all members, but in rare cases you will want to skip specific members.

By setting the CARGOMAKEWORKSPACESKIPMEMBERS environment variable to hold the member names to skip (seperated by a ';' character), you can define if you want those members not to participate in the flow.

In the below example we will skip member3 and member4 (should be defined in the workspace level Makefile.toml):

toml [env] CARGO_MAKE_WORKSPACE_SKIP_MEMBERS = "member3;member4"

However there are some cases you will want to skip specific members only if a specific condition is met.

For example, you want to build a member module only if we are running on a rust nightly compiler.

This is a simple example of a conditioned skip for member3 and memeber4 (should be defined in the workspace level Makefile.toml):

toml [tasks.workspace-task] condition = { channels = ["beta", "stable"] } env = { "CARGO_MAKE_MEMBER_TASK" = "member-task", "CARGO_MAKE_WORKSPACE_SKIP_MEMBERS" = "member3;member4" } run_task = "do-on-members"

You will have to invoke this as a composite flow:

sh cargo make workspace-task --no-workspace

Init and End tasks

Every task or flow that is executed by the cargo-make has additional 2 tasks.
An init task that gets invoked at the start of all flows and end task that is invoked at the end of all flows.
The names of the init and end tasks are defined in the config section in the toml file, the below shows the default settings:

```toml [config] inittask = "init" endtask = "end"

[tasks.init]

[tasks.end] ```

By default the init and end tasks are empty and can be modified by external toml files or you can simply change the names of the init and end tasks in the external toml files to point to different tasks.
These tasks allow common actions to be invoked no matter what flow you are running.

Important to mention that init and end tasks invocation is different than other tasks.

Therefore it is not recommended to use the init/end tasks also inside your flows.

Catching Errors

By default any error in any task that does not have force=true set to it, will cause the entire flow to fail.
However, there are scenarios in which you would like to run some sort of cleanups before the failed flow finishes.
cargo make enables you to define an on error task which will only be invoked in case the flow failed.
In order to define this special task you must add the onerrortask attribute in the the config section in your Makefile and point it to your task, for example:

```toml [config] onerrortask = "catch"

[tasks.catch] script = [ "echo \"Doing cleanups in catch\"" ] ```

Cli Options

These are the following options available while running cargo-make:

```console USAGE: cargo make [FLAGS] [OPTIONS] [--] [ARGS] OR makers [FLAGS] [OPTIONS] [--] [ARGS]

FLAGS: --disable-check-for-updates Disables the update check during startup --experimental Allows access unsupported experimental predefined tasks. -h, --help Prints help information --list-all-steps Lists all known steps --no-on-error Disable on error flow even if defined in config sections --no-workspace Disable workspace support (tasks are triggered on workspace and not on members) --print-steps Only prints the steps of the build in the order they will be invoked but without invoking them -v, --verbose Sets the log level to verbose (shorthand for --loglevel verbose) -V, --version Prints version information

OPTIONS: --cwd Will set the current working directory. The search for the makefile will be from this directory if defined. -e, --env ... Set environment variables --env-file Set environment variables from provided file -l, --loglevel The log level [default: info] [possible values: verbose, info, error] --makefile The optional toml file containing the tasks definitions [default: Makefile.toml] --output-format The print steps format [default: default] [possible values: default, short-description] -t, --task The task name to execute (can omit the flag if the task name is the last argument) [default: default]

ARGS: The task name to execute ... Task arguments which can be accessed in the task itself. ```

Global Configuration

Some of the default CLI values and cargo-make behaviour can be configured via optional global configuration file config.toml located in the cargo-make directory.

The cargo-make directory location can be defined via CARGOMAKEHOME environment variable value.
If CARGOMAKEHOME has not been defined, the cargo-make default location is:

| OS | Location | | ------- | --------------------------------- | | Linux | $XDGCONFIGHOME or $HOME/.config | | Windows | RoamingAppData | | Mac | $HOME/Library/Preferences |

If for any reason, the above paths are not valid for the given platform, it will default to $HOME/.cargo-make

The following example config.toml shows all possible options with their default values:

```toml

The default log level if not defined by the --loglevel cli argument

log_level = "info"

The default task name if no task was provided as part of the cargo-make invocation

defaulttaskname = "default"

cargo-make checks for updates during invocation.

This configuration defines the minimum amount of time which must pass before cargo-make invocations will try to check for updates.

If the minimum amount of time did not pass, cargo-make will not check for updates (same as --disable-check-for-updates)

Valid values are: always, daily, weekly, monthly

If any other value is provided, it will be treated as weekly.

updatecheckminimum_interval = "weekly"

If set to true and cwd was not provided in the command line arguments and the current cwd is not the project root (Cargo.toml not present),

cargo make will attempt to find the project root by searching the parent directories, until a directory with a Cargo.toml is found.

cargo make will set the cwd to that directory and will use any Makefile.toml found at that location.

searchprojectroot = false ```

Makefile Definition

Config Section

Task

Platform Override

Condition

More info can be found in the types section of the API documentation.

Task Naming Conventions

This section explains the logic behind the default task names.
While the default names logic can be used as a convention for any new task defined in some project Makefile.toml, it is not required.

The default Makefile.toml file comes with three types of tasks:

Single command tasks are named based on their command (in most cases), for example the task that runs cargo build is named build.

toml [tasks.build] command = "cargo" args = ["build"]

This allows to easily understand what this task does.

Tasks that are invoked before/after those tasks are named the same way as the original task but with the pre/post prefix.
For example for task build the default toml also defines pre-build and post-build tasks.

```toml [tasks.pre-build]

[tasks.post-build] ```

In the default Makefile.toml, all pre/post tasks are empty and are there as placeholders for external Makefile.toml to override so custom functionality can be defined easily before/after running a specific task.

Flows are named with the flow suffix, for example: ci-flow

```toml [tasks.ci-flow]

CI task will run cargo build and cargo test with verbose output

dependencies = [ "pre-build", "build-verbose", "post-build", "pre-test", "test-verbose", "post-test" ] ```

This prevents flow task names to conflict with single command task names and quickly allow users to understand that this task is a flow definition.

Articles

Below is a list of articles which explain most of the cargo-make features.

The articles are missing some of the new features which have been added after they were published, such as:

Badge

If you are using cargo-make in your project and want to display it in your project README or website, you can embed the "Built with cargo-make" badge.

Built with cargo-make

Here are few snapshots:

Markdown

md [![Built with cargo-make](https://sagiegurari.github.io/cargo-make/assets/badges/cargo-make.svg)](https://sagiegurari.github.io/cargo-make)

HTML

html <a href="https://sagiegurari.github.io/cargo-make"> <img src="https://sagiegurari.github.io/cargo-make/assets/badges/cargo-make.svg" alt="Built with cargo-make"> </a>

Roadmap

While already feature rich, cargo-make is still young and under development.
You can view the future development items list in the github project issues

Contributing

See contributing guide

Release History

See Changelog

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.