Conan with cmake-conan missing conanbuild.conf - c++

I am want to package a CMake project with conan.
For that I use the following conanfile.py:
import os
from conans import ConanFile, tools
from conan.tools.cmake import CMake, CMakeToolchain
from conans.tools import Version
class BaseLibrary(ConanFile):
name = "base-library"
version = "1.0.0"
description = """This is a test project with a library base::io
and base::math and an executable cli."""
license = "MIT"
generators = "cmake_find_package_multi", "cmake_find_package",
default_options = {"fmt:shared": True}
build_policy = "missing" # if this package is build by default if missing.
settings = "os", "compiler", "build_type", "arch"
exports_sources = "*"
_cmake = None
def requirements(self):
if Version(self.version) >= "1.0.0":
self.requires("fmt/8.0.1")
def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.configure(source_folder=".")
return self._cmake
def build(self):
cmake = self._configure_cmake()
cmake.build()
cmake.install()
def package(self):
cmake = self._configure_cmake()
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
tools.rmdir(os.path.join(self.package_folder, "share"))
def package_info(self):
self.cpp_info.names["cmake_find_package"] = "base"
self.cpp_info.names["cmake_find_package_multi"] = "base"
self.cpp_info.names["pkg_config"] = "base"
which is next to my main CMakeLists.txt file. The CMake project builds without problems and also has a proper install target which installs everything properly: bin,lib,include,share. In CMake I use the conan-cmake module with basically something like this.
When I run
conan create -s build_type=Release . demo/testing
I get the following weird error:
...
Requirements
base-library/1.0.0#demo/testing from local cache - Cache
fmt/8.0.1 from 'conancenter' - Cache
Packages
base-library/1.0.0#demo/testing:4f2b14d304ab8e4391d162a6eb44110cc27a3faa - Build
fmt/8.0.1:d4e9c4f02b4f03edf5a640dcd22779727d782e79 - Cache
Installing (downloading, building) binaries...
fmt/8.0.1: Already installed!
base-library/1.0.0#demo/testing: WARN: Build folder is dirty, removing it: /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa
base-library/1.0.0#demo/testing: Configuring sources in /home/developer/.conan/data/base-library/1.0.0/demo/testing/source
base-library/1.0.0#demo/testing: Copying sources to build folder
base-library/1.0.0#demo/testing: Building your package in /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa
base-library/1.0.0#demo/testing: Generator cmake_find_package created Findfmt.cmake
base-library/1.0.0#demo/testing: Generator cmake_find_package_multi created fmt-config-version.cmake
base-library/1.0.0#demo/testing: Generator cmake_find_package_multi created fmt-config.cmake
base-library/1.0.0#demo/testing: Generator cmake_find_package_multi created fmtTargets.cmake
base-library/1.0.0#demo/testing: Generator cmake_find_package_multi created fmtTarget-release.cmake
base-library/1.0.0#demo/testing: Aggregating env generators
base-library/1.0.0#demo/testing: Calling build()
base-library/1.0.0#demo/testing:
base-library/1.0.0#demo/testing: ERROR: Package '4f2b14d304ab8e4391d162a6eb44110cc27a3faa' build failed
base-library/1.0.0#demo/testing: WARN: Build folder /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa
ERROR: base-library/1.0.0#demo/testing: Error in build() method, line 74
cmake = self._configure_cmake()
while calling '_configure_cmake', line 65
self._cmake = CMake(self)
ConanException: The file /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa/conanbuild.conf does not exist. Please, make sure that it was not generated in another folder.
What is the problem here and how can I resolve this? I could not find anything related to this?

from conans import ConanFile, CMake
...
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
...

I had the same problem and finally found the issue:
The CMake you are importing is the one working with the CMakeToolchain which are both part of the conan.tools.cmake module.
The one that is described in the usage of cmake_find_package[ref] is the one in conans module
So replacing:
from conans import ConanFile, tools
from conan.tools.cmake import CMake, CMakeToolchain
by
from conans import ConanFile, tools, CMake
from conan.tools.cmake import CMakeToolchain
should fix your problem

Related

Conan not respecting compiler.libcxx abi setting

