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 
22 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     if (JA.getType() == types::TY_Nothing) {
44       CmdArgs.push_back("-fsyntax-only");
45     } else if (JA.getType() == types::TY_AST) {
46       CmdArgs.push_back("-emit-ast");
47     } else if (JA.getType() == types::TY_LLVM_IR ||
48                JA.getType() == types::TY_LTO_IR) {
49       CmdArgs.push_back("-emit-llvm");
50     } else if (JA.getType() == types::TY_LLVM_BC ||
51                JA.getType() == types::TY_LTO_BC) {
52       CmdArgs.push_back("-emit-llvm-bc");
53     } else if (JA.getType() == types::TY_PP_Asm) {
54       CmdArgs.push_back("-S");
55     } else {
56       assert(false && "Unexpected output type!");
57     }
58   } else if (isa<AssembleJobAction>(JA)) {
59     CmdArgs.push_back("-triple");
60     CmdArgs.push_back(Args.MakeArgString(TripleStr));
61     CmdArgs.push_back("-emit-obj");
62   } else {
63     assert(false && "Unexpected action class for Flang tool.");
64   }
65 
66   if (Output.isFilename()) {
67     CmdArgs.push_back("-o");
68     CmdArgs.push_back(Output.getFilename());
69   } else {
70     assert(Output.isNothing() && "Invalid output.");
71   }
72 
73   const InputInfo &Input = Inputs[0];
74   assert(Input.isFilename() && "Invalid input.");
75   CmdArgs.push_back(Input.getFilename());
76 
77   const auto& D = C.getDriver();
78   // TODO: Replace flang-new with flang once the new driver replaces the
79   // throwaway driver
80   const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
81   C.addCommand(std::make_unique<Command>(JA, *this,
82                                          ResponseFileSupport::AtFileUTF8(),
83                                          Exec, CmdArgs, Inputs, Output));
84 }
85 
86 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
87 
88 Flang::~Flang() {}
89