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

New cmake layout default src folder #2225

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
55 changes: 29 additions & 26 deletions reference/conanfile/tools/layout.rst
Expand Up @@ -68,29 +68,31 @@ attributes described before:

.. code:: python

def cmake_layout(conanfile, generator=None):
gen = conanfile.conf["tools.cmake.cmaketoolchain:generator"] or generator
if gen:
multi = "Visual" in gen or "Xcode" in gen or "Multi-Config" in gen
elif conanfile.settings.compiler == "Visual Studio" or conanfile.settings.compiler == "msvc":
multi = True
else:
multi = False

conanfile.folders.source = "src"
if multi:
conanfile.folders.build = "build"
conanfile.folders.generators = "build/conan"
else:
build_type = str(conanfile.settings.build_type).lower()
conanfile.folders.build = "cmake-build-{}".format(build_type)
conanfile.folders.generators = os.path.join(conanfile.folders.build, "conan")

conanfile.cpp.source.includedirs = ["."]
if multi:
conanfile.cpp.build.libdirs = ["{}".format(conanfile.settings.build_type)]
else:
conanfile.cpp.build.libdirs = ["."]
def cmake_layout(conanfile, generator=None):
gen = conanfile.conf["tools.cmake.cmaketoolchain:generator"] or generator
if gen:
multi = "Visual" in gen or "Xcode" in gen or "Multi-Config" in gen
elif conanfile.settings.compiler == "Visual Studio" or conanfile.settings.compiler == "msvc":
multi = True
else:
multi = False

conanfile.folders.source = "."
if multi:
conanfile.folders.build = "build"
conanfile.folders.generators = "build/conan"
else:
build_type = str(conanfile.settings.build_type).lower()
conanfile.folders.build = "cmake-build-{}".format(build_type)
conanfile.folders.generators = os.path.join(conanfile.folders.build, "conan")

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 @@ -106,8 +108,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 @@ -116,4 +119,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).