Example #1
0
    def test_32bit_makefile(self):
        if platform.machine() != 'i386':
            py.test.skip("i386 only")
        plat32 = Darwin_i386()
        plat64 = Darwin_x86_64()
        eci = ExternalCompilationInfo()
        cfile_content =r'''
        #include <stdio.h>
        #include <limits.h>

        int main() {
                printf("%d\n", INT_MAX < LONG_MAX);
                return 0;
        }
        '''

        tmpdir = udir.join('32_makefile' + self.__class__.__name__).ensure(dir=1)
        cfile = tmpdir.join('test_int_size.c')
        cfile.write(cfile_content)
        mk = plat32.gen_makefile([cfile], ExternalCompilationInfo(),
                               path=tmpdir)
        mk.write()
        plat32.execute_makefile(mk)
        res = plat32.execute(tmpdir.join('test_int_size'))
        self.check_res(res, '0\n')
        if host_factory == Darwin_x86_64:
            tmpdir = udir.join('64_makefile' + self.__class__.__name__).ensure(dir=1)
            cfile = tmpdir.join('test_int_size.c')
            cfile.write(cfile_content)
            mk = plat64.gen_makefile([cfile], ExternalCompilationInfo(),
                                   path=tmpdir)
            mk.write()
            plat64.execute_makefile(mk)
            res = plat64.execute(tmpdir.join('test_int_size'))
            self.check_res(res, '1\n')
Example #2
0
def _get_test_file():
    tst_file = udir.join('test.c')
    i = 0
    # XXX we want to put typedefs here
    while tst_file.check():
        tst_file = udir.join('test%d.c' % i)
        i += 1
    return tst_file
Example #3
0
 def convert_sources_to_files(self, cache_dir=None, main_clause=None):
     from platformer import udir
     if not self.separate_module_sources:
         return self
     if cache_dir is None:
         cache_dir = udir.join('module_cache').ensure(dir=1)
     num = 0
     files = []
     for source in self.separate_module_sources:
         while 1:
             filename = cache_dir.join('module_%d.c' % num)
             num += 1
             if not filename.check():
                 break
         f = filename.open("w")
         if main_clause:
             f.write(main_clause)
         self.write_c_header(f)
         source = str(source)
         f.write(source)
         if not source.endswith('\n'):
             f.write('\n')
         f.close()
         files.append(str(filename))
     d = self._copy_attributes()
     d['separate_module_sources'] = ()
     d['separate_module_files'] += tuple(files)
     return ExternalCompilationInfo(**d)
Example #4
0
 def convert_sources_to_files(self, cache_dir=None, main_clause=None):
     from platformer import udir
     if not self.separate_module_sources:
         return self
     if cache_dir is None:
         cache_dir = udir.join('module_cache').ensure(dir=1)
     num = 0
     files = []
     for source in self.separate_module_sources:
         while 1:
             filename = cache_dir.join('module_%d.c' % num)
             num += 1
             if not filename.check():
                 break
         f = filename.open("w")
         if main_clause:
             f.write(main_clause)
         self.write_c_header(f)
         source = str(source)
         f.write(source)
         if not source.endswith('\n'):
             f.write('\n')
         f.close()
         files.append(str(filename))
     d = self._copy_attributes()
     d['separate_module_sources'] = ()
     d['separate_module_files'] += tuple(files)
     return ExternalCompilationInfo(**d)
Example #5
0
 def test_nice_errors(self):
     cfile = udir.join('test_nice_errors.c')
     cfile.write('')
     try:
         self.platform.compile([cfile], ExternalCompilationInfo())
     except CompilationError, e:
         filename = cfile.dirpath().join(cfile.purebasename + '.errors')
         assert filename.read('r') == e.err
Example #6
0
 def test_nice_errors(self):
     cfile = udir.join('test_nice_errors.c')
     cfile.write('')
     try:
         self.platform.compile([cfile], ExternalCompilationInfo())
     except CompilationError, e:
         filename = cfile.dirpath().join(cfile.purebasename + '.errors')
         assert filename.read('r') == e.err
