[libcxx] Teach libcxx's lit configuration new ways to find lit.site.cfg

Summary:
Currently to run tests in tree you need to symlink the lit.site.cfg file generated by the cmake build into the source tree, and teach your VCS to ignore it.

This allows the user to specify where to find the lit.site.cfg file two different ways:
* lit_site_config lit parameter
* LIT_SITE_CONFIG enviroment variable. 

example usage:
```
lit -sv --param=libcxx_site_config=path/to/libcxx-build/test/lit.site.cfg path/to/tests
```
Or
```
export LIBCXX_SITE_CONFIG=path/to/libcxx-build/test/lit.site.cfg
lit -sv path/to/tests
```
The command line parameter will override the environment variable. 
If neither options are present a warning is issued and the `lit.cfg` file is loaded directly. 


Reviewers: mclow.lists, jroelofs, danalbert

Reviewed By: danalbert

Subscribers: ddunbar, cfe-commits

Differential Revision: http://reviews.llvm.org/D6255

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@224671 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/lit.cfg b/test/lit.cfg
index 4e6a798..9df0137 100644
--- a/test/lit.cfg
+++ b/test/lit.cfg
@@ -211,6 +211,7 @@
         self.cxx = None
         self.src_root = None
         self.obj_root = None
+        self.library_root = None
         self.env = {}
         self.compile_flags = ['-nostdinc++']
         self.link_flags = ['-nodefaultlibs']
@@ -246,6 +247,7 @@
         self.configure_triple()
         self.configure_src_root()
         self.configure_obj_root()
+        self.configure_library_root()
         self.configure_use_system_lib()
         self.configure_use_clang_verify()
         self.configure_ccache()
@@ -333,6 +335,9 @@
     def configure_obj_root(self):
         self.obj_root = self.get_lit_conf('libcxx_obj_root', self.src_root)
 
+    def configure_library_root(self):
+        self.library_root = self.get_lit_conf('libcxx_library_root', self.obj_root)
+
     def configure_use_system_lib(self):
         # This test suite supports testing against either the system library or
         # the locally built one; the former mode is useful for testing ABI
@@ -483,8 +488,8 @@
         # Configure library search paths
         abi_library_path = self.get_lit_conf('abi_library_path', '')
         if not self.use_system_lib:
-            self.link_flags += ['-L' + self.obj_root + '/lib']
-            self.link_flags += ['-Wl,-rpath,' + self.obj_root + '/lib']
+            self.link_flags += ['-L' + self.library_root]
+            self.link_flags += ['-Wl,-rpath,' + self.library_root]
         if abi_library_path:
             self.link_flags += ['-L' + abi_library_path,
                                 '-Wl,-rpath,' + abi_library_path]
@@ -599,7 +604,7 @@
 
     def configure_env(self):
         if sys.platform == 'darwin' and not self.use_system_lib:
-            self.env['DYLD_LIBRARY_PATH'] = os.path.join(self.obj_root, 'lib')
+            self.env['DYLD_LIBRARY_PATH'] = self.library_root
 
 # name: The name of this test suite.
 config.name = 'libc++'
@@ -610,6 +615,34 @@
 # test_source_root: The root path where tests are located.
 config.test_source_root = os.path.dirname(__file__)
 
+# Infer the test_exec_root from the libcxx_object root.
+libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
+if libcxx_obj_root is not None:
+    config.test_exec_root = os.path.join(libcxx_obj_root, 'test')
+
+# Check that the test exec root is known.
+if config.test_exec_root is None:
+    # Otherwise, we haven't loaded the site specific configuration (the user is
+    # probably trying to run on a test file directly, and either the site
+    # configuration hasn't been created by the build system, or we are in an
+    # out-of-tree build situation).
+    site_cfg = lit_config.params.get('libcxx_site_config',
+                                     os.environ.get('LIBCXX_SITE_CONFIG'))
+    if not site_cfg:
+        lit_config.warning('No site specific configuration file found!'
+                           ' Running the tests in the default configuration.')
+        # TODO: Set test_exec_root to a temporary directory where output files
+        # can be placed. This is needed for ShTest.
+    elif not os.path.isfile(site_cfg):
+        lit_config.fatal(
+            "Specified site configuration file does not exist: '%s'"  %
+            site_cfg)
+    else:
+        lit_config.note('using site specific configuration at %s' % site_cfg)
+        lit_config.load_config(config, site_cfg)
+        raise SystemExit()
+
+
 cfg_variant = getattr(config, 'configuration_variant', '')
 if cfg_variant:
     print 'Using configuration variant: %s' % cfg_variant