Skip to content
This repository was archived by the owner on Jan 8, 2023. It is now read-only.

Commit ef9d59a

Browse files
author
ApsarasX
committed
feat: add link process
1 parent 0d9887b commit ef9d59a

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ jobs:
157157
do
158158
base=$(basename $file)
159159
sudo ./staticscript $file -L lib --emit-llvm -o $base.ll
160-
sudo ./staticscript $file -L lib -o $base.o
160+
sudo ./staticscript $file -L lib -c -o $base.o
161+
sudo ./staticscript $file -L lib -o $base.exe
161162
sudo clang $base.o -lm -o $base.exe
162163
sudo ./$base.exe
163164
done
@@ -169,7 +170,8 @@ jobs:
169170
do
170171
base=$(basename $file)
171172
sudo ./staticscript $file -L lib --emit-llvm -o $base.ll
172-
sudo ./staticscript $file -L lib -o $base.o
173+
sudo ./staticscript $file -L lib -c -o $base.o
174+
sudo ./staticscript $file -L lib -o $base.exe
173175
sudo clang $base.o -lm -o $base.exe
174176
sudo ./$base.exe
175177
done

src/Driver/Driver.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <cstdlib>
12
#include <antlr4-runtime.h>
23
#include "StaticScriptLexer.h"
34
#include "StaticScriptParser.h"
@@ -21,6 +22,7 @@ int main(int argc, char **argv) {
2122
llvm::cl::opt<String> outputFilename("o", llvm::cl::value_desc("output file"), llvm::cl::desc("Write output to <output file>"),
2223
llvm::cl::cat(generalOptsCat));
2324
llvm::cl::opt<bool> emitLLVM("emit-llvm", llvm::cl::desc("Generate the LLVM representation for input file"), llvm::cl::cat(generalOptsCat));
25+
llvm::cl::opt<bool> noLink("c", llvm::cl::desc("Generate the target object file"), llvm::cl::cat(generalOptsCat));
2426
llvm::cl::opt<String> libDir("L", llvm::cl::desc("Specify lib library directory"));
2527
llvm::cl::opt<bool> optLevelO0("O0", llvm::cl::desc("Optimization level 0. Similar to clang -O0"),
2628
llvm::cl::cat(generalOptsCat));
@@ -54,8 +56,18 @@ int main(int argc, char **argv) {
5456
return 0;
5557
}
5658

59+
llvm::StringRef inputBasename = llvm::sys::path::stem(inputFilename);
60+
61+
String outputIRFilename;
62+
String outputObjFilename;
63+
String outputExeFilename;
64+
5765
if (outputFilename.empty()) {
58-
outputFilename.setValue(emitLLVM ? "ss-ir.ll" : "ss-out.o");
66+
outputIRFilename = llvm::formatv("{0}-ir.ll", inputBasename);
67+
outputObjFilename = llvm::formatv("{0}-out.o", inputBasename);
68+
outputExeFilename = llvm::formatv("{0}-out.exe", inputBasename);
69+
} else {
70+
outputIRFilename = outputObjFilename = outputExeFilename = outputFilename;
5971
}
6072

6173
unsigned optLevel = 0;
@@ -126,10 +138,10 @@ int main(int argc, char **argv) {
126138
// 设置data layout
127139
llvmModule.setDataLayout(targetMachine->createDataLayout());
128140

129-
std::error_code ec;
130-
llvm::raw_fd_ostream dest(outputFilename, ec, llvm::sys::fs::OF_None);
131141

132-
reportOnDriverError(bool(ec),"Could not create file: " + ec.message());
142+
std::error_code ec;
143+
llvm::raw_fd_ostream dest(emitLLVM ? outputIRFilename : outputObjFilename, ec, llvm::sys::fs::OF_None);
144+
reportOnDriverError(bool(ec), "Could not create file: " + ec.message());
133145

134146
if (emitLLVM) {
135147
dest << llvmModule;
@@ -144,5 +156,14 @@ int main(int argc, char **argv) {
144156
dest.flush();
145157
dest.close();
146158
fin.close();
159+
160+
if (!emitLLVM && !noLink) {
161+
String linkCmd = llvm::formatv("clang {0} -lm -O2 -o {1}", outputObjFilename, outputExeFilename);
162+
system(linkCmd.data());
163+
if (outputFilename.empty()) {
164+
String rmCmd = llvm::formatv("rm -f {0}", outputObjFilename);
165+
system(rmCmd.data());
166+
}
167+
}
147168
return 0;
148169
}

0 commit comments

Comments
 (0)