Example #7
0
 def test_use_eci(self):
     tmpdir = udir.join('use_eci').ensure(dir=1)
     hfile = tmpdir.join('needed.h')
     hfile.write('#define SOMEHASHDEFINE 42\n')
     eci = ExternalCompilationInfo(include_dirs=[tmpdir])
     cfile = udir.join('use_eci_c.c')
     cfile.write('''
     #include <stdio.h>
     #include "needed.h"
     int main()
     {
         printf("%d\\n", SOMEHASHDEFINE);
         return 0;
     }
     ''')
     executable = self.platform.compile([cfile], eci)
     res = self.platform.execute(executable)
     self.check_res(res)
Example #8
0
 def test_use_eci(self):
     tmpdir = udir.join('use_eci').ensure(dir=1)
     hfile = tmpdir.join('needed.h')
     hfile.write('#define SOMEHASHDEFINE 42\n')
     eci = ExternalCompilationInfo(include_dirs=[tmpdir])
     cfile = udir.join('use_eci_c.c')
     cfile.write('''
     #include <stdio.h>
     #include "needed.h"
     int main()
     {
         printf("%d\\n", SOMEHASHDEFINE);
         return 0;
     }
     ''')
     executable = self.platform.compile([cfile], eci)
     res = self.platform.execute(executable)
     self.check_res(res)
Example #9
0
    def compile_shared_lib(self, outputfilename=None, ignore_a_files=False):
        from platformer import host, udir
        self = self.convert_sources_to_files()
        if ignore_a_files:
            if not [fn for fn in self.link_files if fn.endswith('.a')]:
                ignore_a_files = False  # there are none
        if not self.separate_module_files and not ignore_a_files:
            if sys.platform != 'win32':
                return self
            if not self.export_symbols:
                return self
            basepath = udir.join('module_cache')
        else:
            #basepath = py.path.local(self.separate_module_files[0]).dirpath()
            basepath = udir.join('shared_cache')
        if outputfilename is None:
            # find more or less unique name there
            pth = basepath.join('externmod').new(ext=host.so_ext)
            num = 0
            while pth.check():
                pth = basepath.join('externmod_%d' %
                                    (num, )).new(ext=host.so_ext)
                num += 1
            basepath.ensure(dir=1)
            outputfilename = str(pth.dirpath().join(pth.purebasename))

        if ignore_a_files:
            d = self._copy_attributes()
            d['link_files'] = [
                fn for fn in d['link_files'] if not fn.endswith('.a')
            ]
            self = ExternalCompilationInfo(**d)

        lib = str(
            host.compile([],
                         self,
                         outputfilename=outputfilename,
                         standalone=False))
        d = self._copy_attributes()
        d['libraries'] += (lib, )
        d['separate_module_files'] = ()
        d['separate_module_sources'] = ()
        return ExternalCompilationInfo(**d)
Example #10
0
 def test_preprocess_localbase(self):
     tmpdir = udir.join('test_preprocess_localbase').ensure(dir=1)
     eci = ExternalCompilationInfo()
     os.environ['PLATFORMER_LOCALBASE'] = '/foo/baz'
     try:
         mk = self.platform.gen_makefile(['blip.c'], eci, path=tmpdir)
         mk.write()
     finally:
         del os.environ['PLATFORMER_LOCALBASE']
     Makefile = tmpdir.join('Makefile').read()
     assert 'INCLUDEDIRS = -I/foo/baz/include' in Makefile
     assert 'LIBDIRS = -L/foo/baz/lib' in Makefile
Example #11
0
 def test_two_files(self):
     cfile = udir.join('test_two_files.c')
     cfile.write('''
     #include <stdio.h>
     int func();
     int main()
     {
         printf("%d\\n", func());
         return 0;
     }
     ''')
     cfile2 = udir.join('implement1.c')
     cfile2.write('''
     int func()
     {
         return 42;
     }
     ''')
     executable = self.platform.compile([cfile, cfile2], ExternalCompilationInfo())
     res = self.platform.execute(executable)
     self.check_res(res)
