Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / Libs / SharedLibrary.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import os
28 import sys
29
30 import TestSCons
31
32 test = TestSCons.TestSCons()
33
34 test.write('SConstruct', """
35 import sys
36 env=Environment(WINDOWS_INSERT_DEF=1)
37 env2 = Environment(LIBS = [ 'foo1', 'foo2', 'foo3' ],
38                    LIBPATH = [ '.' ])
39 env.SharedLibrary(target = 'foo1', source = 'f1.c')
40 if sys.platform == 'win32':
41     env.StaticLibrary(target = 'foo1-static', source = 'f1.c')
42 else:
43     env.StaticLibrary(target = 'foo1', source = 'f1.c')
44 SharedLibrary(target = 'foo2',
45               source = Split('f2a.c f2b.c f2c.c'),
46               WINDOWS_INSERT_DEF = 1)
47 env.SharedLibrary(target = 'foo3', source = ['f3a.c', 'f3b.c', 'f3c.c'])
48 env2.Program(target = 'prog', source = 'prog.c')
49 """)
50
51 test.write('SConstructFoo', """
52 env=Environment()
53 obj = env.Object('foo', 'foo.c')
54 Default(env.SharedLibrary(target = 'foo', source = obj))
55 """)
56
57 test.write('SConstructFoo2', """
58 env=Environment()
59 obj = env.SharedObject('bar', 'foo.c')
60 Default(env.Library(target = 'foo', source = obj))
61 """)
62
63 test.write('foo.c', r"""
64 #include <stdio.h>
65
66 void
67 f1(void)
68 {
69         printf("foo.c\n");
70         fflush(stdout);
71 }
72 """)
73
74
75 test.write('f1.c', r"""
76 #include <stdio.h>
77
78 void
79 f1(void)
80 {
81         printf("f1.c\n");
82         fflush(stdout);
83 }
84 """)
85
86 test.write("foo1.def", r"""
87 LIBRARY        "foo1"
88 DESCRIPTION    "Foo1 Shared Library"
89
90 EXPORTS
91    f1
92 """)
93
94 test.write('f2a.c', r"""
95 #include <stdio.h>
96
97 void
98 f2a(void)
99 {
100         printf("f2a.c\n");
101 }
102 """)
103
104 test.write('f2b.c', r"""
105 #include <stdio.h>
106 void
107 f2b(void)
108 {
109         printf("f2b.c\n");
110 }
111 """)
112
113 test.write('f2c.c', r"""
114 #include <stdio.h>
115
116 void
117 f2c(void)
118 {
119         printf("f2c.c\n");
120         fflush(stdout);
121 }
122 """)
123
124 test.write("foo2.def", r"""
125 LIBRARY        "foo2"
126 DESCRIPTION    "Foo2 Shared Library"
127
128 EXPORTS
129    f2a
130    f2b
131    f2c
132 """)
133
134 test.write('f3a.c', r"""
135 #include <stdio.h>
136 void
137 f3a(void)
138 {
139         printf("f3a.c\n");
140 }
141 """)
142
143 test.write('f3b.c', r"""
144 #include <stdio.h>
145 void
146 f3b(void)
147 {
148         printf("f3b.c\n");
149 }
150 """)
151
152 test.write('f3c.c', r"""
153 #include <stdio.h>
154
155 void
156 f3c(void)
157 {
158         printf("f3c.c\n");
159         fflush(stdout);
160 }
161 """)
162
163 test.write("foo3.def", r"""
164 LIBRARY        "foo3"
165 DESCRIPTION    "Foo3 Shared Library"
166
167 EXPORTS
168    f3a
169    f3b
170    f3c
171 """)
172
173 test.write('prog.c', r"""
174 #include <stdio.h>
175 void f1(void);
176 void f2a(void);
177 void f2b(void);
178 void f2c(void);
179 void f3a(void);
180 void f3b(void);
181 void f3c(void);
182 int
183 main(int argc, char *argv[])
184 {
185         argv[argc++] = "--";
186         f1();
187         f2a();
188         f2b();
189         f2c();
190         f3a();
191         f3b();
192         f3c();
193         printf("prog.c\n");
194         return 0;
195 }
196 """)
197
198 test.run(arguments = '.',
199          stderr=TestSCons.noisy_ar,
200          match=TestSCons.match_re_dotall)
201
202 if os.name == 'posix':
203     os.environ['LD_LIBRARY_PATH'] = '.'
204 if sys.platform.find('irix') != -1:
205     os.environ['LD_LIBRARYN32_PATH'] = '.'
206
207 test.run(program = test.workpath('prog'),
208          stdout = "f1.c\nf2a.c\nf2b.c\nf2c.c\nf3a.c\nf3b.c\nf3c.c\nprog.c\n")
209
210 if sys.platform == 'win32' or sys.platform.find('irix') != -1:
211     test.run(arguments = '-f SConstructFoo')
212 else:
213     test.run(arguments = '-f SConstructFoo', status=2, stderr='''\
214 scons: \*\*\* \[.*\] Source file: foo\..* is static and is not compatible with shared target: .*
215 ''',
216     match=TestSCons.match_re_dotall)
217     # Run it again to make sure that we still get the error
218     # even though the static objects already exist.
219     test.run(arguments = '-f SConstructFoo', status=2, stderr='''\
220 scons: \*\*\* \[.*\] Source file: foo\..* is static and is not compatible with shared target: .*
221 ''',
222     match=TestSCons.match_re_dotall)
223
224 test.run(arguments = '-f SConstructFoo2',
225          stderr=TestSCons.noisy_ar,
226          match=TestSCons.match_re_dotall)
227
228 if sys.platform == 'win32':
229     # Make sure we don't insert a .def source file (when
230     # WINDOWS_INSERT_DEF is set) and a .lib target file if
231     # they're specified explicitly.
232
233     test.write('SConstructBar', '''
234 env = Environment(WINDOWS_INSERT_DEF=1)
235 env2 = Environment(LIBS = [ 'foo4' ],
236                    LIBPATH = [ '.' ])
237 env.SharedLibrary(target = ['foo4', 'foo4.lib'], source = ['f4.c', 'foo4.def'])
238 env2.Program(target = 'progbar', source = 'progbar.c')
239 ''')
240
241     test.write('f4.c', r"""
242 #include <stdio.h>
243
244 f4(void)
245 {
246         printf("f4.c\n");
247         fflush(stdout);
248 }
249 """)
250
251     test.write("foo4.def", r"""
252 LIBRARY        "foo4"
253 DESCRIPTION    "Foo4 Shared Library"
254
255 EXPORTS
256    f4
257 """)
258
259     test.write('progbar.c', r"""
260 #include <stdio.h>
261 void f4(void);
262 int
263 main(int argc, char *argv[])
264 {
265         argv[argc++] = "--";
266         f4();
267         printf("progbar.c\n");
268         return 0;
269 }
270 """)
271
272     test.run(arguments = '-f SConstructBar .')
273
274     # Make sure there is (at most) one mention each of the
275     # appropriate .def and .lib files per line.
276     for line in test.stdout().split('\n'):
277         test.fail_test(line.count('foo4.def') > 1)
278         test.fail_test(line.count('foo4.lib') > 1)
279
280     test.run(program = test.workpath('progbar'),
281              stdout = "f4.c\nprogbar.c\n")
282
283 test.pass_test()
284
285 # Local Variables:
286 # tab-width:4
287 # indent-tabs-mode:nil
288 # End:
289 # vim: set expandtab tabstop=4 shiftwidth=4: