source: driver/cfa.cc@ aec68b6

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since aec68b6 was e35a32b, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

global add -Wno-strict-aliasing to cfa command to prevent warnings for cast to different type

  • Property mode set to 100644
File size: 19.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.cc --
8//
9// Author : Peter A. Buhr
10// Created On : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jan 16 07:30:19 2021
13// Update Count : 442
14//
15
16#include <iostream>
17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <climits> // PATH_MAX
20#include <string> // STL version
21#include <algorithm> // find
22
23#include <unistd.h> // execvp
24#include <sys/types.h>
25#include <sys/stat.h>
26
27#include "Common/SemanticError.h"
28#include "config.h" // configure info
29
30using std::cerr;
31using std::endl;
32using std::string;
33using std::to_string;
34
35//#define __DEBUG_H__
36
37#define xstr(s) str(s)
38#define str(s) #s
39
40static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); // "__CFA_FLAG__=" suffix
41
42static void Putenv( char * argv[], string arg ) {
43 // environment variables must have unique names
44 static int flags = 0;
45
46 if ( putenv( (char *)( *new string( string( __CFA_FLAGPREFIX__ + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) ) {
47 cerr << argv[0] << " error, cannot set environment variable." << endl;
48 exit( EXIT_FAILURE );
49 } // if
50} // Putenv
51
52static bool prefix( const string & arg, const string & pre ) { // check if string has prefix
53 return arg.substr( 0, pre.size() ) == pre;
54} // prefix
55
56static inline bool ends_with(const string & str, const string & sfix) {
57 if (sfix.size() > str.size()) return false;
58 return std::equal(str.rbegin(), str.rbegin() + sfix.size(), sfix.rbegin(), sfix.rend());
59}
60
61// check if string has suffix
62static bool suffix( const string & arg ) {
63 enum { NumSuffixes = 3 };
64 static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
65
66 size_t dot = arg.find_last_of( "." );
67 if ( dot == string::npos ) return false;
68 const string * end = suffixes + NumSuffixes;
69 return std::find( suffixes, end, arg.substr( dot + 1 ) ) != end;
70} // suffix
71
72static inline bool dirExists( const string & path ) { // check if directory exists
73 struct stat info;
74 if ( stat( path.c_str(), &info ) != 0 ) return false;
75 return (info.st_mode & S_IFDIR) != 0;
76} // dirExists
77
78static inline string dir(const string & path) {
79 return path.substr(0, path.find_last_of('/'));
80} // dir
81
82// Different path modes
83enum PathMode {
84 Installed, // cfa is installed, use prefix
85 BuildTree, // cfa is in the tree, use source and build tree
86 Distributed // cfa is distributed, use build tree for includes and executable directory for .cfs
87};
88
89// Get path mode from /proc
90PathMode FromProc() {
91 std::string abspath;
92 abspath.resize(PATH_MAX);
93
94 // get executable path from /proc/self/exe
95 ssize_t size = readlink("/proc/self/exe", const_cast<char*>(abspath.c_str()), abspath.size());
96 if(size <= 0) {
97 std::cerr << "Error could not evaluate absolute path from /proc/self/exe" << std::endl;
98 std::cerr << "Failed with " << std::strerror(errno) << std::endl;
99 std::exit(1);
100 }
101
102 // Trim extra characters
103 abspath.resize(size);
104
105 // Are we installed
106 if(abspath.rfind(CFA_BINDIR , 0) == 0) { return Installed; }
107
108 // Is this the build tree
109 if(abspath.rfind(TOP_BUILDDIR, 0) == 0) { return BuildTree; }
110
111 // Does this look like distcc
112 if(abspath.find("/.cfadistcc/") != std::string::npos) { return Distributed; }
113
114 // None of the above? Give up since we don't know where the prelude or include directories are
115 std::cerr << "Cannot find required files from excutable path " << abspath << std::endl;
116 std::exit(1);
117}
118
119
120int main( int argc, char * argv[] ) {
121 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
122 string Major( xstr( CFA_VERSION_MAJOR ) ), Minor( xstr( CFA_VERSION_MINOR ) ), Patch( xstr( CFA_VERSION_PATCH ) );
123
124 string installincdir( CFA_INCDIR ); // fixed location of include files
125 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed
126 string srcdriverdir ( TOP_BUILDDIR "driver"); // fixed location of cc1 and cfa-cpp commands when in tree
127
128 string heading; // banner printed at start of cfa compilation
129 string arg; // current command-line argument during command-line parsing
130 string bprefix; // path where gcc looks for compiler steps
131 string langstd; // language standard
132
133 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
134 string compiler_name; // name of C compiler
135
136 bool x_flag = false; // -x flag
137 bool nonoptarg = false; // no non-option arguments specified, i.e., no file names
138 bool link = true; // link stage occurring
139 bool verbose = false; // -v flag
140 bool quiet = false; // -quiet flag
141 bool debug = true; // -debug flag
142 bool nolib = false; // -nolib flag
143 bool help = false; // -help flag
144 bool CFA_flag = false; // -CFA flag
145 bool cpp_flag = false; // -E or -M flag, preprocessor only
146 bool std_flag = false; // -std= flag
147 bool noincstd_flag = false; // -no-include-stdhdr= flag
148 bool debugging __attribute(( unused )) = false; // -g flag
149 bool m32 = false; // -m32 flag
150 bool m64 = false; // -m64 flag
151 bool compiling_libs = false;
152 int o_file = 0; // -o filename position
153
154 PathMode path = FromProc();
155
156 const char * args[argc + 100]; // cfa command line values, plus some space for additional flags
157 int sargs = 1; // starting location for arguments in args list
158 int nargs = sargs; // number of arguments in args list; 0 => command name
159
160 const char * libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
161 int nlibs = 0;
162
163 #ifdef __DEBUG_H__
164 cerr << "CFA:" << endl;
165 for ( int i = 1; i < argc; i += 1 ) {
166 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
167 } // for
168 #endif // __DEBUG_H__
169
170 // process command-line arguments
171
172 for ( int i = 1; i < argc; i += 1 ) {
173 arg = argv[i]; // convert to string value
174 if ( prefix( arg, "-" ) ) {
175 // pass through arguments
176
177 if ( arg == "-Xlinker" || arg == "-o" ) {
178 args[nargs++] = argv[i]; // pass flag along
179 i += 1;
180 if ( i == argc ) continue; // next argument available ?
181 args[nargs++] = argv[i]; // pass argument along
182 if ( arg == "-o" ) o_file = i; // remember file
183
184 // CFA specific arguments
185
186 } else if ( strncmp(arg.c_str(), "-XCFA", 5) == 0 ) { // CFA pass through
187 if ( arg.size() == 5 ) {
188 i += 1;
189 if ( i == argc ) continue; // next argument available ?
190 Putenv( argv, argv[i] );
191 } else if ( arg[5] == ',' ) { // CFA specific arguments
192 Putenv( argv, argv[i] + 6 );
193 } else { // CFA specific arguments
194 args[nargs++] = argv[i];
195 } // if
196 } else if ( arg == "-CFA" ) {
197 CFA_flag = true; // strip the -CFA flag
198 link = false;
199 args[nargs++] = "-fsyntax-only"; // stop after stage 2
200 } else if ( arg == "-debug" ) {
201 debug = true; // strip the debug flag
202 } else if ( arg == "-nodebug" ) {
203 debug = false; // strip the nodebug flag
204 } else if ( arg == "-quiet" ) {
205 quiet = true; // strip the quiet flag
206 } else if ( arg == "-noquiet" ) {
207 quiet = false; // strip the noquiet flag
208 } else if ( arg == "-no-include-stdhdr" ) {
209 noincstd_flag = true; // strip the no-include-stdhdr flag
210 } else if ( arg == "-nolib" ) {
211 nolib = true; // strip the nolib flag
212 } else if ( arg == "-help" ) {
213 help = true; // strip the help flag
214 } else if ( arg == "-nohelp" ) {
215 help = false; // strip the nohelp flag
216 } else if ( arg == "-cfalib") {
217 compiling_libs = true;
218 } else if ( arg == "-compiler" ) {
219 // use the user specified compiler
220 i += 1;
221 if ( i == argc ) continue; // next argument available ?
222 compiler_path = argv[i];
223 Putenv( argv, arg + "=" + argv[i] );
224
225 // C specific arguments
226
227 } else if ( arg == "-v" ) {
228 verbose = true; // verbosity required
229 args[nargs++] = argv[i]; // pass flag along
230 } else if ( arg == "-g" ) {
231 debugging = true; // symbolic debugging required
232 args[nargs++] = argv[i]; // pass flag along
233 } else if ( arg == "-save-temps" || arg == "--save-temps" ) {
234 args[nargs++] = argv[i]; // pass flag along
235 Putenv( argv, arg ); // save cfa-cpp output
236 } else if ( prefix( arg, "-x" ) ) { // file suffix ?
237 string lang;
238 args[nargs++] = argv[i]; // pass flag along
239 if ( arg.length() == 2 ) { // separate argument ?
240 i += 1;
241 if ( i == argc ) continue; // next argument available ?
242 lang = argv[i];
243 args[nargs++] = argv[i]; // pass argument along
244 } else {
245 lang = arg.substr( 2 );
246 } // if
247 if ( x_flag ) {
248 cerr << argv[0] << " warning, only one -x flag per compile, ignoring subsequent flag." << endl;
249 } else {
250 x_flag = true;
251 Putenv( argv, string( "-x=" ) + lang );
252 } // if
253 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
254 std_flag = true; // -std=XX provided
255 args[nargs++] = argv[i]; // pass flag along
256 } else if ( arg == "-w" ) {
257 args[nargs++] = argv[i]; // pass flag along
258 Putenv( argv, arg );
259 } else if ( prefix( arg, "-W" ) ) { // check before next tests
260 if ( arg == "-Werror" || arg == "-Wall" ) {
261 args[nargs++] = argv[i]; // pass flag along
262 Putenv( argv, argv[i] );
263 } else {
264 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
265 args[nargs] = argv[i]; // conditionally pass argument along
266 const char * warning = argv[i] + adv; // extract warning
267 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
268 Putenv( argv, arg );
269 } // if
270 nargs += 1;
271 } // if
272 } else if ( prefix( arg, "-B" ) ) {
273 bprefix = arg.substr(2); // strip the -B flag
274 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
275 args[nargs++] = argv[i]; // pass flag along
276 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
277 cpp_flag = true; // cpp only
278 } // if
279 link = false; // no linkage required
280 } else if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
281 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
282 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
283 args[nargs++] = argv[i]; // pass flag along
284 i += 1;
285 args[nargs++] = argv[i]; // pass argument along
286 } else if ( arg[1] == 'l' ) {
287 // if the user specifies a library, load it after user code
288 libs[nlibs++] = argv[i];
289 } else if ( arg == "-m32" ) {
290 m32 = true;
291 m64 = false;
292 args[nargs++] = argv[i];
293 } else if ( arg == "-m64" ) {
294 m64 = true;
295 m32 = false;
296 args[nargs++] = argv[i];
297 } else {
298 // concatenate any other arguments
299 args[nargs++] = argv[i];
300 } // if
301 } else {
302 bool cfa = suffix( arg ); // check suffix
303 if ( ! x_flag && cfa ) { // no explicit suffix and cfa suffix ?
304 args[nargs++] = "-x";
305 args[nargs++] = "c";
306 } // if
307 args[nargs++] = argv[i]; // concatenate files
308 if ( ! x_flag && cfa ) { // no explicit suffix and cfa suffix ?
309 args[nargs++] = "-x";
310 args[nargs++] = "none";
311 } // if
312 nonoptarg = true;
313 } // if
314 } // for
315
316 #ifdef __x86_64__
317 args[nargs++] = "-mcx16"; // allow double-wide CAS
318 #endif // __x86_64__
319
320 #ifdef __DEBUG_H__
321 cerr << "args:";
322 for ( int i = 1; i < nargs; i += 1 ) {
323 cerr << " " << args[i];
324 } // for
325 cerr << endl;
326 #endif // __DEBUG_H__
327
328 // -E flag stops at cc1 stage 1, so cfa-cpp in cc1 stage 2 is never executed.
329 if ( cpp_flag && CFA_flag ) {
330 CFA_flag = false;
331 cerr << argv[0] << " warning, both -E and -CFA flags specified, using -E and ignoring -CFA." << endl;
332 } // if
333
334 // add the CFA include-library paths, which allow direct access to header files without directory qualification
335 string libbase;
336 switch(path) {
337 case Installed:
338 args[nargs++] = "-I" CFA_INCDIR;
339 // do not use during build
340 if ( ! noincstd_flag ) {
341 args[nargs++] = "-I" CFA_INCDIR "stdhdr";
342 } // if
343 args[nargs++] = "-I" CFA_INCDIR "concurrency";
344 args[nargs++] = "-I" CFA_INCDIR "containers";
345 libbase = CFA_LIBDIR;
346 break;
347 case BuildTree:
348 case Distributed:
349 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src";
350 // do not use during build
351 if ( ! noincstd_flag ) {
352 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
353 } // if
354 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
355 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
356
357 libbase = TOP_BUILDDIR "libcfa/";
358
359 break;
360 } // if
361
362 // add stdbool to get defines for bool/true/false
363 args[nargs++] = "-imacros";
364 args[nargs++] = "stdbool.h";
365
366 if( compiling_libs ) {
367 Putenv( argv, "-t" );
368 } // if
369
370 string arch( m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU) );
371 if ( ! m32 && ! m64 ) {
372 if ( arch == "x86" ) {
373 args[nargs++] = "-m32";
374 } else if ( arch == "x64" ) {
375 args[nargs++] = "-m64";
376 } // if
377 } // if
378
379 const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug");
380 string libdir = libbase + arch + "-" + config;
381
382 if ( path != Distributed ) {
383 if ( ! nolib && ! dirExists( libdir ) ) {
384 cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
385 cerr << "Was looking for " << libdir << endl;
386 for(int i = 1; i < argc; i++) {
387 cerr << argv[i] << " ";
388 }
389 cerr << endl;
390 libdir = libbase + arch + "-" + "nolib";
391 } // if
392
393 if ( ! dirExists( libdir ) ) {
394 cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
395 cerr << "Was looking for " << libdir << endl;
396 exit( EXIT_FAILURE );
397 } // if
398 } // if
399
400 string preludedir;
401 switch(path) {
402 case Installed : preludedir = libdir; break;
403 case BuildTree : preludedir = libdir + "/prelude"; break;
404 case Distributed : preludedir = dir(argv[0]); break;
405 } // switch
406
407 Putenv( argv, "--prelude-dir=" + preludedir );
408 args[nargs++] = "-include";
409 args[nargs++] = (*new string(preludedir + "/defines.hfa")).c_str();
410
411 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
412 args[nargs++] = libs[i];
413 } // for
414
415 if ( link ) {
416 args[nargs++] = "-Xlinker";
417 args[nargs++] = "--undefined=__cfaabi_dbg_bits_write";
418 args[nargs++] = "-Xlinker";
419 args[nargs++] = "--undefined=__cfaabi_interpose_startup";
420 args[nargs++] = "-Xlinker";
421 args[nargs++] = "--undefined=__cfaabi_appready_startup";
422 args[nargs++] = "-z";
423 args[nargs++] = "execstack";
424
425 // include the cfa library in case it is needed
426 args[nargs++] = ( *new string( string("-L" ) + libdir + (path != Installed ? "/src/.libs" : "")) ).c_str();
427 args[nargs++] = ( *new string( string("-Wl,-rpath," ) + libdir + (path != Installed ? "/src/.libs" : "")) ).c_str();
428 args[nargs++] = "-Wl,--push-state,--as-needed";
429 args[nargs++] = "-lcfathread";
430 args[nargs++] = "-Wl,--pop-state";
431 args[nargs++] = "-Wl,--push-state,--no-as-needed";
432 args[nargs++] = "-lcfa";
433 args[nargs++] = "-Wl,--pop-state";
434 args[nargs++] = "-pthread";
435 #if defined( __x86_64__ ) || defined( __ARM_ARCH )
436 args[nargs++] = "-latomic"; // allow double-wide CAS
437 #endif // __x86_64__
438 args[nargs++] = "-ldl";
439 args[nargs++] = "-lm";
440 } // if
441
442 args[nargs++] = "-fexceptions"; // add exception flags (unconditionally)
443
444 // add flags based on the type of compile
445
446 args[nargs++] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
447 args[nargs++] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
448 args[nargs++] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
449 args[nargs++] = "-D__CFA__";
450 args[nargs++] = "-D__CFORALL__";
451 args[nargs++] = "-D__cforall";
452
453 if ( cpp_flag ) {
454 args[nargs++] = "-D__CPP__";
455 } // if
456
457 if ( CFA_flag ) {
458 Putenv( argv, "-N" );
459 Putenv( argv, "-CFA" );
460 // -CFA implies cc1 stage 2, but gcc does not pass the -o file to this stage because it believe the file is for
461 // the linker. Hence, the -o file is explicit passed to cc1 stage 2 and used as cfa-cpp's output file.
462 if ( o_file ) Putenv( argv, string( "-o=" ) + argv[o_file] );
463 } else {
464 Putenv( argv, "-L" );
465 } // if
466
467 if ( debug ) {
468 heading += " (debug)";
469 args[nargs++] = "-D__CFA_DEBUG__";
470 } else {
471 heading += " (no debug)";
472 } // if
473
474 if ( bprefix.length() == 0 ) {
475 switch(path) {
476 case Installed : bprefix = installlibdir; break;
477 case BuildTree : bprefix = srcdriverdir ; break;
478 case Distributed : bprefix = dir(argv[0]) ; break;
479 } // switch
480 } // if
481 if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/';
482 Putenv( argv, string("-B=") + bprefix );
483
484 args[nargs++] = "-Xlinker"; // used by backtrace
485 args[nargs++] = "-export-dynamic";
486
487 // execute the compilation command
488
489 args[0] = compiler_path.c_str(); // set compiler command for exec
490 // find actual name of the compiler independent of the path to it
491 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
492 if ( p == -1 ) {
493 compiler_name = compiler_path;
494 } else {
495 compiler_name = *new string( compiler_path.substr( p + 1 ) );
496 } // if
497
498 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
499 args[nargs++] = "-no-integrated-cpp";
500 args[nargs++] = "-Wno-deprecated";
501 args[nargs++] = "-Wno-strict-aliasing"; // casting from one type to another
502 #ifdef HAVE_CAST_FUNCTION_TYPE
503 args[nargs++] = "-Wno-cast-function-type";
504 #endif // HAVE_CAST_FUNCTION_TYPE
505 if ( ! std_flag && ! x_flag ) {
506 args[nargs++] = "-std=gnu11"; // default c11, if none specified
507 } // if
508 args[nargs++] = "-fgnu89-inline";
509 args[nargs++] = "-D__int8_t_defined"; // prevent gcc type-size attributes
510 args[nargs++] = ( *new string( string("-B") + bprefix ) ).c_str();
511 } else {
512 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
513 exit( EXIT_FAILURE );
514 } // if
515
516 args[nargs] = nullptr; // terminate
517
518 #ifdef __DEBUG_H__
519 cerr << "nargs: " << nargs << endl;
520 cerr << "args:" << endl;
521 for ( int i = 0; args[i] != nullptr; i += 1 ) {
522 cerr << " \"" << args[i] << "\"" << endl;
523 } // for
524 cerr << endl;
525 #endif // __DEBUG_H__
526
527 if ( ! quiet ) {
528 cerr << "CFA " << "Version " << Version << heading << endl;
529 if ( help ) {
530 cerr <<
531 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
532 "-help\t\t\t: print this help message" << endl <<
533 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
534 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
535 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
536 "...\t\t\t: any other " << compiler_name << " flags" << endl;
537 } // if
538 } // if
539
540 if ( verbose ) {
541 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
542
543 for ( int i = 0; args[i] != nullptr; i += 1 ) {
544 cerr << args[i] << " ";
545 } // for
546 cerr << endl;
547 } // if
548
549 if ( ! nonoptarg ) {
550 cerr << argv[0] << " error, no input files" << endl;
551 exit( EXIT_FAILURE );
552 } // if
553
554 // execute the command and return the result
555
556 execvp( args[0], (char * const *)args ); // should not return
557 perror( "CFA Translator error: execvp" );
558 exit( EXIT_FAILURE );
559} // main
560
561// Local Variables: //
562// tab-width: 4 //
563// mode: c++ //
564// compile-command: "make install" //
565// End: //
Note: See TracBrowser for help on using the repository browser.