@@ -137,14 +137,16 @@ def content(self, info):
137
137
138
138
class _PCGenerator :
139
139
140
- def __init__ (self , conanfile , require , dep , build_context_suffix = None ):
141
- self ._conanfile = conanfile
140
+ def __init__ (self , pkgconfigdeps , require , dep ):
141
+ self ._conanfile = pkgconfigdeps . _conanfile # noqa
142
142
self ._require = require
143
143
self ._dep = dep
144
144
self ._content_generator = _PCContentGenerator (self ._conanfile , self ._dep )
145
145
self ._transitive_reqs = get_transitive_requires (self ._conanfile , dep )
146
- self ._suffix = "" if not build_context_suffix or not require .build else \
147
- build_context_suffix .get (require .ref .name , "" )
146
+ self ._is_build_context = require .build
147
+ self ._build_context_folder = pkgconfigdeps .build_context_folder
148
+ self ._suffix = pkgconfigdeps .build_context_suffix .get (require .ref .name , "" ) \
149
+ if self ._is_build_context else ""
148
150
149
151
def _get_cpp_info_requires_names (self , cpp_info ):
150
152
"""
@@ -249,11 +251,22 @@ def pc_files(self):
249
251
250
252
* Apart from those PC files, if there are any aliases declared, they will be created too.
251
253
"""
254
+ def _fill_pc_files (pc_info ):
255
+ content = self ._content_generator .content (pc_info )
256
+ # If no suffix is defined, we can save the *.pc file in the build_context_folder
257
+ if self ._is_build_context and self ._build_context_folder and not self ._suffix :
258
+ # Issue: https://github.com/conan-io/conan/issues/12342
259
+ # Issue: https://github.com/conan-io/conan/issues/14935
260
+ pc_files [f"{ self ._build_context_folder } /{ pc_info .name } .pc" ] = content
261
+ else :
262
+ # Saving also the suffixed names as usual
263
+ pc_files [f"{ pc_info .name } .pc" ] = content
264
+
252
265
def _update_pc_files (info ):
253
- pc_files [ f" { info . name } .pc" ] = self . _content_generator . content (info )
266
+ _fill_pc_files (info )
254
267
for alias in info .aliases :
255
268
alias_info = _PCInfo (alias , [info .name ], f"Alias { alias } for { info .name } " , None , [])
256
- pc_files [ f" { alias } .pc" ] = self . _content_generator . content (alias_info )
269
+ _fill_pc_files (alias_info )
257
270
258
271
pc_files = {}
259
272
# If the package has no components, then we have to calculate only the root pc file
@@ -277,13 +290,7 @@ def _update_pc_files(info):
277
290
if f"{ pkg_name } .pc" not in pc_files :
278
291
package_info = _PCInfo (pkg_name , pkg_requires , f"Conan package: { pkg_name } " ,
279
292
self ._dep .cpp_info , self ._get_package_aliases (self ._dep ))
280
- # It'll be enough creating a shortened PC file. This file will be like an alias
281
- pc_files [f"{ package_info .name } .pc" ] = self ._content_generator .content (package_info )
282
- for alias in package_info .aliases :
283
- alias_info = _PCInfo (alias , [package_info .name ],
284
- f"Alias { alias } for { package_info .name } " , None , [])
285
- pc_files [f"{ alias } .pc" ] = self ._content_generator .content (alias_info )
286
-
293
+ _update_pc_files (package_info )
287
294
return pc_files
288
295
289
296
@staticmethod
@@ -333,7 +340,17 @@ def __init__(self, conanfile):
333
340
self .build_context_activated = []
334
341
# If specified, the files/requires/names for the build context will be renamed appending
335
342
# a suffix. It is necessary in case of same require and build_require and will cause an error
343
+ # DEPRECATED: consumers should use build_context_folder instead
344
+ # FIXME: Conan 3.x: Remove build_context_suffix attribute
336
345
self .build_context_suffix = {}
346
+ # By default, the "[generators_folder]/build" folder will save all the *.pc files activated
347
+ # in the build_context_activated list.
348
+ # Notice that if the `build_context_suffix` attr is defined, the `build_context_folder` one
349
+ # will have no effect.
350
+ # Issue: https://github.com/conan-io/conan/issues/12342
351
+ # Issue: https://github.com/conan-io/conan/issues/14935
352
+ # FIXME: Conan 3.x: build_context_folder should be "build" by default
353
+ self .build_context_folder = None # Keeping backward-compatibility
337
354
338
355
def _validate_build_requires (self , host_req , build_req ):
339
356
"""
@@ -347,7 +364,7 @@ def _validate_build_requires(self, host_req, build_req):
347
364
if r .ref .name in self .build_context_activated }
348
365
common_names = {r .ref .name for r in host_req .values ()}.intersection (activated_br )
349
366
without_suffixes = [common_name for common_name in common_names
350
- if self .build_context_suffix .get (common_name ) is None ]
367
+ if not self .build_context_suffix .get (common_name )]
351
368
if without_suffixes :
352
369
raise ConanException (f"The packages { without_suffixes } exist both as 'require' and as"
353
370
f" 'build require'. You need to specify a suffix using the "
@@ -363,20 +380,26 @@ def content(self):
363
380
host_req = self ._conanfile .dependencies .host
364
381
build_req = self ._conanfile .dependencies .build # tool_requires
365
382
test_req = self ._conanfile .dependencies .test
366
-
367
- # Check if it exists both as require and as build require without a suffix
368
- self ._validate_build_requires (host_req , build_req )
383
+ # If self.build_context_suffix is not defined, the build requires will be saved
384
+ # in the self.build_context_folder
385
+ # FIXME: Conan 3.x: Remove build_context_suffix attribute and the validation function
386
+ if self .build_context_folder is None : # Legacy flow
387
+ if self .build_context_suffix :
388
+ # deprecation warning
389
+ self ._conanfile .output .warning ("PkgConfigDeps.build_context_suffix attribute has been "
390
+ "deprecated. Use PkgConfigDeps.build_context_folder instead." )
391
+ # Check if it exists both as require and as build require without a suffix
392
+ self ._validate_build_requires (host_req , build_req )
393
+ elif self .build_context_folder is not None and self .build_context_suffix :
394
+ raise ConanException ("It's not allowed to define both PkgConfigDeps.build_context_folder "
395
+ "and PkgConfigDeps.build_context_suffix (deprecated)." )
369
396
370
397
for require , dep in list (host_req .items ()) + list (build_req .items ()) + list (test_req .items ()):
371
- # Require is not used at the moment, but its information could be used,
372
- # and will be used in Conan 2.0
373
398
# Filter the build_requires not activated with PkgConfigDeps.build_context_activated
374
399
if require .build and dep .ref .name not in self .build_context_activated :
375
400
continue
376
-
377
- pc_generator = _PCGenerator (self ._conanfile , require , dep ,
378
- build_context_suffix = self .build_context_suffix )
379
- pc_files .update (pc_generator .pc_files )
401
+ # Save all the *.pc files and their contents
402
+ pc_files .update (_PCGenerator (self , require , dep ).pc_files )
380
403
return pc_files
381
404
382
405
def generate (self ):
0 commit comments