source: driver/cfa.cc@ b544afa

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

move bprefix to environment variable for cc1

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