xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Flang.cpp (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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   CmdArgs.push_back("-triple");
34   CmdArgs.push_back(Args.MakeArgString(TripleStr));
35 
36   if (isa<PreprocessJobAction>(JA)) {
37     CmdArgs.push_back("-E");
38   } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
39     if (JA.getType() == types::TY_Nothing) {
40       CmdArgs.push_back("-fsyntax-only");
41     } else if (JA.getType() == types::TY_AST) {
42       CmdArgs.push_back("-emit-ast");
43     } else if (JA.getType() == types::TY_LLVM_IR ||
44                JA.getType() == types::TY_LTO_IR) {
45       CmdArgs.push_back("-emit-llvm");
46     } else if (JA.getType() == types::TY_LLVM_BC ||
47                JA.getType() == types::TY_LTO_BC) {
48       CmdArgs.push_back("-emit-llvm-bc");
49     } else if (JA.getType() == types::TY_PP_Asm) {
50       CmdArgs.push_back("-S");
51     } else {
52       assert(false && "Unexpected output type!");
53     }
54   } else if (isa<AssembleJobAction>(JA)) {
55     CmdArgs.push_back("-emit-obj");
56   } else {
57     assert(false && "Unexpected action class for Flang tool.");
58   }
59 
60   if (Output.isFilename()) {
61     CmdArgs.push_back("-o");
62     CmdArgs.push_back(Output.getFilename());
63   } else {
64     assert(Output.isNothing() && "Invalid output.");
65   }
66 
67   const InputInfo &Input = Inputs[0];
68   assert(Input.isFilename() && "Invalid input.");
69   CmdArgs.push_back(Input.getFilename());
70 
71   const auto& D = C.getDriver();
72   const char* Exec = Args.MakeArgString(D.GetProgramPath("flang", TC));
73   C.addCommand(std::make_unique<Command>(
74       JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
75 }
76 
77 Flang::Flang(const ToolChain &TC) : Tool("flang", "flang frontend", TC) {}
78 
79 Flang::~Flang() {}
80