aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/cpp/cosmic.js
blob: e11e4da2871d73d78afdc1d1e48d928750c94ae5 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/****************************************************************************
**
** Copyright (C) 2021 Denis Shienkov <denis.shienkov@gmail.com>
** 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.
**
****************************************************************************/

var Cpp = require("cpp.js");
var Environment = require("qbs.Environment");
var File = require("qbs.File");
var FileInfo = require("qbs.FileInfo");
var ModUtils = require("qbs.ModUtils");
var Process = require("qbs.Process");
var TemporaryDir = require("qbs.TemporaryDir");
var TextFile = require("qbs.TextFile");

function compilerName(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return  "cxcorm";
    throw "Unable to deduce compiler name for unsupported architecture: '"
            + architecture + "'";
}

function assemblerName(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return "cacorm";
    throw "Unable to deduce assembler name for unsupported architecture: '"
            + architecture + "'";
}

function linkerName(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return "clnk";
    throw "Unable to deduce linker name for unsupported architecture: '"
            + architecture + "'";
}

function listerName(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return "clabs";
    throw "Unable to deduce lister name for unsupported architecture: '"
            + architecture + "'";
}

function archiverName(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return "clib";
    throw "Unable to deduce archiver name for unsupported architecture: '"
            + architecture + "'";
}

function staticLibrarySuffix(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return ".cxm";
    throw "Unable to deduce static library suffix for unsupported architecture: '"
            + architecture + "'";
}

function executableSuffix(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return ".cxm";
    throw "Unable to deduce executable suffix for unsupported architecture: '"
            + architecture + "'";
}

function objectSuffix(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return ".o";
    throw "Unable to deduce object file suffix for unsupported architecture: '"
            + architecture + "'";
}

function imageFormat(qbs) {
    var architecture = qbs.architecture;
    if (architecture.startsWith("arm"))
        return "cosmic";
    throw "Unable to deduce image format for unsupported architecture: '"
            + architecture + "'";
}

function guessArchitecture(compilerFilePath) {
    var baseName = FileInfo.baseName(compilerFilePath);
    if (baseName === "cxcorm")
        return "arm";
    throw "Unable to deduce architecture for unsupported compiler: '"
            + baseName + "'";
}

