aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/cpp/GenericGCC.qbs
blob: 14ee2753f9fa08c24d0187687e13b6b566e0865f (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
import qbs.base 1.0
import qbs.fileinfo 1.0 as FileInfo
import 'gcc.js' as Gcc
import '../utils.js' as ModUtils

CppModule {
    condition: false

    property var transitiveSOs
    property string toolchainPrefix
    property string toolchainInstallPath
    property string compilerName: 'g++'

    property string compilerPath: {
        var path = ''
        if (toolchainInstallPath) {
            path += toolchainInstallPath
            if (path.substr(-1) !== '/')
                path += '/'
        }
        if (toolchainPrefix)
            path += toolchainPrefix
        path += compilerName
        return path
    }

    Rule {
        id: dynamicLibraryLinker
        multiplex: true
        inputs: ["obj"]
        usings: ['dynamiclibrary', 'staticlibrary']

        Artifact {
            fileName: product.destinationDirectory + "/lib" + product.name + ".so"
            fileTags: ["dynamiclibrary"]
            cpp.transitiveSOs: {
                var result = []
                for (var i in inputs.dynamiclibrary) {
                    var lib = inputs.dynamiclibrary[i]
                    result.push(lib.fileName)
                    var impliedLibs = ModUtils.appendAll(lib, 'transitiveSOs')
                    result = result.concat(impliedLibs)
                }
                return result
            }
        }

        TransformProperties {
            property var libraryPaths: ModUtils.appendAll(product, 'libraryPaths')
            property var dynamicLibraries: ModUtils.appendAll(product, 'dynamicLibraries')
            property var staticLibraries: ModUtils.appendAll(product, 'staticLibraries')
            property var rpaths: ModUtils.appendAll(product, 'rpaths')
        }

        prepare: {
            var args = Gcc.configFlags(product);
            if (product.modules.qbs.targetOS == 'linux')
                args = args.concat([
                    '-Wl,--hash-style=gnu',
                    '-Wl,--as-needed',
                    '-Wl,--allow-shlib-undefined',
                    '-Wl,--no-undefined',
                    '-Wl,-soname=' + FileInfo.fileName(output.fileName)
                ]);
            args = args.concat([
                '-Wl,-rpath,\$ORIGIN',
                '-shared'
            ]);
            for (var i in inputs.obj)
            args.push(inputs.obj[i].fileName);

            var staticLibrariesI = [];
            for (i in inputs.staticlibrary) {
                staticLibrariesI.push(inputs.staticlibrary[i].fileName);
            }
            staticLibrariesI = staticLibrariesI.concat(staticLibraries);

            var dynamicLibrariesI = [];
            for (i in inputs.dynamiclibrary) {
                libraryPaths.push(FileInfo.path(inputs.dynamiclibrary[i].fileName))
                var fileName = FileInfo.fileName(inputs.dynamiclibrary[i].fileName)
                fileName = fileName.substr(3, fileName.length - 6)
                dynamicLibrariesI.push(fileName)
            }
            dynamicLibrariesI = dynamicLibrariesI.concat(dynamicLibraries);

            args.push('-o');
            args.push(output.fileName);
            args = args.concat(Gcc.libs(libraryPaths, rpaths, dynamicLibrariesI, staticLibrariesI));
            var cmd = new Command(product.module.compilerPath, args);
            Gcc.linkEnv(cmd)
            cmd.description = 'linking ' + FileInfo.fileName(output.fileName);
            cmd.highlight = 'linker';
            return cmd;
        }
    }

    Rule {
        id: staticLibraryLinker
        multiplex: true
        inputs: ["obj"]
        usings: ['dynamiclibrary', 'staticlibrary']

        Artifact {
            fileName: product.destinationDirectory + "/lib" + product.name + ".a"
            fileTags: ["staticlibrary"]
            cpp.staticLibraries: {
                var result = []
                for (var i in inputs.staticlibrary) {
                    var lib = inputs.staticlibrary[i]
                    result.push(lib.fileName)
                    var impliedLibs = ModUtils.appendAll(lib, 'staticLibraries')
                    result.concat(impliedLibs)
                }
                return result
            }
            cpp.dynamicLibraries: {
                var result = []
                for (var i in inputs.dynamiclibrary) {
                    var lib = inputs.dynamiclibrary[i]
                    result.push(lib.fileName)
                    var impliedLibs = ModUtils.appendAll(lib, 'dynamicLibraries')
                    result.concat(impliedLibs)
                }
                return result
            }
        }

        prepare: {
            var args = ['rcs', output.fileName];
            for (var i in inputs.obj)
                args.push(inputs.obj[i].fileName);
            var cmd = new Command('ar', args);
            cmd.description = 'creating ' + FileInfo.fileName(output.fileName);
            cmd.highlight = 'linker'
            return cmd;
        }
    }

    Rule {
        id: applicationLinker
        multiplex: true
        inputs: ["obj"]
        usings: ['dynamiclibrary', 'staticlibrary']

        Artifact {
            fileName: product.destinationDirectory + "/" + product.name
            fileTags: ["application"]
        }

        TransformProperties {
            property var libraryPaths: ModUtils.appendAll(product, 'libraryPaths')
            property var dynamicLibraries: ModUtils.appendAllFromArtifacts(product, inputs.dynamiclibrary, 'cpp', 'dynamicLibraries')
            property var staticLibraries: ModUtils.appendAllFromArtifacts(product, inputs.staticlibrary, 'cpp', 'staticLibraries')
            property var rpaths: ModUtils.appendAll(product, 'rpaths')
        }

        prepare: {
            var args = Gcc.configFlags(product);
            for (var i in inputs.obj)
                args.push(inputs.obj[i].fileName)
            args.push('-Wl,-rpath,\$ORIGIN');
            args.push('-o');
            args.push(output.fileName);

            var staticLibrariesI = [];
            for (i in inputs.staticlibrary) {
                staticLibrariesI.push(inputs.staticlibrary[i].fileName);
            }
            staticLibrariesI = staticLibrariesI.concat(staticLibraries);

            var dynamicLibrariesI = [];
            for (i in dynamicLibraries) {
                if (dynamicLibraries[i].match("lib.*\\.so$") != null) {
                    // shared object filename found
                    var libDir = FileInfo.path(dynamicLibraries[i])
                    var libName = FileInfo.fileName(dynamicLibraries[i])
                    libName = libName.substr(3, libName.length - 6)
                    libraryPaths.push(libDir)
                    dynamicLibrariesI.push(libName)
                } else {
                    // shared object libname found
                    dynamicLibrariesI.push(dynamicLibraries[i])
                }
            }

            if (product.modules.qbs.targetOS === 'linux') {
                var transitiveSOs = ModUtils.appendAllFromArtifacts(product, inputs.dynamiclibrary, 'cpp', 'transitiveSOs')
                for (i in transitiveSOs) {
                    args.push("-Wl,-rpath-link=" + FileInfo.path(transitiveSOs[i]))
                }
            }

            for (i in inputs.dynamiclibrary) {
                libraryPaths.push(FileInfo.path(inputs.dynamiclibrary[i].fileName))
                var fileName = FileInfo.fileName(inputs.dynamiclibrary[i].fileName)
                fileName = fileName.substr(3, fileName.length - 6)
                dynamicLibrariesI.push(fileName)
            }

            args = args.concat(Gcc.libs(libraryPaths, rpaths, dynamicLibrariesI, staticLibrariesI));
            var cmd = new Command(product.module.compilerPath, args);
            Gcc.linkEnv(cmd, product)
            cmd.description = 'linking ' + FileInfo.fileName(output.fileName);
            cmd.highlight = 'linker'
            return cmd;
        }
    }

    Rule {
        id: compiler
        inputs: ["cpp", "c"]
        explicitlyDependsOn: ["c++_pch"]

        Artifact {
            fileTags: ["obj"]
            // ### make it possible to override ".obj" in a project file
            fileName: ".obj/" + product.name + "/" + input.baseDir + "/" + input.baseName + ".o"
        }

        TransformProperties {
            property var defines: ModUtils.appendAll(input, 'defines')
            property var includePaths: ModUtils.appendAll(input, 'includePaths')
            property var compilerFlags: ModUtils.appendAll(input, 'compilerFlags')
        }

        prepare: {
            var args = Gcc.configFlags(input);

            // ### what we actually need here is something like product.usedFileTags
            //     that contains all fileTags that have been used when applying the rules.
            if (product.type.indexOf('staticlibrary') >= 0 || product.type.indexOf('dynamiclibrary') >= 0) {
                args.push('-fPIC');
            } else if (product.type.indexOf('application') < 0) {
                throw ("OK, now we got a problem... The product's type must be in {staticlibrary, dynamiclibrary, application}. But it is " + product.type + '.');
            }

            for (var i in defines)
                args.push('-D' + defines[i]);
            for (var i in includePaths)
                args.push('-I' + includePaths[i]);
            if (product.module.precompiledHeader) {
                args.push('-include')
                args.push(product.name)
                var pchPath = product.module.precompiledHeaderDir[0]
                var pchPathIncluded = false
                for (var i in includePaths) {
                    if (includePaths[i] == pchPath) {
                        pchPathIncluded = true
                        break
                    }
                }
                if (!pchPathIncluded)
                    args.push('-I' + pchPath)
            }
            if (input.fileTags.indexOf("c") >= 0) {
                args.push('-x')
                args.push('c')
            }
            args.push('-c');
            args.push(input.fileName);
            args.push('-o');
            args.push(output.fileName);
            args = args.concat(compilerFlags);
            var cmd = new Command(product.module.compilerPath, args);
            cmd.description = 'compiling ';
            Gcc.describe(cmd, input.fileName, output.fileName);
            cmd.highlight = "compiler";
            return cmd;
        }
    }

    Transformer {
        condition: precompiledHeader != null
        inputs: precompiledHeader
        Artifact {
            fileName: product.name + ".gch"
            fileTags: "c++_pch"
        }
        prepare: {
            var args = Gcc.configFlags(product);
            for (var i in product.module.defines)
                args.push('-D' + defines[i]);
            for (var i in product.module.includePaths)
                args.push('-I' + includePaths[i]);
            args.push('-x');
            args.push('c++-header');
            args.push('-c');
            args.push(product.module.precompiledHeader);
            args.push('-o');
            args.push(output.fileName);
            if (product.module.compilerFlags)
                args = args.concat(product.module.compilerFlags);
            var cmd = new Command(product.module.compilerPath, args);
            cmd.description = 'precompiling ' + FileInfo.fileName(input.fileName);
            return cmd;
        }
    }
}