conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
7.97k stars 952 forks source link

Trying to build a stripped down version of opencv custom package fails #16423

Open OroChippw opened 1 month ago

OroChippw commented 1 month ago

What is your question?

I am a C++ beginner and I am trying to use conan2 to manage a streamlined version of opencv4.10.0 (because most of the modules of opencv are not needed). When I try to use conan create to build, an error related to cmake.configure() appears. The following is my connanfile.py `import os import os.path as osp

from conan import ConanFile from conan.tools.cmake import CMakeToolchain , CMake , cmake_layout , CMakeDeps from conan.tools.files import copy

class opencvRecipe(ConanFile): name = "opencv" version = "4.10.0"

# Optional metadata
license = "MIT LICENSE"
author = "OroChi"
description = "A custom-built OpenCV package"
topics = ("opencv", "computer-vision")

# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False]}
default_options = {"shared": False}

# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt" , "include/*" "lib/*"

def layout(self):
    cmake_layout(self)

def generate(self):
    deps = CMakeDeps(self)
    deps.generate()
    tc = CMakeToolchain(self)
    tc.generate()

def build(self):
    cmake = CMake(self)
    cmake.verbose = True
    cmake.configure()
    cmake.build()

def package(self):
    copy(self, "*.h", src=osp.join(self.source_folder , "include") , dst=osp.join(self.package_folder, "include") , keep_path=True)
    copy(self, "*.hpp", src=osp.join(self.source_folder , "include") , dst=osp.join(self.package_folder, "include") , keep_path=True)
    copy(self, "*.a", src=osp.join(self.source_folder , "bin") , dst=osp.join(self.package_folder, "bin") , keep_path=True)
    copy(self, "*.so", src=osp.join(self.source_folder , "lib") , dst=osp.join(self.package_folder, "lib") , keep_path=True)

def package_info(self):
    self.cpp_info.libs = ["opencv_calib3d", "opencv_core", 
        "opencv_dnn", "opencv_features2d", "opencv_flann", 
        "opencv_gapi", "opencv_highgui", "opencv_imgcodecs", 
        "opencv_imgproc", "opencv_ml"]

` The following is my CMakeLists.txt for building the package

cmake_minimum_required(VERSION 3.15)
project(opencv)

set(CMAKE_CXX_STANDARD 11)
set(IMPORTED_LIBRARIES opencv_calib3d opencv_core opencv_dnn
    opencv_features2d opencv_flann opencv_gapi opencv_highgui 
    opencv_imgcodecs opencv_imgproc opencv_ml)

foreach(lib ${IMPORTED_LIBRARIES})
    set(lib_path ${CMAKE_SOURCE_DIR}/lib/lib${lib}.so)
    if(NOT EXISTS ${lib_path})
        message(FATAL_ERROR "Library ${lib_path} not found!")
    endif()
    add_library(${lib} SHARED IMPORTED)
    set_target_properties(${lib} PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/lib${lib}.so) 
endforeach()

When I run conan create ., I get an error

CMake Error at CMakeLists.txt:22 (message):
  Library /home/rsv/.conan2/p/b/openc8942aeaecbf7f/b/lib/libopencv_calib3d.so
  not found!
  opencv_rsv/4.10.0: ERROR: 
Package '692cc5ee9412f21723ff881ad1e17768734d5e69' build failed
opencv_rsv/4.10.0: WARN: Build folder /home/rsv/.conan2/p/b/openc8942aeaecbf7f/b/build/Release
ERROR: opencv_rsv/4.10.0: Error in build() method, line 38
        cmake.configure()
        ConanException: Error 1 while executing

When I cd to the/.conan2/p/b/openc8942aeaecbf7f/b directory, I found that my source code and the files under lib were not copied to that location. How can I solve this problem? Thank you.

Have you read the CONTRIBUTING guide?

OroChippw commented 1 month ago

When the following message appears on my console, does it mean that I have built successfully?

opencv_rsv/4.10.0: Running CMake.build()
opencv_rsv/4.10.0: RUN: cmake --build "/home/rsv/.conan2/p/b/openc55d2287229835/b/build/Release" -- -j20

