summaryrefslogtreecommitdiffstats
path: root/src/boost/tools/build/test/link.py
blob: e0524ef0e0506bb04ad03f1ccec7a6ec5bce88a6 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/python

# Copyright 2014-2015 Steven Watanabe
# 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)

# Tests the link-directory rule used to create the
# common boost/ directory in the new git layout.

import BoostBuild

def ignore_config(t):
    """These files are created by the configuration logic in link.jam
    They may or may not exist, depending on the system."""
    t.ignore("bin/symlink/test-hardlink")
    t.ignore("bin/test-hardlink-source")
    t.ignore("bin/test-symlink")
    t.ignore("bin/test-symlink-source")

def test_basic():
    """Test creation of a single link"""
    t = BoostBuild.Tester()
    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    """)

    t.write("src/dir1/include/file1.h", "file1")

    t.run_build_system()

    t.expect_addition("include/file1.h")
    t.expect_content("include/file1.h", "file1")
    ignore_config(t)
    t.expect_nothing_more()
    t.cleanup()

def test_merge_two():
    """Test merging two directories"""
    t = BoostBuild.Tester()
    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    """)

    t.write("src/dir1/include/file1.h", "file1")
    t.write("src/dir2/include/file2.h", "file2")

    t.run_build_system()

    t.expect_addition("include/file1.h")
    t.expect_content("include/file1.h", "file1")
    t.expect_addition("include/file2.h")
    t.expect_content("include/file2.h", "file2")
    ignore_config(t)
    t.expect_nothing_more()
    t.cleanup()

def test_merge_existing(group1, group2):
    """Test adding a link when a different symlink already exists"""
    t = BoostBuild.Tester()
    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    """)

    t.write("src/dir1/include/file1.h", "file1")
    t.write("src/dir2/include/file2.h", "file2")

    t.run_build_system(group1)

    if "dir1-link" in group1:
        t.expect_addition("include/file1.h")
        t.expect_content("include/file1.h", "file1")
    if "dir2-link" in group1:
        t.expect_addition("include/file2.h")
        t.expect_content("include/file2.h", "file2")
    ignore_config(t)
    t.expect_nothing_more()

    t.run_build_system(group2)

    if "dir1-link" in group2:
        if "dir1-link" not in group1:
            t.expect_addition("include/file1.h")
        else:
            # When a directory is split the link needs to be recreated.
            # On Windows, the test system checks the mod time of the
            # link rather than the link target, so this may be seen as
            # an update.
            t.ignore_touch("include/file1.h")
        t.expect_content("include/file1.h", "file1")
    else:
        t.ignore_removal("include/file1.h")
        
    if "dir2-link" in group2:
        if "dir2-link" not in group1:
            t.expect_addition("include/file2.h")
        else:
            t.ignore_touch("include/file2.h")
        t.expect_content("include/file2.h", "file2")
    else:
        t.ignore_removal("include/file2.h")
    ignore_config(t)
    t.expect_nothing_more()

    t.cleanup()

def test_merge_existing_all():
    test_merge_existing(["dir1-link"], ["dir2-link"])
    test_merge_existing(["dir2-link"], ["dir1-link"])
    test_merge_existing(["dir1-link"], ["dir1-link", "dir2-link"])
    test_merge_existing(["dir2-link"], ["dir1-link", "dir2-link"])

def test_merge_recursive():
    "Test merging several directories including common prefixes"
    t = BoostBuild.Tester()
    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    link-directory dir3-link : src/dir3/include : <location>. ;
    """)

    t.write("src/dir1/include/file1.h", "file1")
    t.write("src/dir2/include/file2.h", "file2")
    t.write("src/dir2/include/nested/file3.h", "file3")
    t.write("src/dir3/include/nested/file4.h", "file4")

    t.run_build_system()

    t.expect_addition("include/file1.h")
    t.expect_content("include/file1.h", "file1")
    t.expect_addition("include/file2.h")
    t.expect_content("include/file2.h", "file2")
    t.expect_addition("include/nested/file3.h")
    t.expect_content("include/nested/file3.h", "file3")
    t.expect_addition("include/nested/file4.h")
    t.expect_content("include/nested/file4.h", "file4")
    ignore_config(t)
    t.expect_nothing_more()

    t.cleanup()

