Declaring the layout when the Conanfile is inside a subfolder

Please, first clone the sources to recreate this project. You can find them in the examples2 repository in GitHub:

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/examples/conanfile/layout/conanfile_in_subfolder

If we have a project intended to package the code that is in the same repo as the conanfile.py, but the conanfile.py is not in the root of the project:

  1. .
  2. ├── CMakeLists.txt
  3. ├── conan
  4. └── conanfile.py
  5. ├── include
  6. └── say.h
  7. └── src
  8. └── say.cpp

The conanfile.py would look like this:

  1. import os
  2. from conan import ConanFile
  3. from conan.tools.files import load, copy
  4. from conan.tools.cmake import CMake
  5. class PkgSay(ConanFile):
  6. name = "say"
  7. version = "1.0"
  8. settings = "os", "compiler", "build_type", "arch"
  9. generators = "CMakeToolchain"
  10. def layout(self):
  11. # The root of the project is one level above
  12. self.folders.root = ".."
  13. # The source of the project (the root CMakeLists.txt) is the source folder
  14. self.folders.source = "."
  15. self.folders.build = "build"
  16. def export_sources(self):
  17. # The path of the CMakeLists.txt and sources we want to export are one level above
  18. folder = os.path.join(self.recipe_folder, "..")
  19. copy(self, "*.txt", folder, self.export_sources_folder)
  20. copy(self, "src/*.cpp", folder, self.export_sources_folder)
  21. copy(self, "include/*.h", folder, self.export_sources_folder)
  22. def source(self):
  23. # Check that we can see that the CMakeLists.txt is inside the source folder
  24. cmake_file = load(self, "CMakeLists.txt")
  25. def build(self):
  26. # Check that the build() method can also access the CMakeLists.txt in the source folder
  27. path = os.path.join(self.source_folder, "CMakeLists.txt")
  28. cmake_file = load(self, path)
  29. cmake = CMake(self)
  30. cmake.configure()
  31. cmake.build()
  32. def package(self):
  33. cmake = CMake(self)
  34. cmake.install()

You can try and create the say package:

  1. $ cd conan
  2. $ conan create .

See also