Skip to content

Commit

Permalink
updated in accordance with the new pyreq API
Browse files Browse the repository at this point in the history
* Removed the decorator, because it's not applicable to new
  python_requires API.
* Similarly removed private _Meta class, which was hacky anyway.
* Removed automatic dependency on b2 package in development mode.
* Updated tests.
  • Loading branch information
grisumbras committed Dec 12, 2020
1 parent 2be803e commit bd9e19d
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 223 deletions.
64 changes: 13 additions & 51 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,34 @@ package:

[source,python]
----
from conans import python_requires
b2 = python_requires("b2-helper/[>=0.7.1]@grisumbras/stable")
class MyConan(ConanFile):
python_requires = "b2-helper/[>=0.7.1]@grisumbras/stable"
----

== Usage

=== Using decorator
=== Using mixin

The simplest way to use the package is via the class decorator
The simplest way to use the package is via the mixin class:

[source,python]
----
@b2.build_with_b2
class MyConan(ConanFile):
name = "..."
version = "..."
settings = "os", "arch", "compiler", "build_type"
python_requires = "b2-helper/[>=0.7.1]@grisumbras/stable"
python_requires_extend = "b2-helper.Mixin"
----

The decorator provides default implementations for methods `build`, `package`
The mixin provides default implementations for methods `build`, `package`
and `test`. In order for it to work without any customization, project's
jamroot file should be in `conanfile.source_folder` and the project should
require no configuration beyond toolset initialization. If customization is
required, the `ConanFile` subclass should override method `b2_setup_builder`:

[source,python]
----
@b2.build_with_b2
class MyConan(ConanFile):
python_requires_extend = "b2-helper.Mixin"
def b2_setup_builder(self, builder):
builder.source_folder = "src"
builder.build_folder = "build"
Expand All @@ -66,52 +65,15 @@ If you need to build specific targets, you can sepcify them via

[source,python]
----
@b2.build_with_b2
class MyConan1(ConanFile):
b2_build_targets = "foo"
python_requires_extend = "b2-helper.Mixin"
b2_build_targets = "foo"
@b2.build_with_b2
class MyConan2(ConanFile):
b2_build_targets = "foo", "bar"
----

By default the decorator adds a build requirement on Boost.Build
_in development mode_. If you wish to disable that set its keyword argument
`build_require_b2` to `False`:

[source,python]
----
@b2.build_with_b2(build_require_b2=False)
class MyConan(ConanFile):
...
python_requires_extend = "b2-helper.Mixin"
b2_build_targets = "foo", "bar"
----

Alternatively, if you want to use a specific version of Boost.Build, set that
keyword argument to a conan reference:

[source,python]
----
@b2.build_with_b2(build_require_b2="boost_build/4.0.0@mycompany/stable)
class MyConan(ConanFile):
...
----

=== Using mixin

Another convenient way to use the package is via a mixin class:

[source,python]
----
class MyConan(b2.Mixin, ConanFile):
name = "..."
version = "..."
settings = "os", "arch", "compiler", "build_type"
----

Using mixin is equivalent to using the class decorator. Note, that since
`ConanFile` class defines methods `build`, `package` and so on, it is important
to put the mixin before it in the base class list.

=== Using helper

The helper can be used by itself pretty much the same way as standard build
Expand Down
59 changes: 3 additions & 56 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ def package_info(self):
self.info.header_only()


b2_reference = "b2/[>=4.0.0]"


def join_arguments(args):
return " ".join(filter(None, args))


def jamify(s):
"""
Convert a valid Python identifier to a string that follows Boost.Build
Expand Down Expand Up @@ -640,12 +633,6 @@ def b2_setup_builder(self, builder):

return builder

def build_requirements(self):
"""Adds build requirement on b2 in development mode."""

if getattr(self, "_b2_reference", None) and self.develop:
self.build_requires(self._b2_reference)

def build(self):
"""Configures and builds default targets."""

Expand Down Expand Up @@ -791,6 +778,7 @@ def configure(self):
file.write(" <%s>%s\n" % (k, path_escaped(v)))
file.write(" ;\n")


def build(self, *targets):
"""
Run Boost.Build and build targets `targets` using the active options,
Expand Down Expand Up @@ -842,49 +830,8 @@ def _build(self, targets):
self.conanfile.run(join_arguments(args))


def build_with_b2(wrapped=None, build_require_b2=True):
def helper(wrapped):
meta = metaclass(build_require_b2)
return six.add_metaclass(meta)(wrapped)

if wrapped is None:
return helper
else:
return helper(wrapped)


def metaclass(build_require_b2=True):
if build_require_b2 and not isinstance(build_require_b2, six.string_types):
return _Meta
else:
class _MetaWithCustomRef(_Meta):
reference = build_require_b2
return _MetaWithCustomRef


def _subclass_index(lst, cls):
index = [i for (i, base) in enumerate(lst) if issubclass(base, cls)]
if index:
return index[0]
else:
return -1


class _Meta(type):
reference = b2_reference

def __new__(cls, name, bases, namespace):
mixin_index = _subclass_index(bases, Mixin)
if mixin_index < 0:
cf_index = _subclass_index(bases, ConanFile)
if cf_index > -1:
bases = list(bases)
bases.insert(cf_index, Mixin)
bases = tuple(bases)

namespace["_b2_reference"] = cls.reference

return type.__new__(cls, name, bases, namespace)
def join_arguments(args):
return " ".join(filter(None, args))


def path_escaped(path):
Expand Down
6 changes: 4 additions & 2 deletions test_package/build-targets/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
ConanFile,
tools,
)
from get_helper_package import b2
from get_helper_package import package_ref


