source: driver/cfa.cc@ 7428ad9

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 7428ad9 was 13a984c, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Force -m64 the same way -m32 is

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