PandA-2024.02
tree-panda-gcc-Parameter.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
43 
44 #include "compiler_wrapper.hpp"
45 #include "cpu_time.hpp"
46 #include "custom_set.hpp"
47 #include "dbgPrintHelper.hpp"
48 #include "fileIO.hpp"
49 #include "module_interface.hpp"
50 #include "string_manipulation.hpp"
51 #include "utility.hpp"
52 #include <climits>
53 #include <cstring>
54 #include <filesystem>
55 #include <getopt.h>
56 #include <iosfwd>
57 #include <vector>
58 
59 #include <boost/algorithm/string/classification.hpp>
60 #include <boost/algorithm/string/predicate.hpp>
61 
62 #include "config_HAVE_I386_CLANG16_COMPILER.hpp"
63 #include "config_PANDA_DATA_INSTALLDIR.hpp"
64 #include "config_RELEASE.hpp"
65 
66 #define OPT_PRINT_FILE_NAME 256
67 #define OPT_INCLUDE 257
68 #define OPT_ISYSTEM 258
69 #define OPT_MF 259
70 #define OPT_MT 260
71 #define OPT_MQ 261
72 #define OPT_IQUOTE 262
73 #define OPT_ISYSROOT 263
74 #define OPT_IMULTILIB 264
75 #define OPT_IPLUGINDIR 265
76 #define OPT_MINUS_INCLUDE 266
77 #define OPT_START_GROUP 267
78 #define OPT_END_GROUP 268
79 #define OPT_MINUS_MAP 269
80 #define OPT_GC_SECTIONS 270
81 
82 tree_panda_gcc_parameter::tree_panda_gcc_parameter(const std::string& _program_name, int _argc, char** const _argv)
83  : Parameter(_program_name, _argc, _argv)
84 {
85  SetDefaults();
86 }
87 
89 
91 {
93 
95  int option_index;
96 
97  const char* const short_options = COMMON_SHORT_OPTIONS_STRING "o:Ss::x:tn:cM::i:C:ru:e:T:" GCC_SHORT_OPTIONS_STRING;
98 
99  const struct option long_options[] = {COMMON_LONG_OPTIONS,
100  {"iplugindir", required_argument, nullptr, OPT_IPLUGINDIR},
101  {"print-file-name", required_argument, nullptr, OPT_PRINT_FILE_NAME},
102  {"MF", required_argument, nullptr, OPT_MF},
103  {"MT", required_argument, nullptr, OPT_MT},
104  {"MQ", required_argument, nullptr, OPT_MQ},
105  {"include", required_argument, nullptr, OPT_MINUS_INCLUDE},
106  {"Map", required_argument, nullptr, OPT_MINUS_MAP},
107  {"start-group", no_argument, nullptr, OPT_START_GROUP},
108  {"end-group", no_argument, nullptr, OPT_END_GROUP},
109  {"gc-sections", no_argument, nullptr, OPT_GC_SECTIONS},
111  {nullptr, 0, nullptr, 0}};
112 
113  if(argc == 1)
114  {
115  PrintUsage(std::cerr);
116  return EXIT_SUCCESS;
117  }
118 
119  while(true)
120  {
121  int next_option = getopt_long_only(argc, argv, short_options, long_options, &option_index);
122 
123  // no more options are available
124  if(next_option == -1)
125  {
126  break;
127  }
128 
129  switch(next_option)
130  {
131  case 'o':
132  setOption(OPT_output_file, GetPath(optarg));
133  break;
134  case 'S':
135  {
136  setOption(OPT_gcc_S, true);
137  break;
138  }
139  case 'C':
140  {
141  setOption(OPT_compress_archive, optarg);
142  break;
143  }
144  case 's':
145  {
146  if(optarg == nullptr)
147  {
148  // -s has been passed
150  }
151  else
152  {
154  std::string parameter(optarg);
155  if(starts_with(parameter, "td="))
156  {
157  setOption(OPT_gcc_standard, parameter.substr(parameter.find('=') + 1));
158  }
159  else
160  {
161  THROW_ERROR("unexpected parameter: " + parameter);
162  }
163  }
164  break;
165  }
166  case 'M':
167  {
168  std::string gcc_extra_options;
169  if(optarg != nullptr)
170  {
171  gcc_extra_options = "-M" + std::string(optarg);
172  }
173  else
174  {
175  gcc_extra_options = "-M";
176  }
177  if(isOption(OPT_gcc_extra_options))
178  {
179  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
180  }
181  setOption(OPT_gcc_extra_options, gcc_extra_options);
182  break;
183  }
184  case 'i':
185  {
186  if(std::string(optarg).find("plugindir=") == 0)
187  {
188  setOption(OPT_gcc_plugindir, std::string(optarg).substr(10));
189  }
190  else
191  {
192  std::string gcc_extra_options;
193  gcc_extra_options = "-i" + std::string(optarg);
194  if(isOption(OPT_gcc_extra_options))
195  {
196  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
197  }
198  setOption(OPT_gcc_extra_options, gcc_extra_options);
199  }
200  break;
201  }
202  case OPT_MINUS_INCLUDE:
203  {
204  std::string gcc_extra_options = "-include " + std::string(optarg);
205  if(isOption(OPT_gcc_extra_options))
206  {
207  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
208  }
209  setOption(OPT_gcc_extra_options, gcc_extra_options);
210  break;
211  }
212  case 'n':
213  {
214  std::string parameter(optarg);
215  std::string gcc_extra_options = "-n" + std::string(parameter);
216  if(isOption(OPT_gcc_extra_options))
217  {
218  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
219  }
220  setOption(OPT_gcc_extra_options, gcc_extra_options);
221  break;
222  }
223  case OPT_IPLUGINDIR:
224  {
225  setOption(OPT_gcc_plugindir, optarg);
226  break;
227  }
228  case OPT_MF:
229  {
230  std::string parameter(optarg);
231  std::string gcc_extra_options = "-MF " + std::string(parameter);
232  if(isOption(OPT_gcc_extra_options))
233  {
234  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
235  }
236  setOption(OPT_gcc_extra_options, gcc_extra_options);
237  break;
238  }
239  case OPT_MT:
240  {
241  std::string parameter(optarg);
242  std::string gcc_extra_options = "-MT " + std::string(parameter);
243  if(isOption(OPT_gcc_extra_options))
244  {
245  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
246  }
247  setOption(OPT_gcc_extra_options, gcc_extra_options);
248  break;
249  }
250  case OPT_MQ:
251  {
252  std::string parameter(optarg);
253  std::string gcc_extra_options = "-MQ " + std::string(parameter);
254  if(isOption(OPT_gcc_extra_options))
255  {
256  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
257  }
258  setOption(OPT_gcc_extra_options, gcc_extra_options);
259  break;
260  }
261  case 'x':
262  {
263  std::string gcc_extra_options = "-x " + std::string(optarg);
264  if(isOption(OPT_gcc_extra_options))
265  {
266  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
267  }
268  setOption(OPT_gcc_extra_options, gcc_extra_options);
269  break;
270  }
271  case 't':
272  {
273  std::string gcc_extra_options = "-t";
274  if(isOption(OPT_gcc_extra_options))
275  {
276  gcc_extra_options = getOption<std::string>(OPT_gcc_extra_options) + " " + gcc_extra_options;
277  }
278  setOption(OPT_gcc_extra_options, gcc_extra_options);
279  break;
280  }
281  case OPT_PRINT_FILE_NAME:
282  {
283  const CompilerWrapper_OptimizationSet optimization_set =
284  getOption<CompilerWrapper_OptimizationSet>(OPT_gcc_optimization_set);
288  Wrap->QueryCompilerConfig("--print-file-name=" + std::string(optarg));
289  return EXIT_SUCCESS;
290  }
291  case OPT_START_GROUP:
292  case OPT_END_GROUP:
293  case OPT_MINUS_MAP:
294  case OPT_GC_SECTIONS:
295  case 'e':
296  case 'u':
297  case 'T':
298  case 'r':
299  {
302  break;
303  }
304  default:
305  {
306  bool exit_success = false;
307  bool res = ManageGccOptions(next_option, optarg);
308  if(res)
309  {
310  res = ManageDefaultOptions(next_option, optarg, exit_success);
311  }
312  if(exit_success)
313  {
314  return EXIT_SUCCESS;
315  }
316  if(res)
317  {
318  return PARAMETER_NOTPARSED;
319  }
320  }
321  }
322  }
323 
324 #if !RELEASE
325  if(isOption(OPT_write_parameter_xml))
326  {
327  const auto file_name = getOption<std::string>(OPT_write_parameter_xml);
328  write_xml_configuration_file(file_name);
329  return EXIT_SUCCESS;
330  }
331 #endif
332  while(optind < argc)
333  {
334  if(std::filesystem::path(argv[optind]).extension() == ".o")
335  {
336  std::string object_files;
337  if(isOption(OPT_obj_files))
338  {
339  object_files = getOption<std::string>(OPT_obj_files) + STR_CST_string_separator;
340  }
341  setOption(OPT_obj_files, object_files + GetPath(argv[optind]));
342  }
343  else if(std::filesystem::path(argv[optind]).extension() == ".a")
344  {
345  std::string archive_files;
346  if(isOption(OPT_archive_files))
347  {
348  archive_files = getOption<std::string>(OPT_archive_files) + STR_CST_string_separator;
349  }
350  setOption(OPT_archive_files, archive_files + GetPath(argv[optind]));
351  }
352  else
353  {
354  std::string input_file;
355  if(isOption(OPT_input_file))
356  {
357  input_file = getOption<std::string>(OPT_input_file) + STR_CST_string_separator;
358  }
359  setOption(OPT_input_file, input_file + GetPath(argv[optind]));
360  }
361  optind++;
362  }
363 
364  CheckParameters();
365 
366  return PARAMETER_PARSED;
367 }
368 
370 {
372 
373  const auto default_compiler = getOption<CompilerWrapper_CompilerTarget>(OPT_default_compiler);
374  const auto flag_cpp = isOption(OPT_input_format) &&
375  getOption<Parameters_FileFormat>(OPT_input_format) == Parameters_FileFormat::FF_CPP;
376  if(flag_cpp)
377  {
379  std::string includes =
380  "-isystem " + relocate_compiler_path(std::string(PANDA_DATA_INSTALLDIR "/panda/ac_types/include")) +
381  " -isystem " + relocate_compiler_path(std::string(PANDA_DATA_INSTALLDIR "/panda/ac_math/include"));
382  if(isOption(OPT_gcc_includes))
383  {
384  includes = getOption<std::string>(OPT_gcc_includes) + " " + includes;
385  }
386  setOption(OPT_gcc_includes, includes);
387  if(!isOption(OPT_gcc_standard))
388  {
389  if(CompilerWrapper::isGccCheck(default_compiler) &&
391  {
392  setOption(OPT_gcc_standard, "gnu++98");
393  }
394  else
395  {
396  setOption(OPT_gcc_standard, "gnu++14");
397  }
398  }
399  }
400  else if(!isOption(OPT_gcc_standard))
401  {
402  if(CompilerWrapper::isGccCheck(default_compiler) &&
404  {
405  setOption(OPT_gcc_standard, "gnu90");
406  }
407  else
408  {
409  setOption(OPT_gcc_standard, "gnu11");
410  }
411  }
412 #if HAVE_I386_CLANG16_COMPILER
413  if(CompilerWrapper::isGccCheck(default_compiler))
414  {
415  std::string gcc_warnings;
416  if(isOption(OPT_gcc_warnings))
417  {
418  gcc_warnings = getOption<std::string>(OPT_gcc_warnings) + STR_CST_string_separator;
419  }
420  const auto addWarning = [&](const std::string& warn) {
421  if(gcc_warnings.find(boost::replace_first_copy(warn, "no-", "")) == std::string::npos)
422  {
423  gcc_warnings += warn + STR_CST_string_separator;
424  }
425  };
426  addWarning("no-incompatible-function-pointer-types");
427  addWarning("no-implicit-function-declaration");
428  addWarning("no-int-conversion");
429  setOption(OPT_gcc_warnings, gcc_warnings);
430  }
431 #endif
432 }
433 
434 void tree_panda_gcc_parameter::PrintHelp(std::ostream& os) const
435 {
436  os << "Usage: " << getOption<std::string>(OPT_program_name)
437  << " [options] <input_file1> [<input_file2> ... <input_fileN>]" << std::endl;
438  os << std::endl;
439  os << "Options: \n"
440  << "\n";
442  os << "\n";
444  os << " -o=<file> Specify the output file name\n"
445  << "\n";
446  PrintGccOptionsUsage(os);
447  os << "\n";
448 }
449 
450 void tree_panda_gcc_parameter::PrintProgramName(std::ostream& os) const
451 {
452  os << "********************************************************************************" << std::endl;
453  os << " _ _" << std::endl;
454  os << " | |_ _ __ ___ ___ _ __ __ _ _ __ __| | __ _ __ _ ___ ___" << std::endl;
455  os << R"( | __| '__/ _ \/ _ \_____| '_ \ / _` | '_ \ / _` |/ _` |_____ / _` |/ __/ __|)" << std::endl;
456  os << " | |_| | | __/ __/_____| |_) | (_| | | | | (_| | (_| |_____| (_| | (_| (__" << std::endl;
457  os << R"( \__|_| \___|\___| | .__/ \__,_|_| |_|\__,_|\__,_| \__, |\___\___|)" << std::endl;
458  os << " |_| |___/" << std::endl;
459  os << "********************************************************************************" << std::endl;
460 }
461 
463 {
464  // ---------- general options ----------- //
466  setOption(OPT_debug_level, DEBUG_LEVEL_NONE);
468  setOption(OPT_output_level, OUTPUT_LEVEL_NONE);
469 
470  setOption(OPT_compiler_opt_level, CompilerWrapper_OptimizationSet::O0);
471 
472  setOption(OPT_print_dot, false);
473 
475  setOption(OPT_parse_pragma, false);
476  setOption(OPT_frontend_statistics, false);
477  setOption(OPT_pretty_print, false);
478 
479  // -- GCC options -- //
480  setOption(OPT_default_compiler, CompilerWrapper::getDefaultCompiler());
481  setOption(OPT_compatible_compilers, CompilerWrapper::getCompatibleCompilers());
482  setOption(OPT_gcc_m32_mx32, "-m32 -mno-sse2");
483  setOption(OPT_without_transformation, true);
484  setOption(OPT_precision, 3);
485  setOption(OPT_compute_size_of, true);
486  setOption(OPT_gcc_c, true);
487  setOption(OPT_gcc_config, false);
488  setOption(OPT_gcc_costs, false);
489  setOption(OPT_gcc_openmp_simd, 0);
490  setOption(OPT_no_clean, false);
491  setOption(OPT_gcc_optimization_set, CompilerWrapper_OptimizationSet::OBAMBU);
492  setOption(OPT_gcc_include_sysdir, false);
493  setOption(OPT_output_directory, ".");
494 #if HAVE_HLS_BUILT
496 #endif
497 }
void write_xml_configuration_file(const std::string &filename)
Write an XML configuration file with the parameters actually stored.
Definition: Parameter.cpp:266
int exit_code
NOTE: this file must be included only by source code of the executable (i.e., the file with the main)...
TVMValue param[3]
static int getCompatibleCompilers()
void setOption(const std::string &name, const G value)
Sets the value of an option.
Definition: Parameter.hpp:516
File containing functions and utilities to support the printing of debug messagges.
#define OUTPUT_LEVEL_NONE
no output print is performed.
#define STR_CST_string_separator
The character used to separate concatenated string.
Definition: utility.hpp:61
char **const argv
The input parameters;.
Definition: Parameter.hpp:311
#define OPT_GC_SECTIONS
#define COMMON_SHORT_OPTIONS_STRING
define the default tool short option string
Definition: Parameter.hpp:201
#define OPT_MINUS_INCLUDE
#define OPT_MQ
int Exec() override
Execute parameter parsing.
static int getDefaultCompiler()
#define OPT_START_GROUP
Base class to model interfaces for high-level synthesis.
static bool isCurrentOrNewer(CompilerWrapper_CompilerTarget ct, CompilerWrapper_CompilerTarget compare)
static bool isGccCheck(CompilerWrapper_CompilerTarget ct)
#define OPT_END_GROUP
Include a set of utilities used to manage CPU time measures.
CompilerWrapper_OptimizationSet
Possible optimization sets.
void PrintUsage(std::ostream &os) const
Print the usage of this tool = PrintHeader() + PrintHelp()
Definition: Parameter.cpp:392
Auxiliary methods for manipulating string.
bool starts_with(const std::string &str, const std::string &pattern)
#define GCC_SHORT_OPTIONS_STRING
define the GCC short option string
Definition: Parameter.hpp:236
#define PARAMETER_PARSED
An integer value to return if parameters have been right parsed.
Definition: Parameter.hpp:93
virtual void CheckParameters()=0
Checks the compatibility among the different parameters and determines the implications.
Definition: Parameter.cpp:139
redefinition of set to manage ordered/unordered structures
utility function used to read files.
#define OPT_MF
This file collects some utility functions and macros.
#define DEBUG_LEVEL_NONE
no debugging print is performed.
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
#define COMMON_LONG_OPTIONS
define default TOOL long options
Definition: Parameter.hpp:204
null deleter
Definition: refcount.hpp:51
tree_panda_gcc_parameter(const std::string &program_name, int argc, char **const argv)
Constructor.
void CheckParameters() override
Checks parameter values to set implicit one.
std::string GetPath(std::filesystem::path path)
Definition: fileIO.hpp:140
refcount< CompilerWrapper > CompilerWrapperRef
Refcount definition for the CompilerWrapper class.
#define OPT_MINUS_MAP
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
void PrintHelp(std::ostream &os) const override
Print the usage of this tool.
void SetDefaults() override
Sets the default values for the tree-panda-gcc tool.
Bambu optimizationss + OPT_compiler_opt_level.
~tree_panda_gcc_parameter() override
Destructor.
Main class for wrapping the frontend compiler.
void PrintGeneralOptionsUsage(std::ostream &os) const
Print the usage of the general common options.
Definition: Parameter.cpp:1105
bool isOption(const std::string &name) const
Tests if an option has been stored.
Definition: Parameter.hpp:548
void PrintOutputOptionsUsage(std::ostream &os) const
Print the usage of the output common options.
Definition: Parameter.cpp:1123
int argc
The number of input paramters.
Definition: Parameter.hpp:308
#define OPT_MT
#define GCC_LONG_OPTIONS
Definition: Parameter.hpp:248
#define OPT_IPLUGINDIR
std::string relocate_compiler_path(const std::string &path, bool resolve_path=false)
Definition: fileIO.hpp:149
bool ManageDefaultOptions(int next_option, char *optarg_param, bool &exit_success)
Manage default options (common to all tools)
Definition: Parameter.cpp:398
#define OPT_PRINT_FILE_NAME
void PrintProgramName(std::ostream &os) const override
Print the name of the program to be included in the header.
#define PARAMETER_NOTPARSED
Definition: Parameter.hpp:94
Implementation of the wrapper to Gcc for C sources.

Generated on Mon Feb 12 2024 13:02:55 for PandA-2024.02 by doxygen 1.8.13