|
| 1 | +.. _conan_tools_premake_premaketoolchain: |
| 2 | + |
| 3 | +PremakeToolchain |
| 4 | +================ |
| 5 | + |
| 6 | +.. include:: ../../../common/experimental_warning.inc |
| 7 | + |
| 8 | +.. important:: |
| 9 | + |
| 10 | + This class will generate files that are only compatible with Premake versions >= 5.0.0 |
| 11 | + |
| 12 | +The ``PremakeToolchain`` generator can be used by name in conanfiles: |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | + :caption: conanfile.py |
| 16 | +
|
| 17 | + class Pkg(ConanFile): |
| 18 | + generators = "PremakeToolchain" |
| 19 | +
|
| 20 | +
|
| 21 | +.. code-block:: text |
| 22 | + :caption: conanfile.txt |
| 23 | +
|
| 24 | + [generators] |
| 25 | + PremakeToolchain |
| 26 | +
|
| 27 | +And it can also be fully instantiated in the conanfile ``generate()`` method: |
| 28 | + |
| 29 | +**Usage Example:** |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + from conan.tools.premake import PremakeToolchain |
| 34 | +
|
| 35 | + class Pkg(ConanFile): |
| 36 | + settings = "os", "compiler", "build_type", "arch" |
| 37 | +
|
| 38 | + def generate(self): |
| 39 | + tc = PremakeToolchain(self) |
| 40 | + tc.extra_defines = ["VALUE=2"] # Add define to main premake workspace |
| 41 | + tc.extra_cflags = ["-Wextra"] # Add cflags to main premake workspace |
| 42 | + tc.extra_cxxflags = ["-Wall", "-Wextra"] # Add cxxflags to main premake workspace |
| 43 | + tc.extra_ldflags = ["-lm"] # Add ldflags to main premake workspace |
| 44 | + tc.project("main").extra_defines = ["TEST=False"] # Add define to "main" project (overriding possible value) |
| 45 | + tc.project("main").extra_cxxflags = ["-FS"] # Add cxxflags to "main" project |
| 46 | + tc.project("test").disable = True # A way of disabling "test" project compilation |
| 47 | + tc.project("foo").kind = "StaticLib" # Override library type of "foo" |
| 48 | + tc.generate() |
| 49 | +
|
| 50 | +Generated files |
| 51 | +--------------- |
| 52 | + |
| 53 | +The ``PremakeToolchain`` generates ``conantoolchain.premake5.lua`` file after a :command:`conan install` (or when building the package |
| 54 | +in the cache) with the information provided in the ``generate()`` method as well as information |
| 55 | +translated from the current ``settings``, ``conf``, etc. |
| 56 | + |
| 57 | +Premake does not expose any kind of API to interact inject/modify the build scripts. |
| 58 | +Furthermore, premake does not have a concept of toolchain so following premake maintainers instructions, (see `premake discussion <https://github.com/premake/premake-core/discussions/2441>`_) |
| 59 | +as premake is built in top of Lua scripts, Conan generated file is a Lua script |
| 60 | +that will override the original premake script adding the following |
| 61 | +information: |
| 62 | + |
| 63 | +* Detection of ``buildtype`` from Conan settings. |
| 64 | + |
| 65 | +* Definition of the C++ standard as necessary. |
| 66 | + |
| 67 | +* Definition of the C standard as necessary. |
| 68 | + |
| 69 | +* Detection of the premake ``action`` based on conan profile and OS. |
| 70 | + |
| 71 | +* Definition of the build folder in order to avoid default premake behavior of |
| 72 | + creating build folder and object files in the source repository (which goes |
| 73 | + against conan good practices). |
| 74 | + |
| 75 | +* Definition of compiler and linker flags and defines based on user configuration values. |
| 76 | + |
| 77 | +* Definition of proper target architecture when cross building. |
| 78 | + |
| 79 | +* Definition of ``fPIC`` flag based on conan options. |
| 80 | + |
| 81 | +* Based on ``shared`` conan option, ``PremakeToolchain`` will set the ``kind`` of the ``workspace`` to ``SharedLib`` or ``StaticLib``. |
| 82 | + |
| 83 | +.. note:: |
| 84 | + |
| 85 | + ``PremakeToolchain`` is not able to override the ``kind`` of a project if that project has already define the ``kind`` attribute (typical case). |
| 86 | + It can only override top-level ``workspace.kind``, which will only affect projects without a defined ``kind``. |
| 87 | + |
| 88 | + **Recomendation**: as premake does not offer any mechanism like CMake's `BUILD_SHARED_LIBS <https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html#variable:BUILD_SHARED_LIBS>`_ |
| 89 | + to externally manage the creation type of libraries, it is recommended while |
| 90 | + using conan to **AVOID** defining the ``kind`` attribute on library project. |
| 91 | + |
| 92 | + |
| 93 | +Reference |
| 94 | +--------- |
| 95 | + |
| 96 | +.. currentmodule:: conan.tools.premake |
| 97 | + |
| 98 | +.. autoclass:: PremakeToolchain |
| 99 | + :members: |
0 commit comments