fix_msvc_output.py 1.5 KB

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