3 # RISC-V multilib list generator.
4 # Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 # Contributed by Andrew Waterman (andrew@sifive.com).
7 # This file is part of GCC.
9 # GCC is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
14 # GCC is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>.
23 # Each argument to this script is of the form
24 # <primary arch>-<abi>-<additional arches>-<extensions>
26 # rv32imafd-ilp32d-rv32g-c,v
27 # means that, in addition to rv32imafd, these configurations can also use the
28 # rv32imafd-ilp32d libraries: rv32imafdc, rv32imafdv, rv32g, rv32gc, rv32gv
31 # rv32imafd-ilp32d--c*b
32 # means that, in addition to rv32imafd, these configurations can also use the
33 # rv32imafd-ilp32d libraries: rv32imafdc-ilp32d, rv32imafdb-ilp32d,
36 from __future__ import print_function
41 from functools import reduce
46 # TODO: Add test for this script.
49 SUPPORTED_ISA_SPEC = ["2.2", "20190608", "20191213"]
50 arches = collections.OrderedDict()
51 abis = collections.OrderedDict()
55 def arch_canonicalize(arch, isa_spec):
56 this_file = os.path.abspath(os.path.join( __file__))
58 os.path.join(os.path.dirname(this_file), "arch-canonicalize")
59 proc = subprocess.Popen([sys.executable, arch_can_script,
60 '-misa-spec=%s' % isa_spec, arch],
61 stdout=subprocess.PIPE)
62 out, err = proc.communicate()
63 return out.decode().strip()
66 # Handle expansion operation.
68 # e.g. "a*b" -> [("a",), ("b",), ("a", "b")]
71 def _expand_combination(ext):
72 exts = list(ext.split("*"))
74 # Add underline to every extension.
76 # _b * zvamo => _b * _zvamo
77 exts = list(map(lambda x: '_' + x, exts))
79 # No need to expand if there is no `*`.
83 # Generate combination!
85 for comb_len in range(1, len(exts)+1):
86 for ext_comb in itertools.combinations(exts, comb_len):
87 ext_combs.append(ext_comb)
92 # Input a list and drop duplicated entry.
94 # ["a", "b", "ab", "a"] -> ["a", "b", "ab"]
98 # Drop duplicated entry.
99 # Convert list to set and then convert back to list.
101 # Add sorted to prevent non-deterministic results in different env.
103 return list(sorted(list(set(x))))
106 # Expand EXT string if there is any expansion operator (*).
108 # "a*b,c" -> ["a", "b", "ab", "c"]
110 def expand_combination(ext):
111 ext = list(filter(None, ext.split(',')))
113 # Expand combination for EXT, got lots of list.
115 # a * b => [[("a",), ("b",)], [("a", "b")]]
116 ext_combs = list(map(_expand_combination, ext))
118 # Then fold to single list.
120 # [[("a",), ("b",)], [("a", "b")]] => [("a",), ("b",), ("a", "b")]
121 ext = list(reduce(lambda x, y: x + y, ext_combs, []))
123 # Fold the tuple to string.
125 # [("a",), ("b",), ("a", "b")] => ["a", "b", "ab"]
126 ext = map(lambda e : reduce(lambda x, y: x + y, e), ext)
128 # Drop duplicated entry.
133 multilib_cfgs = filter(lambda x:not x.startswith("--"), sys.argv[1:])
134 options = filter(lambda x:x.startswith("--"), sys.argv[1:])
136 parser = argparse.ArgumentParser()
137 parser.add_argument("--cmodel", type=str)
138 parser.add_argument('-misa-spec', type=str,
140 choices=SUPPORTED_ISA_SPEC)
141 parser.add_argument("cfgs", type=str, nargs='*')
142 args = parser.parse_args()
145 cmodels = [None] + args.cmodel.split(",")
149 cmodel_options = '/'.join(['mcmodel=%s' % x for x in cmodels[1:]])
150 cmodel_dirnames = ' \\\n'.join(cmodels[1:])
152 for cmodel in cmodels:
153 for cfg in args.cfgs:
155 (arch, abi, extra, ext) = cfg.split('-')
157 print ("Invalid configure string %s, <arch>-<abi>-<extra>-<extensions>\n"
158 "<extra> and <extensions> can be empty, "
159 "e.g. rv32imafd-ilp32--" % cfg)
162 # Compact code model only support rv64.
163 if cmodel == "compact" and arch.startswith("rv32"):
166 arch = arch_canonicalize (arch, args.misa_spec)
169 extra = list(filter(None, extra.split(',')))
170 ext_combs = expand_combination(ext)
171 alts = sum([[x] + [x + y for y in ext_combs] for x in [arch] + extra], [])
172 alts = filter(lambda x: len(x) != 0, alts)
173 alts = list(map(lambda a : arch_canonicalize(a, args.misa_spec), alts))
175 # Drop duplicated entry.
182 reuse.append('march.%s/mabi.%s=march.%s/mabi.%s' % (arch, abi, alt, abi))
185 required.append('march=%s/mabi=%s/mcmodel=%s' % (arch, abi, cmodel))
187 required.append('march=%s/mabi=%s' % (arch, abi))
189 arch_options = '/'.join(['march=%s' % x for x in arches.keys()])
190 arch_dirnames = ' \\\n'.join(arches.keys())
192 abi_options = '/'.join(['mabi=%s' % x for x in abis.keys()])
193 abi_dirnames = ' \\\n'.join(abis.keys())
195 prog = sys.argv[0].split('/')[-1]
196 print('# This file was generated by %s with the command:' % prog)
197 print('# %s' % ' '.join(sys.argv))
199 print('MULTILIB_OPTIONS = %s %s %s' % (arch_options, abi_options, cmodel_options))
200 print('MULTILIB_DIRNAMES = %s %s %s' % (arch_dirnames, abi_dirnames, cmodel_dirnames))
201 print('MULTILIB_REQUIRED = %s' % ' \\\n'.join(required))
202 print('MULTILIB_REUSE = %s' % ' \\\n'.join(reuse))