Working with Variables

Often T2 Linux code needs to set or modify environment variables - either for the build system the section called “Environment Variables”, the command wrapper the section called “Command Wrapper” or hooks the section called “Build System Hooks”.

Since many environment variables contain lists, it is important to take care of delimiters (like colons or semicolons) and the order of list items. Also there must not be any stray white-spaces or delimiters be left in the content after applying a modification.

Functions for Variable Modifications

To make working with variables more convenient in bash some functions fulfilling the requirements above got introduced for the developer:

  • var_append name delimiter content

    The 'content' is appended to the variable referred to with 'name'. If the variable was not empty the 'delimiter' is inserted between the already existing and new content.

    var_append CC_WRAPPER_INSERT ' ' -O3
  • var_insert name delimiter content

    The 'content' is inserted to the beginning of the content of the variable referred to with 'name'. If the variable was non empty the 'delimiter' is inserted between the new and the already existing content.

    var_insert PATH ':' "$SOMEPATH/bin"
  • var_remove name delimiter content

    If the 'content' is present in the variable referred to with 'name' is removed including the associated 'delimiter' - if present.

    var_remove CC_WRAPPER_INSERT ' ' -O2
  • var_remove_regex name delimiter regex

    The regular expression 'regex' is used to remove the matching part including its associated delimiter from the variable referred to with 'name'.

    Example: A ./configure script does not like the option --prefix= and thus it needs to be removed from the confopt variable, var_remove_regex can be used to remove the option:

    var_remove_regex confopt ' ' '--prefix=.*'
  • var_insert_before_regex name delimiter regex

    If the regular expression 'regex' matches a part of the variable referred to with 'name' the 'content' including its delimiter if needed is added before the match.

    var_insert_before_regex patchfiles ' ' 'mypatch.diff' '.*\/foo.d'