File: pkgconfig.py

package info (click to toggle)
fenics-dolfinx 1%3A0.10.0.post4-1exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,028 kB
  • sloc: cpp: 36,535; python: 25,391; makefile: 226; sh: 171; xml: 55
file content (53 lines) | stat: -rw-r--r-- 1,516 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2018 Chris N Richardson
#
# This file is part of DOLFINx (https://www.fenicsproject.org)
#
# SPDX-License-Identifier:    LGPL-3.0-or-later
"""Tool for querying pkg-config files.

This module exists solely to extract the compilation and linking
information saved in the **dolfinx.pc** pkg-config file, needed for JIT
compilation.
"""

import os
import subprocess


def _pkgconfig_query(s):
    pkg_config_exe = os.environ.get("PKG_CONFIG", None) or "pkg-config"
    cmd = [pkg_config_exe, *s.split()]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, _err = proc.communicate()
    rc = proc.returncode
    return (rc, out.rstrip().decode("utf-8"))


def exists(package) -> bool:
    """Test for the existence of a pkg-config file for a named package."""
    return _pkgconfig_query("--exists " + package)[0] == 0


def parse(package):
    """Return a dict containing compile-time definitions."""
    parse_map = {
        "-D": "define_macros",
        "-I": "include_dirs",
        "-L": "library_dirs",
        "-l": "libraries",
    }

    result = {x: [] for x in parse_map.values()}

    # Execute the query to pkg-config and clean the result
    out = _pkgconfig_query(package + " --cflags --libs")[1]
    out = out.replace('\\"', "")

    # Iterate through each token in the output
    for token in out.split():
        key = parse_map.get(token[:2])
        if key:
            t = token[2:].strip()
            result[key].append(t)

    return result