Search code examples
conan

Can I write a Conan recipe to download and install a package from the web?


We just started using conan, so I'm still new. One of our dependent libs doesn't have a conan recipe (it's just a cmake-based project on github). Can I write my own local recipe to download it from github and build it so I can add it to my dependencies like any other conan project?


Solution

  • First checkout the code. You can either do that by using the git tools or run command.

    from conans import ConanFile, CMake, tools
    
    class HelloConan(ConanFile):
    
        def source(self):
            self.run("git clone https://github.com/conan-io/hello.git")
        #or using git tools
            git = tools.Git(folder="hello")
            git.clone("https://github.com/conan-io/hello.git", "master")
    
    

    Then build it as you normally would with either cmake tools or run command

    from conans import ConanFile, CMake
    
    class ExampleConan(ConanFile):
        def build(self):
            cmake = CMake(self)
            self.run('cmake "%s" %s' % (self.source_folder, cmake.command_line))
            self.run('cmake --build . %s' % cmake.build_config)
            self.run('cmake --build . --target install')
            cmake = CMake(self)
    
        #Using cmake tools
            build_folder=self.build_folder)
            cmake.configure()
            cmake.build()
            cmake.test() # Build the "RUN_TESTS" or "test" target
            cmake.install()
    

    For my information look at the documentation for

    Creating a new conanfile.py

    Checkoing out the code

    Building using cmake