Package Development Flow

This section introduces the Conan local development flow, which allows you to work on packages in your local project directory without having to export the contents of the package to the Conan cache first.

This local workflow encourages users to perform trial-and-error in a local sub-directory relative to their recipe, much like how developers typically test building their projects with other build tools. The strategy is to test the conanfile.py methods individually during this phase.

Let’s use this flow for the hello package we created in the previous section.

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

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/tutorial/developing_packages/local_package_development_flow

You can check the contents of the folder:

  1. .
  2. ├── conanfile.py
  3. └── test_package
  4. ├── CMakeLists.txt
  5. ├── conanfile.py
  6. └── src
  7. └── example.cpp

conan source

You will generally want to start with the conan source command. The strategy here is that you’re testing your source method in isolation and downloading the files to a temporary sub-folder relative to the conanfile.py. This relative folder is defined by the self.folders.source property in the layout() method. In this case, as we are using the pre-defined cmake_layout we set the value with the src_folder argument.

Note

In this example we are packaging a third-party library from a remote repository. In the case you have your sources beside your recipe in the same repository, running conan source will not be necessary for most of the cases.

Let’s have a look at the recipe’s source() and layout() method:

  1. ...
  2. def source(self):
  3. # Please be aware that using the head of the branch instead of an immutable tag
  4. # or commit is not a good practice in general.
  5. get(self, "https://github.com/conan-io/libhello/archive/refs/heads/main.zip",
  6. strip_root=True)
  7. def layout(self):
  8. cmake_layout(self, src_folder="src")
  9. ...

Now run the conan source command and check the results:

  1. $ conan source .
  2. conanfile.py (hello/1.0): Calling source() in /Users/.../local_package_development_flow/src
  3. Downloading main.zip
  4. conanfile.py (hello/1.0): Unzipping 3.7KB
  5. Unzipping 100%

You can see that a new src folder has appeared containing all the hello library sources.

  1. .
  2. ├── conanfile.py
  3. ├── src
  4. ├── CMakeLists.txt
  5. ├── LICENSE
  6. ├── README.md
  7. ├── include
  8. └── hello.h
  9. └── src
  10. └── hello.cpp
  11. └── test_package
  12. ├── CMakeLists.txt
  13. ├── conanfile.py
  14. └── src
  15. └── example.cpp

Now it’s easy to check the sources and validate them. Once you’ve got your source method right and it contains the files you expect, you can move on to testing the various attributes and methods related to downloading dependencies.

conan install

After running the conan source command, you can run the conan install command. This command will install all the recipe requirements if needed and prepare all the files necessary for building by running the generate() method.

We can check all the parts from our recipe that are involved in this step:

  1. ...
  2. class helloRecipe(ConanFile):
  3. ...
  4. generators = "CMakeDeps"
  5. ...
  6. def layout(self):
  7. cmake_layout(self, src_folder="src")
  8. def generate(self):
  9. tc = CMakeToolchain(self)
  10. tc.generate()
  11. ...

Now run the conan install command and check the results:

  1. $ conan install .
  2. ...
  3. -------- Finalizing install (deploy, generators) --------
  4. conanfile.py (hello/1.0): Writing generators to ...
  5. conanfile.py (hello/1.0): Generator 'CMakeDeps' calling 'generate()'
  6. conanfile.py (hello/1.0): Calling generate()
  7. ...
  8. conanfile.py (hello/1.0): Generating aggregated env files

You can see that a new build folder appeared with all the files that Conan needs for building the library like a toolchain for CMake and several environment configuration files.

  1. .
  2. ├── build
  3. └── Release
  4. └── generators
  5. ├── CMakePresets.json
  6. ├── cmakedeps_macros.cmake
  7. ├── conan_toolchain.cmake
  8. ├── conanbuild.sh
  9. ├── conanbuildenv-release-x86_64.sh
  10. ├── conanrun.sh
  11. ├── conanrunenv-release-x86_64.sh
  12. ├── deactivate_conanbuild.sh
  13. └── deactivate_conanrun.sh
  14. ├── conanfile.py
  15. ├── src
  16. ├── CMakeLists.txt
  17. ├── CMakeUserPresets.json
  18. ├── LICENSE
  19. ├── README.md
  20. ├── include
  21. └── hello.h
  22. └── src
  23. └── hello.cpp
  24. └── test_package
  25. ├── CMakeLists.txt
  26. ├── conanfile.py
  27. └── src
  28. └── example.cpp

