Skip to content

Commit

Permalink
Provide _INCLUDE_DIR in cmake_find_package generator (#6017)
Browse files Browse the repository at this point in the history
* Provide _INCLUDE_DIR in cmake_find_package generator

* Derive cmake _INCLUDE_DIR from all include paths

Variable _INCLUDE_DIR may be expected to be a pure string (without newlines).
So build it as semicolon-separated string that is essentially a CMake list.

* Review
  • Loading branch information
theirix authored and lasote committed Dec 3, 2019
1 parent 0441503 commit 608ccb0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
14 changes: 13 additions & 1 deletion conans/client/generators/cmake.py
Expand Up @@ -8,7 +8,11 @@
class DepsCppCmake(object):
def __init__(self, cpp_info):
def join_paths(paths):
# Paths are doubled quoted, and escaped (but spaces)
"""
Paths are doubled quoted, and escaped (but spaces)
e.g: set(LIBFOO_INCLUDE_DIRS "/path/to/included/dir" "/path/to/included/dir2")
"""

return "\n\t\t\t".join('"%s"'
% p.replace('\\', '/').replace('$', '\\$').replace('"', '\\"')
for p in paths)
Expand All @@ -24,7 +28,15 @@ def join_defines(values, prefix=""):
replace('"', '\\"'))
for v in values)

def join_paths_single_var(values):
"""
semicolon-separated list of dirs:
e.g: set(LIBFOO_INCLUDE_DIR "/path/to/included/dir;/path/to/included/dir2")
"""
return '"%s"' % ";".join(p.replace('\\', '/').replace('$', '\\$') for p in values)

self.include_paths = join_paths(cpp_info.include_paths)
self.include_path = join_paths_single_var(cpp_info.include_paths)
self.lib_paths = join_paths(cpp_info.lib_paths)
self.res_paths = join_paths(cpp_info.res_paths)
self.bin_paths = join_paths(cpp_info.bin_paths)
Expand Down
2 changes: 2 additions & 0 deletions conans/client/generators/cmake_find_package_common.py
@@ -1,5 +1,6 @@
target_template = """
set({name}_INCLUDE_DIRS{build_type_suffix} {deps.include_paths})
set({name}_INCLUDE_DIR{build_type_suffix} {deps.include_path})
set({name}_INCLUDES{build_type_suffix} {deps.include_paths})
set({name}_RES_DIRS{build_type_suffix} {deps.res_paths})
set({name}_DEFINITIONS{build_type_suffix} {deps.defines})
Expand Down Expand Up @@ -29,6 +30,7 @@
endif()
mark_as_advanced({name}_INCLUDE_DIRS{build_type_suffix}
{name}_INCLUDE_DIR{build_type_suffix}
{name}_INCLUDES{build_type_suffix}
{name}_DEFINITIONS{build_type_suffix}
{name}_LINKER_FLAGS{build_type_suffix}_LIST
Expand Down
4 changes: 4 additions & 0 deletions conans/test/functional/generators/cmake_find_package_test.py
Expand Up @@ -165,6 +165,9 @@ def cmake_find_package_test(self):
if(NOT DEFINED Hello0_INCLUDE_DIRS)
message(FATAL_ERROR "Hello0_INCLUDE_DIRS not declared")
endif()
if(NOT DEFINED Hello0_INCLUDE_DIR)
message(FATAL_ERROR "Hello0_INCLUDE_DIR not declared")
endif()
if(NOT DEFINED Hello0_INCLUDES)
message(FATAL_ERROR "Hello0_INCLUDES not declared")
endif()
Expand Down Expand Up @@ -357,6 +360,7 @@ def package_info(self):
package_path = client.cache.package_layout(ref).package(pref)
modules_path = os.path.join(package_path, "share", "cmake")
self.assertEqual(set(os.listdir(modules_path)), {"FindFindModule.cmake", "my-module.cmake"})

consumer = textwrap.dedent("""
from conans import ConanFile, CMake
Expand Down

0 comments on commit 608ccb0

Please sign in to comment.