GCC Code Coverage Report


Directory: ../
File: src/linker/ExternalLinkerInterface.cpp
Date: 2025-11-25 12:07:23
Coverage Exec Excl Total
Lines: 72.7% 40 15 70
Functions: 100.0% 6 0 6
Branches: 38.3% 41 60 167

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "ExternalLinkerInterface.h"
4
5 #include <iostream>
6
7 #include <exception/CompilerError.h>
8 #include <exception/LinkerError.h>
9 #include <util/SystemUtil.h>
10 #include <util/Timer.h>
11
12 namespace spice::compiler {
13
14 201 void ExternalLinkerInterface::prepare() {
15 // Set target to linker
16
2/4
✓ Branch 3 → 4 taken 201 times.
✗ Branch 3 → 81 not taken.
✓ Branch 4 → 5 taken 201 times.
✗ Branch 4 → 79 not taken.
201 addLinkerFlag("--target=" + cliOptions.targetTriple.str());
17
18 // Static linking
19
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 14 taken 201 times.
201 if (cliOptions.staticLinking)
20 addLinkerFlag("-static");
21
22 // Stripping symbols
23
5/6
✓ Branch 14 → 15 taken 199 times.
✓ Branch 14 → 18 taken 2 times.
✓ Branch 16 → 17 taken 199 times.
✗ Branch 16 → 18 not taken.
✓ Branch 19 → 20 taken 199 times.
✓ Branch 19 → 27 taken 2 times.
201 if (!cliOptions.instrumentation.generateDebugInfo && !cliOptions.targetTriple.isOSDarwin())
24
2/4
✓ Branch 22 → 23 taken 199 times.
✗ Branch 22 → 90 not taken.
✓ Branch 23 → 24 taken 199 times.
✗ Branch 23 → 88 not taken.
398 addLinkerFlag("-Wl,-s");
25
26 // Sanitizers
27
1/5
✓ Branch 27 → 28 taken 201 times.
✗ Branch 27 → 29 not taken.
✗ Branch 27 → 36 not taken.
✗ Branch 27 → 43 not taken.
✗ Branch 27 → 57 not taken.
201 switch (cliOptions.instrumentation.sanitizer) {
28 201 case Sanitizer::NONE:
29 201 break;
30 case Sanitizer::ADDRESS:
31 addLinkerFlag("-lasan");
32 break;
33 case Sanitizer::THREAD:
34 addLinkerFlag("-ltsan");
35 break;
36 case Sanitizer::MEMORY:
37 addLinkerFlag("-L$(clang -print-resource-dir)/lib/x86_64-unknown-linux-gnu");
38 addLinkerFlag("-lclang_rt.msan");
39 requestLibMathLinkage();
40 break;
41 }
42
43 // Web Assembly
44
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 78 taken 201 times.
201 if (cliOptions.targetTriple.isWasm()) {
45 addLinkerFlag("-nostdlib");
46 addLinkerFlag("-Wl,--no-entry");
47 addLinkerFlag("-Wl,--export-all");
48 }
49 201 }
50
51 /**
52 * Start the linking process
53 */
54 201 void ExternalLinkerInterface::link() const {
55
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 201 times.
201 assert(!outputPath.empty());
56
57 // Build the linker command
58
1/2
✓ Branch 5 → 6 taken 201 times.
✗ Branch 5 → 101 not taken.
201 std::stringstream linkerCommandBuilder;
59
1/2
✓ Branch 6 → 7 taken 201 times.
✗ Branch 6 → 99 not taken.
201 const auto [linkerInvokerName, linkerInvokerPath] = SystemUtil::findLinkerInvoker();
60
1/2
✓ Branch 7 → 8 taken 201 times.
✗ Branch 7 → 97 not taken.
201 linkerCommandBuilder << linkerInvokerPath;
61
1/2
✓ Branch 8 → 9 taken 201 times.
✗ Branch 8 → 97 not taken.
201 const auto [linkerName, linkerPath] = SystemUtil::findLinker(cliOptions);
62
2/4
✓ Branch 9 → 10 taken 201 times.
✗ Branch 9 → 95 not taken.
✓ Branch 10 → 11 taken 201 times.
✗ Branch 10 → 95 not taken.
201 linkerCommandBuilder << " -fuse-ld=" << linkerPath;
63 // Append linker flags
64
2/2
✓ Branch 18 → 13 taken 642 times.
✓ Branch 18 → 19 taken 201 times.
843 for (const std::string &linkerFlag : linkerFlags)
65
2/4
✓ Branch 14 → 15 taken 642 times.
✗ Branch 14 → 77 not taken.
✓ Branch 15 → 16 taken 642 times.
✗ Branch 15 → 77 not taken.
642 linkerCommandBuilder << " " << linkerFlag;
66
2/2
✓ Branch 19 → 20 taken 1 time.
✓ Branch 19 → 21 taken 200 times.
201 if (linkLibMath)
67
1/2
✓ Branch 20 → 21 taken 1 time.
✗ Branch 20 → 95 not taken.
1 linkerCommandBuilder << " -lm";
68 // Append output path
69
3/6
✓ Branch 21 → 22 taken 201 times.
✗ Branch 21 → 95 not taken.
✓ Branch 22 → 23 taken 201 times.
✗ Branch 22 → 80 not taken.
✓ Branch 23 → 24 taken 201 times.
✗ Branch 23 → 78 not taken.
201 linkerCommandBuilder << " -o " << outputPath.string();
70 // Append object files
71
2/2
✓ Branch 32 → 27 taken 831 times.
✓ Branch 32 → 33 taken 201 times.
1032 for (const std::string &objectFilePath : objectFilePaths)
72
2/4
✓ Branch 28 → 29 taken 831 times.
✗ Branch 28 → 81 not taken.
✓ Branch 29 → 30 taken 831 times.
✗ Branch 29 → 81 not taken.
831 linkerCommandBuilder << " " << objectFilePath;
73
1/2
✓ Branch 33 → 34 taken 201 times.
✗ Branch 33 → 95 not taken.
201 const std::string linkerCommand = linkerCommandBuilder.str();
74
75 // Print status message
76
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 48 taken 201 times.
201 if (cliOptions.printDebugOutput) {
77 std::cout << "\nLinking with: " << linkerInvokerName << " (invoker) / " << linkerName << " (linker)"; // GCOV_EXCL_LINE
78 std::cout << "\nLinker command: " << linkerCommand; // GCOV_EXCL_LINE
79 std::cout << "\nEmitting executable to path: " << outputPath.string() << "\n"; // GCOV_EXCL_LINE
80 }
81
82 // Call the linker
83
1/2
✓ Branch 48 → 49 taken 201 times.
✗ Branch 48 → 93 not taken.
201 Timer timer;
84
1/2
✓ Branch 49 → 50 taken 201 times.
✗ Branch 49 → 93 not taken.
201 timer.start();
85
1/2
✓ Branch 50 → 51 taken 201 times.
✗ Branch 50 → 93 not taken.
201 const auto [output, exitCode] = SystemUtil::exec(linkerCommand);
86
1/2
✓ Branch 51 → 52 taken 201 times.
✗ Branch 51 → 91 not taken.
201 timer.stop();
87
88 // Check for linker error
89 if (exitCode != 0) { // GCOV_EXCL_LINE
90 const std::string errorMessage = "Linker exited with non-zero exit code\nLinker command: " + linkerCommand; // GCOV_EXCL_LINE
91 throw LinkerError(LINKER_ERROR, errorMessage); // GCOV_EXCL_LINE
92 } // GCOV_EXCL_LINE
93
94 // Print linker result if appropriate
95 if (cliOptions.printDebugOutput && !output.empty()) // GCOV_EXCL_LINE
96 std::cout << "Linking result: " << output << "\n\n"; // GCOV_EXCL_LINE
97
98 // Print link time
99 if (cliOptions.printDebugOutput) // GCOV_EXCL_LINE
100 std::cout << "Total link time: " << timer.getDurationMilliseconds() << " ms\n\n"; // GCOV_EXCL_LINE
101 201 }
102
103 /**
104 * Add another object file to be linked when calling 'link()'
105 *
106 * @param objectFilePath Path to the object file
107 */
108 918 void ExternalLinkerInterface::addObjectFilePath(const std::string &objectFilePath) { objectFilePaths.push_back(objectFilePath); }
109
110 /**
111 * Add another linker flag for the call to the linker executable
112 *
113 * @param flag Linker flag
114 */
115 645 void ExternalLinkerInterface::addLinkerFlag(const std::string &flag) { linkerFlags.push_back(flag); }
116
117 /**
118 * Add another source file to compile and link in (C or C++)
119 *
120 * @param additionalSource Additional source file
121 */
122 2 void ExternalLinkerInterface::addAdditionalSourcePath(std::filesystem::path additionalSource) {
123 // Check if the file exists
124 if (!exists(additionalSource)) { // GCOV_EXCL_LINE
125 const std::string msg = "The additional source file '" + additionalSource.string() + "' does not exist"; // GCOV_EXCL_LINE
126 throw CompilerError(IO_ERROR, msg); // GCOV_EXCL_LINE
127 } // GCOV_EXCL_LINE
128
129 // Add the file to the linker
130 2 additionalSource.make_preferred();
131
2/4
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 31 not taken.
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 29 not taken.
2 addObjectFilePath(additionalSource.string());
132 2 }
133
134 /**
135 * Link against libmath a.k.a. -lm
136 */
137 2 void ExternalLinkerInterface::requestLibMathLinkage() {
138 2 linkLibMath = true;
139 2 }
140
141 } // namespace spice::compiler
142