summaryrefslogtreecommitdiffstats
path: root/src/boost/tools/build/test/builtin_glob_archive.py
blob: 38c97e22ed1baaa186998e6da14abc5fe17ae011 (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
#!/usr/bin/python

# Copyright 2014 Steven Watanabe
# Copyright 2015 Artur Shepilko
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)

# This tests the GLOB_ARCHIVE rule.

import os
import sys
try:
    from StringIO import StringIO
except:
    from io import StringIO
import BoostBuild

vms = ( os.name == 'posix' and sys.platform == 'OpenVMS')

t = BoostBuild.Tester()

## Setup test archive sources and symbols they contain.
sources = {
    "a.cpp" : ["a"],
    "b.cpp" : ["b"],
    "b_match.cpp" : ["b_match"],
    "c/nopath_check.cpp" : ["nopath_check"],
    "CaseCheck.cpp" : ["CaseCheck"],
    "seq_check1.cpp" : ["seq_check1"],
    "seq_check2.cpp" : ["seq_check2"],
    "seq_check3.cpp" : ["seq_check3"],
    "symbols_check.c" : ["symbol", "symbol_match"],
    "members_and_symbols_check.c" : ["member_and_symbol_match"],
    "symbol_case_check.c" : ["SymbolCaseCheck"],
    "main_check.cpp" : ["main"]
}


def create_sources(path, sources):
    for s in sources :
        f = os.path.join(path, s)
        t.write(f, "")
        output = StringIO()
        for sym in sources[s] :
            output.write("int %s() { return 0; }\n" % sym)
        t.write(f, output.getvalue())


def setup_archive(name, sources):
    global archive
    global obj_suffix
    archive = t.adjust_names(name)[0]
    obj_suffix = t.adjust_names(".obj")[0]
    output = StringIO()
    t.write("jamroot.jam","")
    output.write("""\
static-lib %s :
""" % name.split(".")[0])
    ## sort the sources, so we can test order of the globbed members
    for s in sorted(sources) :
        output.write("""\
    %s
""" % s)
    output.write("""\
    ;
""")
    t.write("lib/jamfile.jam", output.getvalue())
    create_sources("lib", sources)
    t.run_build_system(subdir="lib")
    built_archive = "lib/bin/$toolset/debug*/%s" % name
    t.expect_addition(built_archive)
    t.copy(built_archive, name)
    t.rm("lib")


def test_glob_archive(archives, glob, expected, sort_results = False):
    output = StringIO()
    ## replace placeholders
    glob = glob.replace("$archive1", archives[0]).replace("$obj", obj_suffix)
    expected = [ m.replace("$archive1",
               archives[0]).replace("$obj", obj_suffix) for m in expected ]
    if len(archives) > 1 :
        glob = glob.replace("$archive2", archives[1]).replace("$obj", obj_suffix)
        expected = [ m.replace("$archive2",
               archives[1]).replace("$obj", obj_suffix) for m in expected ]
    ## create test jamfile
    if sort_results : glob = "[ SORT %s ]" % glob
    output.write("""\
    for local p in %s
    {
        ECHO $(p) ;
    }
    UPDATE ;
    """ % glob)
    t.write("file.jam", output.getvalue())
    ## run test jamfile and match against expected results
    if sort_results : expected.sort()
    t.run_build_system(["-ffile.jam"], stdout="\n".join(expected + [""]))
    t.rm("file.jam")


## RUN TESTS
setup_archive("auxilliary1.lib", sources)
archive1 = archive
setup_archive("auxilliary2.lib", sources)
archive2 = archive

## all arguments empty
test_glob_archive([archive1], "[ GLOB_ARCHIVE ]", [])

## empty query
test_glob_archive([archive1], "[ GLOB_ARCHIVE $archive1 : ]", [])

## no-match
test_glob_archive([archive1], "[ GLOB_ARCHIVE $archive1 : a ]", [])

## match exact
test_glob_archive([archive1], "[ GLOB_ARCHIVE $archive1 : a$obj ]",
                  ["$archive1(a$obj)"])

## glob wildcards:1
test_glob_archive([archive1], "[ GLOB_ARCHIVE $archive1 : b.* ]",
                  ["$archive1(b$obj)"])

## glob wildcards:2
test_glob_archive([archive1], '[ GLOB_ARCHIVE $archive1 : "\\b?match[\.]*" ]',
                  ["$archive1(b_match$obj)"])

## glob wildcards:3
test_glob_archive([archive1], "[ SORT [ GLOB_ARCHIVE $archive1 : b* ] ]",
                  ["$archive1(b$obj)", "$archive1(b_match$obj)"])

## glob multiple patterns with multiple results.
test_glob_archive([archive1], "[ SORT [ GLOB_ARCHIVE $archive1 : b.* b_* ] ]",
                  ["$archive1(b$obj)", "$archive1(b_match$obj)"])

## glob multiple archives and patterns.
test_glob_archive([archive1, archive2],
                  "[ SORT [ GLOB_ARCHIVE $archive1 $archive2 : b.* b_* ] ]",
                  ["$archive1(b$obj)", "$archive1(b_match$obj)",
                   "$archive2(b$obj)", "$archive2(b_match$obj)"])

## glob same archive multiple times.
test_glob_archive([archive1, archive1],
                  "[ GLOB_ARCHIVE $archive1 $archive2 $archive1 : b.* ]",
                   ["$archive1(b$obj)", "$archive2(b$obj)", "$archive1(b$obj)"])

## returned archive member has no path, even though its source object-file did.
## this is rather NT-specific, where members also store their object-file's path.
test_glob_archive([archive1], "[ GLOB_ARCHIVE $archive1 : nopath_check$obj ]",
                  ["$archive1(nopath_check$obj)"])

## case insensitive matching, when archives support case sensitive member names.
## VMS implementation forces case-insensitive matching and downcased member names.

case_sensitive_members = ( not vms )

if case_sensitive_members:
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : casecheck$obj : true ]",
                      ["$archive1(CaseCheck$obj)"])
elif vms:
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : CaseCheck$obj : false ]",
                      ["$archive1(casecheck$obj)"])


## test the order of matched members, in general it should match the
## insertion sequence.
test_glob_archive([archive1], "[ SORT [ GLOB_ARCHIVE $archive1 : seq_check*$obj ] ]",
                  ["$archive1(seq_check1$obj)", "$archive1(seq_check2$obj)",
                   "$archive1(seq_check3$obj)"])


## glob members by symbols they contain.
## Currently supported only on VMS.
symbol_glob_supported = ( vms )

if symbol_glob_supported :
    ## NOTE: generated symbols are compiler-dependent and may be specifically
    ## mangled (as in C++ case), so globbing by exact symbol is non-trivial.
    ## However, C-generated symbols are likely to have more portable names,
    ## so for the glob-by-symbol tests we glob C-generated archive members.

    ## glob members by exact symbol.
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : : : symbol ]",
                      ["$archive1(symbols_check$obj)"])

    ## glob members by symbol wildcard.
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : : : symbol_* ]",
                      ["$archive1(symbols_check$obj)"])

    ## glob members by member pattern AND symbol pattern.
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : *symbol* : : *member* ]",
                      ["$archive1(members_and_symbols_check$obj)"])

    ## case insensitive symbol glob.
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : : true : symbolcasecheck ]",
                      ["$archive1(symbol_case_check$obj)"])

    ## glob member that contains main symbol.
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : : : main _main ]",
                      ["$archive1(main_check$obj)"])

else:
    test_glob_archive([archive1],
                      "[ GLOB_ARCHIVE $archive1 : : : symbol ]",
                      [])


t.cleanup()