source: driver/cfa.cc@ 2aab69b

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 2aab69b was bbb1b35, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

first attempt at -save-temp for cfa-cpp output, formatting

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