Merged revisions 2725-2865 via svnmerge from
[scons.git] / test / LoadableModule.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 string
29 import sys
30
31 import TestCmd
32 import TestSCons
33
34 dll_ = TestSCons.dll_
35 _dll = TestSCons._dll
36
37 test = TestSCons.TestSCons()
38
39 # Some systems apparently need -ldl on the link line, others don't.
40 no_dl_lib = "env.Program(target = 'dlopenprog', source = 'dlopenprog.c')"
41 use_dl_lib = "env.Program(target = 'dlopenprog', source = 'dlopenprog.c', LIBS=['dl'])"
42
43 dlopen_line = {
44     'darwin' : no_dl_lib,
45     'darwin8' : no_dl_lib,   # ONLY NEEDED FOR 1.5.2
46     'freebsd4' : no_dl_lib,
47     'linux2' : use_dl_lib,
48 }
49 platforms_with_dlopen = dlopen_line.keys()
50
51 test.write('SConstruct', """
52 env = Environment()
53 # dlopenprog tries to dynamically load foo1 at runtime using dlopen().
54 env.LoadableModule(target = 'foo1', source = 'f1.c')
55 """ + dlopen_line.get(sys.platform, ''))
56
57
58 test.write('f1.c', r"""
59 #include <stdio.h>
60
61 void
62 f1(void)
63 {
64         printf("f1.c\n");
65         fflush(stdout);
66 }
67 """)
68
69 dlopenprog = r"""
70 #include <errno.h>
71 #include <stdio.h>
72 #include <dlfcn.h>
73
74 extern int errno;
75
76 int
77 main(int argc, char *argv[])
78 {
79         argv[argc++] = "--";
80         void *foo1_shobj = dlopen("__foo1_name__", RTLD_NOW);
81         if(!foo1_shobj){
82           printf("Error loading foo1 '__foo1_name__' library at runtime, exiting.\n");
83           printf("%d\n", errno);
84           perror("");
85           return -1;
86         }
87         void (*f1)() = dlsym(foo1_shobj, "f1\0");
88         (*f1)();
89         printf("dlopenprog.c\n");
90         dlclose(foo1_shobj);
91         return 0;
92 }
93 """
94
95 # Darwin dlopen()s a bundle named "foo1",
96 # other systems dlopen() a traditional libfoo1.so file.
97 foo1_name = {'darwin' : 'foo1'}.get(sys.platform[:6], dll_+'foo1'+_dll)
98
99 test.write('dlopenprog.c',
100            string.replace(dlopenprog, '__foo1_name__', foo1_name))
101
102 test.run(arguments = '.',
103          stderr=TestSCons.noisy_ar,
104          match=TestSCons.match_re_dotall)
105
106 if string.find(sys.platform, 'darwin') != -1:
107     test.run(program='/usr/bin/file',
108              arguments = "foo1",
109              match = TestCmd.match_re,
110              stdout="foo1: Mach-O bundle (ppc|i386)\n")
111
112 if sys.platform in platforms_with_dlopen:
113     os.environ['LD_LIBRARY_PATH'] = test.workpath()
114     test.run(program = test.workpath('dlopenprog'),
115              stdout = "f1.c\ndlopenprog.c\n")
116                  
117
118
119 test.pass_test()