blob: f7f9f557dbf9091d10238e51a7aa7a9c3c70b068 [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
24 srcdir = d.getVar('S', True)
25 builddir = d.getVar('B', True)
26 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,
32 '%s/*/linux.h' % src_conf_dir,
33 '%s/linux.h' % src_conf_dir)
34
35 # copy the target multilib config files to ${B}
36 for ml_glob in ml_globs:
37 for fn in glob.glob(ml_glob):
38 rel_path = os.path.relpath(fn, src_conf_dir)
39 parent_dir = os.path.dirname(rel_path)
40 bb.utils.mkdirhier('%s/%s' % (build_conf_dir, parent_dir))
41 bb.utils.copyfile(fn, '%s/%s' % (build_conf_dir, rel_path))
42
43 pn = d.getVar('PN', True)
44 multilibs = (d.getVar('MULTILIB_VARIANTS', True) or '').split()
45 if not multilibs and pn != "nativesdk-gcc":
46 return
47
48 mlprefix = d.getVar('MLPREFIX', True)
49
50 if ('%sgcc' % mlprefix) != pn and (not pn.startswith('gcc-cross-canadian')) and pn != "nativesdk-gcc":
51 return
52
53
54 def write_config(root, files, options, dirnames, osdirnames):
55 for ml_conf_file in files:
56 with open(root + '/' + ml_conf_file, 'r') as f:
57 filelines = f.readlines()
58 # recreate multilib configuration variables
59 substs = [
60 (r'^(\s*(MULTILIB_OPTIONS\s*=).*)$', r'\2 %s' % '/'.join(options)),
61 (r'^(\s*MULTILIB_OPTIONS\s*\+=.*)$', ''),
62 (r'^(\s*(MULTILIB_DIRNAMES\s*=).*)$', r'\2 %s' % ' '.join(dirnames)),
63 (r'^(\s*MULTILIB_DIRNAMES\s*\+=.*)$', ''),
64 (r'^(\s*(MULTILIB_OSDIRNAMES\s*=).*)$', r'\2 %s' % ' '.join(osdirnames)),
65 (r'^(\s*MULTILIB_OSDIRNAMES\s*\+=.*)$', ''),
66 ]
67
68 for (i, line) in enumerate(filelines):
69 for subst in substs:
70 line = re.sub(subst[0], subst[1], line)
71 filelines[i] = line
72
73 with open(root + '/' + ml_conf_file, 'w') as f:
74 f.write(''.join(filelines))
75
76 def write_headers(root, files, libdir32, libdir64, libdirx32, libdirn32):
77 def wrap_libdir(libdir):
78 if libdir.find('SYSTEMLIBS_DIR') != -1:
79 return '"%r"'
80 else:
81 return '"/%s/"' % libdir
82
83 for ml_conf_file in files:
84 fn = root + '/' + ml_conf_file
85 if not os.path.exists(fn):
86 continue
87 with open(fn, 'r') as f:
88 filelines = f.readlines()
89
90 # replace lines like
91 # #define GLIBC_DYNAMIC_LINKER32 SYSTEMLIBS_DIR "ld-linux.so.2"
92 # by
93 # #define GLIBC_DYNAMIC_LINKER32 "/lib/" "ld-linux.so.2"
94 # this is needed to put the correct dynamic loader path in the generated binaries
95 substs = [
96 (r'^(#define\s*GLIBC_DYNAMIC_LINKER32\s*)(\S+)(\s*\".*\")$',
97 r'\1' + wrap_libdir(libdir32) + r'\3'),
98 (r'^(#define\s*GLIBC_DYNAMIC_LINKER64\s*)(\S+)(\s*\"\S+\")$',
99 r'\1' + wrap_libdir(libdir64) + r'\3'),
100 (r'^(#define\s*GLIBC_DYNAMIC_LINKER64\s*\"\S+\"\s*)(\S+)(\s*\"\S+\"\s*)(\S+)(\s*\".*\")$',
101 r'\1' + wrap_libdir(libdir64) + r'\3' + wrap_libdir(libdir64) + r'\5'),
102 (r'^(#define\s*GLIBC_DYNAMIC_LINKERX32\s*)(\S+)(\s*\".*\")$',
103 r'\1' + wrap_libdir(libdirx32) + r'\3'),
104 (r'^(#define\s*GLIBC_DYNAMIC_LINKERN32\s*)(\S+)(\s*\".*\")$',
105 r'\1' + wrap_libdir(libdirn32) + r'\3'),
106 (r'^(#define\s*UCLIBC_DYNAMIC_LINKER32\s*)(\S+)(\s*\".*\")$',
107 r'\1' + wrap_libdir(libdir32) + r'\3'),
108 (r'^(#define\s*UCLIBC_DYNAMIC_LINKER64\s*)(\S+)(\s*\".*\")$',
109 r'\1' + wrap_libdir(libdir64) + r'\3'),
110 (r'^(#define\s*UCLIBC_DYNAMIC_LINKERN32\s*)(\S+)(\s*\".*\")$',
111 r'\1' + wrap_libdir(libdirn32) + r'\3'),
112 (r'^(#define\s*UCLIBC_DYNAMIC_LINKER\b\s*)(\S+)(\s*\".*\")$',
113 r'\1' + wrap_libdir(libdir32) + r'\3'),
114 ]
115
116 for (i, line) in enumerate(filelines):
117 for subst in substs:
118 line = re.sub(subst[0], subst[1], line)
119 filelines[i] = line
120
121 with open(root + '/' + ml_conf_file, 'w') as f:
122 f.write(''.join(filelines))
123
124
125 gcc_target_config_files = {
126 'x86_64' : ['gcc/config/i386/t-linux64'],
127 'i586' : ['gcc/config/i386/t-linux64'],
128 'i686' : ['gcc/config/i386/t-linux64'],
129 'mips' : ['gcc/config/mips/t-linux64'],
130 'mips64' : ['gcc/config/mips/t-linux64'],
131 'powerpc' : ['gcc/config/rs6000/t-linux64'],
132 'powerpc64' : ['gcc/config/rs6000/t-linux64'],
133 }
134
135 gcc_header_config_files = {
136 'x86_64' : ['gcc/config/i386/linux64.h'],
137 'i586' : ['gcc/config/i386/linux64.h'],
138 'i686' : ['gcc/config/i386/linux64.h'],
139 'mips' : ['gcc/config/mips/linux.h', 'gcc/config/mips/linux64.h'],
140 'mips64' : ['gcc/config/mips/linux.h', 'gcc/config/mips/linux64.h'],
141 'powerpc' : ['gcc/config/rs6000/linux64.h'],
142 'powerpc64' : ['gcc/config/rs6000/linux64.h'],
143 }
144
145 libdir32 = 'SYSTEMLIBS_DIR'
146 libdir64 = 'SYSTEMLIBS_DIR'
147 libdirx32 = 'SYSTEMLIBS_DIR'
148 libdirn32 = 'SYSTEMLIBS_DIR'
149
150
151 target_arch = (d.getVar('TARGET_ARCH_MULTILIB_ORIGINAL', True) if mlprefix
152 else d.getVar('TARGET_ARCH', True))
153 if pn == "nativesdk-gcc":
154 header_config_files = gcc_header_config_files[d.getVar("SDK_ARCH", True)]
155 write_headers(builddir, header_config_files, libdir32, libdir64, libdirx32, libdirn32)
156 return
157
158 if target_arch not in gcc_target_config_files:
159 bb.warn('gcc multilib setup is not supported for TARGET_ARCH=' + target_arch)
160 return
161
162 target_config_files = gcc_target_config_files[target_arch]
163 header_config_files = gcc_header_config_files[target_arch]
164
165 ml_list = ['DEFAULTTUNE_MULTILIB_ORIGINAL' if mlprefix else 'DEFAULTTUNE']
166 mltunes = [('DEFAULTTUNE_virtclass-multilib-%s' % ml) for ml in multilibs]
167 if mlprefix:
168 mlindex = 0
169 for ml in multilibs:
170 if mlprefix == ml + '-':
171 break
172 mlindex += 1
173
174 ml_list.extend(mltunes[:mlindex] + ['DEFAULTTUNE'] + mltunes[(mlindex + 1):])
175 else:
176 ml_list.extend(mltunes)
177
178 options = []
179 dirnames = []
180 osdirnames = []
181 optsets = []
182
183 for ml in ml_list:
184 tune = d.getVar(ml, True)
185 if not tune:
186 bb.warn("%s doesn't have a corresponding tune. Skipping..." % ml)
187 continue
188 tune_parameters = get_tune_parameters(tune, d)
189
190 tune_baselib = tune_parameters['baselib']
191 if not tune_baselib:
192 bb.warn("Tune %s doesn't have a baselib set. Skipping..." % tune)
193 continue
194
195 if tune_baselib == 'lib64':
196 libdir64 = tune_baselib
197 elif tune_baselib == 'libx32':
198 libdirx32 = tune_baselib
199 elif tune_baselib == 'lib32':
200 libdirn32 = tune_baselib
201 elif tune_baselib == 'lib':
202 libdir32 = tune_baselib
203 else:
204 bb.error('Unknown libdir (%s) of the tune : %s' % (tune_baselib, tune))
205
206 # take out '-' mcpu='s and march='s from parameters
207 opts = []
208 whitelist = (d.getVar("MULTILIB_OPTION_WHITELIST", True) or "").split()
209 for i in tune_parameters['ccargs'].split():
210 if i in whitelist:
211 # Need to strip '-' from option
212 opts.append(i[1:])
213 options.append(" ".join(opts))
214
215 if tune_baselib == 'lib':
216 dirnames.append('32') # /lib => 32bit lib
217 else:
218 dirnames.append(tune_baselib.replace('lib', ''))
219 osdirnames.append('../' + tune_baselib)
220
221 write_config(builddir, target_config_files, options, dirnames, osdirnames)
222 write_headers(builddir, header_config_files, libdir32, libdir64, libdirx32, libdirn32)
223}
224
225gcc_multilib_setup[cleandirs] = "${B}/gcc/config"
226
227EXTRACONFFUNCS += "gcc_multilib_setup"