Conan: Multiple Generators

Created on 16 Dec 2018  路  4Comments  路  Source: conan-io/conan

To help us debug your issue please explain:

  • [x] I've read the CONTRIBUTING guide.
  • [x] I've specified the Conan version, operating system version and any tool that can be relevant.
  • [x] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.

Hi everybody,
I'm using Conan (1.10.1) to get libraries to build an internal (multi-platform) component.
I need to integrate within our build system, which is based on Visual Studio on Windows, Xcode on macOS and CMake on Linux.
My conanfile is mostly as follows

class PackageConan(ConanFile):
    name = "Package"
    description = "My Package"
    settings = "os", "compiler", "arch", "build_type"
    requires = "zlib/1.2.8@alessandro/release"
    exports_sources = "src/*"
    generators = "visual_studio_multi", "xcode"

    def imports(self):
        # Assume install folder is main project's directory
        if self.settings.os == "Windows":
            archDir = "x86" if self.settings.arch == "x86" else "x64"
            buildDir = os.path.join("windows", archDir, str(self.settings.build_type))

        elif self.settings.os == "Macos":
            buildDir = os.path.join("osx", "Build", "Products", str(self.settings.build_type))

        # Copy dynamic libraries to the build output directory
        self.copy("*.dll", src="bin", dst=buildDir)
        self.copy("*.dylib", src="lib", dst=buildDir)

On Windows everything is fine, the property sheets are generated along with the Xcode project file.
On macOS, Conan failed in exporting the Visual Studio generator

Configuration:
[settings]
os=Macos
os_build=Macos
arch=x86_64
arch_build=x86_64
compiler=apple-clang
compiler.version=9.1
compiler.libcxx=libc++
build_type=Release
[options]
[build_requires]
[env]

...

PROJECT: ERROR: Generator visual_studio_multi(file:None) failed
Undefined Visual Studio version 9.1
ERROR: Undefined Visual Studio version 9.1

I wouldn't opt for a different conanfile.py for each platform, so maybe a possible way is defining generators conditionally as explained in #2282?
What's the suggested way of managing such a situation?

question

All 4 comments

Hi @alessandrodn-cybereason

Sure, you can use conditionals in configure(), try something like this:

class HelloConan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    # Don't need to declare generators here
    def configure(self):
        if self.settings.os== "Windows":
            self.generators = "visual_studio", # Trailing comma, or ["visual_studio"]
        else:
            self.generators = "cmake",  # Note the trailing comma

Please let me know if this helps. Thanks!

I have also checked if it is possible to avoid the exception, but visual_studio_multi generator needs the VS version to be able to correctly generate the correct configuration. If in other OS with other compiler and version, it is totally unable to generate something that makes sense, and it seems safer to fail.

Thanks a lot for the answers, I finally deployed as follows:

    def configure(self):
        if self.settings.os == "Windows":
            self.generators.append("visual_studio_multi")

        elif self.settings.os == "Macos":
            self.generators.append("xcode")

Looks good, thanks for commenting and closing!

Was this page helpful?
0 / 5 - 0 ratings