]> mj.ucw.cz Git - moe.git/blob - t/doc/config.rst
3216a468e8b31af6220ddd3f2898c14727646aba
[moe.git] / t / doc / config.rst
1 .. _config:
2
3 .. highlight:: none
4
5 =================
6 Moe configuration
7 =================
8
9 .. contents:: 
10
11 -------------------
12 Configuration logic
13 -------------------
14
15 Moe and the derived modules use many configuration variables indicating i.e. working directories, 
16 task name, compiler commandline and options, current test name and name of test file to use. 
17 All values are treated as text and may contain unicode characters (although this is usually not advisable).
18 A variable may be either set to a simple fixed string or to a string containing expansions of other variables.
19 The configuration library also supports appending text, conditionals and a simple variable hierarchy.
20
21 For the complete configuration syntax grammar, see :mod:`moe.config_parser` documentation.
22
23 Generally, both whitespace and indentation are optional and do not matter, individual 
24 operations must be separated by either ``;`` or newline, ``#`` introduces a comment until the end of the line.
25
26 In *moe*, the config files are assumed to be in UTF-8, but using non-ASCII characters are discouraged outside
27 comments (mainly because not all unix comands handle unicode well).
28
29 Variables and substitution
30 ^^^^^^^^^^^^^^^^^^^^^^^^^^
31
32 The names of the variables may consist of letters, numbers, underscore, dash and a dot. Dot acts as
33 a separator of the variable name hierarchy and may not occur at the beginning or the end of variable name, 
34 nor adjacent to another dot. The tree hierarchy is provided only for user's convenience and does not play
35 any significant role in variable evaluation. 
36
37 The value of a variable may be defined by several operations:
38
39 **SET**, ``VAR = "value"``
40   Sets the variable to the variable-expansion of the string.
41
42 **APPEND**, ``VAR += "value"``
43   Appends the variable-expansion of the string.
44
45 Value is a ``"``-delimited string that may contain substitutions in the form ``{VARNAME}`` i.e.
46 ``"source-{VERSION}.{SUFFIX}"``, where ``VERSION`` and ``SUFFIX`` are the substituted variables. 
47 The string may contain any characters including unicode (although this is not always advisable). 
48 Characters ``"``, ``{``, ``{`` and ``\`` have to be escaped with ``\``, i.e. ``\"``, ``\{``, ``\{`` and ``\\``.
49
50 When evaluating a variable, all the substitutions are evaluated as well. Every variable is evaluated 
51 in the same way, independently of the priority of the operations it occurs in. The substitutions must 
52 therefore be acyclic and a variable may not contain itself as a substitution.
53
54
55 Operation priority
56 ^^^^^^^^^^^^^^^^^^
57
58 Each operation has a priority usually depending on its origin, i.e. builtins, global config file, task config file 
59 or commandline overrides.
60 Only the **SET** operation with the highest priority and higher priority **APPENDs** are used (as if all operations were
61 applied from the least priority operation to the highest). 
62 Operations of the same priority are processed by their definition order (first to last).
63
64 For example, in builtins (priority 0) we may have::
65
66   COMPILE = "{CC} {OPTS} {INFILE} -o {OUTFILE}"
67   OPTS = "-O{OPTIMIZE}"
68   CC = "gcc"
69
70 In config file (priority 30)::
71
72   OPTS += " -W{WARNOPT}" # OPTS is now "-O{OPTIMIZE} -W{WARNOPT}"
73   WARNOPT = "all"
74   OPTIMIZE = "0"
75
76 And in command-line overrides (priority 100)::
77
78   COMPILE = "cp {INFILE} {OUTFILE}"
79
80 Moe would then later set ``INFILE`` and ``OUTFILE`` at some point before evaluating and executing 
81 the compilation command.
82
83 With these *lazy* semantics, it is possible to set a variable to a value depending
84 on both higher- and lower-priority operations. 
85
86 .. note:: 
87   The implementation is actually very efficient and recalculates only the necessary values, 
88   caching the results. Adding/removing operations to variables only recursively invalidates the 
89   (potentially) influenced cached values, these are recalculated lazily on-demand.
90
91
92 Variables hierarchy
93 ^^^^^^^^^^^^^^^^^^^
94
95 The variables form a tree-like hierarchy with levels separated by dots. The variables
96 may be defined either by providing the full name or by inside subtrees. 
97 Subtrees provide all the *defined* variables with given dot-separated prefix.
98 Substituted variable names must be given by their full names. ::
99
100   LOG.DIR = "./log"
101   LOG {
102     VERBOSE = "N" 
103     # Defines LOG.VERBOSE
104     TEST.LOGFILE = "{LOG.DIR}/test.log"
105     # Defines LOG.TEST.LOGFILE 
106   }
107   LOG.TEST { VERBOSE = "Y" }
108   # The subtree name may contain several level names
109
110 Conditionals
111 ^^^^^^^^^^^^
112
113 Any block of operations may be guarded by a condition. The conditions may be nested and
114 consist of expressions containing ``and``, ``or``, ``not`` and variable/string (in)equalities. The strings 
115 may contain substitutions.
116
117 The syntax is the following::
118
119   if (NAME == "{TASK}.cc") or (NAME == "{TASK}.cpp") or 
120     ((LANG_HINT == "C") and (not FOO != "BAR"))
121   {
122     LANG = "C"
123   }
124
125 The curly brackets delimiting the bloc, as well as the brackets in the boolena expression, 
126 are mandatory. ``else`` is not supported. ``if VAR=="FOO" {...}`` is equivalent to ``if "{VAR}"=="FOO" {...}``.
127
128
129 -------------------
130 Module `moe.config`
131 -------------------
132 .. highlight:: python
133 .. automodule:: moe.config
134
135 Exceptions
136 ^^^^^^^^^^
137
138 .. autoclass:: ConfigError
139 .. autoclass:: UndefinedError
140 .. autoclass:: VariableNameError
141 .. autoclass:: VariableFixedError
142 .. autoclass:: CyclicConfigError
143
144 Configuration tree
145 ^^^^^^^^^^^^^^^^^^
146
147 The configuration environment :class:`ConfigTree` is implemented as a flat dictionary of variables 
148 referenced by their full name. 
149
150 .. autoclass:: ConfigTree
151   :members:
152
153 Variables
154 ^^^^^^^^^
155
156 Every variable object :class:`ConfigVar` stores the list of its defining operations, the current
157 cached value and both the variables its current value depends on and the variables that currently depend on it.
158 All the dependencies are calculated upon evaluation and removed upon invalidation.
159 Only the operations and conditions influencing the current value contribute to dependencies.
160
161 .. note:: If no **SET** applies, a variable is still undefined even if some **APPEND** applies. This *might* change.
162
163 .. autoclass:: ConfigVar
164   :members:
165
166 Operations
167 ^^^^^^^^^^
168
169 Every operation object is either **SET** or **APPEND** and may be guarded by a 
170 condition.
171
172 .. autoclass:: Operation
173
174 Conditions
175 ^^^^^^^^^^
176
177 Every condition is defined by a formula, the result is cached in the same way as with :class:`ConfigVar`, including
178 dependency tracking. A condition may depend on another parent condition in case this condition is 
179 nested inside another. In this case the parent condition is checked first and the parent condition is a dependency.
180
181 .. autoclass:: ConfigCondition
182   :members:
183
184 Internals
185 ^^^^^^^^^
186
187 .. autoclass:: ConfigElem
188   :members:
189
190 .. autoclass:: ConfigExpression
191
192
193
194 ----------------------------------------
195 Config parser module `moe.config_parser`
196 ----------------------------------------
197
198 .. automodule:: moe.config_parser