1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing
5**
6** This file is part of Qbs.
7**
8** Commercial License Usage
9** Licensees holding valid commercial Qt licenses may use this file in
10** accordance with the commercial license agreement provided with the
11** Software or, alternatively, in accordance with the terms contained in
12** a written agreement between you and The Qt Company. For licensing terms and
13** conditions see http://www.qt.io/terms-conditions. For further information
14** use the contact form at http://www.qt.io/contact-us.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 or version 3 as published by the Free
19** Software Foundation and appearing in the file LICENSE.LGPLv21 and
20** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
21** following information to ensure the GNU Lesser General Public License
22** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
23** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, The Qt Company gives you certain additional
26** rights.  These rights are described in The Qt Company LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29****************************************************************************/
30
31import qbs.File
32import qbs.FileInfo
33import qbs.ModUtils
34import qbs.TextFile
35import qbs.Utilities
36import "../../modules/Android/ndk/utils.js" as NdkUtils
37import 'gcc.js' as Gcc
38
39LinuxGCC {
40    Depends { name: "Android.ndk" }
41
42    condition: qbs.targetOS.contains("android") && qbs.toolchain && qbs.toolchain.contains("llvm")
43    priority: 2
44    rpaths: []
45
46    // toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android
47    cxxLanguageVersion: "c++14"
48
49    property string archLibsDir:  {
50        switch (qbs.architecture) {
51        case "arm64":
52            return "aarch64";
53        case "armv7a":
54            return "arm";
55        case "x86_64":
56            return qbs.architecture;
57        case "x86":
58            return "i686";
59        }
60    }
61    property string targetDir: "android" + (["armeabi", "armeabi-v7a"].contains(Android.ndk.abi) ? "eabi" : "")
62    property string triple: [archLibsDir, targetSystem, targetDir].join("-")
63    property string libsDir: FileInfo.joinPaths(sysroot, "usr", "lib", triple);
64
65    property string sharedStlFilePath: (libsDir && Android.ndk.appStl.endsWith("_shared"))
66        ? FileInfo.joinPaths(libsDir, dynamicLibraryPrefix + Android.ndk.appStl + dynamicLibrarySuffix)
67        : undefined
68    property string staticStlFilePath: (libsDir && Android.ndk.appStl.endsWith("_static"))
69        ? FileInfo.joinPaths(libsDir, Android.ndk.platformVersion,
70                             NdkUtils.stlFileName(staticLibraryPrefix, Android.ndk,
71                                                  staticLibrarySuffix))
72        : undefined
73
74    Group {
75        name: "Android STL"
76        condition: product.cpp.sharedStlFilePath && product.cpp.shouldLink
77        files: product.cpp.sharedStlFilePath ? [product.cpp.sharedStlFilePath] : []
78        fileTags: "android.stl"
79    }
80
81    toolchainInstallPath: FileInfo.joinPaths(Android.ndk.ndkDir, "toolchains",
82                                             "llvm", "prebuilt",
83                                             Android.ndk.hostArch, "bin")
84
85    property string toolchainTriple: [targetAbi === "androideabi" ? "arm" : targetArch,
86                                      targetSystem, targetAbi].join("-")
87
88    internalVersion: undefined
89    toolchainPrefix: undefined
90
91    machineType: {
92        if (Android.ndk.abi === "armeabi-v7a")
93            return "armv7-a";
94    }
95
96    qbs.optimization: targetAbi === "androideabi" ? "small" : base
97
98    enableExceptions: Android.ndk.appStl !== "system"
99    enableRtti: Android.ndk.appStl !== "system"
100
101    commonCompilerFlags: NdkUtils.commonCompilerFlags(qbs.toolchain, qbs.buildVariant, Android.ndk)
102
103    linkerFlags: NdkUtils.commonLinkerFlags(Android.ndk);
104    driverLinkerFlags: {
105        var flags = ["-fuse-ld=lld", "-Wl,--exclude-libs,libgcc.a", "-nostdlib++"];
106        // See https://android.googlesource.com/platform/ndk/+/ndk-release-r21/docs/BuildSystemMaintainers.md#Unwinding
107        if (Android.ndk.abi === "armeabi-v7a") {
108            flags = flags.concat(["-Wl,--exclude-libs,libgcc_real.a"]);
109            if (Android.ndk.appStl.startsWith("c++"))
110                flags = flags.concat(["-Wl,--exclude-libs,libunwind.a"]);
111        }
112        return flags;
113    }
114
115    platformDriverFlags: ["-fdata-sections", "-ffunction-sections", "-funwind-tables",
116                          "-fstack-protector-strong", "-no-canonical-prefixes"]
117
118    dynamicLibraries: {
119        var libs = ["c", "m"];
120        if (sharedStlFilePath)
121            libs.push(FileInfo.joinPaths(libsDir, Android.ndk.platformVersion,
122                                         NdkUtils.stlFileName(dynamicLibraryPrefix, Android.ndk,
123                                                              dynamicLibrarySuffix)));
124        return libs;
125    }
126    staticLibraries: staticStlFilePath
127
128    // When using ndk r19c, llvm doesn't add sysroot/usr/include/c++/v1 to the path
129    // But it works starting with ndk r20b
130    systemIncludePaths: (Utilities.versionCompare(Android.ndk.version, "20") < 0) ?
131                            FileInfo.joinPaths(sysroot, "usr", "include", "c++", "v1") : ""
132
133    defines: ["ANDROID", "__ANDROID__"]
134
135    binutilsPath: FileInfo.joinPaths(Android.ndk.ndkDir, "toolchains", "llvm", "prebuilt",
136                                     Android.ndk.hostArch, "bin");
137    binutilsPathPrefix: FileInfo.joinPaths(binutilsPath, "llvm-")
138    sysroot: FileInfo.joinPaths(Android.ndk.ndkDir, "toolchains", "llvm", "prebuilt",
139                                Android.ndk.hostArch, "sysroot")
140
141    targetArch: {
142        switch (qbs.architecture) {
143        case "arm64":
144            return "aarch64";
145        case "armv5":
146        case "armv5te":
147            return "armv5te";
148        case "armv7a":
149        case "x86_64":
150            return qbs.architecture;
151        case "x86":
152            return "i686";
153        }
154    }
155
156    target: [targetArch, targetSystem, targetAbi].join("-")
157    targetSystem: "linux"
158    targetAbi: "android" + (["armeabi", "armeabi-v7a"].contains(Android.ndk.abi) ? "eabi" : "") +
159               Android.ndk.platformVersion
160
161    endianness: "little"
162
163    Rule {
164        condition: shouldLink
165        inputs: "dynamiclibrary"
166        Artifact {
167            filePath: FileInfo.joinPaths("stripped-libs", input.fileName)
168            fileTags: "android.nativelibrary"
169        }
170        prepare: {
171            var stripArgs = ["--strip-all", "-o", output.filePath, input.filePath];
172            var stripCmd = new Command(product.cpp.stripPath, stripArgs);
173            stripCmd.description = "Stripping unneeded symbols from " + input.fileName;
174            return stripCmd;
175        }
176    }
177
178    _skipAllChecks: !shouldLink
179
180    validate: {
181        if (_skipAllChecks)
182            return;
183        var baseValidator = new ModUtils.PropertyValidator("qbs");
184        baseValidator.addCustomValidator("architecture", targetArch, function (value) {
185            return value !== undefined;
186        }, "unknown Android architecture '" + qbs.architecture + "'.");
187
188        var validator = new ModUtils.PropertyValidator("cpp");
189        validator.setRequiredProperty("targetArch", targetArch);
190
191        return baseValidator.validate() && validator.validate();
192    }
193}
194