Skip to content

Commit 7dedbbb

Browse files
perseoGIczoidomemsharded
authored
Updated documentation for premake generator (#4090)
* Updated documentation for premake generator Added premake toolchain generator and updated documentation according to new changes * Added shared option note * Update reference/tools/premake/premaketoolchain.rst Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> * Apply suggestions from code review Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> Co-authored-by: James <memsharded@gmail.com> --------- Co-authored-by: Carlos Zoido <mrgalleta@gmail.com> Co-authored-by: James <memsharded@gmail.com>
1 parent 91ee5b0 commit 7dedbbb

File tree

5 files changed

+163
-35
lines changed

5 files changed

+163
-35
lines changed

integrations/premake.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
Conan provides different tools to help manage your projects using Premake. They can be
77
imported from ``conan.tools.premake``. The most relevant tools are:
88

9-
- ``PremakeDeps``: the dependencies generator for Premake, to allow consuming dependencies from Premake projects
9+
- ``PremakeDeps``: the dependencies generator for Premake, to allow consuming dependencies from Premake projects.
10+
11+
- ``PremakeToolchain``: the toolchain generator for Premake. It will create a
12+
wrapper over premake scripts allowing premake workspace and projects
13+
customization.
1014

1115
- ``Premake``: the Premake build helper. It's simply a wrapper around the command line invocation of Premake.
1216

1317
.. seealso::
1418

1519
- Reference for :ref:`conan_tools_premake_premakedeps`.
20+
- Reference for :ref:`conan_tools_premake_premaketoolchain`.
1621
- Reference for :ref:`conan_tools_premake_premake`.
1722

1823

reference/tools/premake.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ conan.tools.premake
66
.. toctree::
77
:maxdepth: 2
88

9-
premake/premake
109
premake/premakedeps
10+
premake/premaketoolchain
11+
premake/premake

reference/tools/premake/premake.rst

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Premake
77

88

99
The ``Premake`` build helper is a wrapper around the command line invocation of Premake. It will abstract the
10-
project configuration command.
10+
project configuration and build command.
1111

1212
The helper is intended to be used in the *conanfile.py* ``build()`` method, to call Premake commands automatically
1313
when a package is being built directly by Conan (create, install)
@@ -17,13 +17,12 @@ when a package is being built directly by Conan (create, install)
1717
.. code-block:: python
1818
1919
from conan.tools.premake import Premake
20-
from conan.tools.microsoft import MSBuild
2120
2221
class Pkg(ConanFile):
2322
settings = "os", "compiler", "build_type", "arch"
2423
25-
# The VCVars generator might be needed in Windows-MSVC
26-
generators = "VCVars"
24+
# The PremakeToolchain generator is always needed to use premake helper
25+
generators = "PremakeToolchain"
2726
2827
def build(self):
2928
p = Premake(self)
@@ -36,16 +35,25 @@ when a package is being built directly by Conan (create, install)
3635
3736
# Automatically determines the correct action:
3837
# - For MSVC, selects vs<version> based on the compiler version
39-
# - Defaults to "gmake2" for other compilers
38+
# - Defaults to "gmake" for other compilers
4039
# p.configure() will run: premake5 --file=myproject.lua <action> --{key}={value} ...
4140
p.configure()
42-
# At the moment Premake does not contain .build() method
43-
# report in Github issues your use cases and feedback to request it
44-
build_type = str(self.settings.build_type)
45-
if self.settings.os == "Windows":
46-
msbuild = MSBuild(self)
47-
msbuild.build("HelloWorld.sln")
48-
else:
49-
self.run(f"make config={build_type.lower()}_x86_64")
50-
p = os.path.join(self.build_folder, "bin", build_type, "HelloWorld")
51-
self.run(f'"{p}"')
41+
# p.build() will invoke proper compiler depending on action (automatically detected by profile)
42+
p.build("HelloWorld.sln")
43+
44+
Reference
45+
---------
46+
47+
.. currentmodule:: conan.tools.premake
48+
49+
.. autoclass:: Premake
50+
:members:
51+
52+
conf
53+
----
54+
55+
The ``Premake`` build helper is affected by these ``[conf]`` variables:
56+
57+
- ``tools.build:verbosity`` which accepts one of ``quiet`` or ``verbose`` and sets the ``--quiet`` flag in ``Premake.configure()``
58+
59+
- ``tools.compilation:verbosity`` which accepts one of ``quiet`` or ``verbose`` and sets the ``--verbose`` flag in ``Premake.build()``

reference/tools/premake/premakedeps.rst

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ PremakeDeps
66
.. include:: ../../../common/experimental_warning.inc
77

88
The ``PremakeDeps`` is the dependencies generator for Premake.
9-
10-
The ``PremakeDeps`` generator can be used by name in conanfiles:
9+
The generator can be used by name in conanfiles:
1110

1211
.. code-block:: python
1312
:caption: conanfile.py
@@ -34,23 +33,39 @@ And it can also be fully instantiated in the conanfile ``generate()`` method:
3433
requires = "zlib/1.2.11"
3534
3635
def generate(self):
37-
bz = PremakeDeps(self)
38-
bz.generate()
39-
40-
Generated files
41-
---------------
36+
deps = PremakeDeps(self)
37+
deps.generate()
4238
43-
When the ``PremakeDeps`` generator is used, every invocation of ``conan install`` will
44-
generate a ``include('conandeps.premake5.lua')`` that can be included and used in the project:
39+
.. important::
4540

41+
The ``PremakeDeps`` generator must be used in conjunction with the
42+
:ref:`PremakeToolchain <conan_tools_premake_premaketoolchain>` generator, as it will
43+
generate a ``include('conandeps.premake5.lua')`` that will be automatically
44+
included by the toolchain.
4645

47-
.. code-block:: lua
48-
49-
-- premake5.lua
50-
51-
include('conandeps.premake5.lua')
46+
Generated files
47+
---------------
5248

53-
workspace "HelloWorld"
54-
conan_setup()
55-
configurations { "Debug", "Release" }
56-
platforms { "x86_64" }
49+
``PremakeDeps`` will generate a ``conandeps.premake5.lua`` script file which
50+
will be injected later by the toolchain and the following files per dependency in the ``conanfile.generators_folder``:
51+
52+
- ``conan_<pkg>.premake5.lua``: will be including the proper script depending on the ``build_type`` and architecture.
53+
54+
- ``conan_<pkg>_vars_<config>.premake5.lua``: will contain essentially the following information for the specific dependency, architecture and build_type:
55+
56+
- ``includedirs``
57+
- ``libdirs``
58+
- ``bindirs``
59+
- ``sysincludedirs``
60+
- ``frameworkdirs``
61+
- ``frameworks``
62+
- ``libs``
63+
- ``syslibs``
64+
- ``defines``
65+
- ``cxxflags``
66+
- ``cflags``
67+
- ``sharedlinkflags``
68+
- ``exelinkflags``
69+
70+
All this information will be loaded in the ``conandeps.premake5.lua`` script
71+
and injected later to the main premake script, allowing a transparent and easy to use dependency management with conan.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)