forked from conan-io/conan
/
test_build_system_layout_helpers.py
278 lines (228 loc) · 10.6 KB
/
test_build_system_layout_helpers.py
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
import os
import platform
import textwrap
import pytest
import six
from conans.model.ref import ConanFileReference
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient, TurboTestClient
from conans.util.files import save, load
@pytest.fixture
def conanfile():
conanfile = str(GenConanfile().with_import("from conans import tools")
.with_import("import os")
.with_setting("build_type").with_setting("arch")
.with_import("from conan.tools.layout import {ly}")
.with_import("from conan.tools.files import AutoPackager"))
conanfile += """
def source(self):
tools.save("include/myheader.h", "")
def build(self):
tools.save("{libpath}/mylib.lib", "")
def layout(self):
{ly}(self)
def package(self):
AutoPackager(self).run()
"""
return conanfile
subfolders_arch = {"armv7": "ARM", "armv8": "ARM64", "x86": None, "x86_64": "x64"}
@pytest.mark.parametrize("arch", ["x86_64", "x86", "armv7", "armv8"])
@pytest.mark.parametrize("build_type", ["Debug", "Release"])
def test_layout_in_cache(conanfile, build_type, arch):
"""The layout in the cache is used too, always relative to the "base" folders that the cache
requires. But by the default, the "package" is not followed
"""
client = TurboTestClient()
ref = ConanFileReference.loads("lib/1.0")
libarch = subfolders_arch.get(arch)
libpath = "{}{}".format(libarch + "/" if libarch else "", build_type)
pref = client.create(ref, args="-s arch={} -s build_type={}".format(arch, build_type),
conanfile=conanfile.format(ly="vs_layout", libpath=libpath))
bf = client.cache.package_layout(pref.ref).build(pref)
pf = client.cache.package_layout(pref.ref).package(pref)
# Check the build folder
assert os.path.exists(os.path.join(os.path.join(bf, libpath), "mylib.lib"))
# Check the package folder
assert os.path.exists(os.path.join(pf, "lib/mylib.lib"))
assert os.path.exists(os.path.join(pf, "include", "myheader.h"))
@pytest.mark.parametrize("arch", ["x86_64", "x86", "armv7", "armv8"])
@pytest.mark.parametrize("build_type", ["Debug", "Release"])
def test_layout_with_local_methods(conanfile, build_type, arch):
"""The layout in the cache is used too, always relative to the "base" folders that the cache
requires. But by the default, the "package" is not followed
"""
client = TestClient()
libarch = subfolders_arch.get(arch)
libpath = "{}{}".format(libarch + "/" if libarch else "", build_type)
client.save({"conanfile.py": conanfile.format(ly="vs_layout", libpath=libpath)})
client.run("install . lib/1.0@ -s build_type={} -s arch={}".format(build_type, arch))
client.run("source .")
# Check the source folder (release)
assert os.path.exists(os.path.join(client.current_folder, "include", "myheader.h"))
client.run("build .")
# Check the build folder (release)
assert os.path.exists(os.path.join(os.path.join(client.current_folder, libpath), "mylib.lib"))
# Check the package
client.run("package .", assert_error=True)
assert "The usage of the 'conan package' local method is disabled when using layout()" \
"" in client.out
@pytest.mark.skipif(platform.system() != "Windows", reason="Removing msvc compiler")
def test_error_no_msvc():
# https://github.com/conan-io/conan/issues/9953
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import cmake_layout
class Pkg(ConanFile):
settings = "os", "compiler", "build_type", "arch"
def layout(self):
cmake_layout(self)
""")
settings_yml = textwrap.dedent("""
os: [Windows]
os_build: [Windows]
arch_build: [x86_64]
compiler:
gcc:
version: ["8"]
build_type: [Release]
arch: [x86_64]
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
save(client.cache.settings_path, settings_yml)
client.run('install . -s os=Windows -s build_type=Release -s arch=x86_64 '
'-s compiler=gcc -s compiler.version=8')
assert "Installing" in client.out
def test_error_no_build_type():
# https://github.com/conan-io/conan/issues/9953
conanfile = textwrap.dedent("""
from conans import ConanFile
from conan.tools.cmake import cmake_layout
class Pkg(ConanFile):
settings = "os", "compiler", "arch"
def layout(self):
cmake_layout(self)
""")
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run('install .', assert_error=True)
assert " 'build_type' setting not defined, it is necessary for cmake_layout()" in client.out
@pytest.mark.skipif(six.PY2, reason="only Py3")
def test_cmake_layout_external_sources():
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import cmake_layout
from conan.tools.files import save, copy, load
class Pkg(ConanFile):
settings = "os", "build_type"
exports_sources = "exported.txt"
def layout(self):
cmake_layout(self, src_folder="src")
def generate(self):
save(self, "generate.txt", "generate")
def source(self):
save(self, "source.txt", "foo")
def build(self):
c1 = load(self, os.path.join(self.source_folder, "source.txt"))
c2 = load(self, os.path.join(self.source_folder, "..", "exported.txt"))
save(self, "build.txt", c1 + c2)
def package(self):
copy(self, "build.txt", self.build_folder, os.path.join(self.package_folder, "res"))
""")
client = TestClient()
client.save({"conanfile.py": conanfile, "exported.txt": "exported_contents"})
client.run("create . foo/1.0@ -s os=Linux")
assert "Packaged 1 '.txt' file: build.txt" in client.out
# Local flow
client.run("install . foo/1.0 -s os=Linux")
assert os.path.exists(os.path.join(client.current_folder, "build", "generators", "generate.txt"))
client.run("source .")
assert os.path.exists(os.path.join(client.current_folder, "src", "source.txt"))
client.run("build .")
contents = load(os.path.join(client.current_folder, "build", "Release", "build.txt"))
assert contents == "fooexported_contents"
client.run("export-pkg . foo/1.0@ --force")
assert "Packaged 1 '.txt' file: build.txt" in client.out
@pytest.mark.skipif(six.PY2, reason="only Py3")
@pytest.mark.parametrize("with_build_type", [True, False])
def test_basic_layout_external_sources(with_build_type):
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.layout import basic_layout
from conan.tools.files import save, load, copy
class Pkg(ConanFile):
settings = "os", "compiler", "arch"{}
exports_sources = "exported.txt"
def layout(self):
basic_layout(self, src_folder="src")
def generate(self):
save(self, "generate.txt", "generate")
def source(self):
save(self, "source.txt", "foo")
def build(self):
c1 = load(self, os.path.join(self.source_folder, "source.txt"))
c2 = load(self, os.path.join(self.source_folder, "..", "exported.txt"))
save(self, "build.txt", c1 + c2)
def package(self):
copy(self, "build.txt", self.build_folder, os.path.join(self.package_folder, "res"))
""")
if with_build_type:
conanfile = conanfile.format(', "build_type"')
else:
conanfile = conanfile.format("")
client = TestClient()
client.save({"conanfile.py": conanfile, "exported.txt": "exported_contents"})
client.run("create . foo/1.0@ -s os=Linux")
assert "Packaged 1 '.txt' file: build.txt" in client.out
# Local flow
build_folder = "build-release" if with_build_type else "build"
client.run("install . foo/1.0 -s os=Linux")
assert os.path.exists(os.path.join(client.current_folder, build_folder, "conan", "generate.txt"))
client.run("source .")
assert os.path.exists(os.path.join(client.current_folder, "src", "source.txt"))
client.run("build .")
contents = load(os.path.join(client.current_folder, build_folder, "build.txt"))
assert contents == "fooexported_contents"
client.run("export-pkg . foo/1.0@ --force")
assert "Packaged 1 '.txt' file: build.txt" in client.out
@pytest.mark.skipif(six.PY2, reason="only Py3")
@pytest.mark.parametrize("with_build_type", [True, False])
def test_basic_layout_no_external_sources(with_build_type):
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.layout import basic_layout
from conan.tools.files import save, load, copy
class Pkg(ConanFile):
settings = "os", "compiler", "arch"{}
exports_sources = "exported.txt"
def layout(self):
basic_layout(self)
def generate(self):
save(self, "generate.txt", "generate")
def build(self):
contents = load(self, os.path.join(self.source_folder, "exported.txt"))
save(self, "build.txt", contents)
def package(self):
copy(self, "build.txt", self.build_folder, os.path.join(self.package_folder,
"res"))
""")
if with_build_type:
conanfile = conanfile.format(', "build_type"')
else:
conanfile = conanfile.format("")
client = TestClient()
client.save({"conanfile.py": conanfile, "exported.txt": "exported_contents"})
client.run("create . foo/1.0@ -s os=Linux")
assert "Packaged 1 '.txt' file: build.txt" in client.out
# Local flow
client.run("install . foo/1.0 -s os=Linux")
build_folder = "build-release" if with_build_type else "build"
assert os.path.exists(os.path.join(client.current_folder, build_folder, "conan", "generate.txt"))
client.run("build .")
contents = load(os.path.join(client.current_folder, build_folder, "build.txt"))
assert contents == "exported_contents"
client.run("export-pkg . foo/1.0@ --force")
assert "Packaged 1 '.txt' file: build.txt" in client.out