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