xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/Flang.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
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 
22fe6060f1SDimitry Andric void Flang::AddFortranDialectOptions(const ArgList &Args,
23fe6060f1SDimitry Andric                                      ArgStringList &CmdArgs) const {
24fe6060f1SDimitry Andric   Args.AddAllArgs(
25fe6060f1SDimitry Andric       CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form,
26fe6060f1SDimitry Andric                 options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp,
27fe6060f1SDimitry Andric                 options::OPT_fopenacc, options::OPT_finput_charset_EQ,
28fe6060f1SDimitry Andric                 options::OPT_fimplicit_none, options::OPT_fno_implicit_none,
29fe6060f1SDimitry Andric                 options::OPT_fbackslash, options::OPT_fno_backslash,
30fe6060f1SDimitry Andric                 options::OPT_flogical_abbreviations,
31fe6060f1SDimitry Andric                 options::OPT_fno_logical_abbreviations,
32fe6060f1SDimitry Andric                 options::OPT_fxor_operator, options::OPT_fno_xor_operator,
33fe6060f1SDimitry Andric                 options::OPT_falternative_parameter_statement,
34fe6060f1SDimitry Andric                 options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8,
35*4824e7fdSDimitry Andric                 options::OPT_fdefault_double_8, options::OPT_flarge_sizes,
36*4824e7fdSDimitry Andric                 options::OPT_fno_automatic});
37fe6060f1SDimitry Andric }
38fe6060f1SDimitry Andric 
39e8d8bef9SDimitry Andric void Flang::AddPreprocessingOptions(const ArgList &Args,
40e8d8bef9SDimitry Andric                                     ArgStringList &CmdArgs) const {
41349cc55cSDimitry Andric   Args.AddAllArgs(CmdArgs,
42349cc55cSDimitry Andric                   {options::OPT_P, options::OPT_D, options::OPT_U,
43349cc55cSDimitry Andric                    options::OPT_I, options::OPT_cpp, options::OPT_nocpp});
44fe6060f1SDimitry Andric }
45fe6060f1SDimitry Andric 
46fe6060f1SDimitry Andric void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
47fe6060f1SDimitry Andric   Args.AddAllArgs(CmdArgs,
48fe6060f1SDimitry Andric                   {options::OPT_module_dir, options::OPT_fdebug_module_writer,
49fe6060f1SDimitry Andric                    options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
50fe6060f1SDimitry Andric                    options::OPT_std_EQ, options::OPT_W_Joined});
51e8d8bef9SDimitry Andric }
52e8d8bef9SDimitry Andric 
53480093f4SDimitry Andric void Flang::ConstructJob(Compilation &C, const JobAction &JA,
54480093f4SDimitry Andric                          const InputInfo &Output, const InputInfoList &Inputs,
55480093f4SDimitry Andric                          const ArgList &Args, const char *LinkingOutput) const {
56480093f4SDimitry Andric   const auto &TC = getToolChain();
57e8d8bef9SDimitry Andric   // TODO: Once code-generation is available, this will need to be commented
58e8d8bef9SDimitry Andric   // out.
59e8d8bef9SDimitry Andric   // const llvm::Triple &Triple = TC.getEffectiveTriple();
60e8d8bef9SDimitry Andric   // const std::string &TripleStr = Triple.getTriple();
61480093f4SDimitry Andric 
62480093f4SDimitry Andric   ArgStringList CmdArgs;
63480093f4SDimitry Andric 
64e8d8bef9SDimitry Andric   // Invoke ourselves in -fc1 mode.
65480093f4SDimitry Andric   CmdArgs.push_back("-fc1");
66480093f4SDimitry Andric 
67e8d8bef9SDimitry Andric   // TODO: Once code-generation is available, this will need to be commented
68e8d8bef9SDimitry Andric   // out.
69e8d8bef9SDimitry Andric   // Add the "effective" target triple.
70e8d8bef9SDimitry Andric   // CmdArgs.push_back("-triple");
71e8d8bef9SDimitry Andric   // CmdArgs.push_back(Args.MakeArgString(TripleStr));
72480093f4SDimitry Andric 
73480093f4SDimitry Andric   if (isa<PreprocessJobAction>(JA)) {
74480093f4SDimitry Andric       CmdArgs.push_back("-E");
75480093f4SDimitry Andric   } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
76480093f4SDimitry Andric     if (JA.getType() == types::TY_Nothing) {
77480093f4SDimitry Andric       CmdArgs.push_back("-fsyntax-only");
78480093f4SDimitry Andric     } else if (JA.getType() == types::TY_AST) {
79480093f4SDimitry Andric       CmdArgs.push_back("-emit-ast");
80480093f4SDimitry Andric     } else if (JA.getType() == types::TY_LLVM_IR ||
81480093f4SDimitry Andric                JA.getType() == types::TY_LTO_IR) {
82480093f4SDimitry Andric       CmdArgs.push_back("-emit-llvm");
83480093f4SDimitry Andric     } else if (JA.getType() == types::TY_LLVM_BC ||
84480093f4SDimitry Andric                JA.getType() == types::TY_LTO_BC) {
85480093f4SDimitry Andric       CmdArgs.push_back("-emit-llvm-bc");
86480093f4SDimitry Andric     } else if (JA.getType() == types::TY_PP_Asm) {
87480093f4SDimitry Andric       CmdArgs.push_back("-S");
88480093f4SDimitry Andric     } else {
89480093f4SDimitry Andric       assert(false && "Unexpected output type!");
90480093f4SDimitry Andric     }
91480093f4SDimitry Andric   } else if (isa<AssembleJobAction>(JA)) {
92480093f4SDimitry Andric     CmdArgs.push_back("-emit-obj");
93480093f4SDimitry Andric   } else {
94480093f4SDimitry Andric     assert(false && "Unexpected action class for Flang tool.");
95480093f4SDimitry Andric   }
96480093f4SDimitry Andric 
97e8d8bef9SDimitry Andric   const InputInfo &Input = Inputs[0];
98e8d8bef9SDimitry Andric   types::ID InputType = Input.getType();
99e8d8bef9SDimitry Andric 
100e8d8bef9SDimitry Andric   // Add preprocessing options like -I, -D, etc. if we are using the
101e8d8bef9SDimitry Andric   // preprocessor (i.e. skip when dealing with e.g. binary files).
102e8d8bef9SDimitry Andric   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
103e8d8bef9SDimitry Andric     AddPreprocessingOptions(Args, CmdArgs);
104e8d8bef9SDimitry Andric 
105fe6060f1SDimitry Andric   AddFortranDialectOptions(Args, CmdArgs);
106fe6060f1SDimitry Andric 
107fe6060f1SDimitry Andric   // Add other compile options
108fe6060f1SDimitry Andric   AddOtherOptions(Args, CmdArgs);
109fe6060f1SDimitry Andric 
110fe6060f1SDimitry Andric   // Forward -Xflang arguments to -fc1
111fe6060f1SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
112fe6060f1SDimitry Andric 
113480093f4SDimitry Andric   if (Output.isFilename()) {
114480093f4SDimitry Andric     CmdArgs.push_back("-o");
115480093f4SDimitry Andric     CmdArgs.push_back(Output.getFilename());
116480093f4SDimitry Andric   } else {
117480093f4SDimitry Andric     assert(Output.isNothing() && "Invalid output.");
118480093f4SDimitry Andric   }
119480093f4SDimitry Andric 
120480093f4SDimitry Andric   assert(Input.isFilename() && "Invalid input.");
121480093f4SDimitry Andric   CmdArgs.push_back(Input.getFilename());
122480093f4SDimitry Andric 
123480093f4SDimitry Andric   const auto& D = C.getDriver();
124e8d8bef9SDimitry Andric   // TODO: Replace flang-new with flang once the new driver replaces the
125e8d8bef9SDimitry Andric   // throwaway driver
126e8d8bef9SDimitry Andric   const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
127e8d8bef9SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this,
128e8d8bef9SDimitry Andric                                          ResponseFileSupport::AtFileUTF8(),
129e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
130480093f4SDimitry Andric }
131480093f4SDimitry Andric 
132e8d8bef9SDimitry Andric Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
133480093f4SDimitry Andric 
134480093f4SDimitry Andric Flang::~Flang() {}
135