conan icon indicating copy to clipboard operation
conan copied to clipboard

[question] How to generate PkgConfig files for tool_requires dependencies and consume them with MesonToolchain?

Open jwillikers opened this issue 3 years ago • 1 comments
trafficstars

Several Meson-based packages in Conan Center use PkgConfig files to find build tools. For instance, the wayland package contains a tool that generates source files called wayland-scanner. To handle the complexities of cross-compilation, the wayland package requires that wayland-scanner be installed on the build machine. Unlike typical build tools, however, it uses wayland-scanner's PkgConfig file to locate the executable instead of searching for it in PATH. Suffice to say this requires that PkgConfig files for tool_requires dependencies be available.

The workaround I implemented in the wayland package is not correct because it relies on a private method in Conan to generate the PkgConfig files for the wayland package. Yes, the wayland package requires itself as a tool_requires, but only when it is being cross-compiled.

# Generate PC files for the tool_requires wayland package to ensure wayland-scanner is found for build machine.
if cross_building(self):
    native_generators_folder = os.path.join(self.generators_folder, "native")
    mkdir(self, native_generators_folder)
    for pc_name, pc_content in get_pc_files_and_content(self, self.dependencies.build["wayland"]).items():
        save(self, os.path.join(native_generators_folder, pc_name), pc_content)
    tc.project_options["build.pkg_config_path"] = native_generators_folder

Another instance of this kind of usage is the xkbcommon package, which also requires the wayland package's PkgConfig files be available in order to find build tools during Meson's generation phase. See conan-io/conan-center-index#11636 for details.

Note that MesonToolchain also requires a special option in order for it to find the PkgConfig files on the build machine: build.pkg_config_path. Relying on the normal pkg_config_path variable or corresponding environment variable will not work in this situation. Ideally, MesonToolchain would also integrate with this use case by automatically defining build.pkg_config_path to include the necessary directory for all tool_requires dependencies.

jwillikers avatar Jul 26 '22 14:07 jwillikers

This seems very similar to what we are doing in CMakeDeps, to allow creating config.cmake scripts for some exceptional tool_requires. It seems that we should follow the same interface (defining it in generate() method explicitly)

memsharded avatar Jul 28 '22 09:07 memsharded