summaryrefslogtreecommitdiff
path: root/tools/build/test/use_requirements.py
blob: 7fe829c0bd1f13bc41b1f2b4007377cf4fae2684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/python

# Copyright 2003 Dave Abrahams
# Copyright 2002, 2003, 2004, 2006 Vladimir Prus
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

import BoostBuild

t = BoostBuild.Tester(use_test_config=False)


# Test that usage requirements on main targets work (and are propagated all the
# way up, and not only to direct dependants).
t.write("jamroot.jam", "")

# Note: 'lib cc ..', not 'lib c'. If using 'lib c: ...' the HP-CXX linker will
# confuse it with the system C runtime.
t.write("jamfile.jam", """\
lib b : b.cpp : <link>shared:<define>SHARED_B : :
    <define>FOO <link>shared:<define>SHARED_B ;
lib cc : c.cpp b ;
exe a : a.cpp cc ;
""")

t.write("b.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
#endif
foo() {}
""")

t.write("c.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
#endif
create_lib_please() {}
""")

t.write("a.cpp", """\
#ifdef FOO
void
# if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
# endif
foo() {}
#endif
int main() { foo(); }
""")

t.run_build_system()
t.run_build_system(["--clean"])


# Test that use requirements on main target work, when they are referred using
# 'dependency' features.

t.write("jamfile.jam", """\
lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
    <link>shared:<define>SHARED_B ;
exe a : a.cpp : <use>b ;
""")

t.write("b.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
#endif
foo() {}
""")

t.write("a.cpp", """\
#ifdef FOO
int main() {}
#endif
""")

t.run_build_system()
t.run_build_system(["--clean"])


# Test that usage requirements on a project work.
t.write("jamfile.jam", "exe a : a.cpp lib//b ;")

t.write("lib/jamfile.jam", """\
project
   : requirements <link>shared:<define>SHARED_B
   : usage-requirements <define>FOO <link>shared:<define>SHARED_B ;
lib b : b.cpp ;
""")

t.write("lib/b.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
#endif
foo() {}
""")

t.run_build_system()


# Test that use requirements are inherited correctly.
t.write("jamfile.jam", "exe a : a.cpp lib/1//b ;")

t.write("a.cpp", """\
#if defined(FOO) && defined(ZOO)
void foo() {}
#endif
int main() { foo(); }
""")

t.write("lib/jamfile.jam", """\
project : requirements : usage-requirements <define>FOO ;
""")

t.write("lib/1/jamfile.jam", """\
project
   : requirements <link>shared:<define>SHARED_B
   : usage-requirements <define>ZOO <link>shared:<define>SHARED_B ;
lib b : b.cpp ;
""")

t.write("lib/1/b.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
#endif
foo() {}
""")

t.run_build_system()
t.run_build_system(["--clean"])


# Test that we correctly handle dependency features in usage requirements on
# target.
t.write("jamfile.jam", """\
lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
    <link>shared:<define>SHARED_B ;

# Here's the test: we should correctly handle dependency feature and get usage
# requirements from 'b'.
lib cc : c.cpp : <link>shared:<define>SHARED_C : : <library>b ;

# This will build only if <define>FOO was propagated from 'c'.
exe a : a.cpp cc ;
""")

t.write("a.cpp", """\
#ifdef FOO
void
# if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
# endif
foo();
#endif

int main() { foo(); }
""")

t.write("c.cpp", """\
int
#if defined(_WIN32) && defined(SHARED_C)
__declspec(dllexport)
#endif
must_export_something;
""")

t.run_build_system()
t.run_build_system(["--clean"])


# Test correct handling of dependency features in project requirements.
t.write("jamfile.jam", "exe a : a.cpp lib1//cc ;")

t.write("lib1/jamfile.jam", """\
project
    : requirements <link>shared:<define>SHARED_C
    : usage-requirements <library>../lib2//b <link>shared:<define>SHARED_C ;
lib cc : c.cpp ;
""")

t.write("lib1/c.cpp", """\
int
#if defined(_WIN32) && defined(SHARED_C)
__declspec(dllexport)
#endif
must_export_something;
""")

t.write("lib2/jamfile.jam", """\
lib b : b.cpp : <link>shared:<define>SHARED_B : : <define>FOO
    <link>shared:<define>SHARED_B ;
""")

t.copy("b.cpp", "lib2/b.cpp")

t.run_build_system()


# Test that targets listed in dependency features in usage requirements are
# built with the correct properties.
t.rm(".")

t.write("jamroot.jam", "")
t.write("jamfile.jam", """\
lib main : main.cpp : <use>libs//lib1 : : <library>libs//lib1 ;
exe hello : hello.cpp main : ;
""")

t.write("main.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_LIB1)
__declspec(dllimport)
#endif
foo();

int main() { foo(); }
""")

t.write("hello.cpp", "\n")
t.write("libs/a.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_LIB1)
__declspec(dllexport)
#endif
foo() {}
""")


# This library should be built with the same properties as 'main'. This is a
# regression test for a bug when they were generated with empty properties, and
# there were ambiguities between variants.
t.write("libs/jamfile.jam", """\
lib lib1 : a_d.cpp : <variant>debug <link>shared:<define>SHARED_LIB1 : :
    <link>shared:<define>SHARED_LIB1 ;
lib lib1 : a.cpp : <variant>release <link>shared:<define>SHARED_LIB1 : :
    <link>shared:<define>SHARED_LIB1 ;
""")

t.write("libs/a_d.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_LIB1)
__declspec(dllexport)
#endif
foo() {}
""")

t.run_build_system(["link=static"])
t.expect_addition("libs/bin/$toolset/debug/link-static/a_d.obj")


# Test that indirect conditionals are respected in usage requirements.
t.rm(".")

t.write("jamroot.jam", """\
rule has-foo ( properties * ) { return <define>HAS_FOO ; }
exe a : a.cpp b ;
lib b : b.cpp : <link>static : : <conditional>@has-foo ;
""")

t.write("a.cpp", """\
#ifdef HAS_FOO
void foo();
int main() { foo(); }
#endif
""")

t.write("b.cpp", """\
void
#if defined(_WIN32) && defined(SHARED_B)
__declspec(dllexport)
#endif
foo() {}
""")

t.run_build_system()
t.expect_addition("bin/$toolset/debug/a.exe")

t.cleanup()