xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Flang.cpp (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 //===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 
10 #include "Flang.h"
11 #include "CommonArgs.h"
12 
13 #include "clang/Driver/Options.h"
14 
15 #include <cassert>
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 void Flang::AddFortranDialectOptions(const ArgList &Args,
23                                      ArgStringList &CmdArgs) const {
24   Args.AddAllArgs(
25       CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form,
26                 options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp,
27                 options::OPT_fopenacc, options::OPT_finput_charset_EQ,
28                 options::OPT_fimplicit_none, options::OPT_fno_implicit_none,
29                 options::OPT_fbackslash, options::OPT_fno_backslash,
30                 options::OPT_flogical_abbreviations,
31                 options::OPT_fno_logical_abbreviations,
32                 options::OPT_fxor_operator, options::OPT_fno_xor_operator,
33                 options::OPT_falternative_parameter_statement,
34                 options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8,
35                 options::OPT_fdefault_double_8, options::OPT_flarge_sizes,
36                 options::OPT_fno_automatic});
37 }
38 
39 void Flang::AddPreprocessingOptions(const ArgList &Args,
40                                     ArgStringList &CmdArgs) const {
41   Args.AddAllArgs(CmdArgs,
42                   {options::OPT_P, options::OPT_D, options::OPT_U,
43                    options::OPT_I, options::OPT_cpp, options::OPT_nocpp});
44 }
45 
46 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
47   Args.AddAllArgs(CmdArgs,
48                   {options::OPT_module_dir, options::OPT_fdebug_module_writer,
49                    options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
50                    options::OPT_std_EQ, options::OPT_W_Joined});
51 }
52 
53 void Flang::ConstructJob(Compilation &C, const JobAction &JA,
54                          const InputInfo &Output, const InputInfoList &Inputs,
55                          const ArgList &Args, const char *LinkingOutput) const {
56   const auto &TC = getToolChain();
57   // TODO: Once code-generation is available, this will need to be commented
58   // out.
59   // const llvm::Triple &Triple = TC.getEffectiveTriple();
60   // const std::string &TripleStr = Triple.getTriple();
61 
62   ArgStringList CmdArgs;
63 
64   // Invoke ourselves in -fc1 mode.
65   CmdArgs.push_back("-fc1");
66 
67   // TODO: Once code-generation is available, this will need to be commented
68   // out.
69   // Add the "effective" target triple.
70   // CmdArgs.push_back("-triple");
71   // CmdArgs.push_back(Args.MakeArgString(TripleStr));
72 
73   if (isa<PreprocessJobAction>(JA)) {
74       CmdArgs.push_back("-E");
75   } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
76     if (JA.getType() == types::TY_Nothing) {
77       CmdArgs.push_back("-fsyntax-only");
78     } else if (JA.getType() == types::TY_AST) {
79       CmdArgs.push_back("-emit-ast");
80     } else if (JA.getType() == types::TY_LLVM_IR ||
81                JA.getType() == types::TY_LTO_IR) {
82       CmdArgs.push_back("-emit-llvm");
83     } else if (JA.getType() == types::TY_LLVM_BC ||
84                JA.getType() == types::TY_LTO_BC) {
85       CmdArgs.push_back("-emit-llvm-bc");
86     } else if (JA.getType() == types::TY_PP_Asm) {
87       CmdArgs.push_back("-S");
88     } else {
89       assert(false && "Unexpected output type!");
90     }
91   } else if (isa<AssembleJobAction>(JA)) {
92     CmdArgs.push_back("-emit-obj");
93   } else {
94     assert(false && "Unexpected action class for Flang tool.");
95   }
96 
97   const InputInfo &Input = Inputs[0];
98   types::ID InputType = Input.getType();
99 
100   // Add preprocessing options like -I, -D, etc. if we are using the
101   // preprocessor (i.e. skip when dealing with e.g. binary files).
102   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
103     AddPreprocessingOptions(Args, CmdArgs);
104 
105   AddFortranDialectOptions(Args, CmdArgs);
106 
107   // Add other compile options
108   AddOtherOptions(Args, CmdArgs);
109 
110   // Forward -Xflang arguments to -fc1
111   Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
112 
113   if (Output.isFilename()) {
114     CmdArgs.push_back("-o");
115     CmdArgs.push_back(Output.getFilename());
116   } else {
117     assert(Output.isNothing() && "Invalid output.");
118   }
119 
120   assert(Input.isFilename() && "Invalid input.");
121   CmdArgs.push_back(Input.getFilename());
122 
123   const auto& D = C.getDriver();
124   // TODO: Replace flang-new with flang once the new driver replaces the
125   // throwaway driver
126   const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC));
127   C.addCommand(std::make_unique<Command>(JA, *this,
128                                          ResponseFileSupport::AtFileUTF8(),
129                                          Exec, CmdArgs, Inputs, Output));
130 }
131 
132 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {}
133 
134 Flang::~Flang() {}
135