function dumpMacros(compilerFilePath) {
    // Note: The COSMIC compiler does not support the predefined
    // macros dumping. So, we do it with the following trick, where we try
    // to create and compile a special temporary file and to parse the console
    // output with the own magic pattern: (""|"key"|"value"|"").

    var outputDirectory = new TemporaryDir();
    var outputFilePath = FileInfo.fromNativeSeparators(FileInfo.joinPaths(outputDirectory.path(),
                                                       "dump-macros.c"));
    var outputFile = new TextFile(outputFilePath, TextFile.WriteOnly);
    outputFile.writeLine("#define VALUE_TO_STRING(x) #x");
    outputFile.writeLine("#define VALUE(x) VALUE_TO_STRING(x)");
    outputFile.writeLine("#define VAR_NAME_VALUE(var) #var VALUE(var)");
    // The COSMIC compiler defines only one pre-defined macro
    // (at least nothing is said about other macros in the documentation).
    var keys = ["__CSMC__"];
    for (var i in keys) {
        var key = keys[i];
        outputFile.writeLine("#if defined (" + key + ")");
        outputFile.writeLine("#pragma message (VAR_NAME_VALUE(" + key + "))");
        outputFile.writeLine("#endif");
    }
    outputFile.close();

    var process = new Process();
    process.exec(compilerFilePath, [outputFilePath], false);
    File.remove(outputFilePath);

    var map = {};
    // COSMIC compiler use the errors output!
    process.readStdErr().trim().split(/\r?\n/g).map(function(line) {
        var match = line.match(/^#message \("(.+)" "(.+)"\)$/);
        if (match)
            map[match[1]] = match[2];
    });
    return map;
}

function dumpVersion(compilerFilePath) {
    var p = new Process();
    p.exec(compilerFilePath, ["-vers"]);
    // COSMIC compiler use the errors output!
    var output = p.readStdErr();
    var match = output.match(/^COSMIC.+V(\d+)\.?(\d+)\.?(\*|\d+)/);
    if (match) {
        var major = match[1] ? parseInt(match[1], 10) : 0;
        var minor = match[2] ? parseInt(match[2], 10) : 0;
        var patch = match[3] ? parseInt(match[3], 10) : 0;
        return { major: major, minor: minor, patch: patch };
    }
}

function guessEndianness(architecture) {
    // There is no mention of supported endianness in the cosmic compiler.
    if (architecture.startsWith("arm"))
        return "big";
    throw "Unable to deduce endianness for unsupported architecture: '"
            + architecture + "'";
}

function dumpDefaultPaths(compilerFilePath, architecture) {
    var rootPath = FileInfo.path(compilerFilePath);
    var includePaths = [];
    if (architecture.startsWith("arm")) {
        var includePath = FileInfo.joinPaths(rootPath, "hcorm");
        if (File.exists(includePath))
            includePaths.push(includePath);
    }

    var libraryPaths = [];
    var libraryPath = FileInfo.joinPaths(rootPath, "lib");
    if (File.exists(libraryPath))
        libraryPaths.push(libraryPath);

    return {
        "includePaths": includePaths,
        "libraryPaths": libraryPaths,
    }
}

function detectRelativePath(baseDirectory, filePath) {
    if (FileInfo.isAbsolutePath(filePath))
        return FileInfo.relativePath(baseDirectory, filePath);
    return filePath;
}

function compilerFlags(project, product, input, outputs, explicitlyDependsOn) {
    var args = [];

    // Up to 128 include files.
    args = args.concat(Cpp.collectPreincludePaths(input).map(function(path) {
        return input.cpp.preincludeFlag + detectRelativePath(product.buildDirectory, path);
    }));

    // Defines.
    args = args.concat(Cpp.collectDefines(input).map(function(define) {
        return input.cpp.defineFlag + detectRelativePath(product.buildDirectory, define);
    }));

    // Up to 128 include paths.
    args = args.concat(Cpp.collectIncludePaths(input).map(function(path) {
        return input.cpp.includeFlag + detectRelativePath(product.buildDirectory, path);
    }));
    args = args.concat(Cpp.collectSystemIncludePaths(input).map(function(path) {
        return input.cpp.systemIncludeFlag + detectRelativePath(product.buildDirectory, path);
    }));

    // Debug information flags.
    if (input.cpp.debugInformation)
        args.push("+debug");

    var architecture = input.qbs.architecture;
    var tag = ModUtils.fileTagForTargetLanguage(input.fileTags.concat(outputs.obj[0].fileTags));

    // Warning level flags.
    switch (input.cpp.warningLevel) {
    case "none":
        // Disabled by default.
        break;
    case "all":
        // Highest warning level.
        args.push("-pw7");
        break;
    }

    // C language version flags.
    if (tag === "c") {
        var knownValues = ["c99"];
        var cLanguageVersion = Cpp.languageVersion(
                    input.cpp.cLanguageVersion, knownValues, "C");
        switch (cLanguageVersion) {
        case "c99":
            args.push("-p", "c99");
            break;
        default:
            break;
        }
    }

    // Objects output directory.
    args.push("-co", detectRelativePath(product.buildDirectory,
                                        FileInfo.path(outputs.obj[0].filePath)));

    // Listing files generation flag.
    if (input.cpp.generateCompilerListingFiles) {
        // Enable listings.
        args.push("-l");
        // Listings output directory.
        args.push("-cl", detectRelativePath(product.buildDirectory,
                                            FileInfo.path(outputs.lst[0].filePath)));
    }

    // Misc flags.
    args = args.concat(Cpp.collectMiscCompilerArguments(input, tag),
                       Cpp.collectMiscDriverArguments(product));

    // Input.
    args.push(detectRelativePath(product.buildDirectory, input.filePath));
    return args;
}

function assemblerFlags(project, product, input, outputs, explicitlyDependsOn) {
    var args = [];

    // Up to 128 include paths.
    args = args.concat(Cpp.collectIncludePaths(input).map(function(path) {
        return input.cpp.includeFlag + detectRelativePath(product.buildDirectory, path);
    }));
    args = args.concat(Cpp.collectSystemIncludePaths(input).map(function(path) {
        return input.cpp.systemIncludeFlag + detectRelativePath(product.buildDirectory, path);
    }));

    // Debug information flags.
    if (input.cpp.debugInformation)
        args.push("-xx");

    // Determine which C-language we"re compiling
    var tag = ModUtils.fileTagForTargetLanguage(input.fileTags.concat(outputs.obj[0].fileTags));

    // Misc flags.
    args = args.concat(Cpp.collectMiscAssemblerArguments(input, tag));

    // Listing files generation flag.
    if (input.cpp.generateAssemblerListingFiles) {
        args.push("-l");
        args.push("+l", detectRelativePath(product.buildDirectory, outputs.lst[0].filePath));
    }

    // Objects output file path.
    args.push("-o", detectRelativePath(product.buildDirectory, outputs.obj[0].filePath));

    // Input.
    args.push(detectRelativePath(product.buildDirectory, input.filePath));
    return args;
}

function linkerFlags(project, product, inputs, outputs) {
    var args = [];

    // Library paths.
    args = args.concat(Cpp.collectLibraryPaths(product).map(function(path) {
        return product.cpp.libraryPathFlag + detectRelativePath(product.buildDirectory, path);
    }));

    // Output.
    args.push("-o", detectRelativePath(product.buildDirectory, outputs.application[0].filePath));

    // Map file generation flag.
    if (product.cpp.generateLinkerMapFile)
        args.push("-m", detectRelativePath(product.buildDirectory, outputs.mem_map[0].filePath));

    // Misc flags.
    args = args.concat(Cpp.collectMiscEscapableLinkerArguments(product),
                       Cpp.collectMiscLinkerArguments(product),
                       Cpp.collectMiscDriverArguments(product));

    // Linker scripts.
    args = args.concat(Cpp.collectLinkerScriptPaths(inputs).map(function(path) {
        return product.cpp.linkerScriptFlag + detectRelativePath(product.buildDirectory, path);
    }));

    // Input objects.
    args = args.concat(Cpp.collectLinkerObjectPaths(inputs).map(function(path) {
        return detectRelativePath(product.buildDirectory, path);
    }));

    // Library dependencies (order has matters).
    args = args.concat(Cpp.collectLibraryDependencies(product).map(function(dep) {
        return detectRelativePath(product.buildDirectory, dep.filePath);
    }));

    return args;
}

function archiverFlags(project, product, inputs, outputs) {
    var args = ["-cl"];

    // Output.
    args.push(detectRelativePath(product.buildDirectory, outputs.staticlibrary[0].filePath));

    // Input objects.
    args = args.concat(Cpp.collectLinkerObjectPaths(inputs).map(function(path) {
        return detectRelativePath(product.buildDirectory, path);
    }));

    return args;
}

function createPath(fullPath) {
    var cmd = new JavaScriptCommand();
    cmd.fullPath = fullPath;
    cmd.silent = true;
    cmd.sourceCode = function() {
        File.makePath(fullPath);
    };
    return cmd;
}

// It is a workaround to rename the generated object file to the desired name.
// Reason is that the Cosmic compiler always generates the object files in the
// format of 'module.o', but we expect it in flexible format, e.g. 'module.c.obj'
// or 'module.c.o' depending on the cpp.objectSuffix property.
function renameObjectFile(project, product, inputs, outputs, input, output) {
    var object = outputs.obj[0];
    var cmd = new JavaScriptCommand();
    cmd.newObject = object.filePath;
    cmd.oldObject = FileInfo.joinPaths(FileInfo.path(object.filePath),
                                       object.baseName + ".o");
    cmd.silent = true;
    cmd.sourceCode = function() { File.move(oldObject, newObject); };
    return cmd;
}

// It is a workaround to rename the generated listing file to the desired name.
// Reason is that the Cosmic compiler always generates the listing files in the
// format of 'module.ls', but we expect it in flexible format, e.g. 'module.c.lst'
// or 'module.c.ls' depending on the cpp.compilerListingSuffix property.
function renameListingFile(project, product, inputs, outputs, input, output) {
    var listing = outputs.lst[0];
    var cmd = new JavaScriptCommand();
    cmd.newListing = listing.filePath;
    cmd.oldListing = FileInfo.joinPaths(FileInfo.path(listing.filePath),
                                        listing.baseName + ".ls");
    cmd.silent = true;
    cmd.sourceCode = function() { File.move(oldListing, newListing); };
    return cmd;
}

function prepareCompiler(project, product, inputs, outputs, input, output, explicitlyDependsOn) {
    var cmds = [];

    // Create output objects path, because the Cosmic doesn't do it.
    var cmd = createPath(FileInfo.path(outputs.obj[0].filePath));
    cmds.push(cmd);

    // Create output listing path, because the Cosmic doesn't do it.
    if (input.cpp.generateCompilerListingFiles) {
        cmd = createPath(FileInfo.path(outputs.lst[0].filePath));
        cmds.push(cmd);
    }

    var args = compilerFlags(project, product, input, outputs, explicitlyDependsOn);
    cmd = new Command(input.cpp.compilerPath, args);
    cmd.workingDirectory = product.buildDirectory;
    cmd.description = "compiling " + input.fileName;
    cmd.highlight = "compiler";
    cmds.push(cmd);

    cmds.push(renameObjectFile(project, product, inputs, outputs, input, output));

    if (input.cpp.generateCompilerListingFiles)
        cmds.push(renameListingFile(project, product, inputs, outputs, input, output));

    return cmds;
}

function prepareAssembler(project, product, inputs, outputs, input, output, explicitlyDependsOn) {
    var cmds = [];

    // Create output objects path, because the Cosmic doesn't do it.
    var cmd = createPath(FileInfo.path(outputs.obj[0].filePath));
    cmds.push(cmd);

    // Create output listing path, because the Cosmic doesn't do it.
    if (input.cpp.generateCompilerListingFiles) {
        cmd = createPath(FileInfo.path(outputs.lst[0].filePath));
        cmds.push(cmd);
    }

    var args = assemblerFlags(project, product, input, outputs, explicitlyDependsOn);
    cmd = new Command(input.cpp.assemblerPath, args);
    cmd.workingDirectory = product.buildDirectory;
    cmd.description = "assembling " + input.fileName;
    cmd.highlight = "compiler";
    cmds.push(cmd);
    return cmds;
}

function prepareLinker(project, product, inputs, outputs, input, output) {
    var primaryOutput = outputs.application[0];
    var args = linkerFlags(project, product, inputs, outputs);
    var cmd = new Command(product.cpp.linkerPath, args);
    cmd.workingDirectory = product.buildDirectory;
    cmd.description = "linking " + primaryOutput.fileName;
    cmd.highlight = "linker";
    return [cmd];
}

function prepareArchiver(project, product, inputs, outputs, input, output) {
    var args = archiverFlags(project, product, inputs, outputs);
    var cmd = new Command(product.cpp.archiverPath, args);
    cmd.workingDirectory = product.buildDirectory;
    cmd.description = "linking " + output.fileName;
    cmd.highlight = "linker";
    return [cmd];
}