Qbs Manual | Command and JavaScriptCommand

Qbs 1.9.1

# Command and JavaScriptCommand

A command is what Qbs executes at build time. It is represented in the language by an object of type `Command`, which runs a process, or `JavaScriptCommand`, which executes arbitrary JavaScript code. A command is always created in the prepare script of a `Rule`.

## Command

A `Command` represents a process that will be invoked at build time. Its constructor arguments are the binary to run and a list of command-line arguments. For instance:

      var insaneCommand = new Command("rm", ["-r", "/"]);

The Rule Item documentation shows a `Command` in context.

## JavaScriptCommand

A `JavaScriptCommand` represents a chunk of JavaScript code that is run at build time. For instance:

      var cmd = new JavaScriptCommand();
      cmd.apology = "Sorry.";
      cmd.sourceCode = function() {
          console.info("I'm a rather pointless command.");
          console.info(apology);
      };

Within the source code, the special identifiers `project` and `product` (giving access to project and product properties, respectively) as well as `inputs` and `outputs` are available. As the example shows, arbitrary properties can be set on the command object and then used within the source code. This technique is typically used to forward values from the prepare script to the command. The Rule Item documentation shows a `JavaScriptCommand` in context.

## Properties

### Common Properties

The following properties are available in both `Command` and `JavaScriptCommand`.

Property | Type | Default | Description
--- | --- | --- | ---
`description` | string | empty | A message that is displayed when the command is executed.
`extendedDescription` | string | empty | A detailed description that is displayed when the command is executed.
`highlight` | string | empty | A tag that can be used to influence how the `description` is displayed. In principle, the values are arbitrary. The Qbs command-line tool understands the following values and maps them to different colors if the output device is a terminal: "compiler" indicates that the command processes source code "linker" indicates that the command links objects "codegen" indicates that the command generates source code "filegen" indicates that the command creates arbitrary files All other values are mapped to the default color.
`silent` | bool | false | A flag that controls whether the `description` is printed. Set it to `true` for commands that users need not know about. Note: If this property is `false`, then `description` must not be empty.

### Command Properties

Property | Type | Default | Description
--- | --- | --- | ---
`arguments` | stringList | empty | The list of arguments to invoke the command with. Explicitly setting this property overrides an argument list provided when instantiating the object.
`environment` | stringList | empty | A list of environment variables that are added to the common build environment. They are provided as a list of strings in the form "varName=value".
`maxExitCode` | int | 0 | The maximum exit code from the process to interpret as success. Setting this should rarely be necessary, as all well-behaved applications use values other than zero to indicate failure.
`program` | string | undefined | The binary to invoke. Explicitly setting this property overrides a path provided when instantiating the object.
`relevantEnvironmentVariables` | stringList | undefined | Names of environment variables that the invoked binary considers. If one of these variables changes in the build environment, the command will be re-run even if the input files are still up to date.
`responseFileThreshold` | int | 32000 on Windows, -1 elsewhere | If this value is greater than zero and less than the length of the full command line, and if `responseFileUsagePrefix` is not empty, the contents of the command line are moved to a temporary file, whose path becomes the entire contents of the argument list. The program is then supposed to read the full argument list from that file. This mechanism is mainly useful to work around Windows limitations regarding the maximum length of the command line and will only work with programs that explicitly support it.
`responseFileArgumentIndex` | int | 0 | Index of the first argument to include in the response file. For example this may be used in conjunction with a compiler wrapper where the first argument (the path to the compiler) must be included on the raw command line.
`responseFileUsagePrefix` | string | empty | The prefix that informs `program` that the rest of the argument is a path to a file containing the actual command line.
`stderrFilterFunction` | function | undefined | A function that takes as input the command's actual standard error output and returns a string that is presented to the user as the command's standard error output. If it is not set, the output is shown unfiltered.
`stdoutFilterFunction` | function | undefined | A function that takes as input the command's actual standard output and returns a string that is presented to the user as the command's standard output. If it is not set, the output is shown unfiltered.
`workingDirectory` | string | empty | The program's working directory.
`stdoutFilePath` | string | undefined | Redirects the filtered standard output content to `stdoutFilePath`. If `stdoutFilePath` is undefined, the filtered standard output is forwarded to Qbs, possibly to be printed to the console.
`stderrFilePath` | string | undefined | Redirects the filtered standard error output content to `stderrFilePath`. If `stderrFilePath` is undefined, the filtered standard error output is forwarded to Qbs, possibly to be printed to the console.

### JavaScriptCommand Properties

Property | Type | Default | Description
--- | --- | --- | ---
`sourceCode` | function | undefined | The JavaScript function to execute.