Example #12
0
    def compile_shared_lib(self, outputfilename=None, ignore_a_files=False):
        from platformer import host, udir
        self = self.convert_sources_to_files()
        if ignore_a_files:
            if not [fn for fn in self.link_files if fn.endswith('.a')]:
                ignore_a_files = False    # there are none
        if not self.separate_module_files and not ignore_a_files:
            if sys.platform != 'win32':
                return self
            if not self.export_symbols:
                return self
            basepath = udir.join('module_cache')
        else:
            #basepath = py.path.local(self.separate_module_files[0]).dirpath()
            basepath = udir.join('shared_cache')
        if outputfilename is None:
            # find more or less unique name there
            pth = basepath.join('externmod').new(ext=host.so_ext)
            num = 0
            while pth.check():
                pth = basepath.join(
                    'externmod_%d' % (num,)).new(ext=host.so_ext)
                num += 1
            basepath.ensure(dir=1)
            outputfilename = str(pth.dirpath().join(pth.purebasename))

        if ignore_a_files:
            d = self._copy_attributes()
            d['link_files'] = [fn for fn in d['link_files']
                                  if not fn.endswith('.a')]
            self = ExternalCompilationInfo(**d)

        lib = str(host.compile([], self, outputfilename=outputfilename,
                               standalone=False))
        d = self._copy_attributes()
        d['libraries'] += (lib,)
        d['separate_module_files'] = ()
        d['separate_module_sources'] = ()
        return ExternalCompilationInfo(**d)
Example #13
0
 def test_simple_enough(self):
     cfile = udir.join('test_simple_enough.c')
     cfile.write('''
     #include <stdio.h>
     int main()
     {
         printf("42\\n");
         return 0;
     }
     ''')
     executable = self.platform.compile([cfile], ExternalCompilationInfo())
     res = self.platform.execute(executable)
     self.check_res(res)
Example #14
0
 def test_two_files(self):
     cfile = udir.join('test_two_files.c')
     cfile.write('''
     #include <stdio.h>
     int func();
     int main()
     {
         printf("%d\\n", func());
         return 0;
     }
     ''')
     cfile2 = udir.join('implement1.c')
     cfile2.write('''
     int func()
     {
         return 42;
     }
     ''')
     executable = self.platform.compile([cfile, cfile2],
                                        ExternalCompilationInfo())
     res = self.platform.execute(executable)
     self.check_res(res)
Example #15
0
 def test_simple_enough(self):
     cfile = udir.join('test_simple_enough.c')
     cfile.write('''
     #include <stdio.h>
     int main()
     {
         printf("42\\n");
         return 0;
     }
     ''')
     executable = self.platform.compile([cfile], ExternalCompilationInfo())
     res = self.platform.execute(executable)
     self.check_res(res)
Example #16
0
 def test_includes_outside_scratchbox(self):
     cfile = udir.join('test_includes_outside_scratchbox.c')
     cfile.write('''
     #include <stdio.h>
     #include "test.h"
     int main()
     {
         printf("%d\\n", XXX_STUFF);
         return 0;
     }
     ''')
     includedir = py.path.local(__file__).dirpath().join('include')
     eci = ExternalCompilationInfo(include_dirs=(includedir,))
     executable = self.platform.compile([cfile], eci)
     res = self.platform.execute(executable)
     self.check_res(res)
Example #17
0
 def test_includes_outside_scratchbox(self):
     cfile = udir.join('test_includes_outside_scratchbox.c')
     cfile.write('''
     #include <stdio.h>
     #include "test.h"
     int main()
     {
         printf("%d\\n", XXX_STUFF);
         return 0;
     }
     ''')
     includedir = py.path.local(__file__).dirpath().join('include')
     eci = ExternalCompilationInfo(include_dirs=(includedir, ))
     executable = self.platform.compile([cfile], eci)
     res = self.platform.execute(executable)
     self.check_res(res)
Example #18
0
    def test_longsize(self):
        if platform.machine() != 'i386':
            py.test.skip("i386 only")
        cfile = udir.join('test_int_size.c')
        cfile.write(r'''
        #include <stdio.h>
        #include <limits.h>

        int main() {
                printf("%ld\n", LONG_MAX);
                return 0;
        }
        ''')
        eci = ExternalCompilationInfo()
        executable = self.platform.compile([cfile], eci)
        res = self.platform.execute(executable)
        self.check_res(res, str(sys.maxint) + '\n')
