Changeset 20788

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

MFT: Set the environment variable SNX_BASE_DIR automatically on all

platforms if it is not already set. Set the new environment variable
SNX_DATA_DIR automatically (relative to SNX_BASE_DIR) on all platforms if
it is not already set.

Files:

Legend:

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

    r20709 r20788  
    11DATE        AUTHOR      CHANGE 
    22----------- ----------- ------------------------------------------------------- 
     3Sep-07-2007 patrick     Set SNX_BASE_DIR automatically on all platforms. Use 
     4                        the new environment variable SNX_DATA_DIR as a way to 
     5                        look up files in SNX_BASE_DIR/share/sonix. 
     6 
    37[1.2.0 released - 8.14.2007]=================================================== 
    48Jul-08-2007 patrick     Debug-enabled code linked against the release runtime 
  • juggler/branches/2.2/modules/sonix/snx/Makefile.in

    r19729 r20788  
    5151                SoundImplementation.cpp         \ 
    5252                StubSoundImplementation.cpp     \ 
     53                snxmain.cpp                     \ 
    5354                sonix.cpp 
    5455 
  • juggler/branches/2.2/modules/sonix/snx/snxmain.cpp

    r20500 r20788  
    3434 *************** <auto-copyright.pl END do not edit this line> ***************/ 
    3535 
    36 #if defined(WIN32) || defined(WIN64) 
    37 #include <windows.h> 
     36#include <snx/snxConfig.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 
     51#include <vpr/vpr.h> 
     52 
    4553 
    4654namespace fs = boost::filesystem; 
    4755 
     56#if defined(WIN32) || defined(WIN64) 
    4857/** 
    4958 * Windows DLL entry point function. This ensures that the environment 
     
    6170      case DLL_PROCESS_ATTACH: 
    6271         { 
    63 #if defined(_MSC_VER) && _MSC_VER >= 1400 
    6472            char* env_dir(NULL); 
     73#if defined(_MSC_VER) && _MSC_VER >= 1400 
    6574            size_t len; 
    6675            _dupenv_s(&env_dir, &len, "SNX_BASE_DIR"); 
    6776#else 
    68             const char* env_dir = std::getenv("SNX_BASE_DIR"); 
    69 #endif 
    70  
    71             if ( NULL == env_dir ) 
    72             { 
    73                char tmppath[1024]; 
    74                std::memset(tmppath, 0, sizeof(tmppath)); 
    75                GetModuleFileName(module, tmppath, sizeof(tmppath)); 
    76  
    77                try 
    78                { 
    79                   fs::path dll_path(tmppath, fs::native); 
    80                   fs::path base_dir = dll_path.branch_path().branch_path(); 
     77            env_dir = std::getenv("SNX_BASE_DIR"); 
     78#endif 
     79 
     80            try 
     81            { 
     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(); 
    8195#if (defined(JUGGLER_DEBUG) || defined(SNX_DEBUG)) && ! defined(_DEBUG) 
    8296                  // The debug DLL linked against the release runtime is in 
     
    8498                  base_dir = base_dir.branch_path(); 
    8599#endif 
     100 
    86101                  const std::string base_dir_str = 
    87102                     base_dir.native_directory_string(); 
     103 
    88104#if defined(_MSC_VER) && _MSC_VER >= 1400 
    89105                  _putenv_s("SNX_BASE_DIR", base_dir_str.c_str()); 
     
    94110#endif 
    95111               } 
    96                catch (fs::filesystem_error& ex) 
    97                { 
    98                   std::cerr << "Automatic assignment of SNX_BASE_DIR failed:\n" 
    99                             << ex.what() << std::endl; 
    100                } 
    101             } 
    102 #if defined(_MSC_VER) && _MSC_VER >= 1400 
    103             else 
    104             { 
    105                std::free(env_dir); 
    106             } 
    107 #endif 
     112               else 
     113               { 
     114                  base_dir = fs::path(env_dir, fs::native); 
     115#if defined(_MSC_VER) && _MSC_VER >= 1400 
     116                  std::free(env_dir); 
     117                  env_dir = NULL; 
     118#endif 
     119               } 
     120 
     121#if defined(_MSC_VER) && _MSC_VER >= 1400 
     122               _dupenv_s(&env_dir, &len, "SNX_DATA_DIR"); 
     123#else 
     124               env_dir = std::getenv("SNX_DATA_DIR"); 
     125#endif 
     126 
     127               // If SNX_BASE_DIR is not set, set a default relative to 
     128               // base_dir. 
     129               if ( NULL == env_dir ) 
     130               { 
     131                  fs::path data_dir(base_dir / "share" / "sonix"); 
     132                  const std::string data_dir_str = 
     133                     data_dir.native_directory_string(); 
     134 
     135#if defined(_MSC_VER) && _MSC_VER >= 1400 
     136                  _putenv_s("SNX_DATA_DIR", data_dir_str.c_str()); 
     137#else 
     138                  std::ostringstream env_stream; 
     139                  env_stream << "SNX_DATA_DIR=" << data_dir_str; 
     140                  putenv(env_stream.str().c_str()); 
     141#endif 
     142               } 
     143#if defined(_MSC_VER) && _MSC_VER >= 1400 
     144               else 
     145               { 
     146                  std::free(env_dir); 
     147                  env_dir = NULL; 
     148               } 
     149#endif 
     150            } 
     151            catch (fs::filesystem_error& ex) 
     152            { 
     153               std::cerr << "Automatic assignment of Sonix environment " 
     154                         << "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 
     162            } 
    108163         } 
    109164         break; 
     
    114169   return TRUE; 
    115170} 
    116  
    117  
     171#else 
     172/** 
     173 * Non-Windows shared library constructor. This ensures that the environment 
     174 * variable \c SNX_BASE_DIR is set as soon as this shared library is loaded. 
     175 * If it is not set, then it sets it based on an assumption about the 
     176 * structure of a Sonix installation. More specifically, an assumption is made 
     177 * that this shared library lives in the \c lib subdirectory of the Sonix 
     178 * installation. Therefore, the root of the Sonix installation is the parent 
     179 * of the directory containing this shared library. 
     180 */ 
     181extern "C" void __attribute ((constructor)) snxLibraryInit() 
     182
     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#if defined(__GNUC__) && __GNUC_MAJOR__ < 4 
     194         dladdr((void*) &snxLibraryInit, &info); 
     195#else 
     196         dladdr(reinterpret_cast<void*>(&snxLibraryInit), &info); 
     197#endif 
     198 
     199      // NOTE: dladdr(3) really does return a non-zero value on success. 
     200      if ( 0 != result ) 
     201      { 
     202         try 
     203         { 
     204            fs::path lib_file(info.dli_fname, fs::native); 
     205            lib_file = fs::system_complete(lib_file); 
     206 
     207#if defined(VPR_OS_IRIX) && defined(_ABIN32) 
     208            const std::string bit_suffix("32"); 
     209#elif defined(VPR_OS_IRIX) && defined(_ABI64) || \ 
     210      defined(VPR_OS_Linux) && defined(__x86_64__) 
     211            const std::string bit_suffix("64"); 
     212#else 
     213            const std::string bit_suffix(""); 
     214#endif 
     215 
     216            // Get the directory containing this shared library. 
     217            const fs::path lib_path = lib_file.branch_path(); 
     218 
     219            // Start the search for the root of the Sonix installation in the 
     220            // parent of the directory containing this shared library. 
     221            base_dir = lib_path.branch_path(); 
     222 
     223            // Use the lib subdirectory to figure out when we have found the 
     224            // root of the Sonix installation tree. 
     225            const fs::path lib_subdir(std::string("lib") + bit_suffix); 
     226 
     227            bool found(false); 
     228            while ( ! found && ! base_dir.empty() ) 
     229            { 
     230               try 
     231               { 
     232                  if ( ! fs::exists(base_dir / lib_subdir) ) 
     233                  { 
     234                     base_dir = base_dir.branch_path(); 
     235                  } 
     236                  else 
     237                  { 
     238                     found = true; 
     239                  } 
     240               } 
     241               catch (fs::filesystem_error&) 
     242               { 
     243                  base_dir = base_dir.branch_path(); 
     244               } 
     245            } 
     246 
     247            if ( found ) 
     248            { 
     249               setenv("SNX_BASE_DIR", 
     250                      base_dir.native_directory_string().c_str(), 1); 
     251            } 
     252         } 
     253         catch (fs::filesystem_error& ex) 
     254         { 
     255            std::cerr << "Automatic assignment of SNX_BASE_DIR failed:\n" 
     256                      << ex.what() << std::endl; 
     257         } 
     258      } 
     259   } 
     260   else 
     261   { 
     262      base_dir = fs::path(env_dir, fs::native); 
     263   } 
     264 
     265   if ( ! base_dir.empty() ) 
     266   { 
     267      // If base_dir were empty, this would result in data_dir being relative 
     268      // to the current working directory. 
     269      const fs::path data_dir = base_dir / SNX_SHARE_DIR; 
     270 
     271      // We use the overwrite value of 0 as a way around testing whether the 
     272      // environment variable is already set. 
     273      setenv("SNX_DATA_DIR", data_dir.native_directory_string().c_str(), 0); 
     274   } 
     275
    118276#endif  /* defined(WIN32) || defined(WIN64) */