gcc-multilib-config.inc 8.6 KB

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