source: src/driver/cfa.cc@ e3d1cc1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since e3d1cc1 was 35f9114, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

change compilation flag-name from -no-include-std to -no-include-stdhdr

  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[b87a5ed]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//
[76c7f65e]7// cfa.cc --
[b87a5ed]8//
[51b73452]9// Author : Peter A. Buhr
10// Created On : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
[35f9114]12// Last Modified On : Sat Aug 6 16:14:55 2016
13// Update Count : 148
[b87a5ed]14//
[51b73452]15
16#include <iostream>
[b87a5ed]17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <unistd.h> // execvp
20#include <string> // STL version
[51b73452]21
[b87a5ed]22#include "config.h" // configure info
[51b73452]23
24using std::cerr;
25using std::endl;
26using std::string;
27
28
29//#define __DEBUG_H__
30
31
32bool prefix( string arg, string pre ) {
[b87a5ed]33 return arg.substr( 0, pre.size() ) == pre;
[51b73452]34} // prefix
35
36
37void shuffle( const char *args[], int S, int E, int N ) {
[b87a5ed]38 // S & E index 1 passed the end so adjust with -1
[51b73452]39#ifdef __DEBUG_H__
[b87a5ed]40 cerr << "shuffle:" << S << " " << E << " " << N << endl;
[51b73452]41#endif // __DEBUG_H__
[b87a5ed]42 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
[51b73452]43#ifdef __DEBUG_H__
[b87a5ed]44 cerr << "\t" << j << " " << j-N << endl;
[51b73452]45#endif // __DEBUG_H__
[b87a5ed]46 args[j] = args[j-N];
47 } // for
[51b73452]48} // shuffle
49
50
51int main( int argc, char *argv[] ) {
[b87a5ed]52 string Version( VERSION ); // current version number from CONFIG
53 string Major( "0" ), Minor( "0" ), Patch( "0" ); // default version numbers
[1db21619]54
[b87a5ed]55 int posn1 = Version.find( "." ); // find the divider between major and minor version numbers
56 if ( posn1 == -1 ) { // not there ?
57 Major = Version;
[51b73452]58 } else {
[b87a5ed]59 Major = Version.substr( 0, posn1 );
60 int posn2 = Version.find( ".", posn1 + 1 ); // find the divider between minor and patch numbers
61 if ( posn2 == -1 ) { // not there ?
62 Minor = Version.substr( posn1 );
63 } else {
64 Minor = Version.substr( posn1 + 1, posn2 - posn1 - 1 );
65 Patch = Version.substr( posn2 + 1 );
66 } // if
[51b73452]67 } // if
68
[00cc023]69 string installincdir( CFA_INCDIR ); // fixed location of include files
70 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands
[51b73452]71
[b87a5ed]72 string heading; // banner printed at start of cfa compilation
73 string arg; // current command-line argument during command-line parsing
74 string Bprefix; // path where gcc looks for compiler command steps
75 string langstd; // language standard
[51b73452]76
[e24f13a]77 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
[b87a5ed]78 string compiler_name; // name of C compiler
[51b73452]79
[b87a5ed]80 bool nonoptarg = false; // indicates non-option argument specified
81 bool link = true; // linking as well as compiling
82 bool verbose = false; // -v flag
83 bool quiet = false; // -quiet flag
84 bool debug = true; // -debug flag
85 bool help = false; // -help flag
86 bool CFA_flag = false; // -CFA flag
87 bool cpp_flag = false; // -E or -M flag, preprocessor only
[de62360d]88 bool std_flag = false; // -std= flag
[35f9114]89 bool noincstd_flag = false; // -no-include-stdhdr= flag
[6e4b913]90 bool debugging __attribute(( unused )) = false; // -g flag
[51b73452]91
[b87a5ed]92 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
93 int sargs = 1; // starting location for arguments in args list
94 int nargs = sargs; // number of arguments in args list; 0 => command name
[51b73452]95
[b87a5ed]96 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
97 int nlibs = 0;
[51b73452]98
99#ifdef __DEBUG_H__
[b87a5ed]100 cerr << "CFA:" << endl;
[51b73452]101#endif // __DEBUG_H__
102
[b87a5ed]103 // process command-line arguments
[51b73452]104
[b87a5ed]105 for ( int i = 1; i < argc; i += 1 ) {
[51b73452]106#ifdef __DEBUG_H__
[b87a5ed]107 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]108#endif // __DEBUG_H__
[b87a5ed]109 arg = argv[i]; // convert to string value
[51b73452]110#ifdef __DEBUG_H__
[b87a5ed]111 cerr << "arg:\"" << arg << "\"" << endl;
[51b73452]112#endif // __DEBUG_H__
[b87a5ed]113 if ( prefix( arg, "-" ) ) {
114 // pass through arguments
115
116 if ( arg == "-Xlinker" || arg == "-o" ) {
117 args[nargs] = argv[i]; // pass the argument along
118 nargs += 1;
119 i += 1;
120 if ( i == argc ) continue; // next argument available ?
121 args[nargs] = argv[i]; // pass the argument along
122 nargs += 1;
123 } else if ( arg == "-XCFA" ) { // CFA pass through
124 i += 1;
125 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
126 nargs += 1;
127
128 // CFA specific arguments
129
130 } else if ( arg == "-CFA" ) {
131 CFA_flag = true; // strip the -CFA flag
132 link = false;
133 args[nargs] = "-E"; // replace the argument with -E
134 nargs += 1;
135 } else if ( arg == "-debug" ) {
136 debug = true; // strip the debug flag
137 } else if ( arg == "-nodebug" ) {
138 debug = false; // strip the nodebug flag
139 } else if ( arg == "-quiet" ) {
140 quiet = true; // strip the quiet flag
141 } else if ( arg == "-noquiet" ) {
142 quiet = false; // strip the noquiet flag
143 } else if ( arg == "-help" ) {
144 help = true; // strip the help flag
145 } else if ( arg == "-nohelp" ) {
146 help = false; // strip the nohelp flag
[35f9114]147 } else if ( arg == "-no-include-stdhdr" ) {
148 noincstd_flag = true; // strip the no-include-stdhdr flag
[b87a5ed]149 } else if ( arg == "-compiler" ) {
150 // use the user specified compiler
151 i += 1;
152 if ( i == argc ) continue; // next argument available ?
153 compiler_path = argv[i];
154 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
155 cerr << argv[0] << " error, cannot set environment variable." << endl;
156 exit( EXIT_FAILURE );
157 } // if
158
[de62360d]159 // C specific arguments
[b87a5ed]160
161 } else if ( arg == "-v" ) {
162 verbose = true; // verbosity required
163 args[nargs] = argv[i]; // pass the argument along
164 nargs += 1;
165 } else if ( arg == "-g" ) {
166 debugging = true; // symbolic debugging required
167 args[nargs] = argv[i]; // pass the argument along
168 nargs += 1;
[de62360d]169 } else if ( prefix( arg, "-std=" ) ) {
[53ba273]170 std_flag = true; // -std=XX provided
[de62360d]171 args[nargs] = argv[i]; // pass the argument along
172 nargs += 1;
[b87a5ed]173 } else if ( prefix( arg, "-B" ) ) {
174 Bprefix = arg.substr(2); // strip the -B flag
175 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
176 nargs += 1;
177 } else if ( prefix( arg, "-b" ) ) {
178 if ( arg.length() == 2 ) { // separate argument ?
179 i += 1;
180 if ( i == argc ) continue; // next argument available ?
181 arg += argv[i]; // concatenate argument
182 } // if
183 // later versions of gcc require the -b option to appear at the start of the command line
184 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
185 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
186 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
187 cerr << argv[0] << " error, cannot set environment variable." << endl;
188 exit( EXIT_FAILURE );
189 } // if
190 sargs += 1;
191 nargs += 1;
192 } else if ( prefix( arg, "-V" ) ) {
193 if ( arg.length() == 2 ) { // separate argument ?
194 i += 1;
195 if ( i == argc ) continue; // next argument available ?
196 arg += argv[i]; // concatenate argument
197 } // if
198 // later versions of gcc require the -V option to appear at the start of the command line
199 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
200 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
201 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
202 cerr << argv[0] << " error, cannot set environment variable." << endl;
203 exit( EXIT_FAILURE );
204 } // if
205 sargs += 1;
206 nargs += 1;
207 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
208 args[nargs] = argv[i]; // pass the argument along
209 nargs += 1;
210 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
211 cpp_flag = true; // cpp only
212 } // if
213 link = false; // no linkage required
214 } else if ( arg[1] == 'l' ) {
215 // if the user specifies a library, load it after user code
216 libs[nlibs] = argv[i];
217 nlibs += 1;
218 } else {
219 // concatenate any other arguments
220 args[nargs] = argv[i];
221 nargs += 1;
222 } // if
223 } else {
224 // concatenate other arguments
225 args[nargs] = argv[i];
226 nargs += 1;
227 nonoptarg = true;
228 } // if
229 } // for
[51b73452]230
[b87a5ed]231#ifdef __DEBUG_H__
232 cerr << "args:";
233 for ( int i = 1; i < nargs; i += 1 ) {
234 cerr << " " << args[i];
235 } // for
236 cerr << endl;
237#endif // __DEBUG_H__
[51b73452]238
[b87a5ed]239 if ( cpp_flag && CFA_flag ) {
240 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
241 exit( EXIT_FAILURE );
242 } // if
[51b73452]243
[b87a5ed]244 string d;
245 if ( debug ) {
246 d = "-d";
247 } // if
[51b73452]248
[6e4b913]249 // add the CFA include-library paths, which allow direct access to header files without directory qualification
250
[b87a5ed]251 args[nargs] = "-I" CFA_INCDIR;
252 nargs += 1;
[d0542c4]253 if ( ! noincstd_flag ) { // do not use during build
254 args[nargs] = "-I" CFA_INCDIR "/stdhdr";
255 nargs += 1;
256 } // if
[76c7f65e]257 args[nargs] = "-I" CFA_INCDIR "/containers";
258 nargs += 1;
259
[b87a5ed]260 if ( link ) {
261 // include the cfa library in case it's needed
262 args[nargs] = "-L" CFA_LIBDIR;
[51b73452]263 nargs += 1;
[b87a5ed]264 args[nargs] = "-lcfa";
[51b73452]265 nargs += 1;
266 } // if
267
[b87a5ed]268 // add the correct set of flags based on the type of compile this is
[8c17ab0]269
[1db21619]270 args[nargs] = ( *new string( string("-D__CFA__=") + Major ) ).c_str();
[51b73452]271 nargs += 1;
[b87a5ed]272 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
[51b73452]273 nargs += 1;
[1db21619]274 args[nargs] = ( *new string( string("-D__CFA_PATCHLEVEL__=") + Patch ) ).c_str();
275 nargs += 1;
[4c82a3c]276 args[nargs] = "-D__CFA__";
277 nargs += 1;
278 args[nargs] = "-D__CFORALL__";
[02e5ab6]279 nargs += 1;
[51b73452]280
[b87a5ed]281 if ( cpp_flag ) {
282 args[nargs] = "-D__CPP__";
283 nargs += 1;
284 } // if
[51b73452]285
[b87a5ed]286 if ( CFA_flag ) {
[4c82a3c]287 args[nargs] = "-D__CFA_PREPROCESS_";
[b87a5ed]288 nargs += 1;
289 } // if
[51b73452]290
[b87a5ed]291 if ( debug ) {
292 heading += " (debug)";
293 args[nargs] = "-D__CFA_DEBUG__";
294 nargs += 1;
295 } else {
296 heading += " (no debug)";
297 } // if
[51b73452]298
[b87a5ed]299 if ( Bprefix.length() == 0 ) {
300 Bprefix = installlibdir;
301 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
302 nargs += 1;
303 } // if
[51b73452]304
[b87a5ed]305 // execute the compilation command
[51b73452]306
[b87a5ed]307 args[0] = compiler_path.c_str(); // set compiler command for exec
308 // find actual name of the compiler independent of the path to it
309 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
310 if ( p == -1 ) {
311 compiler_name = compiler_path;
312 } else {
313 compiler_name = *new string( compiler_path.substr( p + 1 ) );
314 } // if
[51b73452]315
[b87a5ed]316 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
317 args[nargs] = "-no-integrated-cpp";
318 nargs += 1;
[76c7f65e]319 args[nargs] = "-Wno-deprecated";
[b87a5ed]320 nargs += 1;
[de62360d]321 if ( ! std_flag ) { // default c99, if none specified
[53ba273]322 args[nargs] = "-std=gnu99";
[de62360d]323 nargs += 1;
324 } // if
[76c7f65e]325 args[nargs] = "-fgnu89-inline";
[6e991d6]326 nargs += 1;
[b87a5ed]327 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
328 nargs += 1;
[d3b7937]329 args[nargs] = "-lm";
330 nargs += 1;
[b87a5ed]331 } else {
[e24f13a]332 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
[b87a5ed]333 exit( EXIT_FAILURE );
334 } // if
[51b73452]335
[b87a5ed]336 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
337 args[nargs] = libs[i];
338 nargs += 1;
339 } // for
[51b73452]340
[b87a5ed]341 args[nargs] = NULL; // terminate with NULL
[51b73452]342
343#ifdef __DEBUG_H__
[b87a5ed]344 cerr << "nargs: " << nargs << endl;
345 cerr << "args:" << endl;
346 for ( int i = 0; args[i] != NULL; i += 1 ) {
347 cerr << " \"" << args[i] << "\"" << endl;
348 } // for
[51b73452]349#endif // __DEBUG_H__
350
[b87a5ed]351 if ( ! quiet ) {
352 cerr << "CFA " << "Version " << Version << heading << endl;
353
354 if ( help ) {
355 cerr <<
356 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
357 "-help\t\t\t: print this help message" << endl <<
358 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
359 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
360 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
361 "...\t\t\t: any other " << compiler_name << " flags" << endl;
362 } // if
[51b73452]363 } // if
364
[b87a5ed]365 if ( verbose ) {
366 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
[51b73452]367
[b87a5ed]368 for ( int i = 0; args[i] != NULL; i += 1 ) {
369 cerr << args[i] << " ";
370 } // for
371 cerr << endl;
372 } // if
[51b73452]373
[b87a5ed]374 if ( ! nonoptarg ) {
375 cerr << argv[0] << " error, no input files" << endl;
376 exit( EXIT_FAILURE );
377 } // if
[51b73452]378
[b87a5ed]379 // execute the command and return the result
[51b73452]380
[b87a5ed]381 execvp( args[0], (char *const *)args ); // should not return
382 perror( "CFA Translator error: cfa level, execvp" );
383 exit( EXIT_FAILURE );
[51b73452]384} // main
385
386// Local Variables: //
[b87a5ed]387// tab-width: 4 //
388// mode: c++ //
[51b73452]389// compile-command: "make install" //
390// End: //
Note: See TracBrowser for help on using the repository browser.