Skip to content

Commit

Permalink
Merge pull request #8 from Shaikh-Ubaid/arg_c
Browse files Browse the repository at this point in the history
Support `-c` flag for generating object files
  • Loading branch information
czgdp1807 committed Dec 13, 2023
2 parents 82a254b + 41f2693 commit 11ed4d6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
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

0 comments on commit 11ed4d6

Please sign in to comment.