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
AddPreprocessingOptions(const ArgList & Args,ArgStringList & CmdArgs) const22 void Flang::AddPreprocessingOptions(const ArgList &Args,
23 ArgStringList &CmdArgs) const {
24 Args.AddAllArgs(CmdArgs, {options::OPT_D, options::OPT_U, options::OPT_I});
25 }
26
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const27 void Flang::ConstructJob(Compilation &C, const JobAction &JA,
28 const InputInfo &Output, const InputInfoList &Inputs,
29 const ArgList &Args, const char *LinkingOutput) const {
30 const auto &TC = getToolChain();
31 // TODO: Once code-generation is available, this will need to be commented
32 // out.
33 // const llvm::Triple &Triple = TC.getEffectiveTriple();
34 // const std::string &TripleStr = Triple.getTriple();
35
36 ArgStringList CmdArgs;
37
38 // Invoke ourselves in -fc1 mode.
39 CmdArgs.push_back("-fc1");
40
41 // TODO: Once code-generation is available, this will need to be commented
42 // out.
43 // Add the "effective" target triple.
44 // CmdArgs.push_back("-triple");
45 // CmdArgs.push_back(Args.MakeArgString(TripleStr));
46
47 if (isa<PreprocessJobAction>(JA)) {
48 if (C.getArgs().hasArg(options::OPT_test_io))
49 CmdArgs.push_back("-test-io");
50 else
51 CmdArgs.push_back("-E");
52 } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
53 if (JA.getType() == types::TY_Nothing) {
54 CmdArgs.push_back("-fsyntax-only");
55 } else if (JA.getType() == types::TY_AST) {
56 CmdArgs.push_back("-emit-ast");
57 } else if (JA.getType() == types::TY_LLVM_IR ||
58 JA.getType() == types::TY_LTO_IR) {
59 CmdArgs.push_back("-emit-llvm");
60 } else if (JA.getType() == types::TY_LLVM_BC ||
61 JA.getType() == types::TY_LTO_BC) {
62 CmdArgs.push_back("-emit-llvm-bc");
63 } else if (JA.getType() == types::TY_PP_Asm) {
64 CmdArgs.push_back("-S");
65 } else {
66 assert(false && "Unexpected output type!");
67 }
68 } else if (isa<AssembleJobAction>(JA)) {
69 CmdArgs.push_back("-emit-obj");
70 } else {
71 assert(false && "Unexpected action class for Flang tool.");
72 }
73
74 const InputInfo &Input = Inputs[0];
75 types::ID InputType = Input.getType();
76
77 // Add preprocessing options like -I, -D, etc. if we are using the
78 // preprocessor (i.e. skip when dealing with e.g. binary files).
79 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
80 AddPreprocessingOptions(Args, CmdArgs);
81
82 if (Output.isFilename()) {
83 CmdArgs.push_back("-o");
84 CmdArgs.push_back(Output.getFilename());
85 } else {
86 assert(Output.isNothing() && "Invalid output.");
87 }
88
89 assert(Input.isFilename() && "Invalid input.");
90 CmdArgs.push_back(Input.getFilename());
91
92 const auto& D = C.getDriver();
93 // TODO: Replace flang-new with flang once the new driver replaces the
94 // throwaway driver
95 const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
96 C.addCommand(std::make_unique<Command>(JA, *this,
97 ResponseFileSupport::AtFileUTF8(),
98 Exec, CmdArgs, Inputs, Output));
99 }
100
Flang(const ToolChain & TC)101 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
102
~Flang()103 Flang::~Flang() {}
104