Skip to content

Instantly share code, notes, and snippets.

@LeonB
Last active August 29, 2015 14:17
Show Gist options
  • Save LeonB/16183ed09d0c30daa676 to your computer and use it in GitHub Desktop.
Save LeonB/16183ed09d0c30daa676 to your computer and use it in GitHub Desktop.
python wrapperscript for stdin bug winegcc
#!/usr/bin/env python
from subprocess import Popen, PIPE, STDOUT
import os
import sys
import tempfile
import re
if not sys.stdin.isatty():
stdin = sys.stdin.read()
else:
stdin = None
if stdin:
# Create temporary file
tf = tempfile.NamedTemporaryFile(delete=False)
# Write stdin to temporary file
tf.write (stdin)
tf.close()
# Remove first argument
args = sys.argv[1:]
if '-Wl,-r' in args:
cc = 'gcc'
else:
cc = 'winegcc'
# Make winegcc first argument
args.insert(0, cc)
# Remove stdin argument
if args[-1] == '-':
args.pop()
# Remove -xc argument
# for arg in args:
# if arg == '-xc':
# args.remove(arg)
if stdin:
# Insert temporary filename after -v flag
try:
index = args.index('-v')
args.insert(index, tf.name)
except ValueError:
args.insert(1, tf.name)
# Append this so winegcc doesn't generate an app loader
# args.append('-shared')
# args.insert(1, '-shared')
p = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE, env=os.environ)
stdout, stderr = p.communicate(input=stdin)
# remove wine loader script
for arg in args:
match = re.search("/tmp/go-build.+?/", arg)
if match:
baseDir = match.group(0)
filename = baseDir + 'command-line-arguments/_obj/_cgo_.o.so'
if os.path.isfile(filename):
to = baseDir + 'command-line-arguments/_obj/_cgo_.o'
os.rename(filename, to)
break
with open ('cc.log', 'a') as f:
f.write ("stdin: %s\n" % stdin)
f.write ("arguments: %s\n" % args)
f.write ("cmd: %s\n" % ' '.join(args))
f.write ("stdout: %s\n" % stdout)
f.write ("stderr: %s\n" % stderr)
f.write ("exitcode: %s\n" % p.returncode)
f.write ("env: %s\n" % os.environ)
if stdin:
f.write ("tf.name: %s\n" % tf.name)
# Close temporary file fd and remove it
# tf.close()
# os.unlink(tf.name)
sys.stdout.write(stdout)
sys.stderr.write(stderr)
sys.exit(p.returncode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment