| 1 | module build_constraint |
| 2 | |
| 3 | // Environment represents the current build environment. |
| 4 | @[heap] |
| 5 | pub struct Environment { |
| 6 | pub mut: |
| 7 | facts map[string]bool |
| 8 | defines map[string]bool |
| 9 | } |
| 10 | |
| 11 | // new_environment creates a new Environment. |
| 12 | // `facts` is a list of predefined platforms, compilers, build options etc, for example: ['linux', 'tinyc', 'prod', 'amd64'] |
| 13 | // `defines` is a list of the user defines, for example: ['abc', 'gcboehm_opt', 'gg_record', 'show_fps'] |
| 14 | pub fn new_environment(facts []string, defines []string) &Environment { |
| 15 | mut b := &Environment{} |
| 16 | b.facts['true'] = true |
| 17 | for f in facts { |
| 18 | b.facts[f] = true |
| 19 | } |
| 20 | for d in defines { |
| 21 | b.defines[d] = true |
| 22 | } |
| 23 | return b |
| 24 | } |
| 25 | |
| 26 | // eval evaluates the given build `constraint` against the current environment. |
| 27 | // The constraint can be for example something simple like just `linux`, |
| 28 | // but it can be also a more complex logic expression like: `(windows && tinyc) || prod` |
| 29 | pub fn (b &Environment) eval(constraint string) !bool { |
| 30 | mut parser := BParser{ |
| 31 | tokens: lex(constraint)! |
| 32 | } |
| 33 | expr := parser.parse()! |
| 34 | return expr.eval(b) |
| 35 | } |
| 36 | |
| 37 | // is_fact checks whether the given `fact` is present in the environment. |
| 38 | pub fn (b &Environment) is_fact(fact string) bool { |
| 39 | return fact in b.facts |
| 40 | } |
| 41 | |
| 42 | // is_define checks whether the given `define` is present in the environment. |
| 43 | pub fn (b &Environment) is_define(define string) bool { |
| 44 | return define in b.defines |
| 45 | } |
| 46 | |