Imported Upstream version 1.57.0
[platform/upstream/boost.git] / tools / build / test / use_requirements.py
1 #!/usr/bin/python
2
3 # Copyright 2003 Dave Abrahams
4 # Copyright 2002, 2003, 2004, 2006 Vladimir Prus
5 # Distributed under the Boost Software License, Version 1.0.
6 # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
7
8 import BoostBuild
9
10 t = BoostBuild.Tester(use_test_config=False)
11
12
13 # Test that usage requirements on main targets work (and are propagated all the
14 # way up, and not only to direct dependants).
15 t.write("jamroot.jam", "")
16
17 # Note: 'lib cc ..', not 'lib c'. If using 'lib c: ...' the HP-CXX linker will
18 # confuse it with the system C runtime.
19 t.write("jamfile.jam", """\
20 lib b : b.cpp : <link>shared:<define>SHARED_B : :
21     <define>FOO <link>shared:<define>SHARED_B ;
22 lib cc : c.cpp b ;
23 exe a : a.cpp cc ;
24 """)
25
26 t.write("b.cpp", """\
27 void
28 #if defined(_WIN32) && defined(SHARED_B)
29 __declspec(dllexport)
30 #endif
31 foo() {}
32 """)
33
34 t.write("c.cpp", """\
35 void
36 #if defined(_WIN32) && defined(SHARED_B)
37 __declspec(dllexport)
38 #endif
39 create_lib_please() {}
40 """)
41
42 t.write("a.cpp", """\
43 #ifdef FOO
44 void
45 # if defined(_WIN32) && defined(SHARED_B)
46 __declspec(dllexport)
47 # endif
48 foo() {}
49 #endif
50 int main() { foo(); }
51 """)
52
53 t.run_build_system()
54 t.run_build_system(["--clean"])
55
56
57 # Test that use requirements on main target work, when they are referred using
58 # 'dependency' features.
59
60 t.write("jamfile.jam", """\
61 lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
62     <link>shared:<define>SHARED_B ;
63 exe a : a.cpp : <use>b ;
64 """)
65
66 t.write("b.cpp", """\
67 void
68 #if defined(_WIN32) && defined(SHARED_B)
69 __declspec(dllexport)
70 #endif
71 foo() {}
72 """)
73
74 t.write("a.cpp", """\
75 #ifdef FOO
76 int main() {}
77 #endif
78 """)
79
80 t.run_build_system()
81 t.run_build_system(["--clean"])
82
83
84 # Test that usage requirements on a project work.
85 t.write("jamfile.jam", "exe a : a.cpp lib//b ;")
86
87 t.write("lib/jamfile.jam", """\
88 project
89    : requirements <link>shared:<define>SHARED_B
90    : usage-requirements <define>FOO <link>shared:<define>SHARED_B ;
91 lib b : b.cpp ;
92 """)
93
94 t.write("lib/b.cpp", """\
95 void
96 #if defined(_WIN32) && defined(SHARED_B)
97 __declspec(dllexport)
98 #endif
99 foo() {}
100 """)
101
102 t.run_build_system()
103
104
105 # Test that use requirements are inherited correctly.
106 t.write("jamfile.jam", "exe a : a.cpp lib/1//b ;")
107
108 t.write("a.cpp", """\
109 #if defined(FOO) && defined(ZOO)
110 void foo() {}
111 #endif
112 int main() { foo(); }
113 """)
114
115 t.write("lib/jamfile.jam", """\
116 project : requirements : usage-requirements <define>FOO ;
117 """)
118
119 t.write("lib/1/jamfile.jam", """\
120 project
121    : requirements <link>shared:<define>SHARED_B
122    : usage-requirements <define>ZOO <link>shared:<define>SHARED_B ;
123 lib b : b.cpp ;
124 """)
125
126 t.write("lib/1/b.cpp", """\
127 void
128 #if defined(_WIN32) && defined(SHARED_B)
129 __declspec(dllexport)
130 #endif
131 foo() {}
132 """)
133
134 t.run_build_system()
135 t.run_build_system(["--clean"])
136
137
138 # Test that we correctly handle dependency features in usage requirements on
139 # target.
140 t.write("jamfile.jam", """\
141 lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
142     <link>shared:<define>SHARED_B ;
143
144 # Here's the test: we should correctly handle dependency feature and get usage
145 # requirements from 'b'.
146 lib cc : c.cpp : <link>shared:<define>SHARED_C : : <library>b ;
147
148 # This will build only if <define>FOO was propagated from 'c'.
149 exe a : a.cpp cc ;
150 """)
151
152 t.write("a.cpp", """\
153 #ifdef FOO
154 void
155 # if defined(_WIN32) && defined(SHARED_B)
156 __declspec(dllexport)
157 # endif
158 foo();
159 #endif
160
161 int main() { foo(); }
162 """)
163
164 t.write("c.cpp", """\
165 int
166 #if defined(_WIN32) && defined(SHARED_C)
167 __declspec(dllexport)
168 #endif
169 must_export_something;
170 """)
171
172 t.run_build_system()
173 t.run_build_system(["--clean"])
174
175
176 # Test correct handling of dependency features in project requirements.
177 t.write("jamfile.jam", "exe a : a.cpp lib1//cc ;")
178
179 t.write("lib1/jamfile.jam", """\
180 project
181     : requirements <link>shared:<define>SHARED_C
182     : usage-requirements <library>../lib2//b <link>shared:<define>SHARED_C ;
183 lib cc : c.cpp ;
184 """)
185
186 t.write("lib1/c.cpp", """\
187 int
188 #if defined(_WIN32) && defined(SHARED_C)
189 __declspec(dllexport)
190 #endif
191 must_export_something;
192 """)
193
194 t.write("lib2/jamfile.jam", """\
195 lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
196     <link>shared:<define>SHARED_B ;
197 """)
198
199 t.copy("b.cpp", "lib2/b.cpp")
200
201 t.run_build_system()
202
203
204 # Test that targets listed in dependency features in usage requirements are
205 # built with the correct properties.
206 t.rm(".")
207
208 t.write("jamroot.jam", "")
209 t.write("jamfile.jam", """\
210 lib main : main.cpp : <use>libs//lib1 : : <library>libs//lib1 ;
211 exe hello : hello.cpp main : ;
212 """)
213
214 t.write("main.cpp", """\
215 void
216 #if defined(_WIN32) && defined(SHARED_LIB1)
217 __declspec(dllimport)
218 #endif
219 foo();
220
221 int main() { foo(); }
222 """)
223
224 t.write("hello.cpp", "\n")
225 t.write("libs/a.cpp", """\
226 void
227 #if defined(_WIN32) && defined(SHARED_LIB1)
228 __declspec(dllexport)
229 #endif
230 foo() {}
231 """)
232
233
234 # This library should be built with the same properties as 'main'. This is a
235 # regression test for a bug when they were generated with empty properties, and
236 # there were ambiguities between variants.
237 t.write("libs/jamfile.jam", """\
238 lib lib1 : a_d.cpp : <variant>debug <link>shared:<define>SHARED_LIB1 : :
239     <link>shared:<define>SHARED_LIB1 ;
240 lib lib1 : a.cpp : <variant>release <link>shared:<define>SHARED_LIB1 : :
241     <link>shared:<define>SHARED_LIB1 ;
242 """)
243
244 t.write("libs/a_d.cpp", """\
245 void
246 #if defined(_WIN32) && defined(SHARED_LIB1)
247 __declspec(dllexport)
248 #endif
249 foo() {}
250 """)
251
252 t.run_build_system(["link=static"])
253 t.expect_addition("libs/bin/$toolset/debug/link-static/a_d.obj")
254
255
256 # Test that indirect conditionals are respected in usage requirements.
257 t.rm(".")
258
259 t.write("jamroot.jam", """\
260 rule has-foo ( properties * ) { return <define>HAS_FOO ; }
261 exe a : a.cpp b ;
262 lib b : b.cpp : <link>static : : <conditional>@has-foo ;
263 """)
264
265 t.write("a.cpp", """\
266 #ifdef HAS_FOO
267 void foo();
268 int main() { foo(); }
269 #endif
270 """)
271
272 t.write("b.cpp", """\
273 void
274 #if defined(_WIN32) && defined(SHARED_B)
275 __declspec(dllexport)
276 #endif
277 foo() {}
278 """)
279
280 t.run_build_system()
281 t.expect_addition("bin/$toolset/debug/a.exe")
282
283 t.cleanup()