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:

$ git clone
$ 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, but the is not in the root of the project:

├── CMakeLists.txt
├── conan
│   └──
├── include
│   └── say.h
└── src
    └── say.cpp

The would look like this:

import os
from conan import ConanFile
from import load, copy
from import CMake

class PkgSay(ConanFile):
    name = "say"
    version = "1.0"
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeToolchain"

    def layout(self):
        # The root of the project is one level above
        self.folders.root = ".."
        # The source of the project (the root CMakeLists.txt) is the source folder
        self.folders.source = "." = "build"

    def export_sources(self):
        # The path of the CMakeLists.txt and sources we want to export are one level above
        folder = os.path.join(self.recipe_folder, "..")
        copy(self, "*.txt", folder, self.export_sources_folder)
        copy(self, "src/*.cpp", folder, self.export_sources_folder)
        copy(self, "include/*.h", folder, self.export_sources_folder)

    def source(self):
        # Check that we can see that the CMakeLists.txt is inside the source folder
        cmake_file = load(self, "CMakeLists.txt")

    def build(self):
        # Check that the build() method can also access the CMakeLists.txt in the source folder
        path = os.path.join(self.source_folder, "CMakeLists.txt")
        cmake_file = load(self, path)

        cmake = CMake(self)

    def package(self):
        cmake = CMake(self)

You can try and create the say package:

$ cd conan
$ conan create .