source: driver/cfa.cc@ 14347ac

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 14347ac was 14347ac, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

libcfa now properly supports building with --enable-distcc

  • Property mode set to 100644
File size: 18.6 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 : Sun Feb 10 08:28:09 2019
13// Update Count : 281
14//
15
16#include <iostream>
17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <unistd.h> // execvp
20#include <string> // STL version
21#include <string.h> // strcmp
22
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#include "Common/SemanticError.h"
27#include "config.h" // configure info
28
29using std::cerr;
30using std::endl;
31using std::string;
32using std::to_string;
33
34
35//#define __DEBUG_H__
36
37
38bool prefix( string arg, string pre ) {
39 return arg.substr( 0, pre.size() ) == pre;
40} // prefix
41
42enum { NumSuffixes = 2 };
43const string suffixes[NumSuffixes] = { "cfa", "hfa", };
44
45bool suffix( string arg, const char * args[], int & nargs ) {
46 //std::cerr << arg << std::endl;
47 size_t dot = arg.find_last_of( "." );
48 //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
49 if ( dot == string::npos ) return false;
50 string sx = arg.substr( dot + 1 );
51 for ( int i = 0; i < NumSuffixes; i += 1 ) {
52 if ( sx == suffixes[i] ) {
53 args[nargs] = "-x";
54 nargs += 1;
55 args[nargs] = "c";
56 nargs += 1;
57 return true;
58 } // if
59 } // for
60 return false;
61} // suffix
62
63
64void shuffle( const char *args[], int S, int E, int N ) {
65 // S & E index 1 passed the end so adjust with -1
66 #ifdef __DEBUG_H__
67 cerr << "shuffle:" << S << " " << E << " " << N << endl;
68 #endif // __DEBUG_H__
69 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
70 #ifdef __DEBUG_H__
71 cerr << "\t" << j << " " << j-N << endl;
72 #endif // __DEBUG_H__
73 args[j] = args[j-N];
74 } // for
75} // shuffle
76
77static inline bool dirExists(const string & path) {
78 struct stat info;
79 if(stat( path.c_str(), &info ) != 0)
80 return false;
81 else if(info.st_mode & S_IFDIR)
82 return true;
83 else
84 return false;
85} //dirExists
86
87static inline string dir(const string & path) {
88 return path.substr(0, path.find_last_of('/'));
89}
90
91
92#define str(s) #s
93
94int main( int argc, char *argv[] ) {
95 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
96 string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
97
98 string installincdir( CFA_INCDIR ); // fixed location of include files
99 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed
100 string srcdriverdir ( TOP_BUILDDIR "driver"); // fixed location of cc1 and cfa-cpp commands when in tree
101
102 string heading; // banner printed at start of cfa compilation
103 string arg; // current command-line argument during command-line parsing
104 string Bprefix; // path where gcc looks for compiler command steps
105 string langstd; // language standard
106
107 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
108 string compiler_name; // name of C compiler
109
110 bool nonoptarg = false; // indicates non-option argument specified
111 bool link = true; // linking as well as compiling
112 bool verbose = false; // -v flag
113 bool quiet = false; // -quiet flag
114 bool debug = true; // -debug flag
115 bool nolib = false; // -nolib flag
116 bool help = false; // -help flag
117 bool CFA_flag = false; // -CFA flag
118 bool cpp_flag = false; // -E or -M flag, preprocessor only
119 bool std_flag = false; // -std= flag
120 bool noincstd_flag = false; // -no-include-stdhdr= flag
121 bool debugging __attribute(( unused )) = false; // -g flag
122 bool m32 = false; // -m32 flag
123 bool m64 = false; // -m64 flag
124 bool intree = false;
125 bool disttree = false;
126
127 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
128 int sargs = 1; // starting location for arguments in args list
129 int nargs = sargs; // number of arguments in args list; 0 => command name
130
131 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
132 int nlibs = 0;
133
134 #ifdef __DEBUG_H__
135 cerr << "CFA:" << endl;
136 for ( int i = 1; i < argc; i += 1 ) {
137 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
138 } // for
139 #endif // __DEBUG_H__
140
141 // process command-line arguments
142
143 args[nargs] = "-x"; // turn off language
144 nargs += 1;
145 args[nargs] = "c";
146 nargs += 1;
147
148 for ( int i = 1; i < argc; i += 1 ) {
149 arg = argv[i]; // convert to string value
150 if ( prefix( arg, "-" ) ) {
151 // pass through arguments
152
153 if ( arg == "-Xlinker" || arg == "-o" ) {
154 args[nargs] = argv[i]; // pass the argument along
155 nargs += 1;
156 i += 1;
157 if ( i == argc ) continue; // next argument available ?
158 args[nargs] = argv[i]; // pass the argument along
159 nargs += 1;
160 } else if ( arg == "-XCFA" ) { // CFA pass through
161 i += 1;
162 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
163 nargs += 1;
164
165 // CFA specific arguments
166
167 } else if ( arg == "-CFA" ) {
168 CFA_flag = true; // strip the -CFA flag
169 link = false;
170 args[nargs] = "-E"; // replace the argument with -E
171 nargs += 1;
172 } else if ( arg == "-debug" ) {
173 debug = true; // strip the debug flag
174 } else if ( arg == "-nodebug" ) {
175 debug = false; // strip the debug flag
176 } else if ( arg == "-nolib" ) {
177 nolib = true; // strip the nodebug flag
178 } else if ( arg == "-quiet" ) {
179 quiet = true; // strip the quiet flag
180 } else if ( arg == "-noquiet" ) {
181 quiet = false; // strip the noquiet flag
182 } else if ( arg == "-help" ) {
183 help = true; // strip the help flag
184 } else if ( arg == "-nohelp" ) {
185 help = false; // strip the nohelp flag
186 } else if ( arg == "-no-include-stdhdr" ) {
187 noincstd_flag = true; // strip the no-include-stdhdr flag
188 } else if ( arg == "-in-tree" ) {
189 intree = true;
190 } else if ( arg == "-dist-tree" ) {
191 disttree = true;
192 } else if ( arg == "-compiler" ) {
193 // use the user specified compiler
194 i += 1;
195 if ( i == argc ) continue; // next argument available ?
196 compiler_path = argv[i];
197 if ( putenv( (char *)( *new string( string( "__CFA_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
198 cerr << argv[0] << " error, cannot set environment variable." << endl;
199 exit( EXIT_FAILURE );
200 } // if
201
202 // C specific arguments
203
204 } else if ( arg == "-v" ) {
205 verbose = true; // verbosity required
206 args[nargs] = argv[i]; // pass the argument along
207 nargs += 1;
208 } else if ( arg == "-g" ) {
209 debugging = true; // symbolic debugging required
210 args[nargs] = argv[i]; // pass the argument along
211 nargs += 1;
212 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
213 std_flag = true; // -std=XX provided
214 args[nargs] = argv[i]; // pass the argument along
215 nargs += 1;
216 } else if ( arg == "-w" ) {
217 args[nargs] = argv[i]; // pass the argument along
218 nargs += 1;
219 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
220 nargs += 1;
221 } else if ( prefix( arg, "-W" ) ) { // check before next tests
222 if ( arg == "-Werror" || arg == "-Wall" ) {
223 args[nargs] = argv[i]; // pass the argument along
224 nargs += 1;
225 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
226 nargs += 1;
227 } else {
228 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
229 args[nargs] = argv[i]; // conditionally pass the argument along
230 const char * warning = argv[i] + adv; // extract warning
231 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
232 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
233 } // if
234 nargs += 1;
235 } // if
236 } else if ( prefix( arg, "-B" ) ) {
237 Bprefix = arg.substr(2); // strip the -B flag
238 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
239 nargs += 1;
240 } else if ( prefix( arg, "-b" ) ) {
241 if ( arg.length() == 2 ) { // separate argument ?
242 i += 1;
243 if ( i == argc ) continue; // next argument available ?
244 arg += argv[i]; // concatenate argument
245 } // if
246 // later versions of gcc require the -b option to appear at the start of the command line
247 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
248 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
249 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
250 cerr << argv[0] << " error, cannot set environment variable." << endl;
251 exit( EXIT_FAILURE );
252 } // if
253 sargs += 1;
254 nargs += 1;
255 } else if ( prefix( arg, "-V" ) ) {
256 if ( arg.length() == 2 ) { // separate argument ?
257 i += 1;
258 if ( i == argc ) continue; // next argument available ?
259 arg += argv[i]; // concatenate argument
260 } // if
261 // later versions of gcc require the -V option to appear at the start of the command line
262 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
263 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
264 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
265 cerr << argv[0] << " error, cannot set environment variable." << endl;
266 exit( EXIT_FAILURE );
267 } // if
268 sargs += 1;
269 nargs += 1;
270 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
271 args[nargs] = argv[i]; // pass the argument along
272 nargs += 1;
273 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
274 cpp_flag = true; // cpp only
275 } // if
276 link = false; // no linkage required
277 } else if ( arg[1] == 'l' ) {
278 // if the user specifies a library, load it after user code
279 libs[nlibs] = argv[i];
280 nlibs += 1;
281 } else if ( arg == "-m32" ) {
282 m32 = true;
283 m64 = false;
284 args[nargs] = argv[i];
285 nargs += 1;
286 } else if ( arg == "-m64" ) {
287 m64 = true;
288 m32 = false;
289 args[nargs] = argv[i];
290 nargs += 1;
291 } else {
292 // concatenate any other arguments
293 args[nargs] = argv[i];
294 nargs += 1;
295 } // if
296 } else {
297 bool cfa = suffix( arg, args, nargs ); // check suffix
298 args[nargs] = argv[i]; // concatenate file
299 nargs += 1;
300 if ( cfa ) {
301 args[nargs] = "-x";
302 nargs += 1;
303 args[nargs] = "none";
304 nargs += 1;
305 } // if
306 nonoptarg = true;
307 } // if
308 } // for
309
310 #ifdef __x86_64__
311 args[nargs] = "-mcx16"; // allow double-wide CAA
312 nargs += 1;
313 #endif // __x86_64__
314
315 #ifdef __DEBUG_H__
316 cerr << "args:";
317 for ( int i = 1; i < nargs; i += 1 ) {
318 cerr << " " << args[i];
319 } // for
320 cerr << endl;
321 #endif // __DEBUG_H__
322
323 if ( cpp_flag && CFA_flag ) {
324 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
325 exit( EXIT_FAILURE );
326 } // if
327
328 // add the CFA include-library paths, which allow direct access to header files without directory qualification
329 if( !intree ) {
330 args[nargs] = "-I" CFA_INCDIR;
331 nargs += 1;
332 if ( ! noincstd_flag ) { // do not use during build
333 args[nargs] = "-I" CFA_INCDIR "stdhdr";
334 nargs += 1;
335 } // if
336 args[nargs] = "-I" CFA_INCDIR "concurrency";
337 nargs += 1;
338 args[nargs] = "-I" CFA_INCDIR "containers";
339 nargs += 1;
340 } else {
341 args[nargs] = "-I" TOP_SRCDIR "libcfa/src";
342 nargs += 1;
343 if ( ! noincstd_flag ) { // do not use during build
344 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
345 nargs += 1;
346 } // if
347 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
348 nargs += 1;
349 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
350 nargs += 1;
351 }
352
353 // add stdbool to get defines for bool/true/false
354 args[nargs] = "-imacros";
355 nargs += 1;
356 args[nargs] = "stdbool.h";
357 nargs += 1;
358
359 string libbase;
360 if( !intree ) {
361 libbase = CFA_LIBDIR;
362 } else {
363 libbase = TOP_BUILDDIR "libcfa/";
364 args[nargs] = "-D__CFA_FLAG__=-t";
365 nargs += 1;
366 }
367
368 string arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
369 if ( ! m32 && ! m64 ) {
370 if ( arch == "x86" ) {
371 args[nargs] = "-m32";
372 nargs += 1;
373 } else if ( arch == "x64" ) {
374 args[nargs] = "-m64";
375 nargs += 1;
376 } // if
377 } // if
378 const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug");
379 string libdir = libbase + arch + "-" + config;
380
381 if (!disttree) {
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 if(disttree) {
400 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + dir(argv[0])) ).c_str();
401 } else if(intree) {
402 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir + "/prelude") ).c_str();
403 } else {
404 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir) ).c_str();
405 }
406 nargs += 1;
407
408 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
409 args[nargs] = libs[i];
410 nargs += 1;
411 } // for
412
413 if ( link ) {
414 args[nargs] = "-Xlinker";
415 nargs += 1;
416 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
417 nargs += 1;
418 args[nargs] = "-Xlinker";
419 nargs += 1;
420 args[nargs] = "--undefined=__cfaabi_interpose_startup";
421 nargs += 1;
422 args[nargs] = "-Xlinker";
423 nargs += 1;
424 args[nargs] = "--undefined=__cfaabi_appready_startup";
425 nargs += 1;
426
427 // include the cfa library in case it's needed
428 args[nargs] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
429 nargs += 1;
430 args[nargs] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
431 nargs += 1;
432 args[nargs] = "-Wl,--push-state,--as-needed";
433 nargs += 1;
434 args[nargs] = "-lcfathread";
435 nargs += 1;
436 args[nargs] = "-Wl,--pop-state";
437 nargs += 1;
438 args[nargs] = "-lcfa";
439 nargs += 1;
440 args[nargs] = "-lpthread";
441 nargs += 1;
442 args[nargs] = "-ldl";
443 nargs += 1;
444 args[nargs] = "-lrt";
445 nargs += 1;
446 args[nargs] = "-lm";
447 nargs += 1;
448 } // if
449
450 // Add exception flags (unconditionally)
451 args[nargs] = "-fexceptions";
452 nargs += 1;
453
454 // add the correct set of flags based on the type of compile this is
455
456 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
457 nargs += 1;
458 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
459 nargs += 1;
460 args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
461 nargs += 1;
462 args[nargs] = "-D__CFA__";
463 nargs += 1;
464 args[nargs] = "-D__CFORALL__";
465 nargs += 1;
466 args[nargs] = "-D__cforall";
467 nargs += 1;
468
469 if ( cpp_flag ) {
470 args[nargs] = "-D__CPP__";
471 nargs += 1;
472 } // if
473
474 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
475 nargs += 1;
476 if ( CFA_flag ) {
477 args[sargs] = "-D__CFA_FLAG__=-N";
478 args[nargs] = "-D__CFA_PREPROCESS_";
479 nargs += 1;
480 } else {
481 args[sargs] = "-D__CFA_FLAG__=-L";
482 } // if
483 sargs += 1;
484
485 if ( debug ) {
486 heading += " (debug)";
487 args[nargs] = "-D__CFA_DEBUG__";
488 nargs += 1;
489 } else {
490 heading += " (no debug)";
491 } // if
492
493 if ( Bprefix.length() == 0 ) {
494 if(disttree) {
495 Bprefix = dir(argv[0]);
496 } else if(intree) {
497 Bprefix = srcdriverdir;
498 } else {
499 Bprefix = installlibdir;
500 }
501
502 if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
503 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
504 nargs += 1;
505 } // if
506
507 args[nargs] = "-Xlinker"; // used by backtrace
508 nargs += 1;
509 args[nargs] = "-export-dynamic";
510 nargs += 1;
511
512 // execute the compilation command
513
514 args[0] = compiler_path.c_str(); // set compiler command for exec
515 // find actual name of the compiler independent of the path to it
516 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
517 if ( p == -1 ) {
518 compiler_name = compiler_path;
519 } else {
520 compiler_name = *new string( compiler_path.substr( p + 1 ) );
521 } // if
522
523 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
524 args[nargs] = "-no-integrated-cpp";
525 nargs += 1;
526 args[nargs] = "-Wno-deprecated";
527 nargs += 1;
528#ifdef HAVE_CAST_FUNCTION_TYPE
529 args[nargs] = "-Wno-cast-function-type";
530 nargs += 1;
531#endif // HAVE_CAST_FUNCTION_TYPE
532 if ( ! std_flag ) { // default c11, if none specified
533 args[nargs] = "-std=gnu11";
534 nargs += 1;
535 } // if
536 args[nargs] = "-fgnu89-inline";
537 nargs += 1;
538 args[nargs] = "-D__int8_t_defined"; // prevent gcc type-size attributes
539 nargs += 1;
540 args[nargs] = ( *new string( string("-B") + Bprefix ) ).c_str();
541 nargs += 1;
542 } else {
543 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
544 exit( EXIT_FAILURE );
545 } // if
546
547 args[nargs] = NULL; // terminate with NULL
548
549 #ifdef __DEBUG_H__
550 cerr << "nargs: " << nargs << endl;
551 cerr << "args:" << endl;
552 for ( int i = 0; args[i] != NULL; i += 1 ) {
553 cerr << " \"" << args[i] << "\"" << endl;
554 } // for
555 #endif // __DEBUG_H__
556
557 if ( ! quiet ) {
558 cerr << "CFA " << "Version " << Version << heading << endl;
559 if ( help ) {
560 cerr <<
561 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
562 "-help\t\t\t: print this help message" << endl <<
563 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
564 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
565 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
566 "...\t\t\t: any other " << compiler_name << " flags" << endl;
567 } // if
568 } // if
569
570 if ( verbose ) {
571 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
572
573 for ( int i = 0; args[i] != NULL; i += 1 ) {
574 cerr << args[i] << " ";
575 } // for
576 cerr << endl;
577 } // if
578
579 if ( ! nonoptarg ) {
580 cerr << argv[0] << " error, no input files" << endl;
581 exit( EXIT_FAILURE );
582 } // if
583
584 // execute the command and return the result
585
586 execvp( args[0], (char *const *)args ); // should not return
587 perror( "CFA Translator error: cfa level, execvp" );
588 exit( EXIT_FAILURE );
589} // main
590
591// Local Variables: //
592// tab-width: 4 //
593// mode: c++ //
594// compile-command: "make install" //
595// End: //
Note: See TracBrowser for help on using the repository browser.