Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cjson: fix CMake imported targets #4258

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion recipes/cjson/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,44 @@ def package(self):
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))

targets = [{"official": "cjson", "namespaced": "cJSON::cjson"}]
if self.options.utils:
targets.append({"official": "cjson_utils", "namespaced": "cJSON::cjson_utils"})
self._create_module_official_cmake_targets(
os.path.join(self.package_folder, self._module_folder, self._module_file),
targets
)

@staticmethod
def _create_module_official_cmake_targets(module_file, targets):
content = ""
for target in targets:
content += (
"if(TARGET {namespaced} AND NOT TARGET {official})\n"
" add_library({official} INTERFACE IMPORTED)\n"
" target_link_libraries({official} INTERFACE {namespaced})\n"
"endif()\n"
).format(official=target["official"], namespaced=target["namespaced"])
tools.save(module_file, content)

@property
def _module_folder(self):
return os.path.join("lib", "cmake")

@property
def _module_file(self):
return "conan-official-{}-targets.cmake".format(self.name)

def package_info(self):
# FIXME: CMake imported targets shouldn't be namespaced (requires https://github.com/conan-io/conan/issues/7615)
self.cpp_info.names["cmake_find_package"] = "cJSON"
self.cpp_info.names["cmake_find_package_multi"] = "cJSON"

self.cpp_info.components["_cjson"].names["cmake_find_package"] = "cjson"
self.cpp_info.components["_cjson"].names["cmake_find_package_multi"] = "cjson"
self.cpp_info.components["_cjson"].names["pkg_config"] = "libcjson"
self.cpp_info.components["_cjson"].libs = ["cjson"]
self.cpp_info.components["_cjson"].builddirs = [self._module_folder]
self.cpp_info.components["_cjson"].build_modules = [os.path.join(self._module_folder, self._module_file)]
if self.settings.os == "Linux":
self.cpp_info.components["_cjson"].system_libs = ["m"]

Expand All @@ -103,3 +132,5 @@ def package_info(self):
self.cpp_info.components["cjson_utils"].names["pkg_config"] = "libcjson_utils"
self.cpp_info.components["cjson_utils"].libs = ["cjson_utils"]
self.cpp_info.components["cjson_utils"].requires = ["_cjson"]
self.cpp_info.components["cjson_utils"].builddirs = [self._module_folder]
self.cpp_info.components["cjson_utils"].build_modules = [os.path.join(self._module_folder, self._module_file)]
4 changes: 2 additions & 2 deletions recipes/cjson/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.1)
project(test_package C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
conan_basic_setup(TARGETS)

find_package(cJSON REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} cJSON::cjson) # FIXME: remove cJSON:: namespace when fixed in cpp_info of recipe
target_link_libraries(${PROJECT_NAME} cjson)