source: driver/cfa.cc@ a539fc3

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 a539fc3 was a539fc3, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'master' into distcc

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