blob: 34833603de92c6478feb19487b2adaf9361b1464 [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):
Austin Schuh8e17be92019-12-24 09:32:11 -080031 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"]
38 new_values["cortex-m"] = values["arm"]
39 values = new_values
40 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({
50 "@//tools:cpu_k8": values["amd64"],
51 "@//tools:cpu_roborio": values["roborio"],
52 "@//tools:cpu_armhf": values["armhf"],
53 "@//tools:cpu_cortex_m4f": values["cortex-m"],
54 "@//tools:cpu_cortex_m4f_k22": values["cortex-m"],
55 "@//tools:cpu_web": None,
56 })
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040057
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):
Austin Schuh8e17be92019-12-24 09:32:11 -080066 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({
71 "@//tools:cpu_k8": values["64"],
72 "@//tools:cpu_roborio": values["32"],
73 "@//tools:cpu_armhf": values["32"],
74 "@//tools:cpu_cortex_m4f": values["32"],
75 "@//tools:cpu_cortex_m4f_k22": values["32"],
76 "@//tools:cpu_web": None,
77 })
Brian Silvermana7ba3aa2015-10-12 00:33:03 -040078
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):
Austin Schuh8e17be92019-12-24 09:32:11 -080088 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({
93 "@//tools:compiler_gcc": values["gcc"],
94 "@//tools:compiler_clang": values["clang"],
95 "@//tools:compiler_emscripten": None,
96 })