aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/imports/qbs/Probes/PkgConfigProbe.qbs
blob: 0fe81c3cc71c3ce5b308674eca57a9c273509f6f (plain)
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qbs.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

import qbs.Host
import qbs.Process
import qbs.FileInfo

Probe {
    // Inputs
    property string sysroot: qbs.sysroot
    property string executable: 'pkg-config'
    property string name
    property stringList packageNames: [name]
    property string minVersion
    property string exactVersion
    property string maxVersion
    property bool forStaticBuild: false
    property stringList libDirs // Full, non-sysrooted paths, mirroring the environment variable

    // Output
    property stringList cflags // Unmodified --cflags output
    property stringList libs   // Unmodified --libs output

    property stringList defines
    property stringList libraries
    property stringList libraryPaths
    property stringList includePaths
    property stringList compilerFlags
    property stringList linkerFlags
    property string modversion

    configure: {
        if (!packageNames || packageNames.length === 0)
            throw 'PkgConfigProbe.packageNames must be specified.';
        var p = new Process();
        var stdout;
        try {
            var libDirsToSet = libDirs;
            if (sysroot) {
                p.setEnv("PKG_CONFIG_SYSROOT_DIR", sysroot);
                if (!libDirsToSet) {
                    libDirsToSet = [
                        sysroot + "/usr/lib/pkgconfig",
                        sysroot + "/usr/share/pkgconfig"
                    ];
                }
            }
            if (libDirsToSet)
                p.setEnv("PKG_CONFIG_LIBDIR", libDirsToSet.join(FileInfo.pathListSeparator()));
            var versionArgs = [];
            if (minVersion !== undefined)
                versionArgs.push("--atleast-version=" + minVersion);
            if (exactVersion !== undefined)
                versionArgs.push("--exact-version=" + exactVersion);
            if (maxVersion !== undefined)
                versionArgs.push("--max-version=" + maxVersion);
            if (versionArgs.length !== 0
                    && p.exec(executable, versionArgs.concat(packageNames)) !== 0) {
                return;
            }

            // protobuf is reserved as qbs module name, which depends on the protobuflib module
            packageNames = packageNames.map(function(name) {
                return name === "protobuflib" ? "protobuf" : name;
            });

            var args = packageNames;
            if (p.exec(executable, args.concat([ '--cflags' ])) === 0) {
                stdout = p.readStdOut().trim();
                cflags = stdout ? stdout.split(/\s/): [];
                var libsArgs = args.concat("--libs");
                if (forStaticBuild)
                    libsArgs.push("--static");
                if (p.exec(executable, libsArgs) === 0) {
                    stdout = p.readStdOut().trim();
                    libs = stdout ? stdout.split(/\s/): [];
                    if (p.exec(executable, [packageNames[0]].concat([ '--modversion' ])) === 0) {
                        modversion = p.readStdOut().trim();
                        found = true;
                        includePaths = [];
                        defines = []
                        compilerFlags = [];
                        for (var i = 0; i < cflags.length; ++i) {
                            var flag = cflags[i];
                            if (flag.startsWith("-I"))
                                includePaths.push(flag.slice(2));
                            else if (flag.startsWith("-D"))
                                defines.push(flag.slice(2));
                            else
                                compilerFlags.push(flag);
                        }
                        libraries = [];
                        libraryPaths = [];
                        linkerFlags = [];
                        for (i = 0; i < libs.length; ++i) {
                            flag = libs[i];
                            if (flag.startsWith("-l"))
                                libraries.push(flag.slice(2));
                            else if (flag.startsWith("-L"))
                                libraryPaths.push(flag.slice(2));
                            else
                                linkerFlags.push(flag);
                        }
                        console.debug("PkgConfigProbe: found packages " + packageNames);
                        return;
                    }
                }
            }
            found = false;
            cflags = undefined;
            libs = undefined;
        } finally {
            p.close();
        }
    }
}