Skip to content

Commit

Permalink
(#4258) cjson: fix CMake imported targets
Browse files Browse the repository at this point in the history
official imported targets are cjson and cjson_utils
  • Loading branch information
SpaceIm authored Jan 19, 2021
1 parent 4658385 commit 97e551c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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)

0 comments on commit 97e551c

Please sign in to comment.