opencv_rsv/4.10.0: Package '692cc5ee9412f21723ff881ad1e17768734d5e69' built
opencv_rsv/4.10.0: Build folder /home/rsv/.conan2/p/b/openc55d2287229835/b/build/Release
opencv_rsv/4.10.0: Generating the package
opencv_rsv/4.10.0: Packaging in folder /home/rsv/.conan2/p/b/openc55d2287229835/p
opencv_rsv/4.10.0: Calling package()
opencv_rsv/4.10.0: package(): Packaged 221 '.hpp' files
opencv_rsv/4.10.0: package(): Packaged 48 '.h' files
opencv_rsv/4.10.0: package(): Packaged 10 '.so' files
opencv_rsv/4.10.0: Created package revision 228f7cb6d4b41d863ff9ab8f16c1859e
opencv_rsv/4.10.0: Package '692cc5ee9412f21723ff881ad1e17768734d5e69' created
opencv_rsv/4.10.0: Full package reference: opencv_rsv/4.10.0#2cd1b8aae68bc52a9c492e2539dc789f:692cc5ee9412f21723ff881ad1e17768734d5e69#228f7cb6d4b41d863ff9ab8f16c1859e
opencv_rsv/4.10.0: Package folder /home/rsv/.conan2/p/b/openc55d2287229835/p

When I tried to build test_package to call opencv, I first ran conan install .in the test_package directory. When I cd into the build directory and ran cmake .., an error message appeared.

CMake Error at CMakeLists.txt:7 (find_package):
  By not providing "Findopencv_rsv.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "opencv_rsv", but CMake did not find one.

  Could not find a package configuration file provided by "opencv_rsv" with
  any of the following names:

    opencv_rsvConfig.cmake
    opencv_rsv-config.cmake

  Add the installation prefix of "opencv_rsv" to CMAKE_PREFIX_PATH or set
  "opencv_rsv_DIR" to a directory containing one of the above files.  If
  "opencv_rsv" provides a separate development package or SDK, be sure it has
  been installed.
memsharded commented 1 month ago

Hi @OroChippw

Thanks for your question.

I am afraid it is not clear what you are trying to do. Are you putting the conanfile.py inside the opencv source repo? Otherwise the exports doesn't make sense at all, and you need a source() method to download the opencv sources. And not provide your own CMakeLists.txt to build opencv, that is generally not a good idea and the recommendation is to use the opencv original build scripts.

The following is my CMakeLists.txt for building the package

set(CMAKE_CXX_STANDARD 11) set(IMPORTED_LIBRARIES opencv_calib3d opencv_core opencv_dnn opencv_features2d opencv_flann opencv_gapi opencv_highgui opencv_imgcodecs opencv_imgproc opencv_ml)

This doesn't make sense. Aren't you building the opencv package? If you are building it, why would you be importing pre-existing compiled libraries?

When I tried to build test_package to call opencv, I first ran conan install . in the test_package directory. When I cd into the build directory and ran cmake .., an error message appeared.

You should never run conan install inside a test_package folder, this is done automatically, please check https://docs.conan.io/2/tutorial/creating_packages/test_conan_packages.html

I am afraid that I don't understand what you are trying to do.

If you are going to create conan packages, I'd suggest to do the full tutorial in https://docs.conan.io/2/tutorial.html, it will explain the basics about using and creating packages.

OroChippw commented 1 month ago

