File: distutils-to-meson.rst

package info (click to toggle)
numpy 1%3A2.3.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 85,944 kB
  • sloc: python: 255,476; asm: 232,483; ansic: 212,559; cpp: 157,437; f90: 1,575; sh: 845; fortran: 567; makefile: 427; sed: 139; xml: 109; java: 97; perl: 82; cs: 62; javascript: 53; objc: 33; lex: 13; yacc: 9
file content (211 lines) | stat: -rw-r--r-- 6,420 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
.. _f2py-meson-distutils:


1 Migrating to ``meson``
------------------------

As per the timeline laid out in :ref:`distutils-status-migration`,
``distutils`` has ceased to be the default build backend for ``f2py``. This page
collects common workflows in both formats.

.. note::

    This is a ****living**** document, `pull requests <https://numpy.org/doc/stable/dev/howto-docs.html>`_ are very welcome!

1.1 Baseline
~~~~~~~~~~~~

We will start out with a slightly modern variation of the classic Fibonnaci
series generator.

.. code:: fortran

    ! fib.f90
    subroutine fib(a, n)
      use iso_c_binding
       integer(c_int), intent(in) :: n
       integer(c_int), intent(out) :: a(n)
       do i = 1, n
          if (i .eq. 1) then
             a(i) = 0.0d0
          elseif (i .eq. 2) then
             a(i) = 1.0d0
          else
             a(i) = a(i - 1) + a(i - 2)
          end if
       end do
    end

This will not win any awards, but can be a reasonable starting point.

1.2 Compilation options
~~~~~~~~~~~~~~~~~~~~~~~

1.2.1 Basic Usage
^^^^^^^^^^^^^^^^^

This is unchanged:

.. code:: bash

    python -m numpy.f2py -c fib.f90 -m fib
    ❯ python -c "import fib; print(fib.fib(30))"
    [     0      1      1      2      3      5      8     13     21     34
         55     89    144    233    377    610    987   1597   2584   4181
       6765  10946  17711  28657  46368  75025 121393 196418 317811 514229]

1.2.2 Specify the backend
^^^^^^^^^^^^^^^^^^^^^^^^^

.. tab-set::

  .. tab-item:: Distutils
    :sync: distutils

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend distutils

    This is the default for Python versions before 3.12.

  .. tab-item:: Meson
    :sync: meson

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend meson

    This is the only option for Python versions after 3.12.

1.2.3 Pass a compiler name
^^^^^^^^^^^^^^^^^^^^^^^^^^

.. tab-set::

  .. tab-item:: Distutils
    :sync: distutils

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend distutils --fcompiler=gfortran

  .. tab-item:: Meson
    :sync: meson

    .. code-block:: bash

      FC="gfortran" python -m numpy.f2py -c fib.f90 -m fib --backend meson

    Native files can also be used.

Similarly, ``CC`` can be used in both cases to set the ``C`` compiler. Since the
environment variables are generally pretty common across both, so a small
sample is included below.

.. table::

    +------------------------------------+-------------------------------+
    | **Name**                           | **What**                      |
    +------------------------------------+-------------------------------+
    | FC                                 | Fortran compiler              |
    +------------------------------------+-------------------------------+
    | CC                                 | C compiler                    |
    +------------------------------------+-------------------------------+
    | CFLAGS                             | C compiler options            |
    +------------------------------------+-------------------------------+
    | FFLAGS                             | Fortran compiler options      |
    +------------------------------------+-------------------------------+
    | LDFLAGS                            | Linker options                |
    +------------------------------------+-------------------------------+
    | LD\ :sub:`LIBRARY`\ \ :sub:`PATH`\ | Library file locations (Unix) |
    +------------------------------------+-------------------------------+
    | LIBS                               | Libraries to link against     |
    +------------------------------------+-------------------------------+
    | PATH                               | Search path for executables   |
    +------------------------------------+-------------------------------+
    | LDFLAGS                            | Linker flags                  |
    +------------------------------------+-------------------------------+
    | CXX                                | C++ compiler                  |
    +------------------------------------+-------------------------------+
    | CXXFLAGS                           | C++ compiler options          |
    +------------------------------------+-------------------------------+


.. note::

    For Windows, these may not work very reliably, so `native files <https://mesonbuild.com/Native-environments.html>`_ are likely the
    best bet, or by direct `1.3 Customizing builds`_.

1.2.4 Dependencies
^^^^^^^^^^^^^^^^^^

Here, ``meson`` can actually be used to set dependencies more robustly.

.. tab-set::

  .. tab-item:: Distutils
    :sync: distutils

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend distutils -llapack

    Note that this approach in practice is error prone.

  .. tab-item:: Meson
    :sync: meson

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend meson --dep lapack

    This maps to ``dependency("lapack")`` and so can be used for a wide variety
    of dependencies. They can be `customized further <https://mesonbuild.com/Dependencies.html>`_
    to use CMake or other systems to resolve dependencies.

1.2.5 Libraries
^^^^^^^^^^^^^^^

Both ``meson`` and ``distutils`` are capable of linking against libraries.

.. tab-set::

  .. tab-item:: Distutils
    :sync: distutils

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend distutils -lmylib -L/path/to/mylib

  .. tab-item:: Meson
    :sync: meson

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend meson -lmylib -L/path/to/mylib

1.3 Customizing builds
~~~~~~~~~~~~~~~~~~~~~~

.. tab-set::

  .. tab-item:: Distutils
    :sync: distutils

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend distutils --build-dir blah

    This can be technically integrated with other codes, see :ref:`f2py-distutils`.

  .. tab-item:: Meson
    :sync: meson

    .. code-block:: bash

      python -m numpy.f2py -c fib.f90 -m fib --backend meson --build-dir blah

    The resulting build can be customized via the
    `Meson Build How-To Guide <https://mesonbuild.com/howtox.html>`_.
    In fact, the resulting set of files can even be committed directly and used
    as a meson subproject in a separate codebase.