From b7af858881d8da08129097dabd03a5e85de4431d Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 12 Dec 2023 21:38:11 +0530 Subject: [PATCH 1/4] LC: Add the ArgC flag --- src/bin/lc.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/bin/lc.cpp b/src/bin/lc.cpp index ac8a300..4aee533 100644 --- a/src/bin/lc.cpp +++ b/src/bin/lc.cpp @@ -53,6 +53,9 @@ static cl::opt static cl::opt ArgO("o", cl::desc(Options.getOptionHelpText(options::OPT_o)), cl::cat(ClangCheckCategory)); +static cl::opt + ArgC("c", + cl::desc(Options.getOptionHelpText(options::OPT_c)), cl::cat(ClangCheckCategory)); static cl::opt ShowWAT("show-wat", cl::desc("Show WAT (WebAssembly Text Format) and exit"), cl::cat(ClangCheckCategory)); From 4113f2d4f1ebd290c8be4a1c2a67db91f26d0ccc Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 12 Dec 2023 21:38:33 +0530 Subject: [PATCH 2/4] LC: Support generating object files --- src/bin/lc.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/bin/lc.cpp b/src/bin/lc.cpp index 4aee533..104a3f7 100644 --- a/src/bin/lc.cpp +++ b/src/bin/lc.cpp @@ -376,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 &infiles, @@ -711,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 infiles = {tmp_file}; status = link_executable(infiles, outfile, LCompilers::LC::get_runtime_library_dir(), backend, false, false, true, co); return status; From baf35bb543619a17006fb4f5c00411ec80bd7d2b Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 12 Dec 2023 21:39:15 +0530 Subject: [PATCH 3/4] Fix: Use outfile when ArgC is used with LLVM backend --- src/bin/lc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/lc.cpp b/src/bin/lc.cpp index 104a3f7..13304c1 100644 --- a/src/bin/lc.cpp +++ b/src/bin/lc.cpp @@ -745,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); } From 41f2693a09fd56fec8a59c5d127291c0ff224763 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Tue, 12 Dec 2023 21:45:03 +0530 Subject: [PATCH 4/4] CI: Add tests for generating object files --- .github/workflows/CI.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 2c66cbb..70c7126 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -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