| 1 |
import pytest |
| 2 |
import os |
| 3 |
import re |
| 4 |
from rpython.tool.udir import udir |
| 5 |
from rpython.translator.platform import CompilationError, Platform |
| 6 |
from rpython.translator.platform import host |
| 7 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 8 |
|
| 9 |
import py, sys, platform |
| 10 |
if sys.platform != 'win32': |
| 11 |
pytest.skip("Windows only") |
| 12 |
|
| 13 |
|
| 14 |
def get_manifest(executable, allow_missing=False): |
| 15 |
|
| 16 |
manifest_start = '<assembly xmlns=.urn:schemas-microsoft-com:asm.v1. manifestVersion=' |
| 17 |
manifest_end = '</assembly>' |
| 18 |
with open(str(executable), 'rb') as fid: |
| 19 |
exe = fid.read() |
| 20 |
match = re.search(manifest_start, exe) |
| 21 |
if not match: |
| 22 |
if allow_missing: |
| 23 |
return None |
| 24 |
raise ValueError("could not find manifest in %s" % executable) |
| 25 |
start = match.start() |
| 26 |
end = exe.find(manifest_end, start) + len(manifest_end) |
| 27 |
|
| 28 |
assert end > len(manifest_end) |
| 29 |
|
| 30 |
assert exe.find(manifest_start, end) == -1 |
| 31 |
return exe[start:end] |
| 32 |
|
| 33 |
class TestWindows(object): |
| 34 |
platform = host |
| 35 |
|
| 36 |
def test_manifest_exe(self): |
| 37 |
cfile = udir.join('test_simple.c') |
| 38 |
cfile.write(''' |
| 39 |
#include <stdio.h> |
| 40 |
int main() |
| 41 |
{ |
| 42 |
printf("42\\n"); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
''') |
| 46 |
executable = self.platform.compile([cfile], ExternalCompilationInfo()) |
| 47 |
manifest = get_manifest(executable) |
| 48 |
assert "asInvoker" in manifest |
| 49 |
assert "longPathAware" not in manifest |
| 50 |
|
| 51 |
def test_manifest_dll(self): |
| 52 |
cfile = udir.join('test_simple.c') |
| 53 |
cfile.write(''' |
| 54 |
__declspec(dllexport) int times2(int x) |
| 55 |
{ |
| 56 |
return x * 2; |
| 57 |
} |
| 58 |
''') |
| 59 |
executable = self.platform.compile([cfile], |
| 60 |
ExternalCompilationInfo(), |
| 61 |
standalone=False) |
| 62 |
manifest = get_manifest(executable) |
| 63 |
assert "asInvoker" in manifest |
| 64 |
assert "longPathAware" not in manifest |
| 65 |
|
| 66 |
class TestMakefile(object): |
| 67 |
platform = host |
| 68 |
|
| 69 |
def check_res(self, res, expected): |
| 70 |
assert res.out == expected |
| 71 |
assert res.returncode == 0 |
| 72 |
|
| 73 |
def test_manifest(self): |
| 74 |
|
| 75 |
class Translation(): |
| 76 |
icon = None |
| 77 |
manifest = os.path.join(os.path.dirname(__file__), |
| 78 |
'data', 'python.manifest') |
| 79 |
|
| 80 |
class Config(): |
| 81 |
translation = Translation() |
| 82 |
|
| 83 |
tmpdir = udir.join('test_manifest').ensure(dir=1) |
| 84 |
cfile = tmpdir.join('pypy_main.c') |
| 85 |
cfile.write(''' |
| 86 |
#include <stdio.h> |
| 87 |
__declspec(dllexport) int pypy_main_startup(int argc, wchar_t* argv[]) |
| 88 |
{ |
| 89 |
int x = 10; |
| 90 |
int y = x * 2; |
| 91 |
printf("%d\\n", y); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
''') |
| 95 |
mk = self.platform.gen_makefile([cfile], |
| 96 |
ExternalCompilationInfo(), |
| 97 |
path=tmpdir, |
| 98 |
shared=True, |
| 99 |
config = Config(), |
| 100 |
) |
| 101 |
mk.write() |
| 102 |
self.platform.execute_makefile(mk) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
target = mk.exe_name |
| 107 |
basename = target.purebasename |
| 108 |
res = self.platform.execute(target) |
| 109 |
self.check_res(res, '20\n') |
| 110 |
targetw = target.parts()[-2] / basename + 'w.exe' |
| 111 |
res = self.platform.execute(targetw) |
| 112 |
self.check_res(res, '20\n') |
| 113 |
self.platform.execute_makefile(mk, ['debugmode_' + basename + '.exe']) |
| 114 |
debug_target = target.parts()[-2] / 'debugmode_' + basename + '.exe' |
| 115 |
res = self.platform.execute(debug_target) |
| 116 |
self.check_res(res, '20\n') |
| 117 |
|
| 118 |
|
| 119 |
for v in [target, targetw, debug_target]: |
| 120 |
manifest = get_manifest(v) |
| 121 |
assert "asInvoker" in manifest |
| 122 |
assert "longPathAware" in manifest |