forked from conan-io/conan
/
test_ninja.py
229 lines (190 loc) · 9.25 KB
/
test_ninja.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
import os
import textwrap
import platform
import pytest
from conan.tools.cmake import CMakeToolchain
from conans.test.assets.cmake import gen_cmakelists
from conans.test.assets.genconanfile import GenConanfile
from conans.test.assets.sources import gen_function_h, gen_function_cpp
from conans.test.functional.utils import check_vs_runtime, check_exe_run
from conans.test.utils.tools import TestClient
@pytest.fixture
def client():
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain
class Library(ConanFile):
name = "hello"
version = "1.0"
settings = 'os', 'arch', 'compiler', 'build_type'
exports_sources = 'hello.h', '*.cpp', 'CMakeLists.txt'
options = {'shared': [True, False]}
default_options = {'shared': False}
def generate(self):
tc = CMakeToolchain(self, generator="Ninja")
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
self.run(os.sep.join([".", "myapp"]))
def package(self):
cmake = CMake(self)
cmake.install()
""")
test_client = TestClient(path_with_spaces=False)
test_client.save({'conanfile.py': conanfile,
"CMakeLists.txt": gen_cmakelists(libsources=["hello.cpp"],
appsources=["main.cpp"],
install=True),
"hello.h": gen_function_h(name="hello"),
"hello.cpp": gen_function_cpp(name="hello", includes=["hello"]),
"main.cpp": gen_function_cpp(name="main", includes=["hello"],
calls=["hello"])})
return test_client
@pytest.mark.skipif(platform.system() != "Linux", reason="Only Linux")
@pytest.mark.parametrize("build_type,shared", [("Release", False), ("Debug", True)])
@pytest.mark.tool_compiler
@pytest.mark.tool_ninja
def test_locally_build_linux(build_type, shared, client):
settings = "-s os=Linux -s arch=x86_64 -s build_type={} -o hello:shared={}".format(build_type,
shared)
client.run("install . {}".format(settings))
client.run_command('cmake . -G "Ninja" -DCMAKE_TOOLCHAIN_FILE={}'
.format(CMakeToolchain.filename))
client.run_command('ninja')
if shared:
assert "Linking CXX shared library libmylibrary.so" in client.out
else:
assert "Linking CXX static library libmylibrary.a" in client.out
client.run_command("./myapp")
check_exe_run(client.out, ["main", "hello"], "gcc", None, build_type, "x86_64", cppstd=None)
# create should also work
client.run("create . hello/1.0@ {}".format(settings))
assert 'cmake -G "Ninja"' in client.out
assert "main: {}!".format(build_type) in client.out
client.run("install hello/1.0@ -g=deploy -if=mydeploy {}".format(settings))
ldpath = os.path.join(client.current_folder, "mydeploy", "hello")
client.run_command("LD_LIBRARY_PATH='{}' ./mydeploy/hello/myapp".format(ldpath))
check_exe_run(client.out, ["main", "hello"], "gcc", None, build_type, "x86_64", cppstd=None)
@pytest.mark.skipif(platform.system() != "Windows", reason="Only windows")
@pytest.mark.parametrize("build_type,shared", [("Release", False), ("Debug", True)])
@pytest.mark.tool_compiler
@pytest.mark.tool_ninja
def test_locally_build_msvc(build_type, shared, client):
msvc_version = "15"
settings = "-s build_type={} -o hello:shared={}".format(build_type, shared)
client.run("install . {}".format(settings))
client.run_command('conanvcvars.bat && cmake . -G "Ninja" '
'-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake '
'-DCMAKE_BUILD_TYPE={}'.format(build_type))
client.run_command("conanvcvars.bat && ninja")
libname = "mylibrary.dll" if shared else "mylibrary.lib"
assert libname in client.out
client.run_command("myapp.exe")
# TODO: Need full msvc version check
check_exe_run(client.out, ["main", "hello"], "msvc", "19", build_type, "x86_64", cppstd="14")
check_vs_runtime("myapp.exe", client, msvc_version, build_type, architecture="amd64")
check_vs_runtime(libname, client, msvc_version, build_type, architecture="amd64")
# create should also work
client.run("create . hello/1.0@ {}".format(settings))
assert 'cmake -G "Ninja"' in client.out
assert "main: {}!".format(build_type) in client.out
client.run("install hello/1.0@ -g=deploy -if=mydeploy {}".format(settings))
client.run_command(r"mydeploy\hello\myapp.exe")
check_exe_run(client.out, ["main", "hello"], "msvc", "19", build_type, "x86_64", cppstd="14")
@pytest.mark.skipif(platform.system() != "Windows", reason="Only windows")
@pytest.mark.tool_compiler
@pytest.mark.tool_ninja
def test_locally_build_msvc_toolset(client):
msvc_version = "15"
profile = textwrap.dedent("""
[settings]
os=Windows
compiler=msvc
compiler.version=19.0
compiler.runtime=dynamic
compiler.cppstd=14
build_type=Release
arch=x86_64
[conf]
tools.cmake.cmaketoolchain:generator=Ninja
tools.microsoft.msbuild:vs_version = 15
""")
client.save({"profile": profile})
client.run("install . -pr=profile")
client.run_command('conanvcvars.bat && cmake . -G "Ninja" '
'-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake '
'-DCMAKE_BUILD_TYPE=Release')
client.run_command("conanvcvars.bat && ninja")
client.run_command("myapp.exe")
# Checking that compiler is indeed version 19.0, not 19.1-default of VS15
check_exe_run(client.out, ["main", "hello"], "msvc", "19.0", "Release", "x86_64", cppstd="14")
check_vs_runtime("myapp.exe", client, msvc_version, "Release", architecture="amd64")
check_vs_runtime("mylibrary.lib", client, msvc_version, "Release", architecture="amd64")
@pytest.mark.skipif(platform.system() != "Windows", reason="Only windows")
@pytest.mark.parametrize("build_type,shared", [("Release", False), ("Debug", True)])
@pytest.mark.tool_mingw64
@pytest.mark.tool_compiler
@pytest.mark.tool_ninja
def test_locally_build_gcc(build_type, shared, client):
# FIXME: Note the gcc version is still incorrect
gcc = ("-s os=Windows -s compiler=gcc -s compiler.version=4.9 -s compiler.libcxx=libstdc++ "
"-s arch=x86_64 -s build_type={}".format(build_type))
client.run("install . {} -o hello:shared={}".format(gcc, shared))
client.run_command('cmake . -G "Ninja" '
'-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake '
'-DCMAKE_BUILD_TYPE={}'.format(build_type))
libname = "mylibrary.dll" if shared else "libmylibrary.a"
client.run_command("ninja")
assert libname in client.out
client.run_command("myapp.exe")
# TODO: Need full gcc version check
check_exe_run(client.out, ["main", "hello"], "gcc", None, build_type, "x86_64", cppstd=None)
@pytest.mark.skipif(platform.system() != "Darwin", reason="Requires apple-clang")
@pytest.mark.parametrize("build_type,shared", [("Release", False), ("Debug", True)])
@pytest.mark.tool_compiler
@pytest.mark.tool_ninja
def test_locally_build_macos(build_type, shared, client):
client.run('install . -s os=Macos -s arch=x86_64 -s build_type={} -o hello:shared={}'
.format(build_type, shared))
client.run_command('cmake . -G"Ninja" -DCMAKE_TOOLCHAIN_FILE={}'
.format(CMakeToolchain.filename))
client.run_command('ninja')
if shared:
assert "Linking CXX shared library libmylibrary.dylib" in client.out
else:
assert "Linking CXX static library libmylibrary.a" in client.out
command_str = 'DYLD_LIBRARY_PATH="%s" ./myapp' % client.current_folder
client.run_command(command_str)
check_exe_run(client.out, ["main", "hello"], "apple-clang", None, build_type, "x86_64",
cppstd=None)
@pytest.mark.skipif(platform.system() != "Windows", reason="Only windows")
@pytest.mark.tool_visual_studio
def test_ninja_conf():
conanfile = GenConanfile().with_generator("CMakeToolchain").with_settings("os", "compiler",
"build_type", "arch")
profile = textwrap.dedent("""
[settings]
os=Windows
compiler=msvc
compiler.version=19.1
compiler.runtime=dynamic
compiler.cppstd=14
build_type=Release
arch=x86_64
[conf]
tools.cmake.cmaketoolchain:generator=Ninja
""")
client = TestClient()
client.save({"conanfile.py": conanfile,
"profile": profile})
client.run("install . -pr=profile")
conanbuild = client.load("conanbuild.json")
assert '"cmake_generator": "Ninja"' in conanbuild
vcvars = client.load("conanvcvars.bat")
assert "2017" in vcvars
# toolchain cannot define the CMAKE_GENERATOR_TOOLSET for Ninja
cmake = client.load("conan_toolchain.cmake")
assert "CMAKE_GENERATOR_TOOLSET" not in cmake