Skip to content

Commit f36756c

Browse files
committed
fix CPS broken develop2 and add shared testing
1 parent a1e0d30 commit f36756c

File tree

4 files changed

+121
-19
lines changed

4 files changed

+121
-19
lines changed

conan/cps/cps.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, component_type=None):
4040
self.requires = []
4141
self.location = None
4242
self.link_location = None
43+
self.link_languages = None
4344
self.link_libraries = None # system libraries
4445

4546
def serialize(self):
@@ -56,6 +57,8 @@ def serialize(self):
5657
component["link_location"] = self.link_location
5758
if self.link_libraries:
5859
component["link_libraries"] = self.link_libraries
60+
if self.link_languages:
61+
component["link_languages"] = self.link_languages
5962
return component
6063

6164
@staticmethod
@@ -68,6 +71,7 @@ def deserialize(data):
6871
comp.location = data.get("location")
6972
comp.link_location = data.get("link_location")
7073
comp.link_libraries = data.get("link_libraries")
74+
comp.link_languages = data.get("link_languages")
7175
return comp
7276

7377
@staticmethod
@@ -90,12 +94,15 @@ def from_cpp_info(cpp_info, conanfile, libname=None):
9094
cps_comp.location = cpp_info.location
9195
cps_comp.link_location = cpp_info.link_location
9296
cps_comp.link_libraries = cpp_info.system_libs
97+
langs = {"C": "c", "C++": "cpp"}
98+
cps_comp.link_languages = [langs[lang] for lang in cpp_info.languages]
9399
required = cpp_info.requires
94100
cps_comp.requires = [f":{c}" if "::" not in c else c.replace("::", ":") for c in required]
95101
return cps_comp
96102

97103
def update(self, conf, conf_def):
98104
# TODO: conf not used at the moent
105+
self.link_languages = self.link_languages or conf_def.get("link_languages")
99106
self.location = self.location or conf_def.get("location")
100107
self.link_location = self.link_location or conf_def.get("link_location")
101108
self.link_libraries = self.link_libraries or conf_def.get("link_libraries")
@@ -223,6 +230,29 @@ def strip_prefix(dirs):
223230
basefile = basefile[3:]
224231
cpp_info.libs = [basefile]
225232
# FIXME: Missing requires
233+
elif comp.type is CPSComponentType.DYLIB:
234+
if comp.link_location:
235+
link_location = comp.link_location
236+
link_location = link_location.replace("@prefix@/", "")
237+
cpp_info.libdirs = [os.path.dirname(link_location)]
238+
filename = os.path.basename(link_location)
239+
basefile, ext = os.path.splitext(filename)
240+
if basefile.startswith("lib") and ext != ".lib":
241+
basefile = basefile[3:]
242+
cpp_info.libs = [basefile]
243+
location = comp.location
244+
location = location.replace("@prefix@/", "")
245+
cpp_info.bindirs = [os.path.dirname(location)]
246+
else: # TODO: same as archive, refactor
247+
location = comp.location
248+
location = location.replace("@prefix@/", "")
249+
cpp_info.libdirs = [os.path.dirname(location)]
250+
filename = os.path.basename(location)
251+
basefile, ext = os.path.splitext(filename)
252+
if basefile.startswith("lib") and ext != ".lib":
253+
basefile = basefile[3:]
254+
cpp_info.libs = [basefile]
255+
# FIXME: Missing requires
226256
cpp_info.system_libs = comp.link_libraries
227257
else:
228258
for comp_name, comp in self.components.items():

conan/tools/cmake/cmakedeps2/cmakedeps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def generate(self):
259259
pkg_name = self._cmakedeps.get_cmake_filename(dep)
260260
# https://cmake.org/cmake/help/v3.22/guide/using-dependencies/index.html
261261
if cmake_find_mode == FIND_MODE_NONE:
262-
cps = glob.glob(f"**/{pkg_name}.cps", root_dir=dep.package_folder, recursive=True)
262+
cps = glob.glob(os.path.join(dep.package_folder, f"**/{pkg_name}.cps"),
263+
recursive=True)
263264
if cps:
264265
loc = os.path.dirname(os.path.join(dep.package_folder, cps[0]))
265266
loc = loc.replace("\\", "/")

test/functional/toolchains/cmake/test_cps.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99

