summaryrefslogtreecommitdiff
path: root/test/test_cerbero_tools_osxuniversalgenerator.py
blob: 4fc7463724a1ef118a51c714243d1ad3b28eea11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# cerbero - a multi-platform build system for Open Source software
# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
import tempfile
import shutil
import os

from cerbero.config import Architecture
from cerbero.utils import shell
from cerbero.tools.osxuniversalgenerator import OSXUniversalGenerator
from cerbero.tools.osxrelocator import OSXRelocator


TEST_APP = '''\
#include<stdio.h>

extern int foo1(int r);

int main(int arg_count,char ** arg_values)
{
 printf("Hello World %%d\\n", foo1(1));
 return 0;
}'''


TEST_LIB = '''\

int foo1(int r);

int foo1(int r) {
  return r;
}'''


SHARED_LIBRARY = {
    Architecture.X86: 'Mach-O dynamically linked shared library i386',
    Architecture.X86_64: 'Mach-O 64-bit dynamically linked shared library x86_64'}
EXECUTABLE = {
    Architecture.X86: 'Mach-O executable i386',
    Architecture.X86_64: 'Mach-O 64-bit executable x86_64'}


class  OSXUniversalGeneratorTest(unittest.TestCase):

    def setUp(self):
        self.tmp = tempfile.mkdtemp()
        self._create_tree()

    def tearDown(self):
        shutil.rmtree(self.tmp)

    def _create_tree(self):
        for p in [Architecture.X86, Architecture.X86_64, Architecture.UNIVERSAL]:
            for d in ['bin', 'lib', 'share']:
                os.makedirs(os.path.join(self.tmp, p, d))
        self.tmp_sources = os.path.join(self.tmp, 'tmp')
        os.makedirs(self.tmp_sources)

    def _compile(self, arch):
        main_c = os.path.join(self.tmp_sources,  'main.c')
        foo_c = os.path.join(self.tmp_sources, 'foo.c')

        libdir = os.path.join(self.tmp, arch, 'lib')
        libfoo = os.path.join(libdir, 'libfoo.so')
        test_app = os.path.join(self.tmp, arch, 'bin', 'test_app')
        with open(main_c, 'w') as f:
            f.write(TEST_APP)
        with open(foo_c, 'w') as f:
            f.write(TEST_LIB)
        if arch == Architecture.X86:
            arch = 'i386'
        shell.call('gcc -arch %s -o %s -shared %s' % (arch, libfoo, foo_c))
        shell.call('gcc -arch %s -o %s %s -L%s -lfoo' %
                   (arch, test_app, main_c, libdir))


    def _get_file_type(self, path):
        cmd = 'file -bh %s'
        return shell.check_call(cmd % path)[:-1]

    def _check_compiled_files(self):
        for arch in [Architecture.X86, Architecture.X86_64]:
            res = self._get_file_type(
                    os.path.join(self.tmp, arch, 'lib', 'libfoo.so'))
            self.assertEquals(res, SHARED_LIBRARY[arch])
            res = self._get_file_type(
                    os.path.join(self.tmp, arch, 'bin', 'test_app'))
            self.assertEquals(res, EXECUTABLE[arch])

    def testMergeDirs(self):
        self._compile(Architecture.X86)
        self._compile(Architecture.X86_64)
        self._check_compiled_files()
        gen = OSXUniversalGenerator(
                os.path.join(self.tmp, Architecture.UNIVERSAL))
        gen.merge_dirs([
                os.path.join(self.tmp, Architecture.X86),
                os.path.join(self.tmp, Architecture.X86_64)])

        # bash-3.2$ file libfoo.so 
        # libfoo.so: Mach-O universal binary with 2 architectures
        # libfoo.so (for architecture i386):	Mach-O dynamically linked shared library i386
        # libfoo.so (for architecture x86_64):	Mach-O 64-bit dynamically linked shared library x86_64

        ftype = self._get_file_type(os.path.join(self.tmp,
            Architecture.UNIVERSAL, 'lib', 'libfoo.so'))
        for arch in [Architecture.X86, Architecture.X86_64]:
            self.assertTrue(SHARED_LIBRARY[arch] in ftype)
        ftype = self._get_file_type(os.path.join(self.tmp,
            Architecture.UNIVERSAL, 'bin', 'test_app'))
        for arch in [Architecture.X86, Architecture.X86_64]:
            self.assertTrue(EXECUTABLE[arch] in ftype)

    def testMergeFiles(self):
        for arch in [Architecture.X86, Architecture.X86_64]:
            with open(os.path.join(self.tmp, arch, 'share', 'test'), 'w') as f:
                f.write("test")
        gen = OSXUniversalGenerator(
                os.path.join(self.tmp, Architecture.UNIVERSAL))
        gen.merge_files(['share/test'],
                [os.path.join(self.tmp, Architecture.X86),
                 os.path.join(self.tmp, Architecture.X86_64)])
        self.assertTrue(os.path.exists(os.path.join(self.tmp,
        Architecture.UNIVERSAL, 'share', 'test')))

    def testMergeCopyAndLink(self):
        for arch in [Architecture.X86, Architecture.X86_64]:
            file1 = os.path.join(self.tmp, arch, 'share', 'test1')
            file2 = os.path.join(self.tmp, arch, 'share', 'test2')
            with open(file1, 'w') as f:
                f.write("test")
            os.symlink(file1, file2)

        gen = OSXUniversalGenerator(
                os.path.join(self.tmp, Architecture.UNIVERSAL))
        gen.merge_dirs([
                os.path.join(self.tmp, Architecture.X86),
                os.path.join(self.tmp, Architecture.X86_64)])

        file1 = os.path.join(self.tmp, Architecture.UNIVERSAL, 'share', 'test1')
        file2 = os.path.join(self.tmp, Architecture.UNIVERSAL, 'share', 'test2')

        self.assertTrue(os.path.exists(file1))
        self.assertTrue(os.path.exists(file2))
        self.assertEquals(os.readlink(file2), file1)

    def testMergePCFiles(self):
        for arch in [Architecture.X86, Architecture.X86_64]:
            pc_file = os.path.join(self.tmp, arch, 'test.pc')
            with open(pc_file, 'w') as f:
                f.write(os.path.join(self.tmp, arch, 'lib', 'test'))
        gen = OSXUniversalGenerator(
                os.path.join(self.tmp, Architecture.UNIVERSAL))
        gen.merge_files(['test.pc'],
                [os.path.join(self.tmp, Architecture.X86),
                 os.path.join(self.tmp, Architecture.X86_64)])
        pc_file = os.path.join(self.tmp, Architecture.UNIVERSAL, 'test.pc')
        self.assertEquals(open(pc_file).readline(),
                os.path.join(self.tmp, Architecture.UNIVERSAL, 'lib', 'test'))

    def testMergedLibraryPaths(self):
        def check_prefix(path):
            if self.tmp not in path:
                return
            self.assertTrue(uni_prefix in path)
            self.assertTrue(x86_prefix not in path)
            self.assertTrue(x86_64_prefix not in path)

        self._compile(Architecture.X86)
        self._compile(Architecture.X86_64)
        self._check_compiled_files()
        uni_prefix = os.path.join(self.tmp, Architecture.UNIVERSAL)
        x86_prefix = os.path.join(self.tmp, Architecture.X86)
        x86_64_prefix = os.path.join(self.tmp, Architecture.X86_64)
        gen = OSXUniversalGenerator(uni_prefix)
        gen.merge_dirs([x86_prefix, x86_64_prefix])
        libfoo = os.path.join(self.tmp, Architecture.UNIVERSAL, 'lib', 'libfoo.so')
        libname = OSXRelocator.library_id_name(libfoo)
        check_prefix(libname)
        for p in OSXRelocator.list_shared_libraries(libfoo):
            check_prefix(p)