Added fix for TeX includes with same name as subdirs.
[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 sys
29
30 import TestCmd
31 import TestSCons
32
33 dll_ = TestSCons.dll_
34 _dll = TestSCons._dll
35
36 test = TestSCons.TestSCons()
37
38 # Some systems apparently need -ldl on the link line, others don't.
39 no_dl_lib = "env.Program(target = 'dlopenprog', source = 'dlopenprog.c')"
40 use_dl_lib = "env.Program(target = 'dlopenprog', source = 'dlopenprog.c', LIBS=['dl'])"
41
42 dlopen_line = {
43     'darwin' : no_dl_lib,
44     'darwin8' : no_dl_lib,   # ONLY NEEDED FOR 1.5.2
45     'freebsd4' : no_dl_lib,
46     'linux2' : use_dl_lib,
47 }
48 platforms_with_dlopen = dlopen_line.keys()
49
50 test.write('SConstruct', """
51 env = Environment()
52 # dlopenprog tries to dynamically load foo1 at runtime using dlopen().
53 env.LoadableModule(target = 'foo1', source = 'f1.c')
54 """ + dlopen_line.get(sys.platform, ''))
55
56
57 test.write('f1.c', r"""
58 #include <stdio.h>
59
60 void
61 f1(void)
62 {
63         printf("f1.c\n");
64         fflush(stdout);
65 }
66 """)
67
68 dlopenprog = r"""
69 #include <errno.h>
70 #include <stdio.h>
71 #include <dlfcn.h>
72
73 extern int errno;
74
75 int
76 main(int argc, char *argv[])
77 {
78         argv[argc++] = "--";
79         void *foo1_shobj = dlopen("__foo1_name__", RTLD_NOW);
80         if(!foo1_shobj){
81           printf("Error loading foo1 '__foo1_name__' library at runtime, exiting.\n");
82           printf("%d\n", errno);
83           perror("");
84           return -1;
85         }
86         void (*f1)() = dlsym(foo1_shobj, "f1\0");
87         (*f1)();
88         printf("dlopenprog.c\n");
89         dlclose(foo1_shobj);
90         return 0;
91 }
92 """
93
94 # Darwin dlopen()s a bundle named "foo1",
95 # other systems dlopen() a traditional libfoo1.so file.
96 foo1_name = {'darwin' : 'foo1'}.get(sys.platform[:6], dll_+'foo1'+_dll)
97
98 test.write('dlopenprog.c',
99            dlopenprog.replace('__foo1_name__', foo1_name))
100
101 test.run(arguments = '.',
102          stderr=TestSCons.noisy_ar,
103          match=TestSCons.match_re_dotall)
104
105 # TODO: Add new Intel-based Macs?  Why are we only picking on Macs?
106 #if sys.platform.find('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 # My laptop prints "foo1: Mach-O 64-bit bundle x86_64"
112
113 if sys.platform in platforms_with_dlopen:
114     os.environ['LD_LIBRARY_PATH'] = test.workpath()
115     test.run(program = test.workpath('dlopenprog'),
116              stdout = "f1.c\ndlopenprog.c\n")
117                  
118
119
120 test.pass_test()
121
122 # Local Variables:
123 # tab-width:4
124 # indent-tabs-mode:nil
125 # End:
126 # vim: set expandtab tabstop=4 shiftwidth=4: