Index: driver/cfa.cc
===================================================================
--- driver/cfa.cc	(revision aced69a2edd4dd87c115f5edd516e9e03ea9ea80)
+++ driver/cfa.cc	(revision a539fc3160f56f4694967a0d08b3cb4c7696376e)
@@ -33,11 +33,11 @@
 using std::to_string;
 
-//#define __DEBUG_H__
-
+// #define __DEBUG_H__
 
 static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );		// "N__=" suffix
 
 void Putenv( char * argv[], string arg ) {
-	static int flags = 0;								// environment variables must have unique names
+	// environment variables must have unique names
+	static int flags = 0;
 
 	if ( putenv( (char *)( *new string( string( __CFA_FLAGPREFIX__ + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) ) {
@@ -47,8 +47,13 @@
 } // Putenv
 
-
-bool prefix( const string & arg, const string & pre ) {	// check if string has prefix
+// check if string has prefix
+bool prefix( const string & arg, const string & pre ) {
 	return arg.substr( 0, pre.size() ) == pre;
 } // prefix
+
+inline bool ends_with(const string & str, const string & sfix) {
+	if (sfix.size() > str.size()) return false;
+	return std::equal(str.rbegin(), str.rbegin() + sfix.size(), sfix.rbegin(), sfix.rend());
+}
 
 bool suffix( const string & arg ) {						// check if string has suffix
@@ -68,4 +73,8 @@
 	return (info.st_mode & S_IFDIR) != 0;
 } // dirExists
+
+static inline string dir(const string & path) {
+	return path.substr(0, path.find_last_of('/'));
+}
 
 
@@ -105,4 +114,6 @@
 	bool m64 = false;									// -m64 flag
 	bool intree = false;								// build in tree
+	bool compiling_libs = false;
+	bool disttree = false;
 	int o_file = 0;										// -o filename position
 
@@ -162,4 +173,8 @@
 			} else if ( arg == "-in-tree" ) {
 				intree = true;
+			} else if ( arg == "-dist-tree" ) {
+				disttree = true;
+			} else if ( arg == "-cfalib") {
+				compiling_libs = true;
 			} else if ( arg == "-compiler" ) {
 				// use the user specified compiler
@@ -293,4 +308,7 @@
 	} else {
 		libbase = TOP_BUILDDIR "libcfa/";
+	} // if
+
+	if( compiling_libs ) {
 		Putenv( argv, "-t" );
 	} // if
@@ -305,9 +323,32 @@
 	} // if
 
-	string libdir( libbase + arch + "-" + (nolib ? "nolib" : (debug ? "debug": "nodebug")) );
-	if ( ! dirExists( libdir ) ) {
-		cerr << argv[0] << " internal error, cannot find prelude directory " << libdir << endl;
-		exit( EXIT_FAILURE );
-	} // if
+	const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug");
+	string libdir = libbase + arch + "-" + config;
+
+	if (!disttree) {
+		if ( ! nolib && ! dirExists( libdir ) ) {
+			cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
+			cerr << "Was looking for " << libdir << endl;
+			for(int i = 1; i < argc; i++) {
+				cerr << argv[i] << " ";
+			}
+			cerr << endl;
+			libdir = libbase + arch + "-" + "nolib";
+		} // if
+
+		if ( ! dirExists( libdir ) ) {
+			cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
+			cerr << "Was looking for " << libdir << endl;
+			exit( EXIT_FAILURE );
+		} // if
+	} // if
+
+	if(disttree) {
+		Putenv( argv, "--prelude-dir=" + dir(argv[0]) );
+	} else if(intree) {
+		Putenv( argv, "--prelude-dir=" + libdir + "/prelude" );
+	} else {
+		Putenv( argv, "--prelude-dir=" + libdir );
+	}
 
 	for ( int i = 0; i < nlibs; i += 1 ) {				// copy non-user libraries after all user libraries
@@ -361,6 +402,4 @@
 	} // if
 
-	Putenv( argv, "--prelude-dir=" + libdir + (intree ? "/prelude" : "") );
-
 	if ( debug ) {
 		heading += " (debug)";
@@ -371,7 +410,13 @@
 
 	if ( bprefix.length() == 0 ) {
-		bprefix = ! intree ? installlibdir : srcdriverdir;
+		if(disttree) {
+			bprefix = dir(argv[0]);
+		} else if(intree) {
+			bprefix = srcdriverdir;
+		} else {
+			bprefix = installlibdir;
+		}
 		if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/';
-		Putenv( argv, ( *new string( string("-B=") + bprefix ) ).c_str() );
+		Putenv( argv, string("-B=") + bprefix );
 	} // if
 
@@ -415,9 +460,9 @@
 		cerr << " \"" << args[i] << "\"" << endl;
 	} // for
+	cerr << endl;
 	#endif // __DEBUG_H__
 
 	if ( ! quiet ) {
 		cerr << "CFA " << "Version " << Version << heading << endl;
-
 		if ( help ) {
 			cerr <<
