fix_py2_protobuf.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import subprocess
  2. import os
  3. import process_command_files as pcf
  4. def run(*args):
  5. # print >>sys.stderr, args
  6. return subprocess.check_output(list(args), shell=False).strip()
  7. def gen_renames_1(d):
  8. for l in d.split('\n'):
  9. l = l.strip()
  10. if ' ' in l:
  11. yield l.split(' ')[-1]
  12. def gen_renames_2(p, d):
  13. for s in gen_renames_1(d):
  14. yield s + ' ' + p + s
  15. def gen_renames(p, d):
  16. return '\n'.join(gen_renames_2(p, d)).strip() + '\n'
  17. def rename_syms(where, ret, libs):
  18. p = 'py2_'
  19. # join libs
  20. run(where + 'llvm-ar', 'qcL', ret, *libs)
  21. # find symbols to rename
  22. syms = run(where + 'llvm-nm', '--extern-only', '--defined-only', '-A', ret)
  23. # prepare rename plan
  24. renames = gen_renames(p, syms)
  25. with open('syms', 'w') as f:
  26. f.write(renames)
  27. # rename symbols
  28. run(where + 'llvm-objcopy', '--redefine-syms=syms', ret)
  29. # back-rename some symbols
  30. args = [
  31. where + 'llvm-objcopy',
  32. '--redefine-sym',
  33. p + 'init_api_implementation=init6google8protobuf8internal19_api_implementation',
  34. '--redefine-sym',
  35. p + 'init_message=init6google8protobuf5pyext8_message',
  36. '--redefine-sym',
  37. p + 'init6google8protobuf8internal19_api_implementation=init6google8protobuf8internal19_api_implementation',
  38. '--redefine-sym',
  39. p + 'init6google8protobuf5pyext8_message=init6google8protobuf5pyext8_message',
  40. '--redefine-sym',
  41. p + '_init6google8protobuf8internal19_api_implementation=_init6google8protobuf8internal19_api_implementation',
  42. '--redefine-sym',
  43. p + '_init6google8protobuf5pyext8_message=_init6google8protobuf5pyext8_message',
  44. ret,
  45. ]
  46. run(*args)
  47. return ret
  48. def fix_py2(cmd, have_comand_files=False, prefix='lib', suffix='a'):
  49. args = cmd
  50. if have_comand_files:
  51. args = pcf.get_args(cmd)
  52. if 'protobuf_old' not in str(args):
  53. return cmd
  54. py2_libs = [prefix + 'contrib-libs-protobuf_old.' + suffix, prefix + 'pypython-protobuf-py2.' + suffix]
  55. def need_rename(x):
  56. for v in py2_libs:
  57. if v in x:
  58. return True
  59. return False
  60. old = []
  61. lib = []
  62. where = os.path.dirname(cmd[0]) + '/'
  63. for x in args:
  64. if need_rename(x):
  65. lib.append(x)
  66. else:
  67. old.append(x)
  68. name = rename_syms(where, 'libprotoherobora.' + suffix, lib)
  69. if not have_comand_files:
  70. return old + [name]
  71. for file in cmd:
  72. if pcf.is_cmdfile_arg(file):
  73. cmd_file_path = pcf.cmdfile_path(file)
  74. args = pcf.read_from_command_file(cmd_file_path)
  75. if not 'protobuf_old' in str(args):
  76. continue
  77. with open(cmd_file_path, 'w') as afile:
  78. for arg in args:
  79. if not need_rename(arg):
  80. afile.write(arg + '\n')
  81. afile.write(name)
  82. return cmd