source: driver/cfa.cc@ 7f51b9d

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

formatting, make helper routines static

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