1480093f4SDimitry Andric //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===// 2480093f4SDimitry Andric // 3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6480093f4SDimitry Andric // 7480093f4SDimitry Andric //===----------------------------------------------------------------------===// 8480093f4SDimitry Andric 9480093f4SDimitry Andric 10480093f4SDimitry Andric #include "Flang.h" 11480093f4SDimitry Andric #include "CommonArgs.h" 12480093f4SDimitry Andric 13480093f4SDimitry Andric #include "clang/Driver/Options.h" 14480093f4SDimitry Andric 15480093f4SDimitry Andric #include <cassert> 16480093f4SDimitry Andric 17480093f4SDimitry Andric using namespace clang::driver; 18480093f4SDimitry Andric using namespace clang::driver::tools; 19480093f4SDimitry Andric using namespace clang; 20480093f4SDimitry Andric using namespace llvm::opt; 21480093f4SDimitry Andric 22*e8d8bef9SDimitry Andric void Flang::AddPreprocessingOptions(const ArgList &Args, 23*e8d8bef9SDimitry Andric ArgStringList &CmdArgs) const { 24*e8d8bef9SDimitry Andric Args.AddAllArgs(CmdArgs, {options::OPT_D, options::OPT_U, options::OPT_I}); 25*e8d8bef9SDimitry Andric } 26*e8d8bef9SDimitry Andric 27480093f4SDimitry Andric void Flang::ConstructJob(Compilation &C, const JobAction &JA, 28480093f4SDimitry Andric const InputInfo &Output, const InputInfoList &Inputs, 29480093f4SDimitry Andric const ArgList &Args, const char *LinkingOutput) const { 30480093f4SDimitry Andric const auto &TC = getToolChain(); 31*e8d8bef9SDimitry Andric // TODO: Once code-generation is available, this will need to be commented 32*e8d8bef9SDimitry Andric // out. 33*e8d8bef9SDimitry Andric // const llvm::Triple &Triple = TC.getEffectiveTriple(); 34*e8d8bef9SDimitry Andric // const std::string &TripleStr = Triple.getTriple(); 35480093f4SDimitry Andric 36480093f4SDimitry Andric ArgStringList CmdArgs; 37480093f4SDimitry Andric 38*e8d8bef9SDimitry Andric // Invoke ourselves in -fc1 mode. 39480093f4SDimitry Andric CmdArgs.push_back("-fc1"); 40480093f4SDimitry Andric 41*e8d8bef9SDimitry Andric // TODO: Once code-generation is available, this will need to be commented 42*e8d8bef9SDimitry Andric // out. 43*e8d8bef9SDimitry Andric // Add the "effective" target triple. 44*e8d8bef9SDimitry Andric // CmdArgs.push_back("-triple"); 45*e8d8bef9SDimitry Andric // CmdArgs.push_back(Args.MakeArgString(TripleStr)); 46480093f4SDimitry Andric 47480093f4SDimitry Andric if (isa<PreprocessJobAction>(JA)) { 48*e8d8bef9SDimitry Andric if (C.getArgs().hasArg(options::OPT_test_io)) 49*e8d8bef9SDimitry Andric CmdArgs.push_back("-test-io"); 50*e8d8bef9SDimitry Andric else 51480093f4SDimitry Andric CmdArgs.push_back("-E"); 52480093f4SDimitry Andric } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) { 53480093f4SDimitry Andric if (JA.getType() == types::TY_Nothing) { 54480093f4SDimitry Andric CmdArgs.push_back("-fsyntax-only"); 55480093f4SDimitry Andric } else if (JA.getType() == types::TY_AST) { 56480093f4SDimitry Andric CmdArgs.push_back("-emit-ast"); 57480093f4SDimitry Andric } else if (JA.getType() == types::TY_LLVM_IR || 58480093f4SDimitry Andric JA.getType() == types::TY_LTO_IR) { 59480093f4SDimitry Andric CmdArgs.push_back("-emit-llvm"); 60480093f4SDimitry Andric } else if (JA.getType() == types::TY_LLVM_BC || 61480093f4SDimitry Andric JA.getType() == types::TY_LTO_BC) { 62480093f4SDimitry Andric CmdArgs.push_back("-emit-llvm-bc"); 63480093f4SDimitry Andric } else if (JA.getType() == types::TY_PP_Asm) { 64480093f4SDimitry Andric CmdArgs.push_back("-S"); 65480093f4SDimitry Andric } else { 66480093f4SDimitry Andric assert(false && "Unexpected output type!"); 67480093f4SDimitry Andric } 68480093f4SDimitry Andric } else if (isa<AssembleJobAction>(JA)) { 69480093f4SDimitry Andric CmdArgs.push_back("-emit-obj"); 70480093f4SDimitry Andric } else { 71480093f4SDimitry Andric assert(false && "Unexpected action class for Flang tool."); 72480093f4SDimitry Andric } 73480093f4SDimitry Andric 74*e8d8bef9SDimitry Andric const InputInfo &Input = Inputs[0]; 75*e8d8bef9SDimitry Andric types::ID InputType = Input.getType(); 76*e8d8bef9SDimitry Andric 77*e8d8bef9SDimitry Andric // Add preprocessing options like -I, -D, etc. if we are using the 78*e8d8bef9SDimitry Andric // preprocessor (i.e. skip when dealing with e.g. binary files). 79*e8d8bef9SDimitry Andric if (types::getPreprocessedType(InputType) != types::TY_INVALID) 80*e8d8bef9SDimitry Andric AddPreprocessingOptions(Args, CmdArgs); 81*e8d8bef9SDimitry Andric 82480093f4SDimitry Andric if (Output.isFilename()) { 83480093f4SDimitry Andric CmdArgs.push_back("-o"); 84480093f4SDimitry Andric CmdArgs.push_back(Output.getFilename()); 85480093f4SDimitry Andric } else { 86480093f4SDimitry Andric assert(Output.isNothing() && "Invalid output."); 87480093f4SDimitry Andric } 88480093f4SDimitry Andric 89480093f4SDimitry Andric assert(Input.isFilename() && "Invalid input."); 90480093f4SDimitry Andric CmdArgs.push_back(Input.getFilename()); 91480093f4SDimitry Andric 92480093f4SDimitry Andric const auto& D = C.getDriver(); 93*e8d8bef9SDimitry Andric // TODO: Replace flang-new with flang once the new driver replaces the 94*e8d8bef9SDimitry Andric // throwaway driver 95*e8d8bef9SDimitry Andric const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); 96*e8d8bef9SDimitry Andric C.addCommand(std::make_unique<Command>(JA, *this, 97*e8d8bef9SDimitry Andric ResponseFileSupport::AtFileUTF8(), 98*e8d8bef9SDimitry Andric Exec, CmdArgs, Inputs, Output)); 99480093f4SDimitry Andric } 100480093f4SDimitry Andric 101*e8d8bef9SDimitry Andric Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {} 102480093f4SDimitry Andric 103480093f4SDimitry Andric Flang::~Flang() {} 104