Tips: Factorize options between several analyses

This tip can only be achieved with a Committed configuration. See the Configuration files section to learn more about the different kinds of configuration.

All examples in this section can be replayed with our demo-caesar repository, used for our Introduction tutorial. Feel free to fork this repository to try it by yourself.

With the include option

The "include" option allows to inline another configuration object(s) where the "include" option is.

The "include" option requires one or several paths to other existing files. These files should only only a single configuration object:

  "include": "another_config.json"
  // or
  "include": [ "another_config_1.json", "another_config_2.json" ]

For each option in the included configuration:

  • If the option has not been declared before the "include", it will be added to the parent configuration.

  • If the option has already been declared before the special option "include":

    • If the option type is a list of values (such as "cpp-extra-args" or "files"), the value of the option in the included configuration will be concatenated to the existing one of the parent configuration.

    • Otherwise, the value of the option in the included configuration will replace the value in the main configuration.

For any option declared after the "include" option in the parent configuration, the value of the option will either be added, be concatenated or will replace the one declared in the included configuration by following the same rules as above.

The type of options can be checked in the TrustInSoft Analyzer documentation.

In the case of several included configuration, the first configuration to include is processed according to the above rules. Then the resulting configuration will be considered as the parent one for the second configuration to include, and so on...

For a better clarity and readability of your analysis configuration objects, it is recommended to write any "include"option first and then the other options.

In this case, any option that is declared several times will either replace the included configuration value or be concatenated according to the option type.

.trustinsoft/config.json
[
  {
    "include": "common.json",
    "name": "Test shift values 7 and -3",
    "cpp-extra-args": "-DFOO" // will be appended to "-I ."
  },
  {
    "include": "common.json",
    "name": "Test from program inputs",
    "main": "main_with_input", // will overwrite "main": "main"
    "val-program-name": "a.out",
    "val-args": "|People of Earth, your attention please|7"
  }
]
.trustinsoft/common.json
{
    "prefix_path": "..",
    "files": [ "caesar.c", "main.c" ],
    "cpp-extra-args": "-I .",
    "main": "main"
}

With a Build preparation script

Since the Build preparation stage allows to generate the .trustinsoft/config.json file, it can also be used to add options to any analysis configuration on the fly.

First, rename your existing .trustinsoft/config.json file into .trustinsoft/orig_config.json (or any other name, but make sure to modify the INPUT_FILE variable in the Build preparation script below).

Then, remove all the options you want to factorize for all the configuration objects.

.trustinsoft/config.json
[
  {
    "name": "Test shift values 7 and -3",
  },
  {
    "name": "Test from program inputs",
    "main": "main_with_input",
    "val-program-name": "a.out",
    "val-args": "|People of Earth, your attention please|7"
  }
]

/* Options to factorize:

   "files": [ "caesar.c", "main.c" ]
   "cpp-extra-args": "-I ."
   "prefix_path": ".."
*/

Then, create the .trustinsoft/prepare.sh script (or adapt your existing one with the following script):

.trustinsoft/prepare.sh
#!/bin/bash

set -e

readonly INPUT_FILE="orig_config.json"
readonly COMMON_OPTIONS='{
   "files": [ "caesar.c", "main.c" ],
   "cpp-extra-args": "-I .",
   "prefix_path": ".."
}'

test -e "$INPUT_FILE" || { echo "$INPUT_FILE not found"; exit 1; }

minify --type js "$INPUT_FILE" \
  | jq --argjson common "$COMMON_OPTIONS" 'map($common + .)' \
  > config.json

Finally, update the COMMON_OPTIONS variable to add the options to add to each analysis configuration of .trustinsoft/orig_config.json, commit, push the changes and that's all.

TheCOMMON_OPTIONS are concatenated in each analysis configuration before any options of your .trustinsoft/orig_config.json, hence any duplicated option will either be concatenated to the previous one (for options like"files" or"cpp-extra-args") or will replace the previous one (for options like"main"or"val-args").

See the With the include option section for the exact rules when a same option is given several times.

Last updated