I have the following conan profile:
$ conan profile show default
Configuration for profile default:
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=11
compiler.cppstd=20
compiler.libcxx=libstdc++
build_type=Release
[options]
[conf]
[build_requires]
[env]
Note that compiler.libcxx is the old ABI (libstdc++) and not the new ABI (libstdc++11). This is intentional. Also I'm specifying cppstd=20.
I then made a custom conanfile for {fmt}, which tries to package up fmt 9.0. That looks like, in its entirety:
from conans import ConanFile, CMake, tools
from conan.tools.cmake import CMakeToolchain, cmake_layout
class ConanFmt(ConanFile):
name = "fmt"
version = "9.0.0"
settings = "os", "compiler", "cppstd", "build_type", "arch"
def source(self):
git = tools.Git(folder="fmt")
git.clone("https://github.com/fmtlib/fmt.git", "9.0.0", shallow=True)
def layout(self):
cmake_layout(self)
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def build(self):
cmake = CMake(self)
cmake.definitions["FMT_DOC"] = False
cmake.definitions["FMT_TEST"] = False
cmake.definitions["FMT_INSTALL"] = True
cmake.definitions["CMAKE_EXPORT_COMPILE_COMMANDS"] = True
cmake.configure(source_folder="fmt")
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
def package_info(self):
self.cpp_info.libs = ["fmt"]
When I try to go ahead and package this up, like say:
$ conan create . demo/testing -s build_type=Release
This does actually go ahead and clone the {fmt} repo, and correctly build and package up the contents of the library.
Except that:
there is no _GLIBCXX_USE_CXX11_ABI definition set (despite what this page) says.
{fmt} was compiled with C++11, not C++20
... but it was built with Release (-O3 -DNDEBUG)
Conan does appear to provide variables like CONAN_CMAKE_CXX_STANDARD=20, CONAN_STD_CXX_FLAG=-std=c++2a, and CONAN_LIBCXX=libstdc++. But {fmt}'s build doesn't actually use those, so that isn't very useful.
How do I actually get conan to propagate through these fields properly? Is that something that must be explicitly specified in the conanfile.py (even if it's in the profile)?

Conan : Could not find a package configuration file provided by "Protobuf"

I've been trying now to install grpc (C++) via bazel and cmake.. both have failures.. BUT i thought to myself give conan a try.. but there's an error about protobuf.. so I added it to the requirements and yet it's not helping.. any ideas?
Conanfile.py
from conans import ConanFile, CMake, tools
GRPC_VERSION = "v1.48.0"
class GRPCExample(ConanFile):
name = "grpc-example"
version = GRPC_VERSION
url = "https://github.com/grpc/grpc"
description = "Built from grpcs example code in Github"
topics = ("grpc", "example")
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False]}
default_options = {"shared": False}
generators = "cmake"
requires = [("protobuf/3.21.4")]
def source(self):
self.run("git clone --depth 1 --branch " + GRPC_VERSION + " https://github.com/grpc/grpc")
def build(self):
cmake = CMake(self)
cmake.configure(source_folder="grpc/examples/cpp/helloworld")
cmake.build()
def package(self):
self.copy("*.h", dst="include", src="grpc/examples/cpp/helloworld")
self.copy("*.lib", dst="lib", keep_path=False)
self.copy("*.dll", dst="bin", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.dylib", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["grpc-example"]
-- Found Threads: TRUE
CMake Error at /home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac/grpc/examples/cpp/cmake/common.cmake:101 (find_package):
Could not find a package configuration file provided by "Protobuf" with any
of the following names:
ProtobufConfig.cmake
protobuf-config.cmake
Add the installation prefix of "Protobuf" to CMAKE_PREFIX_PATH or set
"Protobuf_DIR" to a directory containing one of the above files. If
"Protobuf" provides a separate development package or SDK, be sure it has
been installed.
Call Stack (most recent call first):
CMakeLists.txt:24 (include)
-- Configuring incomplete, errors occurred!
See also "/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac/CMakeFiles/CMakeOutput.log".
grpc-example/v1.48.0#stonks/dev:
grpc-example/v1.48.0#stonks/dev: ERROR: Package '1df012f840559ffd3600baf4353bf1a2db1635ac' build failed
grpc-example/v1.48.0#stonks/dev: WARN: Build folder /home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac
ERROR: grpc-example/v1.48.0#stonks/dev: Error in build() method, line 25
cmake.configure(source_folder="grpc/examples/cpp/helloworld")
ConanException: Error 1 while executing cd '/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac' && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Release" -DCONAN_IN_LOCAL_CACHE="ON" -DCONAN_COMPILER="gcc" -DCONAN_COMPILER_VERSION="11" -DCONAN_CXX_FLAGS="-m64" -DCONAN_SHARED_LINKER_FLAGS="-m64" -DCONAN_C_FLAGS="-m64" -DCONAN_LIBCXX="libstdc++11" -DBUILD_SHARED_LIBS="OFF" -DCMAKE_INSTALL_PREFIX="/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/package/1df012f840559ffd3600baf4353bf1a2db1635ac" -DCMAKE_INSTALL_BINDIR="bin" -DCMAKE_INSTALL_SBINDIR="bin" -DCMAKE_INSTALL_LIBEXECDIR="bin" -DCMAKE_INSTALL_LIBDIR="lib" -DCMAKE_INSTALL_INCLUDEDIR="include" -DCMAKE_INSTALL_OLDINCLUDEDIR="include" -DCMAKE_INSTALL_DATAROOTDIR="share" -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY="ON" -DCONAN_EXPORTED="1" -Wno-dev '/home/emcp/.conan/data/grpc-example/v1.48.0/stonks/dev/build/1df012f840559ffd3600baf4353bf1a2db1635ac/grpc/examples/cpp/helloworld'

Converting a gcc command to conan / cmake based build [duplicate]

This question already has answers here:
How to link to the C math library with CMake?
(3 answers)
How do I add a linker or compile flag in a CMake file?
(7 answers)
Closed 9 months ago.
I am using the modern conan method for compiling. I am cross compiling an application and am trying to figure out how to convert my existing gcc command to conan / cmake solution.
Here is the gcc command:
arm-linux-gnueabihf-gcc -Wall -static -o sctc sctc.cpp -lm -lstdc++
Pretty simple.
I have successfully built the application using conan/cmake. However it is not running, I think because I am unable to invoke add the options:
-static
-lm
-lstdc++
I think. Also, would like to know how to add -Wall.
How are people debugging the command lines? Any docs or blogs about it?
Here is the conanfile.py
from conans import ConanFile, tools
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps
import os
class SctsConan(ConanFile):
name = "sctc"
version = "1.0.0"
license = "<Put the package license here>"
author = "<Put your name here> <And your email here>"
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
generators = "compiler_args"
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def requirements(self):
self.requires("fff/1.1")
self.requires("gtest/cci.20210126")
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
deps = CMakeDeps(self)
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package_info(self):
self.cpp_info.libs = ["sctc"]
Here is the profile
[settings]
os=Linux
compiler=gcc
compiler.version=9.4
compiler.libcxx=libstdc++11
build_type=Release
arch=armv6
[env]
CC=arm-linux-gnueabihf-gcc
CXX=arm-linux-gnueabihf-g++-9
Here is the CMakeFile.txt
cmake_minimum_required(VERSION 3.14)
project(sctc)
add_executable(sctc sctc.cpp )
Any pointers to documentation are welcome. I tried searching for answers but was not able to find anything that I thought relevant. I am new to conan and cmake but am enjoying working with both.
Cheers,
Dave

Pass numpy's include dir to Cmake from Setuptools

I have a C++ library which I have successfully exposed to python using Pybind11.
In the CmakeLists.txt file, I have added the numpy include like this:
include_directories("C:\\Python37\\Lib\\site-packages\\numpy\\core\\include")
This works, but is undesirable. I would like to pass the numpy include directory from my setup.py file.
My setup.py file looks very much like this one:
import os
import re
import sys
import sysconfig
import platform
import subprocess
from distutils.version import LooseVersion
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError(
"CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
if platform.system() == "Windows":
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)',
out.decode()).group(1))
if cmake_version < '3.1.0':
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(
cfg.upper(),
extdir)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j2']
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(
env.get('CXXFLAGS', ''),
self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args,
cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args,
cwd=self.build_temp)
print() # Add an empty line for cleaner output
setup(
name='python_cpp_example',
version='0.1',
author='Benjamin Jack',
author_email='benjamin.r.jack#gmail.com',
description='A hybrid Python/C++ test project',
long_description='',
# add extension module
ext_modules=[CMakeExtension('python_cpp_example')],
# add custom build_ext command
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
)
After having a look at some SO questions like this, I know that you can get the numpy include directory with numpy.get_include().
However, adding the include directory to the path inside the function build_extension with this line ext.include_dirs.append(numpy.get_include()) seems to have no effect.
I'd like to know how to pass the include directory properly.
Is your cmake build using the wrong numpy path or not finding numpy at all? If it's the wrong path, you could try prepending instead of appending numpy.get_include():
ext.include_dirs.insert(0,numpy.get_include())