Example #19
0
    def test_standalone_library(self):
        tmpdir = udir.join('standalone_library').ensure(dir=1)
        c_file = tmpdir.join('stand1.c')
        c_file.write('''
        #include <math.h>
        #include <stdio.h>

        int main()
        {
            printf("%f\\n", pow(2.0, 2.0));
        }''')
        if sys.platform != 'win32':
            eci = ExternalCompilationInfo(libraries=['m'], )
        else:
            eci = ExternalCompilationInfo()
        executable = self.platform.compile([c_file], eci)
        res = self.platform.execute(executable)
        assert res.out.startswith('4.0')
Example #20
0
    def test_standalone_library(self):
        tmpdir = udir.join('standalone_library').ensure(dir=1)
        c_file = tmpdir.join('stand1.c')
        c_file.write('''
        #include <math.h>
        #include <stdio.h>

        int main()
        {
            printf("%f\\n", pow(2.0, 2.0));
        }''')
        if sys.platform != 'win32':
            eci = ExternalCompilationInfo(
                libraries = ['m'],
                )
        else:
            eci = ExternalCompilationInfo()
        executable = self.platform.compile([c_file], eci)
        res = self.platform.execute(executable)
        assert res.out.startswith('4.0')
Example #21
0
 def test_simple_makefile(self):
     tmpdir = udir.join('simple_makefile' + self.__class__.__name__).ensure(dir=1)
     cfile = tmpdir.join('test_simple_enough.c')
     cfile.write('''
     #include <stdio.h>
     int main()
     {
         printf("42\\n");
         return 0;
     }
     ''')
     mk = self.platform.gen_makefile([cfile], ExternalCompilationInfo(),
                            path=tmpdir)
     mk.write()
     self.platform.execute_makefile(mk)
     res = self.platform.execute(tmpdir.join('test_simple_enough'))
     assert res.out == '42\n'
     if self.strict_on_stderr:
         assert res.err == ''
     assert res.returncode == 0
     assert '-lrt' in tmpdir.join("Makefile").read()
Example #22
0
    def test_64_32_results(self):
        if platform.machine() != 'i386':
            py.test.skip("i386 only")
        plat32 = Darwin_i386()
        plat64 = Darwin_x86_64()
        cfile = udir.join('test_int_size.c')
        cfile.write(r'''
        #include <stdio.h>
        #include <limits.h>

        int main() {
                printf("%d\n", INT_MAX < LONG_MAX);
                return 0;
        }
        ''')
        eci = ExternalCompilationInfo()
        executable = plat32.compile([cfile], eci)
        res = plat32.execute(executable)
        self.check_res(res, '0\n')
        if host_factory == Darwin_x86_64:
            executable = plat64.compile([cfile], eci)
            res = plat64.execute(executable)
            self.check_res(res, '1\n')
Example #23
0
    def test_frameworks(self):
        objcfile = udir.join('test_simple.m')
        objcfile.write(r'''
        #import <Foundation/Foundation.h>
        #include "test.h"

        int main (int argc, const char * argv[]) {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            NSArray *args = [[NSProcessInfo processInfo] arguments];
            NSCountedSet *cset = [[NSCountedSet alloc] initWithArray:args];

            printf("%d\n", XXX_STUFF);

            [cset release];
            [pool release];
            return 0;
        }
        ''')
        includedir = py.path.local(__file__).dirpath().join('include')
        eci = ExternalCompilationInfo(frameworks=('Cocoa',),
                                      include_dirs=(includedir,))
        executable = self.platform.compile([objcfile], eci)
        res = self.platform.execute(executable)
        self.check_res(res)
Example #24
0
from platformer import platform, ExternalCompilationInfo, udir

eci = ExternalCompilationInfo(includes=["math.h"])
c_file = udir.join('x.c')
c_file.write('int main() { printf("%f\\n", pow(2.0, 3.0)); return 0; }')
exe_file = platform.compile([c_file], eci)
res = platform.execute(exe_file)
assert res.returncode == 0
assert res.out.startswith('8.0')
Example #25
0
 def test_link_files(self):
     tmpdir = udir.join('link_files' + self.__class__.__name__).ensure(dir=1)
     eci = ExternalCompilationInfo(link_files=['/foo/bar.a'])
     mk = self.platform.gen_makefile(['blip.c'], eci, path=tmpdir)
     mk.write()
     assert 'LINKFILES = /foo/bar.a' in tmpdir.join('Makefile').read()