1 //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "CommonArgs.h"
13 #include "clang/Driver/Options.h"
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
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();
29 ArgStringList CmdArgs;
31 CmdArgs.push_back("-fc1");
33 CmdArgs.push_back("-triple");
34 CmdArgs.push_back(Args.MakeArgString(TripleStr));
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");
52 assert(false && "Unexpected output type!");
54 } else if (isa<AssembleJobAction>(JA)) {
55 CmdArgs.push_back("-emit-obj");
57 assert(false && "Unexpected action class for Flang tool.");
60 if (Output.isFilename()) {
61 CmdArgs.push_back("-o");
62 CmdArgs.push_back(Output.getFilename());
64 assert(Output.isNothing() && "Invalid output.");
67 const InputInfo &Input = Inputs[0];
68 assert(Input.isFilename() && "Invalid input.");
69 CmdArgs.push_back(Input.getFilename());
71 const auto& D = C.getDriver();
72 const char* Exec = Args.MakeArgString(D.GetProgramPath("flang", TC));
73 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
76 Flang::Flang(const ToolChain &TC)
77 : Tool("flang", "flang frontend", TC, RF_Full) {}