fix_msvc_output.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import subprocess
  2. import os
  3. import sys
  4. import json
  5. # Explicitly enable local imports
  6. # Don't forget to add imported scripts to inputs of the calling command!
  7. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  8. import process_command_files as pcf
  9. import process_whole_archive_option as pwa
  10. def out2err(cmd):
  11. return subprocess.Popen(cmd, stdout=sys.stderr).wait()
  12. def decoding_needed(strval):
  13. if sys.version_info >= (3, 0, 0):
  14. return isinstance(strval, bytes)
  15. else:
  16. return False
  17. def out2err_cut_first_line(cmd):
  18. p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
  19. first_line = True
  20. while True:
  21. line = p.stdout.readline()
  22. line = line.decode('utf-8') if decoding_needed(line) else line
  23. if not line:
  24. break
  25. if first_line:
  26. sys.stdout.write(line)
  27. first_line = False
  28. else:
  29. sys.stderr.write(line)
  30. return p.wait()
  31. if __name__ == '__main__':
  32. args = sys.argv[1:]
  33. mode = args[0]
  34. plugins = []
  35. if mode == 'link' and '--start-plugins' in args:
  36. ib = args.index('--start-plugins')
  37. ie = args.index('--end-plugins')
  38. plugins = args[ib + 1:ie]
  39. args = args[:ib] + args[ie + 1:]
  40. for p in plugins:
  41. res = subprocess.check_output([sys.executable, p] + args).decode().strip()
  42. if res:
  43. args = json.loads(res)
  44. args, wa_peers, wa_libs = pwa.get_whole_archive_peers_and_libs(pcf.skip_markers(args[1:]))
  45. cmd = pwa.ProcessWholeArchiveOption('WINDOWS', wa_peers, wa_libs).construct_cmd(args)
  46. run = out2err
  47. if mode in ('cl', 'ml'):
  48. # First line of cl.exe and ml64.exe stdout is useless: it prints input file
  49. run = out2err_cut_first_line
  50. sys.exit(run(cmd))