source: driver/cfa.cc@ 7fdae38

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

formatting, for flags with arguments remove spurious "-x c" before argument

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