conan-io / conan

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

[question] add flags for configure #14431

Open k-morozov opened 1 year ago

k-morozov commented 1 year ago

What is your question?

Hello everyone!

I'm writting a simple concurancy project with gtest. I need a diffrent sanitizers for test (thread, memory, etc). I use conan2 for downloading gtest from conan-center. I like this simplicity. But for the sanitizers to work correctly, I need to build gtest with certain flags. I can use conan to build gtest, but I haven't found how I can set the flags I need. I suppose that here we can set it:

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")
        if self.settings.compiler == "clang":
            if self.settings.build_type == "Debug":
                pass

Can you help me?

Have you read the CONTRIBUTING guide?

amritpalsingh52 commented 1 year ago

Here are the steps to build gtest with specific flags using Conan:

Create a Conan recipe for gtest: First, you need to create a Conan recipe for gtest. If you don't have one already, you can generate it using the conan new command: This command will create a basic Conan recipe for gtest version 1.11.0 in a directory called "gtest/1.11.0" with the necessary files, including conanfile.py.

Modify the Conan recipe: Open the conanfile.py in the "gtest/1.11.0" directory, and inside the build() method, add the necessary CMake flags for the sanitizers you want to enable. from conans import ConanFile, CMake

class GTestConan(ConanFile): name = "gtest" version = "1.11.0" license = "BSD-3-Clause" url = "https://github.com/google/googletest" description = "Google Test C++ testing framework" topics = ("conan", "gtest", "testing", "framework")

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

generators = "cmake"

def source(self):
    self.run("git clone https://github.com/google/googletest.git -b release-1.11.0")

def build(self):
    cmake = CMake(self)
    cmake.definitions["gtest_build_tests"] = False
    cmake.definitions["gtest_build_samples"] = False
    cmake.definitions["gtest_build_gmock"] = False
    cmake.configure(source_folder="googletest")
    # Add CMake flags for the sanitizers you want to enable
    cmake.build()

def package(self):
    self.copy("*.h", dst="include", src="googletest/googletest/include")
    self.copy("*.a", dst="lib", keep_path=False)
    self.copy("*.lib", dst="lib", keep_path=False)

def package_info(self):
    self.cpp_info.libs = ["gtest"]
memsharded commented 1 year ago

Hi @amritpalsingh52

Thanks for your contribution.

It seems that you are using quite legacy generators like the cmake generators and helpers like from conans import CMake. These are strongly discouraged, they shouldn't be used anymore, but the new CMakeDeps and CMakeToolchain generators. The key is not using from conans anymore in recipes, but using from conan

memsharded commented 1 year ago

Hi @k-morozov

Thanks for your question. The way to inject compiler flags in existing recipes is using the conf system, type conan config list to see all possibilities. The ones for flags are like tools.build:cxxflags: https://docs.conan.io/2/reference/config_files/global_conf.html