github.com/sercand/please@v13.4.0+incompatible/src/parse/rules/config_rules.build_defs (about) 1 """Contains the built-in rules select() and config_setting(). 2 3 These are typically used for platform detection and mimic the Bazel rules of 4 similar names. They are not yet complete. 5 """ 6 7 8 def select(conditions:dict): 9 """Chooses one of a set of config_setting options. 10 11 This can be used to select parts of rules based on config_setting values, for example 12 srcs = select({'//config:linux': ['my_linux.go'], '//config:darwin': ['my_darwin.go']}) 13 to configure per-platform choices. 14 15 Args: 16 conditions (dict): Set of conditions to select from. 17 """ 18 pass # This function is implemented natively. 19 20 21 def config_setting(name:str, values:dict, visibility:list=None): 22 """Matches a configuration state and allows triggering particular attributes. 23 24 For now this only supports the 'values' aspect of Bazel's rule, we may add 25 more parts later. 26 27 This creates a build rule which always succeeds at building; its output 28 indicates whether the value was matched or not. 29 30 Args: 31 name (str): Name of the rule 32 values (dict): Values to select. If these are all matched then the rule is 33 satisfied. Currently we only support 'cpu', 'compiler' and 'compilation_mode', 34 although users are encouraged to avoid the latter where possible 35 (the dbg/opt cflags settings should be preferred instead). 36 For example: {'cpu': 'amd64'} is successful if the CPU architecture is amd64. 37 visibility (list): Visibility of the rule. 38 """ 39 items = sorted(values.items()) 40 on = any([_config_on(k, v) for k, v in sorted(values.items())]) 41 return build_rule( 42 name = name, 43 outs = [name], 44 cmd = 'echo true > $OUT' if on else 'echo false > $OUT', 45 visibility = visibility, 46 labels = ['config:on' if on else 'config:off'], 47 ) 48 49 50 def _config_on(name, value): 51 """Returns a single config condition.""" 52 if name == 'cpu': 53 return _CPU_ALIASES.get(value, value) == CONFIG.ARCH 54 elif name == 'compilation_mode': 55 return False # We don't allow this to be determined at parse time (tbh I thought Blaze didn't either...) 56 elif name == 'compiler': 57 # Unsure what exactly the semantics of this are supposed to be. Taking a guess. 58 return CONFIG.CC_TOOL in _COMPILER_ALIASES.get(value, value) 59 elif name == 'define': 60 # This is the closest thing we have to Bazel's defines right now. 61 return value.upper() in CONFIG 62 else: 63 raise ParseError('Unknown config_setting key %s' % name) 64 65 66 # We name CPUs as Go does, but support some additional aliases to match Bazel. 67 _CPU_ALIASES = { 68 'x86_64': 'amd64', 69 'k8': 'amd64', 70 'piii': 'x86', 71 } 72 73 # Similarly 'llvm' is an acceptable alias for Clang. 74 _COMPILER_ALIASES = { 75 'llvm': 'clang', 76 }