aboutsummaryrefslogtreecommitdiffstats
path: root/packaging-tools/patch_qt.py
blob: f138dde693a2e099783befb747eab48481450b4e (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#############################################################################
#
# Copyright (C) 2022 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# This file is part of the release tools of the Qt Toolkit.
#
# $QT_BEGIN_LICENSE:GPL-EXCEPT$
# 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 https://www.qt.io/terms-conditions. For further
# information use the contact form at https://www.qt.io/contact-us.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3 as published by the Free Software
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
#
# $QT_END_LICENSE$
#
#############################################################################

import os
import re
import fileinput


def _fileIterator(artifactsDir):
    print('Patching build time paths from: {0}'.format(artifactsDir))
    for root, dirs, files in os.walk(artifactsDir):
        for fileName in files:
            yield os.path.join(os.path.join(root, fileName))


def _getPatchers(product):
    if product == 'qt_framework':
        return [patchAbsoluteLibPathsFromFile, eraseQmakePrlBuildDir, patchQConfigPri]
    else:
        # default
        return [patchAbsoluteLibPathsFromFile, eraseQmakePrlBuildDir]


def patchFiles(artifactsDir, product):
    print('Patching files from: {0}'.format(artifactsDir))
    patchers = _getPatchers(product)
    for filePath in _fileIterator(artifactsDir):
        for patcher in patchers:
            patcher(filePath)


def patchQtEdition(artifactsDir, licheckFileName, releaseDate):
    for root, dirs, files in os.walk(artifactsDir):
        for fileName in files:
            if fileName == 'qconfig.pri':
                _patchQtEdition(os.path.join(root, fileName), licheckFileName, releaseDate)
                return


def _patchQtEdition(filePath, licheckFileName, releaseDate):
    for line in fileinput.FileInput(filePath, inplace=True):
        if 'QT_EDITION' in line:
            edition_line = 'QT_EDITION = Enterprise'
            licheck_line = 'QT_LICHECK = ' + licheckFileName
            release_line = 'QT_RELEASE_DATE = ' + releaseDate
            print(edition_line.rstrip('\n'))
            print(licheck_line.rstrip('\n'))
            print(release_line.rstrip('\n'))
        else:
            print(line.rstrip('\n'))


def patchQConfigPri(filePath):
    for line in fileinput.FileInput(filePath, inplace=True):
        patchedLine = patchQConfigPriFromLine(line)
        print(patchedLine.rstrip('\n'))


def patchQConfigPriFromLine(line):
    if 'QMAKE_DEFAULT_LIBDIRS' in line:
        return line.split('=')[0].strip() + ' ='
    if 'QMAKE_DEFAULT_INCDIRS' in line:
        return line.split('=')[0].strip() + ' ='
    else:
        return line


def eraseQmakePrlBuildDir(filePath):
    # Erase lines starting with 'QMAKE_PRL_BUILD_DIR' from .prl files
    for line in fileinput.FileInput(filePath, inplace=True):
        patchedLine = patchQmakePrlBuildDirFromLine(line)
        print(patchedLine.rstrip('\n'))


def patchQmakePrlBuildDirFromLine(line):
    return '' if line.startswith('QMAKE_PRL_BUILD_DIR') else line


def patchAbsoluteLibPathsFromFile(filePath):
    for line in fileinput.FileInput(filePath, inplace=True):
        patchedLine = patchAbsoluteLibPathsFromLine(line, filePath.split(".")[-1])
        print(patchedLine.rstrip('\n'))


def patchAbsoluteLibPathsFromLine(line, fileExtension):
    """
    Captures XXX in e.g. /usr/lib/libXXX.so, /usr/lib64/libXXX.a, and C:\XXX.lib
    Paths are not allowed to contain whitespace though
      [^\s\"]+ - start of path
      "/lib", [\\/]
      ([a-zA-Z0-9\_\-\.\+]+) - capture group for the actual library name
      ".so", ".a", ".lib" suffix
      (\.[0-9]+)? - capture group for for versioned libraries
    """

    def _removeWhiteSpace(line):
        """Remove white space from paths if found inside quoted blocks."""
        eraseEnabled = False
        result = ""
        for char in line:
            if char == "\"":
                # toggle on/off
                eraseEnabled = not eraseEnabled
            if eraseEnabled and char == " ":
                continue
            result += char
        return result

    if fileExtension == "cmake":
        # from cmake files patch only lines containing "find_extra_libs"
        cmakeFindExtraLibsSearchRegexp = re.compile(r'_*._find_extra_libs\(')
        if not re.search(cmakeFindExtraLibsSearchRegexp, line):
            return line

    expressions = [
        re.compile(r'[^\s\"]+/lib([a-zA-Z0-9\_\-\.\+]+)\.(so|a|tbd)(\.[0-9]+)?\b'),
        re.compile(r'[^\s\"]+[\\/]([a-zA-Z0-9\_\-\.\+]+)\.(lib)(\.[0-9]+)?\b')
    ]

    def _substituteLib(match):
        if (match.group(0).startswith("$$[QT_")):
            return match.group(0)
        result = "" if fileExtension == "cmake" else "-l"  # .pri, .prl, .la, .pc
        result += match.group(1)
        return result

    for regex in expressions:
        # check if there are any matches?
        if re.search(regex, line):
            line = _removeWhiteSpace(line)
            line = regex.sub(_substituteLib, line)
            break

    return line