| 1 |
|
| 2 |
import py, sys, ctypes, os |
| 3 |
from rpython.tool.udir import udir |
| 4 |
from rpython.translator.platform import CompilationError, Platform |
| 5 |
from rpython.translator.platform import host |
| 6 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 7 |
|
| 8 |
def test_compilationerror_repr(): |
| 9 |
|
| 10 |
|
| 11 |
c = CompilationError('', '*'*1000) |
| 12 |
assert repr(c) == 'CompilationError(err="""\n\t%s""")' % ('*'*1000,) |
| 13 |
c = CompilationError('*'*1000, '') |
| 14 |
assert repr(c) == 'CompilationError(out="""\n\t%s""")' % ('*'*1000,) |
| 15 |
|
| 16 |
class TestPlatform(object): |
| 17 |
platform = host |
| 18 |
strict_on_stderr = True |
| 19 |
|
| 20 |
def check_res(self, res, expected='42\n'): |
| 21 |
assert res.out == expected |
| 22 |
if self.strict_on_stderr: |
| 23 |
assert res.err == '' |
| 24 |
assert res.returncode == 0 |
| 25 |
|
| 26 |
def test_simple_enough(self): |
| 27 |
cfile = udir.join('test_simple_enough.c') |
| 28 |
cfile.write(''' |
| 29 |
#include <stdio.h> |
| 30 |
int main() |
| 31 |
{ |
| 32 |
printf("42\\n"); |
| 33 |
return 0; |
| 34 |
} |
| 35 |
''') |
| 36 |
executable = self.platform.compile([cfile], ExternalCompilationInfo()) |
| 37 |
res = self.platform.execute(executable) |
| 38 |
self.check_res(res) |
| 39 |
|
| 40 |
def test_two_files(self): |
| 41 |
cfile = udir.join('test_two_files.c') |
| 42 |
cfile.write(''' |
| 43 |
#include <stdio.h> |
| 44 |
int func(); |
| 45 |
int main() |
| 46 |
{ |
| 47 |
printf("%d\\n", func()); |
| 48 |
return 0; |
| 49 |
} |
| 50 |
''') |
| 51 |
cfile2 = udir.join('implement1.c') |
| 52 |
cfile2.write(''' |
| 53 |
int func() |
| 54 |
{ |
| 55 |
return 42; |
| 56 |
} |
| 57 |
''') |
| 58 |
executable = self.platform.compile([cfile, cfile2], ExternalCompilationInfo()) |
| 59 |
res = self.platform.execute(executable) |
| 60 |
self.check_res(res) |
| 61 |
|
| 62 |
def test_nice_errors(self): |
| 63 |
cfile = udir.join('test_nice_errors.c') |
| 64 |
cfile.write('') |
| 65 |
try: |
| 66 |
executable = self.platform.compile([cfile], ExternalCompilationInfo()) |
| 67 |
except CompilationError as e: |
| 68 |
filename = cfile.dirpath().join(cfile.purebasename + '.errors') |
| 69 |
assert filename.read('r') == e.err |
| 70 |
else: |
| 71 |
py.test.fail("Did not raise") |
| 72 |
|
| 73 |
def test_use_eci(self): |
| 74 |
tmpdir = udir.join('use_eci').ensure(dir=1) |
| 75 |
hfile = tmpdir.join('needed.h') |
| 76 |
hfile.write('#define SOMEHASHDEFINE 42\n') |
| 77 |
eci = ExternalCompilationInfo(include_dirs=[tmpdir]) |
| 78 |
cfile = udir.join('use_eci_c.c') |
| 79 |
cfile.write(''' |
| 80 |
#include <stdio.h> |
| 81 |
#include "needed.h" |
| 82 |
int main() |
| 83 |
{ |
| 84 |
printf("%d\\n", SOMEHASHDEFINE); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
''') |
| 88 |
executable = self.platform.compile([cfile], eci) |
| 89 |
res = self.platform.execute(executable) |
| 90 |
self.check_res(res) |
| 91 |
|
| 92 |
def test_standalone_library(self): |
| 93 |
tmpdir = udir.join('standalone_library').ensure(dir=1) |
| 94 |
c_file = tmpdir.join('stand1.c') |
| 95 |
c_file.write(''' |
| 96 |
#include <math.h> |
| 97 |
#include <stdio.h> |
| 98 |
|
| 99 |
int main() |
| 100 |
{ |
| 101 |
printf("%f\\n", pow(2.0, 2.0)); |
| 102 |
}''') |
| 103 |
if sys.platform != 'win32': |
| 104 |
eci = ExternalCompilationInfo( |
| 105 |
libraries = ['m'], |
| 106 |
) |
| 107 |
else: |
| 108 |
eci = ExternalCompilationInfo() |
| 109 |
executable = self.platform.compile([c_file], eci) |
| 110 |
res = self.platform.execute(executable) |
| 111 |
assert res.out.startswith('4.0') |
| 112 |
|
| 113 |
def test_environment_inheritance(self): |
| 114 |
|
| 115 |
cmd = 'import os; print os.environ["_SOME_VARIABLE_%d"]' |
| 116 |
env = {'_SOME_VARIABLE_1':'xyz'} |
| 117 |
env['PATH'] = os.environ['PATH'] |
| 118 |
res = self.platform.execute(sys.executable, ['-c', cmd % 1], |
| 119 |
env=env) |
| 120 |
assert 'xyz' in res.out |
| 121 |
os.environ['_SOME_VARIABLE_2'] = 'zyz' |
| 122 |
try: |
| 123 |
res = self.platform.execute(sys.executable, ['-c', cmd % 2]) |
| 124 |
assert 'zyz' in res.out |
| 125 |
finally: |
| 126 |
del os.environ['_SOME_VARIABLE_2'] |
| 127 |
|
| 128 |
def test_key(self): |
| 129 |
class XPlatform(Platform): |
| 130 |
relevant_environ = ['CPATH'] |
| 131 |
|
| 132 |
def __init__(self): |
| 133 |
self.cc = 'xcc' |
| 134 |
x = XPlatform() |
| 135 |
res = x.key() |
| 136 |
assert res.startswith("XPlatform cc='xcc' CPATH=") |
| 137 |
|
| 138 |
def test_equality(): |
| 139 |
class X(Platform): |
| 140 |
def __init__(self): |
| 141 |
pass |
| 142 |
class Y(Platform): |
| 143 |
def __init__(self, x): |
| 144 |
self.x = x |
| 145 |
|
| 146 |
assert X() == X() |
| 147 |
assert Y(3) == Y(3) |
| 148 |
assert Y(2) != Y(3) |
| 149 |
|
| 150 |
|
| 151 |
def test_is_host_build(): |
| 152 |
from platform import machine |
| 153 |
from rpython.translator import platform |
| 154 |
assert platform.host == platform.platform |
| 155 |
|
| 156 |
assert platform.is_host_build() |
| 157 |
|
| 158 |
if sys.platform != 'darwin' and machine().startswith('arm'): |
| 159 |
platform.set_platform('arm', None) |
| 160 |
assert platform.host != platform.platform |
| 161 |
assert not platform.is_host_build() |