Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support -c flag for generating object files #8

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ jobs:
./src/bin/lc tests/array_01.cpp --backend=llvm --extra-arg="-Isrc/runtime/include" --

./src/bin/lc tests/array_02.cpp --backend=llvm --extra-arg="-Isrc/runtime/include" --

# Test generating object files
./src/bin/lc examples/expr2.c --backend c --extra-arg="-Isrc/runtime/include" -o c_o -c --
./src/bin/lc examples/expr2.c --backend llvm --extra-arg="-Isrc/runtime/include" -o llvm_o -c --
ls -1
62 changes: 61 additions & 1 deletion src/bin/lc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ static cl::opt<bool>
static cl::opt<std::string>
ArgO("o",
cl::desc(Options.getOptionHelpText(options::OPT_o)), cl::cat(ClangCheckCategory));
static cl::opt<bool>
ArgC("c",
cl::desc(Options.getOptionHelpText(options::OPT_c)), cl::cat(ClangCheckCategory));
static cl::opt<bool>
ShowWAT("show-wat",
cl::desc("Show WAT (WebAssembly Text Format) and exit"), cl::cat(ClangCheckCategory));
Expand Down Expand Up @@ -373,6 +376,50 @@ int compile_to_binary_object(Allocator &al, const std::string &infile,
return 0;
}

int compile_c_to_object_file(const std::string &infile,
const std::string &outfile) {

std::string CC = "cc";
char *env_CC = std::getenv("LFORTRAN_CC");
if (env_CC) CC = env_CC;
std::string options;
options += "-I" + LCompilers::LC::get_runtime_library_c_header_dir() + " ";
std::string cmd = CC + " " + options + " -o " + outfile + " -c " + infile;
int err = system(cmd.c_str());
if (err) {
std::cout << "The command '" + cmd + "' failed." << std::endl;
return 1;
}
return 0;
}

int compile_cpp_to_object_file(const std::string &infile,
const std::string &outfile) {

std::string CXX = "g++";
std::string options;
options += "-I" + LCompilers::LC::get_runtime_library_c_header_dir() + " ";
std::string cmd = CXX + " " + options + " -o " + outfile + " -c " + infile;
int err = system(cmd.c_str());
if (err) {
std::cout << "The command '" + cmd + "' failed." << std::endl;
return 1;
}
return 0;
}

int compile_fortran_to_object_file(const std::string &infile,
const std::string &outfile) {

std::string cmd = "gfortran -fno-backtrace -o " + outfile + " -c " + infile;
int err = system(cmd.c_str());
if (err) {
std::cout << "The command '" + cmd + "' failed." << std::endl;
return 1;
}
return 0;
}

// infile is an object file
// outfile will become the executable
int link_executable(const std::vector<std::string> &infiles,
Expand Down Expand Up @@ -698,7 +745,7 @@ int main(int argc, const char **argv) {
tmp_file = outfile + "__generated__.f90";
status = compile_to_fortran(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu);
} else if (backend == Backend::llvm) {
tmp_file = outfile + "__generated__.o";
tmp_file = ArgC ? outfile : (outfile + "__generated__.o");
status = compile_to_binary_object(al, infile, tmp_file, (LCompilers::ASR::TranslationUnit_t*)tu);
}

Expand All @@ -708,6 +755,19 @@ int main(int argc, const char **argv) {

LCompilers::CompilerOptions co;
co.arg_o = ArgO;

if (ArgC) {
// generate object file from source code
if (backend == Backend::c) {
status = compile_c_to_object_file(tmp_file, outfile);
} else if (backend == Backend::cpp) {
status = compile_cpp_to_object_file(tmp_file, outfile);
} else if (backend == Backend::fortran) {
status = compile_fortran_to_object_file(tmp_file, outfile);
}
return status;
}

std::vector<std::string> infiles = {tmp_file};
status = link_executable(infiles, outfile, LCompilers::LC::get_runtime_library_dir(), backend, false, false, true, co);
return status;
Expand Down
Loading