← Back to team overview

kicad-developers team mailing list archive

Environment variables patch.

 

As you all know, we have had many users struggle with setting up the
environment variables used in the footprint library table and 3D model
code.  The attached patch sets the process environment variables
KIGITHUB and KISYS3DMOD.  KIGITHUB should work for all platforms since
it is the URL for the kicad library repos on GitHub which is always
https://github.com/KiCad.  KISYS3DMOD uses the 3D model library install
path created during the CMake configuration.  This works fine for me on
windows and linux.  OSX may be a different story due to the way the
bundle paths are defined.  Would on of the OSX devs please test this and
give me some feedback?  Please don't forget to remove these environment
variables that are set elsewhere.  Otherwise, the environment variable
in this patch will be ignored.  My guess is that if we get the default
environment variable set correctly that it would solve the problem for
the largest percentage of users until I get some time to work on the
dialog to edit them.  I will also add KISYSMOD to this code once have
the default OSX paths established.

Thanks,

Wayne
=== modified file 'CMakeModules/config.h.cmake'
--- CMakeModules/config.h.cmake	2014-10-31 20:44:09 +0000
+++ CMakeModules/config.h.cmake	2015-01-17 01:34:11 +0000
@@ -61,6 +61,9 @@
 /// The install prefix defined in CMAKE_INSTALL_PREFIX.
 #define DEFAULT_INSTALL_PATH            "@CMAKE_INSTALL_PREFIX@"
 
+/// The install prefex used for KiCad's libraries.
+#define KICAD_DATA_PATH                 "@CMAKE_INSTALL_PREFIX@/@KICAD_DATA@"
+
 /// When defined, build the GITHUB_PLUGIN for pcbnew.
 #cmakedefine BUILD_GITHUB_PLUGIN
 

=== modified file 'common/pgm_base.cpp'
--- common/pgm_base.cpp	2015-01-07 15:04:57 +0000
+++ common/pgm_base.cpp	2015-01-16 21:09:54 +0000
@@ -51,14 +51,15 @@
 #include <confirm.h>
 
 
-#define KICAD_COMMON                    wxT( "kicad_common" )
+#define KICAD_COMMON                     wxT( "kicad_common" )
 
 // some key strings used to store parameters in KICAD_COMMON
 
-const wxChar PGM_BASE::workingDirKey[]  =  wxT( "WorkingDir" );     // public
+const wxChar PGM_BASE::workingDirKey[] = wxT( "WorkingDir" );     // public
 
