Changeset 20785

Show
Ignore:
Timestamp:
09/07/07 15:49:03 (1 year ago)
Author:
patrick
Message:

MFT: Set the environment variable VPR_BASE_DIR automatically on all platforms

when it is not already set.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/branches/2.2/modules/vapor/ChangeLog

    r20778 r20785  
    11DATE       AUTHOR   CHANGE 
    22---------- -------- ----------------------------------------------------------- 
     32007-09-07 patrick  Set VPR_BASE_DIR automatically on all platforms. 
    342007-09-06 patrick  Implemented stack trace printing on Mac OS X. 
    45 
  • juggler/branches/2.2/modules/vapor/vpr/Makefile.in

    r19729 r20785  
    6060                md 
    6161 
    62 SRCS=           SystemBase.cpp 
     62SRCS=           SystemBase.cpp  \ 
     63                vprmain.cpp 
    6364 
    6465include $(MKPATH)/dpp.obj-subdir.mk 
  • juggler/branches/2.2/modules/vapor/vpr/vprmain.cpp

    r20500 r20785  
    3434 *************** <auto-copyright.pl END do not edit this line> ***************/ 
    3535 
    36 #if defined(WIN32) || defined(WIN64) 
    37 #include <windows.h> 
     36#include <vpr/vprConfig.h> 
     37 
    3838#include <iostream> 
    3939#include <sstream> 
    4040#include <cstdlib> 
    4141#include <string> 
     42 
     43#if ! defined(WIN32) && ! defined(WIN64) 
     44#  include <dlfcn.h> 
     45#endif 
     46 
    4247#include <boost/filesystem/path.hpp> 
     48#include <boost/filesystem/operations.hpp> 
    4349#include <boost/filesystem/exception.hpp> 
    4450 
     
    4652namespace fs = boost::filesystem; 
    4753 
     54#if defined(WIN32) || defined(WIN64) 
    4855/** 
    4956 * Windows DLL entry point function. This ensures that the environment 
     
    114121   return TRUE; 
    115122} 
    116  
    117  
     123#else 
     124/** 
     125 * Non-Windows shared library constructor. This ensures that the environment 
     126 * variable \c VPR_BASE_DIR is set as soon as this shared library is loaded. 
     127 * If it is not set, then it sets it based on an assumption about the 
     128 * structure of a VPR installation. More specifically, an assumption is made 
     129 * that this shared library lives in the \c lib subdirectory of the VPR 
     130 * installation. Therefore, the root of the VPR installation is the parent of 
     131 * the directory containing this shared library. 
     132 */ 
     133extern "C" void __attribute ((constructor)) vprLibraryInit() 
     134
     135   const char* env_dir = std::getenv("VPR_BASE_DIR"); 
     136 
     137   if ( NULL == env_dir ) 
     138   { 
     139      Dl_info info; 
     140      info.dli_fname = 0; 
     141      const int result = 
     142#if defined(__GNUC__) && __GNUC_MAJOR__ < 4 
     143         dladdr((void*) &vprLibraryInit, &info); 
     144#else 
     145         dladdr(static_cast<void*>(&vprLibraryInit), &info); 
     146#endif 
     147 
     148      // NOTE: dladdr(3) really does return a non-zero value on success. 
     149      if ( 0 != result ) 
     150      { 
     151         try 
     152         { 
     153            fs::path lib_file(info.dli_fname, fs::native); 
     154            lib_file = fs::system_complete(lib_file); 
     155 
     156#if defined(VPR_OS_IRIX) && defined(_ABIN32) 
     157            const std::string bit_suffix("32"); 
     158#elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ 
     159      defined(VPR_OS_Linux) && defined(__x86_64__) 
     160            const std::string bit_suffix("64"); 
     161#else 
     162            const std::string bit_suffix(""); 
     163#endif 
     164 
     165            // Get the directory containing this shared library. 
     166            const fs::path lib_path = lib_file.branch_path(); 
     167 
     168            // Start the search for the root of the VPR installation in the 
     169            // parent of the directory containing this shared library. 
     170            fs::path base_dir = lib_path.branch_path(); 
     171 
     172            // Use the lib subdirectory to figure out when we have found the 
     173            // root of the VPR installation tree. 
     174            const fs::path lib_subdir(std::string("lib") + bit_suffix); 
     175 
     176            bool found(false); 
     177            while ( ! found && ! base_dir.empty() ) 
     178            { 
     179               try 
     180               { 
     181                  if ( ! fs::exists(base_dir / lib_subdir) ) 
     182                  { 
     183                     base_dir = base_dir.branch_path(); 
     184                  } 
     185                  else 
     186                  { 
     187                     found = true; 
     188                  } 
     189               } 
     190               catch (fs::filesystem_error&) 
     191               { 
     192                  base_dir = base_dir.branch_path(); 
     193               } 
     194            } 
     195 
     196            if ( found ) 
     197            { 
     198               setenv("VPR_BASE_DIR", 
     199                      base_dir.native_directory_string().c_str(), 1); 
     200            } 
     201         } 
     202         catch (fs::filesystem_error& ex) 
     203         { 
     204            std::cerr << "Automatic assignment of VPR_BASE_DIR failed:\n" 
     205                      << ex.what() << std::endl; 
     206         } 
     207      } 
     208   } 
     209
    118210#endif  /* defined(WIN32) || defined(WIN64) */