Skip to content

Commit

Permalink
New cmake layout default src folder (#2225)
Browse files Browse the repository at this point in the history
* Update docs for the new default cmake_layout() source folder

* Update creating_packages/getting_started.rst

Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>

* Fix wrong code block

Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
  • Loading branch information
tapia and czoido committed Sep 14, 2021
1 parent e68969f commit 5c15b20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
11 changes: 6 additions & 5 deletions creating_packages/getting_started.rst
Expand Up @@ -26,18 +26,19 @@ Using the :command:`conan new` command will create a "Hello World" C++ library e
$ mkdir hellopkg && cd hellopkg
$ conan new hello/0.1 --template=cmake_lib
File saved: conanfile.py
File saved: src/CMakeLists.txt
File saved: CMakeLists.txt
File saved: src/hello.cpp
File saved: src/hello.h
File saved: test_package/conanfile.py
File saved: test_package/src/CMakeLists.txt
File saved: test_package/CMakeLists.txt
File saved: test_package/src/example.cpp
The generated files are:

- **conanfile.py**: On the root folder, there is a *conanfile.py* which is the main recipe file, responsible for defining how the package is built and consumed.
- **src** folder: the *src* folder that contains the simple C++ "hello" library with a simple generic *CMakeLists.txt* to build it, with nothing specific about Conan in it.
- **conanfile.py**: On the root folder, there is a *conanfile.py* which is the main recipe file, responsible for defining how the package is built and consumed.
- **CMakeLists.txt**: A simple generic *CMakeLists.txt*, with nothing specific about Conan in it.
- **src** folder: the *src* folder that contains the simple C++ "hello" library.
- (optional) **test_package** folder: contains an *example* application that will require and link with the created package.
It is not mandatory, but it is useful to check that our package is correctly created.

Expand All @@ -59,7 +60,7 @@ Let's have a look at the package recipe *conanfile.py*:
default_options = {"shared": False, "fPIC": True}
# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "src/*"
exports_sources = "CMakeLists.txt", "src/*"
def config_options(self):
if self.settings.os == "Windows":
Expand Down
13 changes: 8 additions & 5 deletions reference/conanfile/tools/layout.rst
Expand Up @@ -78,7 +78,7 @@ attributes described before:
else:
multi = False
conanfile.folders.source = "src"
conanfile.folders.source = "."
if multi:
conanfile.folders.build = "build"
conanfile.folders.generators = "build/conan"
Expand All @@ -87,11 +87,13 @@ attributes described before:
conanfile.folders.build = "cmake-build-{}".format(build_type)
conanfile.folders.generators = os.path.join(conanfile.folders.build, "conan")
conanfile.cpp.source.includedirs = ["."]
conanfile.cpp.source.includedirs = ["src"]
if multi:
conanfile.cpp.build.libdirs = ["{}".format(conanfile.settings.build_type)]
conanfile.cpp.build.bindirs = ["{}".format(conanfile.settings.build_type)]
else:
conanfile.cpp.build.libdirs = ["."]
conanfile.cpp.build.bindirs = ["."]
First, it is important to notice that the layout depends on the CMake generator that will be used.
So if defined from ``[conf]``, that value will be used. If defined in recipe, then the recipe should
Expand All @@ -107,8 +109,9 @@ Finally, the location where the libraries are created also depends. For multi-co
will be located in a dedicated folder inside the build folder, while for single-config, the libraries will
be located directly in the build folder.

This helper defines a few things, for example that the source folder is called ``"src"``. This could be customized
without fully changing the layout:
This helper defines a few things, for example that the source folder is called ``"."``, meaning that Conan will
search the main `CMakeLists.txt` in the same directory were the conanfile is (most likely the project root).
This could be customized without fully changing the layout:

def layout(self):
cmake_layout(self)
Expand All @@ -117,4 +120,4 @@ without fully changing the layout:

Even if this pre-defined layout doesn't suit your specific projects layout, it is a good example how you could
implement your own logic (and probably put it in a common ``python_require`` if you are going to use it in multiple
packages).
packages).

0 comments on commit 5c15b20

Please sign in to comment.