Now that all the files necessary for building are generated, you can move on to testing the build() method.

conan build

Running the After conan build command will invoke the build() method:

  1. ...
  2. class helloRecipe(ConanFile):
  3. ...
  4. def build(self):
  5. cmake = CMake(self)
  6. cmake.configure()
  7. cmake.build()
  8. ...

Let’s run conan build:

  1. $ conan build .
  2. ...
  3. -- Conan toolchain: C++ Standard 11 with extensions ON
  4. -- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
  5. -- Configuring done
  6. -- Generating done
  7. -- Build files have been ...
  8. conanfile.py (hello/1.0): CMake command: cmake --build ...
  9. conanfile.py (hello/1.0): RUN: cmake --build ...
  10. [100%] Built target hello

For most of the recipes, the build() method should be very simple, and you can also invoke the build system directly, without invoking Conan, as you have all the necessary files available for building. If you check the contents of the src folder, you’ll find a CMakeUserPresets.json file that you can use to configure and build the conan-release preset. Let’s try it:

  1. $ cd src
  2. $ cmake --preset conan-release
  3. ...
  4. -- Configuring done
  5. -- Generating done
  6. $ cmake --build --preset conan-release
  7. ...
  8. [100%] Built target hello

You can check that the results of invoking CMake directly are equivalent to the ones we got using the conan build command.

Note

We use CMake presets in this example. This requires CMake >= 3.23 because the “include” from CMakeUserPresets.json to CMakePresets.json is only supported since that version. If you prefer not to use presets you can use something like:

  1. cmake <path> -G <CMake generator> -DCMAKE_TOOLCHAIN_FILE=<path to
  2. conan_toolchain.cmake> -DCMAKE_BUILD_TYPE=Release

Conan will show the exact CMake command everytime you run conan install in case you can’t use the presets feature.

conan export-pkg

Now that we built the package binaries locally we can also package those artifacts in the Conan local cache using the conan export-pkg command. Please note that this command will create the package in the Conan cache and test it running the test_package after that.

  1. $ conan export-pkg .
  2. conanfile.py (hello/1.0) package(): Packaged 1 '.h' file: hello.h
  3. conanfile.py (hello/1.0) package(): Packaged 1 '.a' file: libhello.a
  4. conanfile.py (hello/1.0): Package 'b1d267f77ddd5d10d06d2ecf5a6bc433fbb7eeed' created
  5. conanfile.py (hello/1.0): Created package revision f09ef573c22f3919ba26ee91ae444eaa
  6. ...
  7. conanfile.py (hello/1.0): Package folder /Users/...
  8. conanfile.py (hello/1.0): Exported package binary
  9. ...
  10. [ 50%] Building CXX object CMakeFiles/example.dir/src/example.cpp.o
  11. [100%] Linking CXX executable example
  12. [100%] Built target example
  13. -------- Testing the package: Running test() --------
  14. hello/1.0 (test package): Running test()
  15. hello/1.0 (test package): RUN: ./example
  16. hello/1.0: Hello World Release!
  17. hello/1.0: __x86_64__ defined
  18. hello/1.0: __cplusplus201103
  19. hello/1.0: __GNUC__4
  20. hello/1.0: __GNUC_MINOR__2
  21. hello/1.0: __clang_major__14
  22. hello/1.0: __apple_build_version__14000029

Now you can list the packages in the local cache and check that the hello/1.0 package was created.

  1. $ conan list hello/1.0
  2. Local Cache
  3. hello
  4. hello/1.0

See also