Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set package_folder attribute of ConanFile instance before build #316

Merged

Conversation

robertmrk
Copy link
Contributor

Some of the C++ projects which use CMake as a build system have an install target. Their listsfiles already contain the definition of what and how should be installed. Defining the installation steps of the project in the package method of the ConanFile duplicates this information. Ideally I would like to maintain the code for installation at only one place.
So for projects with an install target, in the package method of the ConanFile we can build the install target instead of defining again which files need to be copied.
For this packaging method to work, the CMAKE_INSTALL_PREFIX of the project should be set at the CMake configuration step, when the build method is called, but at the moment the package_folder attribute of the ConanFile instance is set only before the package method is called.
This PR fixes this issue by setting the package_folder attribute before the build method is called. So in the build method, the project can be configured with the right CMAKE_INSTALL_PREFIX and by building the install target in the package method all the required files of the project would get installed in the package directory.

def build(self):
    cmake = CMake(self.settings)
    self.run("cmake %s %s -DCMAKE_INSTALL_PREFIX=%s" % \
        (self.conanfile_directory, cmake.command_line, self.package_folder))
    self.run("cmake --build . %s" % cmake.build_config)

def package(self):
    self.run("cmake --build . --target install")

The package_folder attribute of the ConanFile instance is set before
calling its package method. Set this attribute before calling its
build method instead, so the build method of the ConanFile instance
can use this attribute for configuring the install prefix of the
project it builds.
@@ -31,7 +31,6 @@ def new_method(pattern, src=""):
conanfile.copy_bins = wrap(DEFAULT_BIN)
conanfile.copy_res = wrap(DEFAULT_RES)
try:
conanfile.package_folder = package_folder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't remove this. There might be some users using it.

@memsharded
Copy link
Member

Seems a very good idea, nice and practical solution. Just if you could leave the package_folder before the package() too, as some users might use that value there too. Also, if you have a working example, would you mind to copy and paste some snippets and open a docs issue? Or just open the issue. I would like to add this to the docs. Thanks very much for your contribution!

@robertmrk
Copy link
Contributor Author

I reverted the changes in the create_package function.
OK, I'll open an issue for the docs soon.
You're welcome. I'm glad I could help.

@memsharded memsharded merged commit ce762c8 into conan-io:develop Jun 14, 2016
@memsharded
Copy link
Member

Merged, it will be released in 0.10. Thanks again!

@memsharded memsharded added this to the 0.10 milestone Jun 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants