Cmake cannot not find include/lib directories generated by Conan

215 views Asked by At

I am trying to define both a recipe file and a test_package for NATS 3rd party library. Here is the folder structure which I have:

nats
|--conanfile.py
|--test_package
   |--CMakeLists.txt
   |--conanfile.py
   |--test.cpp

After running command

conan create . "nats/3.6.1@myname/test" -pr <some_profile_file> 

both nats library and its dependency - openssl are being successfully built, because after running conan search nats and conan search openss I can see the following packages in localhost:

Existing package recipes:
nats/3.6.1@myname/test
openssl/3.1.1_0@jenkins/stable

conanfile.py recipe file for nats has the following structure:

import os
from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout

required_conan_version = ">=1.59.0"

class NatsConan(ConanFile):
    name = "nats"
    description = "NATS is a simple, secure and high-performance messaging system."
    version = "3.6.1"
    license = "Apache-2.0"
    settings = "os", "compiler", "build_type", "arch"
    
    options = {
        "shared"                 : [True, False],
        "nats_tls"               : [True, False],
        "nats_build_streaming"   : [True, False],

        "windows_sdk"            : "ANY",
        "full_compiler_version"  : "ANY"
    }

    default_options = {
        "shared"                 : False,
        "nats_tls"               : False,
        "nats_build_streaming"   : False,

        "windows_sdk"            : "10.0",
        "full_compiler_version"  : ""
    }

    short_paths = True

    def config_options(self):
        if self.settings.os != "Windows":
            del self.options.windows_sdk

    def layout(self):
        cmake_layout(self)

    def generate(self):
        cmake = CMakeDeps(self)
        cmake.generate()
        tc = CMakeToolchain(self)
        tc.variables["NATS_BUILD_STREAMING"] = self.options.nats_build_streaming
        tc.variables["NATS_BUILD_WITH_TLS"] = self.options.nats_tls
        tc.variables["BUILD_SHARED_LIBS"] = self.options.shared
        #tc.variables["NATS_BUILD_OPENSSL_STATIC_LIBS"] = True
        tc.generate()

    def source(self):
        git = Git(self)
        git.clone(url = "https://github.com/nats-io/nats.c.git", target = ".")
        git.checkout("v3.6.1")

    def requirements(self):
        self.requires("openssl/3.1.1_0@jenkins/stable")
        if self.options.nats_build_streaming:
            self.requires("protobuf/3.18.0_13@jenkins/stable")

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

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_info(self):
        self.cpp_info.libs = ["nats"]

test_packages's CMakeLists.txt file see below:

cmake_minimum_required(VERSION 3.5.1)
project(test_application CXX)

find_package(nats CONFIG REQUIRED)

add_executable(test_application test.cpp)
target_link_libraries(test_application nats::nats)

And the conanfile.py file for that:

import os

from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.build import can_run

class TestApplicationConan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps", "CMakeToolchain"

    def requirements(self):
        self.requires(self.tested_reference_str)

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

    def layout(self):
        cmake_layout(self)

    def test(self):
        if can_run(self):
            cmd = os.path.join(self.cpp.build.bindir, "test_application")
            self.run(cmd, env="conanrun")

The problem is that when conan command runs test() for the test_package it just can't find neither include directories (I have #include <nats/nats.h> in the test.cpp file and called a function from nats) nor library directories. I have tried to add this line in the CMakeLists.txt file:

include_directories(${CMAKE_INCLUDE_PATH})

and resolved only compile error. But unfortunately I can't resolve the linkage error, because, I guess, I need to somehow specify in the CMakeLists.txt file where the generated lib folder is located.

Also, I don't think that including ${CMAKE_INCLUDE_PATH} explicitly is the right solution (it seems to me like a workaround). IMO, there should be a generated file which should be included and nothing else, e.g., I have added this generated file

include(C:/.conan/d6e9bb/1/lib/cmake/cnats/cnats-config.cmake)

in the CMakeLists.txt, but this didn't help either :(.

I don't understand what I am missing or maybe I misunderstood something related to Conan and CMake interaction. My Conan version is 1.59.0.

0

There are 0 answers