1010
@pytest.mark.tool("cmake", "4.2")
11-
def test_cps():
11+
@pytest.mark.parametrize("shared", [False, True])
12+
def test_cps(shared):
1213
c = TestClient()
1314
c.run("new cmake_lib")
1415
conanfile = textwrap.dedent("""\
@@ -67,7 +68,9 @@ def package_info(self):
6768
# First, try with the standard mypkg-config.cmake consumption
6869
c.save({"conanfile.py": conanfile,
6970
"CMakeLists.txt": cmake})
70-
c.run("create")
71+
72+
shared_arg = "-o &:shared=True" if shared else ""
73+
c.run(f"create {shared_arg}")
7174
assert "mypkg/0.1: Hello World Release!" in c.out
7275

7376
# Lets consume directly with CPS
@@ -119,5 +122,5 @@ def test(self):
119122
shutil.rmtree(os.path.join(c.current_folder, "test_package", "build"))
120123
c.save({"test_package/conanfile.py": test_conanfile,
121124
"test_package/CMakeLists.txt": test_cmake})
122-
c.run("create --build=missing -c tools.cmake.cmakedeps:new=will_break_next")
125+
c.run(f"create {shared_arg} --build=never -c tools.cmake.cmakedeps:new=will_break_next")
123126
assert "mypkg/0.1: Hello World Release!" in c.out

test/integration/cps/test_cps.py

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,38 +122,106 @@ def package_info(self):
122122
assert 'set(zlib_LIBS_RELEASE zlib)' in cmake
123123

124124

125-
def test_cps_merge():
126-
folder = temp_folder()
127-
cps_base = textwrap.dedent("""
125+
def test_cps_shared_in_pkg():
126+
c = TestClient()
127+
cps = textwrap.dedent("""\
128128
{
129-
"components" : {
130-
"mypkg" : {
129+
"components" :
130+
{
131+
"mypkg" :
132+
{
131133
"includes" : [ "@prefix@/include" ],
132-
"type" : "archive"
134+
"type" : "dylib"
133135
}
134136
},
135-
"cps_version" : "0.12.0",
136-
"name" : "mypkg",
137-
"version" : "1.0"
137+
"cps_path" : "@prefix@/cps",
138+
"cps_version" : "0.13.0",
139+
"name" : "mypkg"
138140
}
139141
""")
140-
cps_conf = textwrap.dedent("""
142+
cps_release = textwrap.dedent("""\
141143
{
142-
"components" : {
143-
"mypkg" : {
144-
"link_languages" : [ "cpp" ],
145-
"location" : "@prefix@/lib/mypkg.lib"
144+
"components" :
145+
{
146+
"mypkg" :
147+
{
148+
"link_location" : "@prefix@/lib/mypkg.lib",
149+
"location" : "@prefix@/bin/mypkg.dll"
146150
}
147151
},
148152
"configuration" : "Release",
149153
"name" : "mypkg"
150154
}
151155
""")
156+
cps = "".join(cps.splitlines())
157+
cps_release = "".join(cps_release.splitlines())
158+
conanfile = textwrap.dedent(f"""
159+
import os, json
160+
from conan.tools.files import save
161+
from conan import ConanFile
162+
class Pkg(ConanFile):
163+
name = "mypkg"
164+
version = "1.0"
165+
166+
def package(self):
167+
cps = '{cps}'
168+
cps_path = os.path.join(self.package_folder, "mypkg.cps")
169+
save(self, cps_path, cps)
170+
cps = '{cps_release}'
171+
cps_path = os.path.join(self.package_folder, "[email protected]")
172+
save(self, cps_path, cps)
173+
174+
def package_info(self):
175+
from conan.cps import CPS
176+
self.cpp_info = CPS.load("mypkg.cps").to_conan()
177+
""")
178+
c.save({"pkg/conanfile.py": conanfile})
179+
c.run("create pkg")
180+
181+
settings = "-s os=Windows -s compiler=msvc -s compiler.version=191 -s arch=x86_64"
182+
183+
c.run(f"install --requires=mypkg/1.0 {settings} -g CMakeDeps")
184+
cmake = c.load("mypkg-release-x86_64-data.cmake")
185+
assert 'set(mypkg_INCLUDE_DIRS_RELEASE "${mypkg_PACKAGE_FOLDER_RELEASE}/include")' in cmake
186+
assert 'set(mypkg_LIB_DIRS_RELEASE "${mypkg_PACKAGE_FOLDER_RELEASE}/lib")'
187+
assert 'set(mypkg_LIBS_RELEASE mypkg)' in cmake
188+
189+
190+
def test_cps_merge():
191+
folder = temp_folder()
192+
193+
cps_base = textwrap.dedent("""{
194+
"components": {
195+
"mypkg":{
196+
"includes": ["@prefix@/include"],
197+
"type": "archive"
198+
}
199+
},
200+
"cps_path": "@prefix@/cps",
201+
"cps_version": "0.13.0",
202+
"name": "mypkg"
203+
}
204+
""")
205+
cps_conf = textwrap.dedent("""{
206+
"components" : {
207+
"mypkg" : {
208+
"link_languages" : [ "cpp" ],
209+
"location" : "@prefix@/lib/mypkg.lib"
210+
}
211+
},
212+
"configuration" : "Release",
213+
"name" : "mypkg"
214+
}
215+
""")
152216
save_files(folder, {"mypkg.cps": cps_base,
153217
"[email protected]": cps_conf})
154218
cps = CPS.load(os.path.join(folder, "mypkg.cps"))
155219
json_cps = cps.serialize()
156-
print(json.dumps(json_cps, indent=2))
220+
mypkg = json_cps["components"]["mypkg"]
221+
assert mypkg["includes"] == ["@prefix@/include"]
222+
assert mypkg["location"] == "@prefix@/lib/mypkg.lib"
223+
assert mypkg["type"] == "archive"
224+
assert mypkg["link_languages"] == ["cpp"]
157225

158226

159227
def test_extended_cpp_info():

0 commit comments

Comments
 (0)