File: pkgconf.py

package info (click to toggle)
dh-fortran 0.57
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 816 kB
  • sloc: f90: 5,705; python: 1,485; perl: 610; makefile: 80; sh: 7
file content (139 lines) | stat: -rw-r--r-- 4,792 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
#!/usr/bin/env python3
"""


Copyright (C) 2025 Alastair McKinstry <mckinstry@debian.org>
Released under the GPL-3 GNU Public License.
"""

# Changes:
#

import click
import dhfortran.debhelper as dh
import dhfortran.cli as cli
import dhfortran.compilers as cmplrs
from os.path import basename


##
## Pkgconfig-file -specific interface for debhelper
##
class PkgconfFileHelper(dh.DhFortranHelper):

    def __init__(self, options):
        super().__init__(options, "fortran-pkgconf", "dh_fortran_pkgconf")

    def compute_dest(self, src_file, vendor=None, flavor=None):
        """Where does pkgconfig file go ? Should be called by base DebHelper() to move files"""

    def process_file(self, pkg, filename, target_pkg, target_dest=None):
        cli.verbose_print(
            f"DEBUG process_file [pkgconf]  name {filename} target_pkg {target_pkg} target_dest {target_dest}"
        )
        fmoddir = cmplrs.get_fmoddir(self.options.flavor)
        flibdir = cmplrs.flibdir + "/" + cmplrs.get_abi_vendor(self.options.flavor)
        fc = self.options.flavor

        update = {"@FMODDIR@": fmoddir, "@FLIBDIR@": flibdir, "@FC@": fc}
        updated = set()

        pkgdir = f"{cmplrs.flibdir}/{self.options.flavor}/pkgconfig"
        newname = f"{target_pkg}/{pkgdir}/{basename(filename)}"

        with open(filename, "r") as f:
            lines = f.readlines()
        for i, line in enumerate(lines):
            for v in update:
                if v in line:
                    line[i] = line[i].replace(v, update[v])
                    updated.add(v)
        # Updating vars is more risky. Update only if subst not already done
        for i, line in enumerate(lines):
            if line.startswith("fmoddir="):
                if "{prefix}" in line and "@FMODDIR@" in updated:
                    continue
                lines[i] = f"fmoddir={fmoddir}"
            if line.startswith("flibdir="):
                if "{prefix}" in line and "@FLIBDIR@" in updated:
                    continue
                lines[i] = f"flibdir={flibdir}"
            if line.startswith("FC="):
                if "{prefix}" in line and "@FC@" in updated:
                    continue
                lines[i] = f"FC={fc}"

        self.install_dir(f"{target_pkg}/{pkgdir}")
        with open(newname, "w") as n:
            for line in lines:
                print(line, file=n, end='')


@click.command(
    context_settings=dict(
        ignore_unknown_options=True,
    )
)
@click.option("--flavor", help="Fortran compiler flavor, eg. gfortran-15")
@click.argument("files", nargs=-1, type=click.UNPROCESSED)
@cli.debhelper_common_args
def dh_fortran_pkgconf(files, *args, **kwargs):
    """
    *dh_fortran_pkgconf* is a debhelper program that rewrites pkgconfig files and
    updates paths to FMODDIR and FLIBDIR to find the correct Fortran modules files
    and libraries for a given compiler.
    
    *dh_fortran_pkgconf* is expected to be automatically added using the debhelper
    "addon" <fortran> ie. either automatically, by build-depending on 'dh-sequence-fortran',
    or explicitly:

        dh $@ --with fortran

    <dh_fortran_pkgconf> Searches the debian/ directory for files B<debian/pkg.fortran-pkgconf>
    which lists pkgconfig files to process, with the same syntax as debhelper install files.

    OPTIONS
        --sourcedir=dir

    Look in the specified directory for files to be installed.

    USAGE

    The source pkgconfig file will be read and any placeholders @FMODDIR@, @FLIBDIR@ and @FC@
    will be updated to the correct values. If these are not found, relevant lines in the
    value section will be updated (eg. fmoddir= lines).
    
    TODO

    (1) relative entries such as prefix defines may be broken by moving pc file
    (2) Insert fmoddir, flibdir if missing


    The fortran-pkgconf file syntax follows dh_install: pairs of sources and optional target directories.
    If the target directory is absolute (starts with a  '/'), this directory is used in the target package.
    If the target  does not absolute, it will be treated as a subdirectory of the pkgconfig path.

    The rewritten pkgconfig files will by default be added to /usr/lib/$multiarch/fortran/$compiler-flavor/pkgconfig
    (eg usr/lib/aarch64-linux-gnu/fortran/gfortran-15/pkgconfig/mypkg.pc )
    This can then be used in the PKG_CONFIG_PATH
    """

    
    cli.verbose_print(f"dh_fortran_pkgconf called with {kwargs}")

    flavor = cmplrs.get_flavor(kwargs["flavor"])
    cli.validate_flavor(flavor)
    kwargs.update(
        {
            "flavor": flavor,
        }
    )
    d = PkgconfFileHelper(dh.build_options(**kwargs))
    d.process_and_move_files(*files)
    ...


if __name__ == "__main__":
    import pytest

    pytest.main(["tests/pkgconf.py"])