def test_merge_recursive_existing(group1, group2):
    "Test merging several directories including common prefixes."
    t = BoostBuild.Tester()
    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    link-directory dir3-link : src/dir3/include : <location>. ;
    link-directory dir4-link : src/dir4/include : <location>. ;
    link-directory dir5-link : src/dir5/include : <location>. ;
    """)

    t.write("src/dir1/include/file1.h", "file1")
    t.write("src/dir2/include/nested/file2.h", "file2")
    t.write("src/dir3/include/nested/file3.h", "file3")
    t.write("src/dir4/include/nested/xxx/yyy/file4.h", "file4")
    t.write("src/dir5/include/nested/xxx/yyy/file5.h", "file5")

    t.run_build_system(group1)
    t.run_build_system(group2 + ["-d+12"])

    def check_file(target, file):
        if target in group2:
            if target in group1:
                t.ignore_touch(file)
            else:
                t.expect_addition(file)

    check_file("dir1-link", "include/file1.h")
    check_file("dir2-link", "include/nested/file2.h")
    check_file("dir3-link", "include/nested/file3.h")
    check_file("dir4-link", "include/nested/xxx/yyy/file4.h")
    check_file("dir5-link", "include/nested/xxx/yyy/file5.h")
    ignore_config(t)
    t.expect_nothing_more()

    t.cleanup()

def test_merge_recursive_existing_all():
    # These should create a link
    test_merge_recursive_existing(["dir2-link"], ["dir2-link", "dir1-link"])
    test_merge_recursive_existing(["dir2-link"], ["dir1-link", "dir2-link"])
    # These should create a directory
    test_merge_recursive_existing(["dir2-link"], ["dir2-link", "dir3-link"])
    test_merge_recursive_existing(["dir2-link"], ["dir3-link", "dir2-link"])
    # It should work even if we have to create many intermediate subdirectories
    test_merge_recursive_existing(["dir4-link"], ["dir4-link", "dir5-link"])
    test_merge_recursive_existing(["dir4-link"], ["dir5-link", "dir4-link"])

def test_include_scan():
    """Make sure that the #include scanner finds the headers"""
    t = BoostBuild.Tester()
    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    obj test : test.cpp :
        <include>include
        <implicit-dependency>dir1-link
        <implicit-dependency>dir2-link ;
    """)

    t.write("src/dir1/include/file1.h", "#include <file2.h>\n")
    t.write("src/dir2/include/file2.h", "int f();\n")
    t.write("test.cpp", """\
    #include <file1.h>
    int main() { f(); }
    """);

    t.run_build_system(["test"])

    t.expect_addition("bin/$toolset/debug*/test.obj")

    t.run_build_system()
    t.expect_nothing_more()

    t.cleanup()

def test_include_scan_merge_existing():
    """Make sure that files are replaced if needed when merging in
    a new directory"""
    t = BoostBuild.Tester()

    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    obj test : test.cpp :
        <include>include
        <implicit-dependency>dir1-link
        <implicit-dependency>dir2-link ;
    """)

    t.write("src/dir1/include/file1.h", "int f();")
    t.write("src/dir2/include/file2.h", "#include <file1.h>")
    t.write("test.cpp", """\
    #include <file2.h>
    int main() { f(); }
    """)

    t.run_build_system(["dir2-link"])

    t.run_build_system(["test"])
    t.expect_addition("include/file1.h")
    t.expect_addition("bin/$toolset/debug*/test.obj")
    t.ignore_touch("include/file2.h")
    t.expect_nothing_more()

    t.cleanup()

def test_update_file_link(params1, params2):
    """Tests the behavior of updates when changing the link mode.
    The link needs to be updated iff the original was a copy."""
    t = BoostBuild.Tester()

    t.write("jamroot.jam", """\
    import link ;
    import project ;
    import property-set ;
    import modules ;

    if --no-symlinks in [ modules.peek : ARGV ]
    {
        modules.poke link : .can-symlink : false ;
    }

    if --no-hardlinks in [ modules.peek : ARGV ]
    {
        modules.poke link : .can-hardlink : false ;
    }

    .project = [ project.current ] ;
    .has-files = [ glob include/file1.h ] ;
    
    rule can-link ( properties * ) {
        if ( ! [ link.can-symlink $(.project) ] ) &&
           ( ! [ link.can-hardlink $(.project) ] )
        {
            ECHO links unsupported ;
        }
    }

    # Use two directories so that we link to individual files.
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    alias check-linking : : <conditional>@can-link ;
    """)
    t.write("src/dir1/include/file1.h", "file1")
    t.write("src/dir2/include/file2.h", "file2")

    t.run_build_system(params1)
    ignore_config(t)
    t.expect_addition("include/file1.h")
    t.expect_addition("include/file2.h")
    t.expect_nothing_more()

    using_links = "links unsupported" not in t.stdout()

    t.touch("src/dir1/include/file1.h")

    t.run_build_system(params2)
    if not using_links: t.expect_touch("include/file1.h")
    ignore_config(t)
    t.expect_nothing_more()

    t.cleanup()

def test_update_file_link_all():
    """Test all nine possible combinations of two runs."""
    possible_args = [[], ["--no-symlinks"], ["--no-symlinks", "--no-hardlinks"]]
    for arg1 in possible_args:
        for arg2 in possible_args:
            test_update_file_link(arg1, arg2)

def test_error_duplicate():
    """Test that linking a single file from
    multiple sources causes a hard error."""
    t = BoostBuild.Tester()

    t.write("jamroot.jam", """\
    import link ;
    link-directory dir1-link : src/dir1/include : <location>. ;
    link-directory dir2-link : src/dir2/include : <location>. ;
    """)

    t.write("src/dir1/include/file1.h", "file1")
    t.write("src/dir2/include/file1.h", "file2")

    t.run_build_system(status=1)
    t.expect_output_lines(
        ["error: Cannot create link include/file1.h to src/dir2/include/file1.h.",
         "error: Link previously defined to another file, src/dir1/include/file1.h."])

    t.cleanup()

test_basic()
test_merge_two()
test_merge_existing_all()
test_merge_recursive()
test_merge_recursive_existing_all()
test_include_scan()
test_include_scan_merge_existing()
test_update_file_link_all()
test_error_duplicate()