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