Changeset 20769

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

Change the run-time behavior of the automatic assignment of SNX_BASE_DIR
back to the way it worked prior to r20762. Namely, the work to set
SNX_BASE_DIR automatically is only performed if the environment variable is
not already set. Further, SNX_DATA_DIR is now set relative to SNX_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/sonix/snx/snxmain.cpp

    r20764 r20769  
    7070      case DLL_PROCESS_ATTACH: 
    7171         { 
     72            char* env_dir(NULL); 
     73#if defined(_MSC_VER) && _MSC_VER >= 1400 
     74            size_t len; 
     75            _dupenv_s(&env_dir, &len, "SNX_BASE_DIR"); 
     76#else 
     77            env_dir = std::getenv("SNX_BASE_DIR"); 
     78#endif 
     79 
    7280            try 
    7381            { 
    74                char tmppath[1024]; 
    75                std::memset(tmppath, 0, sizeof(tmppath)); 
    76                GetModuleFileName(module, tmppath, sizeof(tmppath)); 
    77  
    78                const fs::path dll_path(tmppath, fs::native); 
    79                fs::path base_dir = dll_path.branch_path().branch_path(); 
     82               fs::path base_dir; 
     83 
     84               // If SNX_BASE_DIR is not set, look up the path to this DLL and 
     85               // use it to provide a default setting for that environment 
     86               // variable. 
     87               if ( NULL == env_dir ) 
     88               { 
     89                  char tmppath[1024]; 
     90                  std::memset(tmppath, 0, sizeof(tmppath)); 
     91                  GetModuleFileName(module, tmppath, sizeof(tmppath)); 
     92 
     93                  const fs::path dll_path(tmppath, fs::native); 
     94                  base_dir = dll_path.branch_path().branch_path(); 
    8095#if (defined(JUGGLER_DEBUG) || defined(SNX_DEBUG)) && ! defined(_DEBUG) 
    81                // The debug DLL linked against the release runtime is in 
    82                // <base_dir>\lib\debug. 
    83                base_dir = base_dir.branch_path(); 
    84 #endif 
    85  
    86                char* env_dir(NULL); 
    87 #if defined(_MSC_VER) && _MSC_VER >= 1400 
    88                size_t len; 
    89                _dupenv_s(&env_dir, &len, "SNX_BASE_DIR"); 
    90 #else 
    91                env_dir = std::getenv("SNX_BASE_DIR"); 
    92 #endif 
    93  
    94                if ( NULL == env_dir ) 
    95                { 
     96                  // The debug DLL linked against the release runtime is in 
     97                  // <base_dir>\lib\debug. 
     98                  base_dir = base_dir.branch_path(); 
     99#endif 
     100 
    96101                  const std::string base_dir_str = 
    97102                     base_dir.native_directory_string(); 
     
    105110#endif 
    106111               } 
    107 #if defined(_MSC_VER) && _MSC_VER >= 1400 
    108112               else 
    109113               { 
     114                  base_dir = fs::path(env_dir, fs::native); 
     115#if defined(_MSC_VER) && _MSC_VER >= 1400 
    110116                  std::free(env_dir); 
    111                } 
    112 #endif 
     117                  env_dir = NULL; 
     118#endif 
     119               } 
    113120 
    114121#if defined(_MSC_VER) && _MSC_VER >= 1400 
     
    118125#endif 
    119126 
     127               // If SNX_BASE_DIR is not set, set a default relative to 
     128               // base_dir. 
    120129               if ( NULL == env_dir ) 
    121130               { 
     
    136145               { 
    137146                  std::free(env_dir); 
     147                  env_dir = NULL; 
    138148               } 
    139149#endif 
     
    143153               std::cerr << "Automatic assignment of Sonix environment " 
    144154                         << "variables failed:\n" << ex.what() << std::endl; 
     155 
     156#if defined(_MSC_VER) && _MSC_VER >= 1400 
     157               if ( NULL != env_dir ) 
     158               { 
     159                  std::free(env_dir); 
     160               } 
     161#endif 
    145162            } 
    146163         } 
     
    164181extern "C" void __attribute ((constructor)) snxLibraryInit() 
    165182{ 
    166    Dl_info info; 
    167    info.dli_fname = 0; 
    168    const int result = dladdr(reinterpret_cast<const void*>(&snxLibraryInit), 
    169                              &info); 
    170  
    171    // NOTE: dladdr(3) really does return a non-zero value on success. 
    172    if ( 0 != result ) 
    173    { 
    174       try 
     183   fs::path base_dir; 
     184   const char* env_dir = std::getenv("SNX_BASE_DIR"); 
     185 
     186   // If SNX_BASE_DIR is not set, look up the path to this shared library and 
     187   // use it to provide a default setting for that environment variable. 
     188   if ( NULL == env_dir ) 
     189   { 
     190      Dl_info info; 
     191      info.dli_fname = 0; 
     192      const int result = 
     193         dladdr(reinterpret_cast<const void*>(&snxLibraryInit), &info); 
     194 
     195      // NOTE: dladdr(3) really does return a non-zero value on success. 
     196      if ( 0 != result ) 
    175197      { 
    176          fs::path lib_file(info.dli_fname, fs::native); 
    177          lib_file = fs::system_complete(lib_file); 
     198         try 
     199         { 
     200            fs::path lib_file(info.dli_fname, fs::native); 
     201            lib_file = fs::system_complete(lib_file); 
    178202 
    179203#if defined(VPR_OS_IRIX) && defined(_ABIN32) 
    180          const std::string bit_suffix("32"); 
     204            const std::string bit_suffix("32"); 
    181205#elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ 
    182206      defined(VPR_OS_Linux) && defined(__x86_64__) 
    183          const std::string bit_suffix("64"); 
    184 #else 
    185          const std::string bit_suffix(""); 
    186 #endif 
    187  
    188          // Get the directory containing this shared library. 
    189          const fs::path lib_path = lib_file.branch_path(); 
    190  
    191          // Start the search for the root of the Sonix installation in the 
    192          // parent of the directory containing this shared library. 
    193          fs::path base_dir = lib_path.branch_path(); 
    194  
    195          // Use the lib subdirectory to figure out when we have found the root 
    196          // of the Sonix installation tree. 
    197          const fs::path lib_subdir(std::string("lib") + bit_suffix); 
    198  
    199          bool found(false); 
    200          while ( ! found && ! base_dir.empty() ) 
     207            const std::string bit_suffix("64"); 
     208#else 
     209            const std::string bit_suffix(""); 
     210#endif 
     211 
     212            // Get the directory containing this shared library. 
     213            const fs::path lib_path = lib_file.branch_path(); 
     214 
     215            // Start the search for the root of the Sonix installation in the 
     216            // parent of the directory containing this shared library. 
     217            base_dir = lib_path.branch_path(); 
     218 
     219            // Use the lib subdirectory to figure out when we have found the 
     220            // root of the Sonix installation tree. 
     221            const fs::path lib_subdir(std::string("lib") + bit_suffix); 
     222 
     223            bool found(false); 
     224            while ( ! found && ! base_dir.empty() ) 
     225            { 
     226               try 
     227               { 
     228                  if ( ! fs::exists(base_dir / lib_subdir) ) 
     229                  { 
     230                     base_dir = base_dir.branch_path(); 
     231                  } 
     232                  else 
     233                  { 
     234                     found = true; 
     235                  } 
     236               } 
     237               catch (fs::filesystem_error&) 
     238               { 
     239                  base_dir = base_dir.branch_path(); 
     240               } 
     241            } 
     242 
     243            if ( found ) 
     244            { 
     245               setenv("SNX_BASE_DIR", 
     246                      base_dir.native_directory_string().c_str(), 1); 
     247            } 
     248         } 
     249         catch (fs::filesystem_error& ex) 
    201250         { 
    202             try 
    203             { 
    204                if ( ! fs::exists(base_dir / lib_subdir) ) 
    205                { 
    206                   base_dir = base_dir.branch_path(); 
    207                } 
    208                else 
    209                { 
    210                   found = true; 
    211                } 
    212             } 
    213             catch (fs::filesystem_error&) 
    214             { 
    215                base_dir = base_dir.branch_path(); 
    216             } 
    217          } 
    218  
    219          if ( found ) 
    220          { 
    221             const fs::path data_dir = base_dir / SNX_SHARE_DIR; 
    222  
    223             // We use the overwrite value of 0 as a way around testing whether 
    224             // the environment variables are already set. 
    225             setenv("SNX_BASE_DIR", base_dir.native_directory_string().c_str(), 
    226                    0); 
    227             setenv("SNX_DATA_DIR", data_dir.native_directory_string().c_str(), 
    228                    0); 
     251            std::cerr << "Automatic assignment of SNX_BASE_DIR failed:\n" 
     252                      << ex.what() << std::endl; 
    229253         } 
    230254      } 
    231       catch (fs::filesystem_error& ex) 
    232       { 
    233          std::cerr << "Automatic assignment of Sonix environment " 
    234                    << "variables failed:\n" << ex.what() << std::endl; 
    235       } 
     255   } 
     256   else 
     257   { 
     258      base_dir = fs::path(env_dir, fs::native); 
     259   } 
     260 
     261   if ( ! base_dir.empty() ) 
     262   { 
     263      // If base_dir were empty, this would result in data_dir being relative 
     264      // to the current working directory. 
     265      const fs::path data_dir = base_dir / SNX_SHARE_DIR; 
     266 
     267      // We use the overwrite value of 0 as a way around testing whether the 
     268      // environment variable is already set. 
     269      setenv("SNX_DATA_DIR", data_dir.native_directory_string().c_str(), 0); 
    236270   } 
    237271}