Add config file so b4 uses inbox.sourceware.org automatically
[official-gcc.git] / gcc / config / riscv / multilib-generator
blob25cb6762ea732f56714b32315fd6fa9278d03573
1 #!/usr/bin/env python3
3 # RISC-V multilib list generator.
4 # Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 # Contributed by Andrew Waterman (andrew@sifive.com).
6
7 # This file is part of GCC.
8
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)
12 # any later version.
13
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.
18
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>
25 # Example 1:
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
30 # Example 2:
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,
34 #                             rv32imafdcb-ilp32d
36 from __future__ import print_function
37 import sys
38 import os
39 import collections
40 import itertools
41 from functools import reduce
42 import subprocess
43 import argparse
46 # TODO: Add test for this script.
49 SUPPORTED_ISA_SPEC = ["2.2", "20190608", "20191213"]
50 arches = collections.OrderedDict()
51 abis = collections.OrderedDict()
52 required = []
53 reuse = []
55 def arch_canonicalize(arch, isa_spec):
56   this_file = os.path.abspath(os.path.join( __file__))
57   arch_can_script = \
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")]
69 #      "a"   -> [("a",)]
71 def _expand_combination(ext):
72   exts = list(ext.split("*"))
74   # Add underline to every extension.
75   # e.g.
76   #  _b * zvamo => _b * _zvamo
77   exts = list(map(lambda x: '_' + x, exts))
79   # No need to expand if there is no `*`.
80   if len(exts) == 1:
81     return [(exts[0],)]
83   # Generate combination!
84   ext_combs = []
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)
89   return ext_combs
92 # Input a list and drop duplicated entry.
93 # e.g.
94 #   ["a", "b", "ab", "a"] -> ["a", "b", "ab"]
96 def unique(x):
97   #
98   # Drop duplicated entry.
99   # Convert list to set and then convert back to list.
100   #
101   # Add sorted to prevent non-deterministic results in different env.
102   #
103   return list(sorted(list(set(x))))
106 # Expand EXT string if there is any expansion operator (*).
107 # e.g.
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.
114   # e.g.
115   #   a * b => [[("a",), ("b",)], [("a", "b")]]
116   ext_combs = list(map(_expand_combination, ext))
118   # Then fold to single list.
119   # e.g.
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.
124   # e.g.
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.
129   ext = unique(ext)
131   return ext
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,
139                     default='20191213',
140                     choices=SUPPORTED_ISA_SPEC)
141 parser.add_argument("cfgs", type=str, nargs='*')
142 args = parser.parse_args()
144 if args.cmodel:
145   cmodels = [None] + args.cmodel.split(",")
146 else:
147   cmodels = [None]
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:
154     try:
155       (arch, abi, extra, ext) = cfg.split('-')
156     except:
157       print ("Invalid configure string %s, <arch>-<abi>-<extra>-<extensions>\n"
158              "<extra> and <extensions> can be empty, "
159              "e.g. rv32imafd-ilp32--" % cfg)
160       sys.exit(1)
162     # Compact code model only support rv64.
163     if cmodel == "compact" and arch.startswith("rv32"):
164       continue
166     arch = arch_canonicalize (arch, args.misa_spec)
167     arches[arch] = 1
168     abis[abi] = 1
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.
176     alts = unique(alts)
178     for alt in alts:
179       if alt == arch:
180         continue
181       arches[alt] = 1
182       reuse.append('march.%s/mabi.%s=march.%s/mabi.%s' % (arch, abi, alt, abi))
184     if cmodel:
185       required.append('march=%s/mabi=%s/mcmodel=%s' % (arch, abi, cmodel))
186     else:
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))