Sorry that my description is not clear and has caused you trouble. My purpose is to compile a streamlined opencv4.10.0 that only uses some modules.

  1. I downloaded the source code from the official repository of opencv and compiled it in the Linux environment (removed the modules I don’t need). After compilation, I got the header file of opencv and the corresponding *.so link library file.
  2. Based on the compiled opencv source code, I tried to build a local opencv package and use conan2 to manage it. I built the file conanfile.py in the compiled directory (this seems successful? When I use conan list opencv_rsv to check, it shows that the cache has been loaded)
  3. In the third step, I tried to imitate example, build test_package, call opencv_rsv, when I cd into the test_package directory and run cmake.., it shows the error as follows when using the package like a normal project,How should I solve this problem? Or where is my thinking and steps wrong?

    
    CMake Error at CMakeLists.txt:7 (find_package):
    By not providing "Findopencv_rsv.cmake" in CMAKE_MODULE_PATH this project
    has asked CMake to find a package configuration file provided by
    "opencv_rsv", but CMake did not find one.
    
    Could not find a package configuration file provided by "opencv_rsv" with
    any of the following names:
    
    opencv_rsvConfig.cmake
    opencv_rsv-config.cmake
    
    Add the installation prefix of "opencv_rsv" to CMAKE_PREFIX_PATH or set
    "opencv_rsv_DIR" to a directory containing one of the above files.  If
    "opencv_rsv" provides a separate development package or SDK, be sure it has
    been installed.

-- Configuring incomplete, errors occurred!

The content of my CMakeList.txt file is as follows

cmake_minimum_required(VERSION 3.15) project(opencv_rsv_test CXX)

set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(opencv_rsv REQUIRED)

add_executable(main src/main.cpp)


Yes, I should use conan test test_package for testing, and should not use conan install. (Thank you for pointing this out)
Thank you very much for your reply! Sorry for the trouble.😶
memsharded commented 1 month ago

I downloaded the source code from the official repository of opencv and compiled it in the Linux environment (removed the modules I don’t need). After compilation, I got the header file of opencv and the corresponding *.so link library file.

Why wouldn't you do it in the source() and build() methods of the recipe? They are there exactly for this purpose.

Based on the compiled opencv source code, I tried to build a local opencv package and use conan2 to manage it. I built the file conanfile.py in the compiled directory (this seems successful? When I use conan list opencv_rsv to check, it shows that the cache has been loaded)

This is typically done with the conan export-pkg, to package existing pre-compiled binaries. This step would be mostly the package() method, and it doesn't need any kind of extra CMakeLists.txt. I still don't understand what your extra CMakeLists.txt is trying to do. You can check https://docs.conan.io/2/tutorial/creating_packages/other_types_of_packages/package_prebuilt_binaries.html#packaging-already-pre-built-binaries. Still, much more recommended to use source() and build() to automate that.

In the third step, I tried to imitate example, build test_package, call opencv_rsv, when I cd into the test_package directory and run cmake.., it shows the error as follows when using the package like a normal project,How should I solve this problem? Or where is my thinking and steps wrong?

This shouldn't be done. The test_package will be automatically called by Conan when calling either conan create or conan export-pkg, but you should not cd into the folder and do nothing. In the examples or in the docs, this is never done.

Maybe you can start from the test_package of the ConanCenter recipe in conan-center-index repo?

If not, another good idea is to share a repo with your code, including the test_package so we can reproduce it, that would really help to check what could be missing.

OroChippw commented 1 month ago

According to the tutorial you provided, https://docs.conan.io/2/tutorial/creating_packages/other_types_of_packages/package_prebuilt_binaries.html#packaging-already-pre-built-binaries.I rebuilt the package. (I will try to use source() and build() methods for automated building later. Thank you.😶) If I have a brand new project and want to use the custom package in conan2, how should I build my CMakeLists.txt? Is my following construction method correct?

cmake_minimum_required(VERSION 3.15)
project(opencv_rsv_test CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(opencv_rsv CONFIG REQUIRED)

add_executable(main src/main.cpp)

Should I use conanfile.txt in combination with CMakeLists.txt as in the tutorial https://docs.conan.io/2/tutorial/consuming_packages/use_tools_as_conan_packages.html, enter the build directory, and activate the environment with $ source conanbuild.sh, and then perform subsequent operations such as cmake..

Thank you again for your reply and advice.❤️

memsharded commented 1 month ago

If I have a brand new project and want to use the custom package in conan2, how should I build my CMakeLists.txt? Is my following construction method correct?

There are some issues:

The link that you are providing is for another use case, which is using tools. OpenCV is mostly a library, not a tool (such as CMake), so it doesn't have to be managed as a tool.

The basic tutorial for consuming conan packages is located in https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html. There it will explain the basics, to use some existing packages: