blob: 45ba612ba20bd63568c03415d6747e56c19afef0 [file] [log] [blame]
Brian Silvermana7ba3aa2015-10-12 00:33:03 -04001# This file contains replacements for select where the keys have more abstract
2# meanings so we can map multiple conditions to the same value easily and
3# quickly find issues where something new isn't handled.
4# It will also make adding ORs when it makes sense easy to do nicely.
5
James Kuszmaul27da8142019-07-21 16:13:55 -07006# TODO(james): Decide what to do about webassembly/emscripten CPU and
7# compiler configurations. Bazel does not seem to handle the fact that a select
8# statement may not logically need to be evaluated for certain configurations
9# (e.g., most targets can't be build for --cpu=web, so handling "web" in the
10# cpu_select should notionally be unnecessary).
Brian Silverman7a7c24d2018-09-01 17:49:09 -070011all_cpus = [
12 "amd64",
13 "roborio",
14 "armhf",
Austin Schuhde821712019-08-03 18:16:49 -070015 "cortex-m",
Brian Silverman7a7c24d2018-09-01 17:49:09 -070016]
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040017
Brian Silverman7a7c24d2018-09-01 17:49:09 -070018"""All of the CPUs we know about."""
19
20"""A select wrapper for CPU architectures.
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040021
22Args:
23 values: A mapping from architecture names (as strings) to other things.
Austin Schuhde821712019-08-03 18:16:49 -070024 Currently amd64, roborio, armhf, and cortex are recognized.
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040025 'else' is also allowed as a default.
Austin Schuhde821712019-08-03 18:16:49 -070026 'arm' is allowed instead of roborio, armhf, and cortex.
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040027Returns a select which evaluates to the correct element of values.
Brian Silverman7a7c24d2018-09-01 17:49:09 -070028"""
29
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040030def cpu_select(values):
Brian Silverman0d57fc82016-01-24 21:02:53 -050031 if 'arm' in values:
32 new_values = {}
33 for cpu in values:
34 if cpu != 'arm':
35 new_values[cpu] = values[cpu]
36 new_values['armhf'] = values['arm']
37 new_values['roborio'] = values['arm']
Austin Schuhde821712019-08-03 18:16:49 -070038 new_values['cortex-m'] = values['arm']
Brian Silverman0d57fc82016-01-24 21:02:53 -050039 values = new_values
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040040 for cpu in all_cpus:
41 if cpu not in values:
42 if 'else' in values:
43 values[cpu] = values['else']
44 else:
45 fail('Need to handle %s CPUs!' % cpu, 'values')
46 for key in values:
47 if key not in all_cpus and key != 'else':
48 fail('Not sure what a %s CPU is!' % key, 'values')
49 return select({
Austin Schuhf6b94632019-02-02 22:11:27 -080050 '@//tools:cpu_k8': values['amd64'],
51 '@//tools:cpu_roborio': values['roborio'],
52 '@//tools:cpu_armhf': values['armhf'],
Austin Schuhde821712019-08-03 18:16:49 -070053 '@//tools:cpu_cortex_m4f': values['cortex-m'],
54 '@//tools:cpu_cortex_m4f_k22': values['cortex-m'],
James Kuszmaul27da8142019-07-21 16:13:55 -070055 '@//tools:cpu_web': None,
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040056 })
57
Brian Silverman7a7c24d2018-09-01 17:49:09 -070058"""A select wrapper for address space sizes.
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040059
60Args:
61 values: A mapping from address space sizes (as strings) to other things.
62Returns a select which evaluates to the correct element of values.
Brian Silverman7a7c24d2018-09-01 17:49:09 -070063"""
64
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040065def address_size_select(values):
66 if '32' not in values:
67 fail('Need to handle 32 bit addresses!', 'values')
68 if '64' not in values:
69 fail('Need to handle 64 bit addresses!', 'values')
70 return select({
Austin Schuhf6b94632019-02-02 22:11:27 -080071 '@//tools:cpu_k8': values['64'],
72 '@//tools:cpu_roborio': values['32'],
73 '@//tools:cpu_armhf': values['32'],
Austin Schuhde821712019-08-03 18:16:49 -070074 '@//tools:cpu_cortex_m4f': values['32'],
75 '@//tools:cpu_cortex_m4f_k22': values['32'],
James Kuszmaul27da8142019-07-21 16:13:55 -070076 '@//tools:cpu_web': None,
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040077 })
78
Brian Silverman7a7c24d2018-09-01 17:49:09 -070079"""A select wrapper for compilers.
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040080
81Args:
82 values: A mapping from compiler names (as strings) to other things.
83 Currently 'gcc' and 'clang' are recognized.
84Returns a select which evaluates to the correct element of values.
Brian Silverman7a7c24d2018-09-01 17:49:09 -070085"""
86
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040087def compiler_select(values):
88 if 'gcc' not in values:
89 fail('Need to handle gcc!', 'values')
90 if 'clang' not in values:
91 fail('Need to handle clang!', 'values')
92 return select({
Brian Silvermand3ad1652018-02-18 22:16:29 -050093 '@//tools:compiler_gcc': values['gcc'],
94 '@//tools:compiler_clang': values['clang'],
James Kuszmaul27da8142019-07-21 16:13:55 -070095 '@//tools:compiler_emscripten': None,
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040096 })