如何在不复制源代码的情况下使用Conan构建我的项目。

huangapple go评论138阅读模式
英文:

How can build my project with conan without copying sources

问题

我有一个使用CMake配置并使用conan create构建的C/C++项目,使用conanfile.py

我不需要将我的源代码导出到我将要上传的包中。我只需要将生成的库放入包中。因此,在conanfile.py中,我不需要使用export_sources。此外,export()方法(或其等效的export属性)我不知道为什么会花费太多时间将我的源文件从其原始文件夹复制到Conan缓存文件夹中,尽管只有一个1KB大小的.txt文件。

首先,我想描述一下我所说的每种类型的文件夹:

**原始文件夹:**路径conanfile.py以及CMakeLists.txt和我的源代码(.cpp),在src目录中。还请注意,我在这个路径中执行conan create

Conan缓存文件夹:Conan创建自己的临时文件夹以工作:构建、源、导出等等。在conanfile.pysource()build()等方法中,ConanFile的属性,如source_folder,已经指向这些缓存文件夹,而不是原始文件夹。还请注意,Conan还将conanfile.py复制到这些缓存文件夹中,并且__file__的路径也指向这些缓存文件夹。

因此,在build()方法中,当我执行"self.run('cmake'"时,我需要引用原始文件夹,但据我所知,ConanFile没有它。这对我来说似乎很奇怪,因为我不认为我的情况那么奇怪。我希望Conan已经为此提供了解决方案。

那么,这是一种不好的做法,还是Conan有我不知道的解决方案?

请注意,no_copy_source属性不会产生差异,因为它仅涉及Conan缓存文件夹之间的不复制。

如果无论如何我需要将我的源代码从原始路径复制到Conan缓存文件夹中,为什么exportcopy甚至对于同一台机器上的单个1KB文件也需要太多时间?(通过太多时间,我指的是,复制那个小文件花费了1分钟。它在等待什么?)

在任何情况下,我应该如何操作?

conanfile.py

from conans import ConanFile, CMake, tools
import shutil, os, inspect

class MyConan(ConanFile):
    name = "MyLib"
    version = "0.0.1"
    generators = "cmake"
    no_copy_source = True
    exports = "CMakeLists.txt" # 复制它需要太多时间

    def source(self):
        current_dir = os.getcwd() # 这已经指向缓存文件夹

    def build(self):
        cmake = CMake(self)
        # self.run('cmake %s --preset="my-preset"') 如何引用我的原始源文件夹?或者如何复制我的源代码到Conan缓存文件夹而不花费太多时间
        # cmake.build()

命令:

cd /conanfile/和/sources的路径
conan create . 0.0.1/whatever/hotfix
英文:

I have C/C++ project configuring with cmake and building with conan create, using conanfile.py.

I do not need to export my sources in packages that I will upload. I need only put the generated library in the package. So, I do not need to use export_sources in conanfile.py. Also, export() method (or its equivalent export attribute), I do not know why, but takes too much time to copy my source files from its original folder into conan cache folders, although there is only one 1KB small .txt file.

First, I want to describe what I mean with each type of folder:

Original folder: The path conanfile.py as well as CMakeLists.txt and my sources(.cpp), in src directory. Also note that I execute conan create within this path.

Conan cache folders: Conan creates its own temporary folders to work: build, source, export, etc. In source(), build(), etc. methods of conanfile.py, attributes like source_folder of ConanFile already refers to these cache folders, and not to original folder. Also note that conan copies conanfile.py also in these cache folders and the path of __file__ also gives these cache folders.

So, in build() method, when I do "self.run('cmake " I need to refer to the original folder but ConanFile does not have it, as far as I know. It seemed to strange to me because I do not believe that my case is so strange. I would expect that Conan already has a solution for this.

So, is this a bad practice or is there a solution of Conan that I do not know?

Note that no_copy_source attribute does not make a difference, because it is about only no copying between Conan cache folders.

If anyway I need to copy my sources from the original path into the Conan cache folders, why export, or copy takes too much time even for a single 1kb file of same machine? (With too much time I mean, for copying that small file, it got stuck 1 minute. What does it wait for?)

Either case, how should I do?

conanfile.py

from conans import ConanFile, CMake, tools
import shutil, os, inspect

class MyConan(ConanFile):
    name = "MyLib"
    version = "0.0.1"
    generators = "cmake"
    no_copy_source = True
    exports = "CMakeLists.txt" # Takes too much time to copy it

    def source(self):
        current_dir = os.getcwd() # This already points to cache folders
        

    def source(self):
        # self.source_folder is pointing to cache folders. I have already lost path of original folder 


    def build(self):
        cmake = CMake(self)
        #self.run('cmake %s --preset="my-preset"') HOW TO REFER TO my original source folder? Or HOW TO copy my sources to Conan cache folders without taking too much time
        #cmake.build()

command:

cd /path/of/conanfile/and/sources
conan create . 0.0.1/whatever/hotfix

答案1

得分: 0

Conan通过“conan create”在Conan缓存中构建时,总会以某种方式复制源代码。目标是确保从源代码创建的包是完全可重现的,同时保证了干净的构建。

如果你不想上传源代码的副本,你可以使用“scm”方法,它会捕获坐标(URL,提交),而不是进行复制。但仍然会在Conan缓存中进行新的克隆/检出。目标是确保事情完全可行。

如果你只想打包你本地构建的最终二进制文件,你可以使用“conan export-pkg”功能(参见此链接)。但这不允许以后构建包,例如自动添加新配置,也不允许使用“--build=missing”,例如。

英文:

The build in the Conan cache via conan create will always do a copy somehow of the sources. The goal is to make sure the package creation is fully reproducible from source, and also guaranteeing a clean build.

If you don't want to upload a copy of the sources, you can use the scm approach, which captures the coordinates (url, commit), instead of doing a copy. Still, it will do a new clone/checkout in the Conan cache. The goal is to make things fully

If you only want to package the final binaries, that you build locally, you can use the conan export-pkg functionality (see this link). But this won't allow building the package later, for example to automatically add a new configuration, and will not allow the --build=missing, for example.

huangapple
  • 本文由 发表于 2023年6月12日 19:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定