Changeset 20770

Show
Ignore:
Timestamp:
09/06/07 09:04:08 (1 year ago)
Author:
patrick
Message:

Change the run-time behavior of the automatic assignment of VJ_BASE_DIR
back to the way it worked prior to r20755. Namely, the work to set VJ_BASE_DIR
automatically is only performed if the environment variable is not already
set. Further, VJ_DATA_DIR is now set relative to VJ_BASE_DIR whether it was
assigned automatically by this code or manually by the user.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • juggler/trunk/modules/vrjuggler/vrj/vrjmain.cpp

    r20764 r20770  
    6161      case DLL_PROCESS_ATTACH: 
    6262         { 
     63            char* env_dir(NULL); 
     64#if defined(_MSC_VER) && _MSC_VER >= 1400 
     65            size_t len; 
     66            _dupenv_s(&env_dir, &len, "VJ_BASE_DIR"); 
     67#else 
     68            env_dir = std::getenv("VJ_BASE_DIR"); 
     69#endif 
     70 
    6371            try 
    6472            { 
    65                char tmppath[1024]; 
    66                std::memset(tmppath, 0, sizeof(tmppath)); 
    67                GetModuleFileName(module, tmppath, sizeof(tmppath)); 
    68  
    69                const fs::path dll_path(tmppath, fs::native); 
    70                fs::path base_dir = dll_path.branch_path().branch_path(); 
     73               fs::path base_dir; 
     74 
     75               // If VJ_BASE_DIR is not set, look up the path to this DLL and 
     76               // use it to provide a default setting for that environment 
     77               // variable. 
     78               if ( NULL == env_dir ) 
     79               { 
     80                  char tmppath[1024]; 
     81                  std::memset(tmppath, 0, sizeof(tmppath)); 
     82                  GetModuleFileName(module, tmppath, sizeof(tmppath)); 
     83 
     84                  const fs::path dll_path(tmppath, fs::native); 
     85                  base_dir = dll_path.branch_path().branch_path(); 
    7186#if (defined(JUGGLER_DEBUG) || defined(VJ_DEBUG)) && ! defined(_DEBUG) 
    72                // The debug DLL linked against the release runtime is in 
    73                // <base_dir>\lib\debug. 
    74                base_dir = base_dir.branch_path(); 
    75 #endif 
    76  
    77                char* env_dir(NULL); 
    78 #if defined(_MSC_VER) && _MSC_VER >= 1400 
    79                size_t len; 
    80                _dupenv_s(&env_dir, &len, "VJ_BASE_DIR"); 
    81 #else 
    82                env_dir = std::getenv("VJ_BASE_DIR"); 
    83 #endif 
    84  
    85                if ( NULL == env_dir ) 
    86                { 
     87                  // The debug DLL linked against the release runtime is in 
     88                  // <base_dir>\lib\debug. 
     89                  base_dir = base_dir.branch_path(); 
     90#endif 
     91 
    8792                  const std::string base_dir_str = 
    8893                     base_dir.native_directory_string(); 
     
    96101#endif 
    97102               } 
    98 #if defined(_MSC_VER) && _MSC_VER >= 1400 
    99103               else 
    100104               { 
     105                  base_dir = fs::path(env_dir, fs::native); 
     106#if defined(_MSC_VER) && _MSC_VER >= 1400 
    101107                  std::free(env_dir); 
    102                } 
    103 #endif 
     108                  env_dir = NULL; 
     109#endif 
     110               } 
    104111 
    105112#if defined(_MSC_VER) && _MSC_VER >= 1400 
     
    109116#endif 
    110117 
     118               // If VJ_BASE_DIR is not set, set a default relative to 
     119               // base_dir. 
    111120               if ( NULL == env_dir ) 
    112121               { 
     
    127136               { 
    128137                  std::free(env_dir); 
     138                  env_dir = NULL; 
    129139               } 
    130140#endif 
     
    134144               std::cerr << "Automatic assignment of VR Juggler environment " 
    135145                         << "variables failed:\n" << ex.what() << std::endl; 
     146 
     147#if defined(_MSC_VER) && _MSC_VER >= 1400 
     148               if ( NULL != env_dir ) 
     149               { 
     150                  std::free(env_dir); 
     151               } 
     152#endif 
    136153            } 
    137154         } 
     
    155172extern "C" void __attribute ((constructor)) vrjLibraryInit() 
    156173{ 
    157    Dl_info info; 
    158    info.dli_fname = 0; 
    159    const int result = dladdr(reinterpret_cast<const void*>(&vrjLibraryInit), 
    160                              &info); 
    161  
    162    // NOTE: dladdr(3) really does return a non-zero value on success. 
    163    if ( 0 != result ) 
    164    { 
    165       try 
     174   fs::path base_dir; 
     175   const char* env_dir = std::getenv("VJ_BASE_DIR"); 
     176 
     177   // If VJ_BASE_DIR is not set, look up the path to this shared library and 
     178   // use it to provide a default setting for that environment variable. 
     179   if ( NULL == env_dir ) 
     180   { 
     181      Dl_info info; 
     182      info.dli_fname = 0; 
     183      const int result = 
     184         dladdr(reinterpret_cast<const void*>(&vrjLibraryInit), &info); 
     185 
     186      // NOTE: dladdr(3) really does return a non-zero value on success. 
     187      if ( 0 != result ) 
    166188      { 
    167          fs::path lib_file(info.dli_fname, fs::native); 
    168          lib_file = fs::system_complete(lib_file); 
     189         try 
     190         { 
     191            fs::path lib_file(info.dli_fname, fs::native); 
     192            lib_file = fs::system_complete(lib_file); 
    169193 
    170194#if defined(VPR_OS_IRIX) && defined(_ABIN32) 
    171          const std::string bit_suffix("32"); 
     195            const std::string bit_suffix("32"); 
    172196#elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ 
    173197      defined(VPR_OS_Linux) && defined(__x86_64__) 
    174          const std::string bit_suffix("64"); 
    175 #else 
    176          const std::string bit_suffix(""); 
    177 #endif 
    178  
    179          // Get the directory containing this shared library. 
    180          const fs::path lib_path = lib_file.branch_path(); 
    181  
    182          // Start the search for the root of the VR Juggler installation in 
    183          // the parent of the directory containing this shared library. 
    184          fs::path base_dir = lib_path.branch_path(); 
    185  
    186          // Use the lib subdirectory to figure out when we have found the root 
    187          // of the VR Juggler installation tree. 
    188          const fs::path lib_subdir(std::string("lib") + bit_suffix); 
    189  
    190          bool found(false); 
    191          while ( ! found && ! base_dir.empty() ) 
     198            const std::string bit_suffix("64"); 
     199#else 
     200            const std::string bit_suffix(""); 
     201#endif 
     202 
     203            // Get the directory containing this shared library. 
     204            const fs::path lib_path = lib_file.branch_path(); 
     205 
     206            // Start the search for the root of the VR Juggler installation in 
     207            // the parent of the directory containing this shared library. 
     208            base_dir = lib_path.branch_path(); 
     209 
     210            // Use the lib subdirectory to figure out when we have found the 
     211            // root of the VR Juggler installation tree. 
     212            const fs::path lib_subdir(std::string("lib") + bit_suffix); 
     213 
     214            bool found(false); 
     215            while ( ! found && ! base_dir.empty() ) 
     216            { 
     217               try 
     218               { 
     219                  if ( ! fs::exists(base_dir / lib_subdir) ) 
     220                  { 
     221                     base_dir = base_dir.branch_path(); 
     222                  } 
     223                  else 
     224                  { 
     225                     found = true; 
     226                  } 
     227               } 
     228               catch (fs::filesystem_error&) 
     229               { 
     230                  base_dir = base_dir.branch_path(); 
     231               } 
     232            } 
     233 
     234            if ( found ) 
     235            { 
     236               setenv("VJ_BASE_DIR", 
     237                      base_dir.native_directory_string().c_str(), 1); 
     238            } 
     239         } 
     240         catch (fs::filesystem_error& ex) 
    192241         { 
    193             try 
    194             { 
    195                if ( ! fs::exists(base_dir / lib_subdir) ) 
    196                { 
    197                   base_dir = base_dir.branch_path(); 
    198                } 
    199                else 
    200                { 
    201                   found = true; 
    202                } 
    203             } 
    204             catch (fs::filesystem_error&) 
    205             { 
    206                base_dir = base_dir.branch_path(); 
    207             } 
    208          } 
    209  
    210          if ( found ) 
    211          { 
    212             const fs::path data_dir = base_dir / VJ_SHARE_DIR; 
    213  
    214             // We use the overwrite value of 0 as a way around testing whether 
    215             // the environment variables are already set. 
    216             setenv("VJ_BASE_DIR", base_dir.native_directory_string().c_str(), 
    217                    0); 
    218             setenv("VJ_DATA_DIR", data_dir.native_directory_string().c_str(), 
    219                    0); 
     242            std::cerr << "Automatic assignment of VJ_BASE_DIR failed:\n" 
     243                      << ex.what() << std::endl; 
    220244         } 
    221245      } 
    222       catch (fs::filesystem_error& ex) 
    223       { 
    224          std::cerr << "Automatic assignment of VR Juggler environment " 
    225                    << "variables failed:\n" << ex.what() << std::endl; 
    226       } 
     246   } 
     247   else 
     248   { 
     249      base_dir = fs::path(env_dir, fs::native); 
     250   } 
     251 
     252   if ( ! base_dir.empty() ) 
     253   { 
     254      // If base_dir were empty, this would result in data_dir being relative 
     255      // to the current working directory. 
     256      const fs::path data_dir = base_dir / VJ_SHARE_DIR; 
     257 
     258      // We use the overwrite value of 0 as a way around testing whether the 
     259      // environment variable is already set. 
     260      setenv("VJ_DATA_DIR", data_dir.native_directory_string().c_str(), 0); 
    227261   } 
    228262}