-static const wxChar languageCfgKey[] =  wxT( "LanguageID" );
-static const wxChar kicadFpLibPath[] =  wxT( "KicadFootprintLibraryPath" );
+static const wxChar languageCfgKey[]   = wxT( "LanguageID" );
+static const wxChar kicadFpLibPath[]   = wxT( "KicadFootprintLibraryPath" );
+static const wxChar pathEnvVariables[] = wxT( "EnvironmentVariables" );
 
 
 /**
@@ -353,7 +354,8 @@
 
     wxInitAllImageHandlers();
 
-    m_pgm_checker = new wxSingleInstanceChecker( pgm_name.GetName().Lower() + wxT( "-" ) + wxGetUserId(), GetKicadLockFilePath() );
+    m_pgm_checker = new wxSingleInstanceChecker( pgm_name.GetName().Lower() + wxT( "-" ) +
+                                                 wxGetUserId(), GetKicadLockFilePath() );
 
     if( m_pgm_checker->IsAnotherRunning() )
     {
@@ -393,6 +395,16 @@
 
     SetLanguagePath();
 
+    // Useful local environment variable settings.
+    m_local_env_vars[ wxString( wxT( "KIGITHUB" ) ) ] =
+                      wxString( wxT( "https://github.com/KiCad"; ) );
+
+    wxFileName tmpFileName;
+    tmpFileName.AssignDir( wxString( wxT( KICAD_DATA_PATH ) ) );
+    tmpFileName.AppendDir( wxT( "modules" ) );
+    tmpFileName.AppendDir( wxT( "packages3d" ) );
+    m_local_env_vars[ wxString( wxT( "KISYS3DMOD" ) ) ] = tmpFileName.GetPath();
+
     // OS specific instantiation of wxConfigBase derivative:
     m_common_settings = GetNewConfig( KICAD_COMMON );
 
@@ -473,6 +485,28 @@
     }
 
     m_editor_name = m_common_settings->Read( wxT( "Editor" ) );
+
+    wxString entry, oldPath;
+    wxArrayString entries;
+    long index = 0L;
+
+    oldPath = m_common_settings->GetPath();
+    m_common_settings->SetPath( pathEnvVariables );
+
+    while( m_common_settings->GetNextEntry( entry, index ) )
+    {
+        wxLogDebug( wxT( "Enumerating over entry %s, %ld." ), GetChars( entry ), index );
+        entries.Add( entry );
+    }
+
+    for( unsigned i = 0;  i < entries.GetCount();  i++ )
+    {
+        wxString val = m_common_settings->Read( entries[i], wxEmptyString );
+        m_local_env_vars[ entries[i]  ] = val;
+        SetLocalEnvVariable( entries[i], val );
+    }
+
+    m_common_settings->SetPath( oldPath );
 }
 
 
@@ -485,6 +519,20 @@
         wxString cur_dir = wxGetCwd();
 
         m_common_settings->Write( workingDirKey, cur_dir );
+
+        // Save the local environment variables.
+        m_common_settings->SetPath( pathEnvVariables );
+
+        for( std::map<wxString, wxString>::iterator it = m_local_env_vars.begin();
+             it != m_local_env_vars.end();
+             ++it )
+        {
+            wxLogDebug( wxT( "Saving environment varaiable config entry %s as %s" ),
+                        GetChars( it->first ),  GetChars( it->second ) );
+            m_common_settings->Write( it->first, it->second );
+        }
+
+        m_common_settings->SetPath( wxT( ".." ) );
     }
 }
 
@@ -659,3 +707,21 @@
     }
 }
 
+
+bool PGM_BASE::SetLocalEnvVariable( const wxString& aName, const wxString& aValue )
+{
+    wxString env;
+
+    // Check to see if the environment variable is already set.
+    if( wxGetEnv( aName, &env ) )
+    {
+        wxLogDebug( wxT( "Environment variable %s already set to %s." ),
+                    GetChars( aName ), GetChars( env ) );
+        return env == aValue;
+    }
+
+    wxLogDebug( wxT( "Setting local environment variable %s to %s." ),
+                GetChars( aName ), GetChars( aValue ) );
+
+    return wxSetEnv( aName, aValue );
+}

=== modified file 'include/pgm_base.h'
--- include/pgm_base.h	2014-09-07 19:01:26 +0000
+++ include/pgm_base.h	2015-01-16 21:09:54 +0000
@@ -31,6 +31,7 @@
 #ifndef  PGM_BASE_H_
 #define  PGM_BASE_H_
 
+#include <map>
 #include <wx/filename.h>
 #include <search_stack.h>
 #include <wx/gdicmn.h>
@@ -109,7 +110,7 @@
     /**
      * Function UseSystemPdfBrowser
      * returns true if the PDF browser is the default (system) PDF browser
-     * and false if the PDF browser is the prefered (selected) browser, else
+     * and false if the PDF browser is the preferred (selected) browser, else
      * returns false if there is no selected browser
      */
     VTBL_ENTRY bool UseSystemPdfBrowser() const
@@ -119,7 +120,7 @@
 
     /**
      * Function ForceSystemPdfBrowser
-     * forces the use of system PDF browser, even if a preferend PDF browser is set.
+     * forces the use of system PDF browser, even if a preferred PDF browser is set.
      */
     VTBL_ENTRY void ForceSystemPdfBrowser( bool aFlg ) { m_use_system_pdf_browser = aFlg; }
 
@@ -169,6 +170,22 @@
     VTBL_ENTRY void WritePdfBrowserInfos();
 
     /**
+     * Function SetLocalEnvVariable
+     *
+     * Sets the environment variable \a aName to \a aValue.
+     *
+     * This function first checks to see if the environment variable \a aName is already
+     * defined.  If it is not defined, then the environment variable \a aName is set to
+     * a value.  Otherwise, the environment variable is left unchanged.  This allows the user
+     * to override environment variables for testing purposes.
+     *
+     * @param aName is a wxString containing the environment variable name.
+     * @param aValue is a wxString containing the environment variable value.
+     * @return true if the environment variable \a Name was set to \a aValue.
+     */
+    VTBL_ENTRY bool SetLocalEnvVariable( const wxString& aName, const wxString& aValue );
+
+    /**
      * Function App
      * returns a bare naked wxApp, which may come from wxPython, SINGLE_TOP, or kicad.exe.
      * Use this function instead of wxGetApp().
@@ -247,6 +264,10 @@
     wxString        m_editor_name;
     wxSize          m_help_size;
 
+    /// Local environment variable expansion settings such as KIGITHUB, KISYSMOD, and KISYS3DMOD.
+    /// library table.
+    std::map<wxString, wxString>  m_local_env_vars;
+
     wxApp*          m_wx_app;
 
     // The PGM_* classes can have difficulties at termination if they


Follow ups