@b2.build_with_b2
class MyConan(ConanFile):
build_requires = "b2/[*]"
python_requires = package_ref
python_requires_extend = "b2-helper.Mixin"
exports_sources = "*.jam", "*.cpp"

b2_build_targets = "install1", "install2"
Expand Down
6 changes: 4 additions & 2 deletions test_package/build_folder/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@


from conans import ConanFile
from get_helper_package import b2
from get_helper_package import package_ref
import os


@b2.build_with_b2
class B2ToolTestConan(ConanFile):
build_requires = "b2/[*]"
python_requires = package_ref
python_requires_extend = "b2-helper.Mixin"
exports_sources = "*.cpp", "*.jam"

def b2_setup_builder(self, builder):
Expand Down
9 changes: 6 additions & 3 deletions test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Copyright (c) 2020 Dmitry Arkhipov <[email protected]>
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)


from conans import (
ConanFile,
tools,
Expand All @@ -8,9 +14,6 @@
class TestB2Helper(ConanFile):
_tests = (
"minimal",
"explicit_require_b2",
"custom_build_requires",
"custom_b2_reference",
"source_folder",
"build_folder",
"options",
Expand Down
22 changes: 0 additions & 22 deletions test_package/custom_b2_reference/conanfile.py

This file was deleted.

24 changes: 0 additions & 24 deletions test_package/custom_build_requires/conanfile.py

This file was deleted.

7 changes: 5 additions & 2 deletions test_package/default-properties/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@


from conans import ConanFile
from get_helper_package import b2
from get_helper_package import package_ref


@b2.build_with_b2
class B2ToolTestConan(ConanFile):
build_requires = "b2/[*]"
python_requires = package_ref
python_requires_extend = "b2-helper.Mixin"

settings = "os", "compiler", "build_type", "arch",
options = {"shared": [True, False]}
default_options = {"shared": False}
Expand Down
14 changes: 0 additions & 14 deletions test_package/explicit_require_b2/conanfile.py

This file was deleted.

13 changes: 0 additions & 13 deletions test_package/explicit_require_b2/jamroot.jam

This file was deleted.

9 changes: 0 additions & 9 deletions test_package/explicit_require_b2/main.cpp

This file was deleted.

6 changes: 4 additions & 2 deletions test_package/free/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@


from conans import ConanFile
from get_helper_package import b2
from get_helper_package import package_ref
import os


@b2.build_with_b2
class B2ToolTestConan(ConanFile):
build_requires = "b2/[*]"
python_requires = package_ref
python_requires_extend = "b2-helper.Mixin"
exports_sources = "*.cpp", "*.hpp", "*.jam"

def b2_setup_builder(self, builder):
Expand Down
6 changes: 4 additions & 2 deletions test_package/minimal/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@


from conans import ConanFile
from get_helper_package import b2
from get_helper_package import package_ref


b2.build_with_b2
class MyConan(ConanFile):
"""This is pretty much the bare minimum package definition"""

build_requires = "b2/[*]"
python_requires = package_ref
python_requires_extend = "b2-helper.Mixin"
exports_sources = "*.jam", "*.cpp"
1 change: 0 additions & 1 deletion test_package/modules/get_helper_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@


package_ref = os.environ.get("PACKAGE_REFERENCE")
b2 = python_requires(package_ref)
6 changes: 4 additions & 2 deletions test_package/options/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@


from conans import ConanFile
from get_helper_package import b2
from get_helper_package import package_ref


@b2.build_with_b2
class B2ToolTestConan(ConanFile):
build_requires = "b2/[*]"
python_requires = package_ref
python_requires_extend = "b2-helper.Mixin"
exports_sources = "*.cpp", "*.jam"

def b2_setup_builder(self, builder):
Expand Down
Loading

0 comments on commit bd9e19d

Please sign in to comment.