aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/cpp/android-gcc.qbs
blob: b5dc367ee464d8f19cfa01405480325b18b549a1 (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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of the Qt Build Suite.
**
** 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
import qbs.File
import qbs.FileInfo
import qbs.ModUtils
import qbs.TextFile
import qbs.Utilities
import "../../modules/Android/ndk/utils.js" as NdkUtils
import 'gcc.js' as Gcc

LinuxGCC {
    Depends { name: "Android.ndk" }

    condition: qbs.targetOS.contains("android") &&
               qbs.toolchain && qbs.toolchain.contains("gcc")
    priority: 2
    rpaths: [rpathOrigin]

    property string toolchainDir: {
        if (qbs.toolchain && qbs.toolchain.contains("clang")) {
            return Utilities.versionCompare(Android.ndk.version, "10") <= 0
                    ? "llvm-" + Android.ndk.toolchainVersionNumber : "llvm";
        }
        return NdkUtils.getBinutilsPath(Android.ndk, toolchainTriple + "-")
    }

    property string cxxStlBaseDir: FileInfo.joinPaths(Android.ndk.ndkDir, "sources", "cxx-stl")
    property string gabiBaseDir: FileInfo.joinPaths(cxxStlBaseDir, "gabi++")
    property string stlPortBaseDir: FileInfo.joinPaths(cxxStlBaseDir, "stlport")
    property string gnuStlBaseDir: FileInfo.joinPaths(cxxStlBaseDir, "gnu-libstdc++",
                                                      Android.ndk.toolchainVersionNumber)
    property string llvmStlBaseDir: FileInfo.joinPaths(cxxStlBaseDir, "llvm-libc++")
    property string stlBaseDir: {
        if (Android.ndk.appStl.startsWith("gabi++_"))
            return gabiBaseDir;
        else if (Android.ndk.appStl.startsWith("stlport_"))
            return stlPortBaseDir;
        else if (Android.ndk.appStl.startsWith("gnustl_"))
            return gnuStlBaseDir;
        else if (Android.ndk.appStl.startsWith("c++_"))
            return llvmStlBaseDir;
        return undefined;
    }

    property string stlLibsDir: {
        if (stlBaseDir) {
            var infix = Android.ndk.abi;
            if (Android.ndk.armMode === "thumb" && !Android.ndk.haveUnifiedStl)
                infix = FileInfo.joinPaths(infix, "thumb");
            return FileInfo.joinPaths(stlBaseDir, "libs", infix);
        }
        return undefined;
    }

    property string sharedStlFilePath: (stlLibsDir && Android.ndk.appStl.endsWith("_shared"))
        ? FileInfo.joinPaths(stlLibsDir, dynamicLibraryPrefix + Android.ndk.appStl + dynamicLibrarySuffix)
        : undefined
    property string staticStlFilePath: (stlLibsDir && Android.ndk.appStl.endsWith("_static"))
        ? FileInfo.joinPaths(stlLibsDir, staticLibraryPrefix + Android.ndk.appStl + staticLibrarySuffix)
        : undefined

    Group {
        name: "Android STL"
        condition: product.cpp.sharedStlFilePath
        files: product.cpp.sharedStlFilePath ? [product.cpp.sharedStlFilePath] : []
        fileTags: ["android.unstripped-stl"]
    }

    toolchainInstallPath: FileInfo.joinPaths(Android.ndk.ndkDir, "toolchains",
                                             toolchainDir, "prebuilt",
                                             Android.ndk.hostArch, "bin")

    property string toolchainTriple: [targetAbi === "androideabi" ? "arm" : targetArch,
                                      targetSystem, targetAbi].join("-")

    toolchainPrefix: {
        if (qbs.toolchain && qbs.toolchain.contains("clang"))
            return undefined;
        return toolchainTriple + "-";
    }

    machineType: {
        if (Android.ndk.abi === "armeabi")
            return "armv5te";
        if (Android.ndk.abi === "armeabi-v7a")
            return "armv7-a";
    }

    qbs.optimization: targetAbi === "androideabi" ? "small" : base

    enableExceptions: Android.ndk.appStl !== "system"
    enableRtti: Android.ndk.appStl !== "system"

    commonCompilerFlags: NdkUtils.commonCompilerFlags(qbs.toolchain, qbs.buildVariant,
                                                      Android.ndk.abi, Android.ndk.armMode)

    linkerFlags: NdkUtils.commonLinkerFlags(Android.ndk.abi)

    platformDriverFlags: ["-no-canonical-prefixes"]

    libraryPaths: {
        var prefix = FileInfo.joinPaths(sysroot, "usr");
        var paths = [];
        if (Android.ndk.abi === "mips64" || Android.ndk.abi === "x86_64") // no lib64 for arm64-v8a
            paths.push(FileInfo.joinPaths(prefix, "lib64"));
        paths.push(FileInfo.joinPaths(prefix, "lib"));
        return paths;
    }

    dynamicLibraries: {
        var libs = ["c", "m"];
        if (sharedStlFilePath)
            libs.push(sharedStlFilePath);
        return libs;
    }
    staticLibraries: {
        var libs = ["gcc"];
        if (staticStlFilePath)
            libs.push(staticStlFilePath);
        return libs;
    }
    systemIncludePaths: {
        var includes = [];
        if (Android.ndk.useUnifiedHeaders) {
            // Might not be needed with Clang in a future NDK release
            includes.push(FileInfo.joinPaths(sysroot, "usr", "include", toolchainTriple));
        }
        if (Android.ndk.appStl === "system") {
            includes.push(FileInfo.joinPaths(cxxStlBaseDir, "system", "include"));
        } else if (Android.ndk.appStl.startsWith("gabi++")) {
            includes.push(FileInfo.joinPaths(gabiBaseDir, "include"));
        } else if (Android.ndk.appStl.startsWith("stlport")) {
            includes.push(FileInfo.joinPaths(stlPortBaseDir, "stlport"));
        } else if (Android.ndk.appStl.startsWith("gnustl")) {
            includes.push(FileInfo.joinPaths(gnuStlBaseDir, "include"));
            includes.push(FileInfo.joinPaths(gnuStlBaseDir, "libs", Android.ndk.abi, "include"));
            includes.push(FileInfo.joinPaths(gnuStlBaseDir, "include", "backward"));
        } else if (Android.ndk.appStl.startsWith("c++_")) {
            if (Utilities.versionCompare(Android.ndk.version, "13") >= 0)
                includes.push(FileInfo.joinPaths(llvmStlBaseDir, "include"));
            else
                includes.push(FileInfo.joinPaths(llvmStlBaseDir, "libcxx", "include"));
            includes.push(FileInfo.joinPaths(llvmStlBaseDir + "abi", "libcxxabi", "include"));
        }
        return includes;
    }
    defines: {
        var list = ["ANDROID"];
        if (Android.ndk.useUnifiedHeaders) {
            // Might be superseded by an -mandroid-version or similar Clang compiler flag in future
            list.push("__ANDROID_API__=" + Android.ndk.platformVersion);
        }
        return list;
    }
    binutilsPath: FileInfo.joinPaths(Android.ndk.ndkDir, "toolchains",
                                     NdkUtils.getBinutilsPath(Android.ndk, toolchainTriple + "-"),
                                     "prebuilt", Android.ndk.hostArch, "bin");
    binutilsPathPrefix: Gcc.pathPrefix(binutilsPath, toolchainTriple + "-")
    driverFlags: qbs.toolchain.contains("clang")
                 ? ["-gcc-toolchain", FileInfo.path(binutilsPath)].concat(base || []) : base
    syslibroot: FileInfo.joinPaths(Android.ndk.ndkDir, "platforms",
                                   Android.ndk.platform, "arch-"
                                   + NdkUtils.abiNameToDirName(Android.ndk.abi))
    sysroot: !Android.ndk.useUnifiedHeaders
             ? syslibroot
             : FileInfo.joinPaths(Android.ndk.ndkDir, "sysroot")

    targetArch: {
        switch (qbs.architecture) {
        case "arm64":
            return "aarch64";
        case "armv5":
        case "armv5te":
            return "armv5te";
        case "armv7a":
        case "x86_64":
            return qbs.architecture;
        case "x86":
            return "i686";
        case "mips":
        case "mipsel":
            return "mipsel";
        case "mips64":
        case "mips64el":
            return "mips64el";
        }
    }

    targetVendor: "none"
    targetSystem: "linux"
    targetAbi: "android" + (["armeabi", "armeabi-v7a"].contains(Android.ndk.abi) ? "eabi" : "")

    endianness: "little"

    Rule {
        inputs: ["android.unstripped-stl"]
        Artifact {
            filePath: FileInfo.joinPaths("stripped-libs", input.fileName);
            fileTags: ["android.stripped-stl"]
        }
        prepare: {
            var args = ["--strip-unneeded", "-o", output.filePath, input.filePath];
            var cmd = new Command(product.cpp.stripPath, args);
            cmd.description = "stripping " + input.fileName;
            return [cmd];
        }
    }

    Rule {
        inputs: ["dynamiclibrary"]
        explicitlyDependsOn: ["android.stripped-stl"];
        outputFileTags: ["android.nativelibrary", "android.gdbserver-info", "android.stl-info"]
        outputArtifacts: {
            var artifacts = [{
                    filePath: FileInfo.joinPaths("stripped-libs",
                                                 inputs["dynamiclibrary"][0].fileName),
                    fileTags: ["android.nativelibrary"]
            }];
            if (product.moduleProperty("qbs", "buildVariant") === "debug") {
                artifacts.push({
                        filePath: "android.gdbserver-info.txt",
                        fileTags: ["android.gdbserver-info"]
                });
            }
            if (explicitlyDependsOn["android.stripped-stl"])
                artifacts.push({filePath: "android.stl-info.txt", fileTags: ["android.stl-info"]});
            return artifacts;
        }

        prepare: {
            var copyCmd = new JavaScriptCommand();
            copyCmd.silent = true;
            copyCmd.sourceCode = function() {
                File.copy(inputs["dynamiclibrary"][0].filePath,
                          outputs["android.nativelibrary"][0].filePath);
                var arch = product.moduleProperty("Android.ndk", "abi");
                var destDir = FileInfo.joinPaths("lib", arch);
                if (product.moduleProperty("qbs", "buildVariant") === "debug") {
                    arch = NdkUtils.abiNameToDirName(arch);
                    var srcPath = FileInfo.joinPaths(
                        product.moduleProperty("Android.ndk", "ndkDir"),
                                "prebuilt/android-" + arch, "gdbserver/gdbserver");
                    var targetPath = FileInfo.joinPaths(destDir,
                        product.moduleProperty("Android.ndk", "gdbserverFileName"));
                    var infoFile = new TextFile(outputs["android.gdbserver-info"][0].filePath,
                                                TextFile.WriteOnly);
                    infoFile.writeLine(srcPath);
                    infoFile.writeLine(targetPath);
                    infoFile.close();
                }
                var strippedStlList = explicitlyDependsOn["android.stripped-stl"];
                if (strippedStlList) {
                    var srcPath = strippedStlList[0].filePath;
                    var targetPath = FileInfo.joinPaths(destDir, FileInfo.fileName(srcPath));
                    var infoFile = new TextFile(outputs["android.stl-info"][0].filePath,
                                                TextFile.WriteOnly);
                    infoFile.writeLine(srcPath);
                    infoFile.writeLine(targetPath);
                    infoFile.close();
                }
            }
            var stripArgs = ["--strip-unneeded", outputs["android.nativelibrary"][0].filePath];
            var stripCmd = new Command(product.moduleProperty("cpp", "stripPath"), stripArgs);
            stripCmd.description = "Stripping unneeded symbols from "
                    + outputs["android.nativelibrary"][0].fileName;
            return [copyCmd, stripCmd];
        }
    }

    validate: {
        if (!_skipAllChecks)
            return;
        var baseValidator = new ModUtils.PropertyValidator("qbs");
        baseValidator.addCustomValidator("architecture", targetArch, function (value) {
            return value !== undefined;
        }, "unknown Android architecture '" + qbs.architecture + "'.");

        var validator = new ModUtils.PropertyValidator("cpp");
        validator.setRequiredProperty("targetArch", targetArch);

        return baseValidator.validate() && validator.validate();
    }
}