[ci skip] Add note that this change may break SetOption() + ninja usage with fix
[scons.git] / test / LoadableModule.py
blob18a9c43c98a332d4c57935ccf093cfd68cb82ad7
1 #!/usr/bin/env python
3 # __COPYRIGHT__
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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27 import os
28 import sys
30 import TestCmd
31 import TestSCons
33 dll_ = TestSCons.dll_
34 _dll = TestSCons._dll
36 test = TestSCons.TestSCons()
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'])"
42 dlopen_line = {
43 'darwin' : no_dl_lib,
44 'freebsd4' : no_dl_lib,
45 'linux2' : use_dl_lib,
46 'linux3' : use_dl_lib,
48 platforms_with_dlopen = list(dlopen_line.keys())
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, ''))
57 test.write('f1.c', r"""
58 #include <stdio.h>
60 void
61 f1(void)
63 printf("f1.c\n");
64 fflush(stdout);
66 """)
68 dlopenprog = r"""
69 #include <errno.h>
70 #include <stdio.h>
71 #include <dlfcn.h>
73 extern int errno;
75 int
76 main(int argc, char *argv[])
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;
86 void (*f1)() = dlsym(foo1_shobj, "f1\0");
87 (*f1)();
88 printf("dlopenprog.c\n");
89 dlclose(foo1_shobj);
90 return 0;
92 """
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)
98 test.write('dlopenprog.c',
99 dlopenprog.replace('__foo1_name__', foo1_name))
101 test.run(arguments = '.',
102 stderr=TestSCons.noisy_ar,
103 match=TestSCons.match_re_dotall)
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"
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")
120 test.pass_test()
122 # Local Variables:
123 # tab-width:4
124 # indent-tabs-mode:nil
125 # End:
126 # vim: set expandtab tabstop=4 shiftwidth=4: