forked from conan-io/conan
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmake_targets_test.py
242 lines (205 loc) · 7.35 KB
/
cmake_targets_test.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
import platform
import unittest
import pytest
from nose.plugins.attrib import attr
from conans.test.utils.tools import TestClient
conanfile_py = """
from conans import ConanFile
class HelloConan(ConanFile):
name = "Hello"
version = "0.1"
exports = "*"
build_policy="missing"
def package(self):
self.copy("*", "include")
"""
hello = """
#pragma once
#include <iostream>
void hello(){std::cout<<"Hello World!";}
"""
conanfile = """[requires]
Hello/0.1@lasote/testing
"""
cmake = """set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
project(MyHello CXX)
cmake_minimum_required(VERSION 2.8.12)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_executable(say_hello main.cpp)
target_link_libraries(say_hello CONAN_PKG::Hello)
"""
main = """
#include "hello.h"
int main(){
hello();
}
"""
@attr("slow")
@pytest.mark.slow
class CMakeTargetsTest(unittest.TestCase):
def test_transitive_flags(self):
client = TestClient()
conanfile = """from conans import ConanFile
class Charlie(ConanFile):
def package_info(self):
self.cpp_info.sharedlinkflags = ["CharlieFlag"]
"""
client.save({"conanfile.py": conanfile})
client.run("create . Charlie/0.1@user/testing")
conanfile = """from conans import ConanFile
class Beta(ConanFile):
requires = "Charlie/0.1@user/testing"
def package_info(self):
self.cpp_info.sharedlinkflags = ["BetaFlag"]
"""
client.save({"conanfile.py": conanfile})
client.run("create . Beta/0.1@user/testing")
conanfile = """from conans import ConanFile
class Alpha(ConanFile):
requires = "Beta/0.1@user/testing"
"""
client.save({"conanfile.py": conanfile})
client.run("install . -g cmake")
cmake = client.load("conanbuildinfo.cmake")
self.assertIn('set(CONAN_SHARED_LINKER_FLAGS '
'"CharlieFlag BetaFlag ${CONAN_SHARED_LINKER_FLAGS}")', cmake)
@pytest.mark.tool_cmake
def test_header_only(self):
client = TestClient()
client.save({"conanfile.py": conanfile_py,
"hello.h": hello})
client.run("export . lasote/testing")
client.save({"conanfile.txt": conanfile,
"CMakeLists.txt": cmake,
"main.cpp": main}, clean_first=True)
client.run('install . -g cmake')
if platform.system() == "Windows":
client.run_command('cmake . -G "Visual Studio 15 Win64"')
else:
client.run_command('cmake .')
self.assertNotIn("WARN: Unknown compiler '", client.out)
self.assertNotIn("', skipping the version check...", client.out)
self.assertIn("Configuring done", client.out)
self.assertIn("Generating done", client.out)
self.assertIn("Build files have been written", client.out)
client.save({"conanfile.txt": conanfile,
"CMakeLists.txt": cmake.replace("conanbuildinfo.cmake",
"conanbuildinfo_multi.cmake"),
"main.cpp": main}, clean_first=True)
if platform.system() == "Windows":
debug_install = '-s compiler="Visual Studio" -s compiler.version=14 -s compiler.runtime=MDd'
release_install = '-s compiler="Visual Studio" -s compiler.version=14 -s compiler.runtime=MD'
client.run('install . %s -s build_type=Debug -g cmake_multi' % debug_install)
client.run('install . %s -s build_type=Release -g cmake_multi' % release_install)
client.run_command('cmake . -G "Visual Studio 14 Win64"')
self.assertNotIn("WARN: Unknown compiler '", client.out)
self.assertNotIn("', skipping the version check...", client.out)
self.assertIn("Configuring done", client.out)
self.assertIn("Generating done", client.out)
self.assertIn("Build files have been written", client.out)
@unittest.skipUnless(platform.system() == "Darwin", "Requires Macos")
def test_apple_framework(self):
client = TestClient()
conanfile_fr = conanfile_py + '''
def package_info(self):
self.cpp_info.sharedlinkflags = ["-framework Foundation"]
self.cpp_info.exelinkflags = self.cpp_info.sharedlinkflags
'''
client.save({"conanfile.py": conanfile_fr,
"hello.h": hello})
client.run("export . lasote/testing")
client.save({"conanfile.txt": conanfile,
"CMakeLists.txt": cmake,
"main.cpp": main}, clean_first=True)
client.run("install . -g cmake")
bili = client.load("conanbuildinfo.cmake")
self.assertIn("-framework Foundation", bili)
@unittest.skipUnless(platform.system() == "Darwin", "Requires Macos")
def test_custom_apple_framework(self):
"""Build a custom apple framework and reuse it"""
client = TestClient()
lib_c = r"""
#include <stdio.h>
#include "MyFramework.h"
void hello(){
printf("HELLO FRAMEWORK!");
}
"""
lib_h = r"""
void hello();
"""
conanfile = '''
import os
from shutil import copyfile
from conans import ConanFile, CMake
class MyFrameworkConan(ConanFile):
options = {"shared": [True, False]}
default_options = "shared=True"
exports = "*"
generators = "cmake"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
self.copy(pattern="*", src="lib", keep_path=True)
def package_info(self):
flag_f_location = '-F "%s"' % self.package_folder
self.cpp_info.cflags.append(flag_f_location)
self.cpp_info.sharedlinkflags.extend([flag_f_location, "-framework MyFramework"])
self.cpp_info.exelinkflags = self.cpp_info.sharedlinkflags
'''
cmake = """
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
project(MyHello C)
cmake_minimum_required(VERSION 2.8.12)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)
add_library(MyFramework MyFramework.c MyFramework.h)
set_target_properties(MyFramework PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION C
MACOSX_FRAMEWORK_IDENTIFIER MyFramework
PUBLIC_HEADER MyFramework.h
)
"""
files = {"MyFramework.c": lib_c, "conanfile.py": conanfile, "MyFramework.h": lib_h,
"CMakeLists.txt": cmake}
client.save(files, clean_first=True)
client.run("create . MyFramework/1.0@user/testing")
reuse = """from conans import ConanFile, CMake
class HelloConan(ConanFile):
requires = "MyFramework/1.0@user/testing"
generators = "cmake"
exports = "*"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
"""
cmake = """
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CXX_ABI_COMPILED 1)
project(MyHello C)
cmake_minimum_required(VERSION 2.8.12)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(KEEP_RPATHS)
add_executable(say_hello main.c)
"""
main = """
#include "MyFramework/MyFramework.h"
int main(){
hello();
}
"""
files = {"main.c": main, "conanfile.py": reuse,
"CMakeLists.txt": cmake}
client.save(files, clean_first=True)
client.run("install . ")
client.run("build . ")
client.run_command("bin/say_hello")
self.assertIn("HELLO FRAMEWORK!", client.out)