conan icon indicating copy to clipboard operation
conan copied to clipboard

[bug] ignored --source-folder option while using 'conan build ...' command with CMakeToolchain and cmake_layout

Open todorico opened this issue 3 years ago • 7 comments
trafficstars

In package development flow using the conan build ... command, I get an error with the CMakeToolchain configure method, because the --source-folder option is ignored.

Environment Details (include every applicable attribute)

  • Operating System+version: Windows 10
  • Compiler+version: Visual Studio 16
  • Conan version: 1.51.0
  • Python version: 3.9.13

Steps to reproduce (Include if Applicable)

I can reproduce the issue using the fmt recipe written for 2.0 conan here: recipe

  1. cd to the fmt recipe folder
  2. use those package development flow commands:
conan install . fmt/9.0.0@user/channel --install-folder out --output-folder out
conan source . --install-folder out --source-folder out/source
conan build . --source-folder out/source --build-folder out

The conan build ... command will fail because the CMake configure command will always use the conan recipe folder as source folder and not the one specified in the command line.

Logs (Executed commands with output) (Include/Attach if Applicable)

Using lockfile: 'C:\Users\t.odorico\Downloads\conan_test\conan-center-index\recipes\fmt\all\out/conan.lock'
Using cached profile from lockfile
conanfile.py (fmt/9.0.0@user/channel): Calling build()
conanfile.py (fmt/9.0.0@user/channel): CMake command: cmake -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="C:/Users/t.odorico/Downloads/conan_test/conan-center-index/recipes/fmt/all/out/build/generators/conan_toolchain.cmake" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DFMT_DOC="False" -DFMT_TEST="False" -DFMT_INSTALL="True" -DFMT_LIB_DIR="lib" -DFMT_OS="True" "C:\Users\t.odorico\Downloads\conan_test\conan-center-index\recipes\fmt\all\."
CMake Warning:
  Ignoring extra path from command line:

   "C:\Users\t.odorico\Downloads\conan_test\conan-center-index\recipes\fmt\all\."


CMake Error: The source directory "C:/Users/t.odorico/Downloads/conan_test/conan-center-index/recipes/fmt/all" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
ERROR: conanfile.py (fmt/9.0.0@user/channel): Error in build() method, line 85
        cmake.configure(variables=cache_entries)
        ConanException: Error 1 while executing cmake -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="C:/Users/t.odorico/Downloads/conan_test/conan-center-index/recipes/fmt/all/out/build/generators/conan_toolchain.cmake" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" -DFMT_DOC="False" -DFMT_TEST="False" -DFMT_INSTALL="True" -DFMT_LIB_DIR="lib" -DFMT_OS="True" "C:\Users\t.odorico\Downloads\conan_test\conan-center-index\recipes\fmt\all\."

todorico avatar Aug 03 '22 09:08 todorico

Hi @todorico,

The new version of fmt uses the package layout feature, specifically the cmake_layout (you can read more about this in the Conan docs)

I think that for what you are trying to do, this would make it:

conan install . fmt/9.0.0@user/channel -if=out
conan source . -if=out
conan build . -if=out

Can you check?

Hope this helps.

czoido avatar Aug 03 '22 10:08 czoido

Hi @czoido,

In this case it will work because the sources will be extracted in the conanfile folder by default and the CMakeToolchain call configure() on this folder (prefix_dirs…/fmt/all). However, using the --source-folder out/source option, I was expecting the CMakeToolchain to call configure() on this folder (prefix_dirs…/fmt/all/out/source) even using cmake_layout in the conanfile.

I don't understand if it is the intended behavior to ignore the --source-folder option. I find it surprising because the other options (--install-folder, --build-folder, etc…) are not.

I wanted to override the source folder using the --source-folder option to keep my working tree clean while developing packages. Taking the fmt recipe as an example:

If I use those commands:

conan install . fmt/9.0.0@user/channel --install-folder out --output-folder out
conan source . --install-folder out --source-folder out/source
conan build . --source-folder out/source --build-folder out

It will generate a clean working tree with the following tree

fmt/all/
    patches/
    test_package/
    out/
         build/
         source/
         package/
    conandata.yml
    conanfile.py

But if I use those commands:

conan install . fmt/9.0.0@user/channel -if=out
conan source . -if=out
conan build . -if=out

Everything will be mixed in the same folder, which I don't find practical for iterating on my conanfile. If it's not possible to override the source folder with --source-folder would it be possible to store everything in a same prefix folder ?

Thanks for your time and quick answers 😄

todorico avatar Aug 03 '22 12:08 todorico

Hi @todorico,

I'm afraid that if you want this to work with the cmake_layout() you have to set the source folder there, something like:

    def layout(self):
        cmake_layout(self, src_folder="out/src")

If not, when you do the conan build, and following the cmake_layout, Conan will interpret that the sources are in .

czoido avatar Aug 03 '22 12:08 czoido

Ok, so the cmake_layout source folder has priority over the --source-folder CLI option. As I found it surprising, I wasn't sure it was the intended behavior. Thanks a lot for clarifying this.

todorico avatar Aug 03 '22 13:08 todorico

Last thing I want to ask you @czoido, if in this case the cmake_layout has priority, I wonder if there could be a mechanism to override the default cmake_layout(src_folder) value globally without changing the conanfile ? I saw that there is tools.cmake.cmake_layout:build_folder_vars in configuration, is there a thing like tools.cmake.cmake_layout:src_folder ? It would be very useful for my use case !

todorico avatar Aug 03 '22 13:08 todorico

Hi @todorico,

I'll mark this as look:into so I can discuss with the team.

Thanks for the feedback!

czoido avatar Aug 04 '22 10:08 czoido

Thank you very much for your time @czoido!

todorico avatar Aug 04 '22 14:08 todorico