1 //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 
10 #include "Flang.h"
11 #include "CommonArgs.h"
12 
13 #include "clang/Driver/Options.h"
14 
15 #include <cassert>
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const22 void Flang::ConstructJob(Compilation &C, const JobAction &JA,
23                          const InputInfo &Output, const InputInfoList &Inputs,
24                          const ArgList &Args, const char *LinkingOutput) const {
25   const auto &TC = getToolChain();
26   const llvm::Triple &Triple = TC.getEffectiveTriple();
27   const std::string &TripleStr = Triple.getTriple();
28 
29   ArgStringList CmdArgs;
30 
31   CmdArgs.push_back("-fc1");
32 
33   // TODO: Eventually all actions will require a triple (e.g. `-triple
34   // aarch64-unknown-linux-gnu`). However, `-triple` is currently not supported
35   // by `flang-new -fc1`, so we only add it selectively to actions that we
36   // don't support/execute just yet.
37   if (isa<PreprocessJobAction>(JA)) {
38     if (C.getArgs().hasArg(options::OPT_test_io))
39       CmdArgs.push_back("-test-io");
40     else
41       CmdArgs.push_back("-E");
42   } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
43     CmdArgs.push_back("-triple");
44     CmdArgs.push_back(Args.MakeArgString(TripleStr));
45     if (JA.getType() == types::TY_Nothing) {
46       CmdArgs.push_back("-fsyntax-only");
47     } else if (JA.getType() == types::TY_AST) {
48       CmdArgs.push_back("-emit-ast");
49     } else if (JA.getType() == types::TY_LLVM_IR ||
50                JA.getType() == types::TY_LTO_IR) {
51       CmdArgs.push_back("-emit-llvm");
52     } else if (JA.getType() == types::TY_LLVM_BC ||
53                JA.getType() == types::TY_LTO_BC) {
54       CmdArgs.push_back("-emit-llvm-bc");
55     } else if (JA.getType() == types::TY_PP_Asm) {
56       CmdArgs.push_back("-S");
57     } else {
58       assert(false && "Unexpected output type!");
59     }
60   } else if (isa<AssembleJobAction>(JA)) {
61     CmdArgs.push_back("-triple");
62     CmdArgs.push_back(Args.MakeArgString(TripleStr));
63     CmdArgs.push_back("-emit-obj");
64   } else {
65     assert(false && "Unexpected action class for Flang tool.");
66   }
67 
68   if (Output.isFilename()) {
69     CmdArgs.push_back("-o");
70     CmdArgs.push_back(Output.getFilename());
71   } else {
72     assert(Output.isNothing() && "Invalid output.");
73   }
74 
75   const InputInfo &Input = Inputs[0];
76   assert(Input.isFilename() && "Invalid input.");
77   CmdArgs.push_back(Input.getFilename());
78 
79   const auto& D = C.getDriver();
80   // TODO: Replace flang-new with flang once the new driver replaces the
81   // throwaway driver
82   const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
83   C.addCommand(std::make_unique<Command>(JA, *this,
84                                          ResponseFileSupport::AtFileUTF8(),
85                                          Exec, CmdArgs, Inputs, Output));
86 }
87 
Flang(const ToolChain & TC)88 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
89 
~Flang()90 Flang::~Flang() {}
91