blob: e90580d1cdbc8831ae970e63ae9d457ebe94bdcc [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# following code modifies these definitions in the gcc config
2# MULTILIB_OPTIONS
3# MULTILIB_DIRNAMES
4# MULTILIB_OSDIRNAMES
5# GLIBC_DYNAMIC_LINKER32
6# GLIBC_DYNAMIC_LINKER64
7# GLIBC_DYNAMIC_LINKERX32
8# GLIBC_DYNAMIC_LINKERN32
9# For more information on use of these variables look at these files in the gcc source code
10# gcc/config/i386/t-linux64
11# gcc/config/mips/t-linux64
12# gcc/config/rs6000/t-linux64
13# gcc/config/i386/linux64.h
14# gcc/config/mips/linux64.h
15# gcc/config/rs6000/linux64.h
16
17MULTILIB_OPTION_WHITELIST ??= "-m32 -m64 -mx32 -mabi=n32 -mabi=32 -mabi=64"
18
19python gcc_multilib_setup() {
20 import re
21 import shutil
22 import glob
23
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 srcdir = d.getVar('S')
25 builddir = d.getVar('B')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 src_conf_dir = '%s/gcc/config' % srcdir
27 build_conf_dir = '%s/gcc/config' % builddir
28
29 bb.utils.remove(build_conf_dir, True)
30 ml_globs = ('%s/*/t-linux64' % src_conf_dir,
31 '%s/*/linux64.h' % src_conf_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050032 '%s/aarch64/t-aarch64' % src_conf_dir,
33 '%s/aarch64/aarch64.h' % src_conf_dir,
34 '%s/aarch64/aarch64-cores.def' % src_conf_dir,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 '%s/*/linux.h' % src_conf_dir,
36 '%s/linux.h' % src_conf_dir)
37
38 # copy the target multilib config files to ${B}
39 for ml_glob in ml_globs:
40 for fn in glob.glob(ml_glob):
41 rel_path = os.path.relpath(fn, src_conf_dir)
42 parent_dir = os.path.dirname(rel_path)
43 bb.utils.mkdirhier('%s/%s' % (build_conf_dir, parent_dir))
44 bb.utils.copyfile(fn, '%s/%s' % (build_conf_dir, rel_path))
45
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 pn = d.getVar('PN')
47 multilibs = (d.getVar('MULTILIB_VARIANTS') or '').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 if not multilibs and pn != "nativesdk-gcc":
49 return
50
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 mlprefix = d.getVar('MLPREFIX')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
53 if ('%sgcc' % mlprefix) != pn and (not pn.startswith('gcc-cross-canadian')) and pn != "nativesdk-gcc":
54 return
55
56
57 def write_config(root, files, options, dirnames, osdirnames):
58 for ml_conf_file in files:
59 with open(root + '/' + ml_conf_file, 'r') as f:
60 filelines = f.readlines()
61 # recreate multilib configuration variables
62 substs = [
63 (r'^(\s*(MULTILIB_OPTIONS\s*=).*)$', r'\2 %s' % '/'.join(options)),
64 (r'^(\s*MULTILIB_OPTIONS\s*\+=.*)$', ''),
65 (r'^(\s*(MULTILIB_DIRNAMES\s*=).*)$', r'\2 %s' % ' '.join(dirnames)),
66 (r'^(\s*MULTILIB_DIRNAMES\s*\+=.*)$', ''),
67 (r'^(\s*(MULTILIB_OSDIRNAMES\s*=).*)$', r'\2 %s' % ' '.join(osdirnames)),
68 (r'^(\s*MULTILIB_OSDIRNAMES\s*\+=.*)$', ''),
69 ]
70
71 for (i, line) in enumerate(filelines):
72 for subst in substs:
73 line = re.sub(subst[0], subst[1], line)
74 filelines[i] = line
75
76 with open(root + '/' + ml_conf_file, 'w') as f:
77 f.write(''.join(filelines))
78
79 def write_headers(root, files, libdir32, libdir64, libdirx32, libdirn32):
80 def wrap_libdir(libdir):
81 if libdir.find('SYSTEMLIBS_DIR') != -1:
82 return '"%r"'
83 else:
84 return '"/%s/"' % libdir
85
86 for ml_conf_file in files:
87 fn = root + '/' + ml_conf_file
88 if not os.path.exists(fn):
89 continue
90 with open(fn, 'r') as f:
91 filelines = f.readlines()
92
93 # replace lines like
94 # #define GLIBC_DYNAMIC_LINKER32 SYSTEMLIBS_DIR "ld-linux.so.2"
95 # by
96 # #define GLIBC_DYNAMIC_LINKER32 "/lib/" "ld-linux.so.2"
97 # this is needed to put the correct dynamic loader path in the generated binaries
98 substs = [
99 (r'^(#define\s*GLIBC_DYNAMIC_LINKER32\s*)(\S+)(\s*\".*\")$',
100 r'\1' + wrap_libdir(libdir32) + r'\3'),
101 (r'^(#define\s*GLIBC_DYNAMIC_LINKER64\s*)(\S+)(\s*\"\S+\")$',
102 r'\1' + wrap_libdir(libdir64) + r'\3'),
103 (r'^(#define\s*GLIBC_DYNAMIC_LINKER64\s*\"\S+\"\s*)(\S+)(\s*\"\S+\"\s*)(\S+)(\s*\".*\")$',
104 r'\1' + wrap_libdir(libdir64) + r'\3' + wrap_libdir(libdir64) + r'\5'),
105 (r'^(#define\s*GLIBC_DYNAMIC_LINKERX32\s*)(\S+)(\s*\".*\")$',
106 r'\1' + wrap_libdir(libdirx32) + r'\3'),
107 (r'^(#define\s*GLIBC_DYNAMIC_LINKERN32\s*)(\S+)(\s*\".*\")$',
108 r'\1' + wrap_libdir(libdirn32) + r'\3'),
109 (r'^(#define\s*UCLIBC_DYNAMIC_LINKER32\s*)(\S+)(\s*\".*\")$',
110 r'\1' + wrap_libdir(libdir32) + r'\3'),
111 (r'^(#define\s*UCLIBC_DYNAMIC_LINKER64\s*)(\S+)(\s*\".*\")$',
112 r'\1' + wrap_libdir(libdir64) + r'\3'),
113 (r'^(#define\s*UCLIBC_DYNAMIC_LINKERN32\s*)(\S+)(\s*\".*\")$',
114 r'\1' + wrap_libdir(libdirn32) + r'\3'),
115 (r'^(#define\s*UCLIBC_DYNAMIC_LINKER\b\s*)(\S+)(\s*\".*\")$',
116 r'\1' + wrap_libdir(libdir32) + r'\3'),
117 ]
118
119 for (i, line) in enumerate(filelines):
120 for subst in substs:
121 line = re.sub(subst[0], subst[1], line)
122 filelines[i] = line
123
124 with open(root + '/' + ml_conf_file, 'w') as f:
125 f.write(''.join(filelines))
126
127
128 gcc_target_config_files = {
129 'x86_64' : ['gcc/config/i386/t-linux64'],
130 'i586' : ['gcc/config/i386/t-linux64'],
131 'i686' : ['gcc/config/i386/t-linux64'],
132 'mips' : ['gcc/config/mips/t-linux64'],
133 'mips64' : ['gcc/config/mips/t-linux64'],
134 'powerpc' : ['gcc/config/rs6000/t-linux64'],
135 'powerpc64' : ['gcc/config/rs6000/t-linux64'],
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500136 'aarch64' : ['gcc/config/aarch64/t-aarch64'],
137 'arm' : ['gcc/config/aarch64/t-aarch64'],
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 }
139
140 gcc_header_config_files = {
141 'x86_64' : ['gcc/config/i386/linux64.h'],
142 'i586' : ['gcc/config/i386/linux64.h'],
143 'i686' : ['gcc/config/i386/linux64.h'],
144 'mips' : ['gcc/config/mips/linux.h', 'gcc/config/mips/linux64.h'],
145 'mips64' : ['gcc/config/mips/linux.h', 'gcc/config/mips/linux64.h'],
146 'powerpc' : ['gcc/config/rs6000/linux64.h'],
147 'powerpc64' : ['gcc/config/rs6000/linux64.h'],
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 'aarch64' : ['gcc/config/aarch64/aarch64.h'],
149 'arm' : ['gcc/config/aarch64/aarch64.h'],
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 }
151
152 libdir32 = 'SYSTEMLIBS_DIR'
153 libdir64 = 'SYSTEMLIBS_DIR'
154 libdirx32 = 'SYSTEMLIBS_DIR'
155 libdirn32 = 'SYSTEMLIBS_DIR'
156
157
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 target_arch = (d.getVar('TARGET_ARCH_MULTILIB_ORIGINAL') if mlprefix
159 else d.getVar('TARGET_ARCH'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 if pn == "nativesdk-gcc":
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 header_config_files = gcc_header_config_files[d.getVar("SDK_ARCH")]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 write_headers(builddir, header_config_files, libdir32, libdir64, libdirx32, libdirn32)
163 return
164
165 if target_arch not in gcc_target_config_files:
166 bb.warn('gcc multilib setup is not supported for TARGET_ARCH=' + target_arch)
167 return
168
169 target_config_files = gcc_target_config_files[target_arch]
170 header_config_files = gcc_header_config_files[target_arch]
171
172 ml_list = ['DEFAULTTUNE_MULTILIB_ORIGINAL' if mlprefix else 'DEFAULTTUNE']
Patrick Williams213cb262021-08-07 19:21:33 -0500173 mltunes = [('DEFAULTTUNE:virtclass-multilib-%s' % ml) for ml in multilibs]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 if mlprefix:
175 mlindex = 0
176 for ml in multilibs:
177 if mlprefix == ml + '-':
178 break
179 mlindex += 1
180
181 ml_list.extend(mltunes[:mlindex] + ['DEFAULTTUNE'] + mltunes[(mlindex + 1):])
182 else:
183 ml_list.extend(mltunes)
184
185 options = []
186 dirnames = []
187 osdirnames = []
188 optsets = []
189
190 for ml in ml_list:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191 tune = d.getVar(ml)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 if not tune:
193 bb.warn("%s doesn't have a corresponding tune. Skipping..." % ml)
194 continue
195 tune_parameters = get_tune_parameters(tune, d)
196
197 tune_baselib = tune_parameters['baselib']
198 if not tune_baselib:
199 bb.warn("Tune %s doesn't have a baselib set. Skipping..." % tune)
200 continue
201
202 if tune_baselib == 'lib64':
203 libdir64 = tune_baselib
204 elif tune_baselib == 'libx32':
205 libdirx32 = tune_baselib
206 elif tune_baselib == 'lib32':
207 libdirn32 = tune_baselib
208 elif tune_baselib == 'lib':
209 libdir32 = tune_baselib
210 else:
211 bb.error('Unknown libdir (%s) of the tune : %s' % (tune_baselib, tune))
212
213 # take out '-' mcpu='s and march='s from parameters
214 opts = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500215 whitelist = (d.getVar("MULTILIB_OPTION_WHITELIST") or "").split()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500216 for i in d.expand(tune_parameters['ccargs']).split():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 if i in whitelist:
218 # Need to strip '-' from option
219 opts.append(i[1:])
220 options.append(" ".join(opts))
221
222 if tune_baselib == 'lib':
223 dirnames.append('32') # /lib => 32bit lib
224 else:
225 dirnames.append(tune_baselib.replace('lib', ''))
226 osdirnames.append('../' + tune_baselib)
227
228 write_config(builddir, target_config_files, options, dirnames, osdirnames)
229 write_headers(builddir, header_config_files, libdir32, libdir64, libdirx32, libdirn32)
230}
231
232gcc_multilib_setup[cleandirs] = "${B}/gcc/config"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500233gcc_multilib_setup[vardepsexclude] = "SDK_ARCH"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234
235EXTRACONFFUNCS += "gcc_multilib_setup"