C++ Why can't the linker see my files?

Building a native module for Node.js under Cygwin / Windows:
I have a monkey.cc file with this:
#include <monkey/monkey.h>
running
node-waf configure build
I get the following
'configure' finished successfully (0.351s)
Waf: Entering directory `/usr/src/build'
[2/2] cxx_link: build/default/monkey_1.o -> build/default/monkey.node build/default/libmonkey.dll.a
Creating library file: default/libmonkey.dll.a
then the following error:
default/monkey_1.o:/usr/src/build/../monkey.cc:144: undefined reference to `_monkeyFoo'
monkeyFoo is defined in monkey.h which is in a directory named monkey. I am running the above command from the directory containing monkey directory and monkey.cc file.
EDIT:
wscript, which is the python script that node-waf runs looks like this:
import os
srcdir = '.'
blddir = './build'
VERSION = '0.0.2'
def set_options(opt):
opt.tool_options('compiler_cxx')
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
def build(bld):
monkey = bld.new_task_gen('cxx', 'shlib', 'node_addon')
monkey.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall", "-L/usr/lib", "-lssl"]
monkey.chmod = 0755
monkey.target = 'monkey'
monkey.source = 'monkey.cc'
What am I missing???
That's a linker error, not a compiler error. Do you have a definition for the function? (Not just a declaration.) And are you sure it's being linked in?
Add monkey.lib='crypto' in the wscript.