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

[fix] build_modules were failing for markdown generator #8942

Merged
merged 2 commits into from May 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 44 additions & 23 deletions conans/client/generators/markdown.py
Expand Up @@ -32,9 +32,6 @@
{%- if cpp_info.cxxflags %}
* CXX_FLAGS: {{ join_list_sources(cpp_info.cxxflags) }}
{%- endif %}
{%- if cpp_info.build_modules %}
* Build modules (see [below](#build-modules)): {{ join_list_sources(cpp_info.build_modules) }}
{%- endif %}
{%- endmacro %}
""")

Expand All @@ -49,6 +46,17 @@

target_link_libraries(<library_name> CONAN_PKG::{{ cpp_info.get_name("cmake") }})
```

{% set build_modules = cpp_info.build_modules.get('cmake', None) %}
{% if build_modules %}
This generator will include some _build modules_:
{% for bm in build_modules -%}
* `{{ bm }}`
```
{{ '/'.join([cpp_info.rootpath, bm])|read_pkg_file|indent(width=2) }}
```
{%- endfor -%}
{%- endif %}
""")

generator_cmake_find_package_tpl = textwrap.dedent("""
Expand All @@ -75,6 +83,17 @@
Remember to adjust your build system settings to match the binaries you are linking with. You can
use the [CMake build helper](https://docs.conan.io/en/latest/reference/build_helpers/cmake.html) and
the ``cmake`` generator from a *conanfile.py* or the new [toolchain paradigm](https://docs.conan.io/en/latest/creating_packages/toolchains.html).

{% set build_modules = cpp_info.build_modules.get('cmake_find_package', None) %}
{% if build_modules %}
This generator will include some _build modules_:
{% for bm in build_modules -%}
* `{{ bm }}`
```
{{ '/'.join([cpp_info.rootpath, bm])|read_pkg_file|indent(width=2) }}
```
{%- endfor -%}
{%- endif %}
""")

generator_pkg_config_tpl = textwrap.dedent("""
Expand All @@ -89,6 +108,17 @@
{%- endfor -%}
{%- endif -%}.
Use your *pkg-config* tool as usual to consume the information provided by the Conan package.

{% set build_modules = cpp_info.build_modules.get('pkg_config', None) %}
{% if build_modules %}
This generator will include some _build modules_:
{% for bm in build_modules -%}
* `{{ bm }}`
```
{{ '/'.join([cpp_info.rootpath, bm])|read_pkg_file|indent(width=2) }}
```
{%- endfor -%}
{%- endif %}
""")

requirement_tpl = textwrap.dedent("""
Expand Down Expand Up @@ -139,6 +169,9 @@
these generators they have to be listed in the _conanfile.py_ file or using the command
line argument ``--generator/-g`` in the ``conan install`` command.

* [``cmake``](#Generator-cmake)
* [``cmake_find_package``](#Generator-cmake_find_package)
* [``pkg_config``](#Generator-pkg_config)

{% include 'generator_cmake' %}
{% include 'generator_cmake_find_package' %}
Expand All @@ -155,20 +188,6 @@
{%- endfor %}
```

{%- if cpp_info.build_modules %}
---
## Build modules

Modules exported by this recipe. They are automatically included when using Conan generators:

{% for name, build_module in build_modules %}
**{{ name }}**
```
{{ build_module }}
```
{% endfor %}
{% endif %}

---
---
Conan **{{ conan_version }}**. JFrog LTD. [https://conan.io](https://conan.io). Autogenerated {{ now.strftime('%Y-%m-%d %H:%M:%S') }}.
Expand All @@ -192,11 +211,6 @@ def _list_required_by(self, cpp_info):
if cpp_info.name in other_cpp_info.public_deps:
yield other_name, other_cpp_info

def _read_build_modules(self, cpp_info):
for build_module in cpp_info.build_modules:
filename = os.path.join(cpp_info.rootpath, build_module)
yield build_module, open(filename, 'r').read()

@property
def filename(self):
pass
Expand All @@ -213,6 +227,14 @@ def content(self):
env = Environment(loader=dict_loader)
template = env.get_template('package.md')

def read_pkg_file(filename):
try:
return open(filename, 'r').read()
except IOError:
return '# Error reading file content. Please report.'

env.filters['read_pkg_file'] = read_pkg_file

from conans import __version__ as conan_version
ret = {}
for name, cpp_info in self.conanfile.deps_cpp_info.dependencies:
Expand All @@ -221,7 +243,6 @@ def content(self):
headers=self._list_headers(cpp_info),
requires=list(self._list_requires(cpp_info)),
required_by=list(self._list_required_by(cpp_info)),
build_modules=self._read_build_modules(cpp_info),
conan_version=conan_version,
now=datetime.datetime.now()
)
Expand Down
28 changes: 28 additions & 0 deletions conans/test/integration/generators/markdown_test.py
Expand Up @@ -25,3 +25,31 @@ def package_info(self):

self.assertIn("Generates the file FindFooBar.cmake", content)
self.assertIn("find_package(FooBar)", content)

def test_with_build_modules(self):
conanfile = textwrap.dedent("""
import os
from conans import ConanFile

class HelloConan(ConanFile):
exports_sources = 'bm.cmake'
def package(self):
self.copy('bm.cmake', dst='lib/cmake')

def package_info(self):
self.cpp_info.filenames['cmake_find_package'] = 'FooBar'
self.cpp_info.names['cmake_find_package'] = 'foobar'
self.cpp_info.names['cmake_find_package_multi'] = 'foobar_multi'
self.cpp_info.names['pkg_config'] = 'foobar_cfg'
self.cpp_info.build_modules['cmake_find_package'] = ['lib/cmake/bm.cmake']
""")
client = TestClient()
client.save({"conanfile.py": conanfile,
"bm.cmake": "Content of build_module" })
client.run("create . bar/0.1.0@user/testing")
client.run("install bar/0.1.0@user/testing -g markdown")
content = client.load("bar.md")

self.assertIn("Generates the file FindFooBar.cmake", content)
self.assertIn("* `lib/cmake/bm.cmake`", content)
self.assertIn("Content of build_module", content)