KiCad PCB EDA Suite
systemdirsappend.cpp File Reference
#include <wx/stdpaths.h>
#include <common.h>
#include <search_stack.h>
#include <pgm_base.h>
#include <config.h>

Go to the source code of this file.

Functions

void SystemDirsAppend (SEARCH_STACK *aSearchStack)
 Function SystemDirsAppend appends system places to aSearchStack in a platform specific way, and pertinent to KiCad programs. More...
 

Function Documentation

void SystemDirsAppend ( SEARCH_STACK aSearchStack)

Function SystemDirsAppend appends system places to aSearchStack in a platform specific way, and pertinent to KiCad programs.

It seems to be a place to collect bad ideas and keep them out of view.

Definition at line 37 of file systemdirsappend.cpp.

References SEARCH_STACK::AddPaths(), PGM_BASE::GetExecutablePath(), and Pgm().

Referenced by PGM_KICAD::OnPgmInit(), PGM_BASE::SetLanguagePath(), and setSearchPaths().

38 {
39  // No clearing is done here, the most general approach is NOT to assume that
40  // our appends will be the only thing in the stack. This function has no
41  // knowledge of caller's intentions.
42 
43  // wxPathList::AddEnvList() is broken, use SEARCH_STACK::AddPaths().
44  // SEARCH_STACK::AddPaths() will verify readability and existence of
45  // each directory before adding.
46  SEARCH_STACK maybe;
47 
48  // User environment variable path is the first search path. Chances are
49  // if the user is savvy enough to set an environment variable they know
50  // what they are doing. It should take precedence over anything else.
51  // Otherwise don't set it.
52  maybe.AddPaths( wxGetenv( wxT( "KICAD" ) ) );
53 
54 #ifdef __WXMAC__
55  // Add the directory for the user-dependent, program specific data files.
56  maybe.AddPaths( GetOSXKicadUserDataDir() );
57 
58  // Global machine specific application data
59  maybe.AddPaths( GetOSXKicadMachineDataDir() );
60 
61  // Global application specific data files inside bundle
62  maybe.AddPaths( GetOSXKicadDataDir() );
63 #else
64  // This is from CMAKE_INSTALL_PREFIX.
65  // Useful when KiCad is installed by `make install`.
66  // Use as second ranked place.
67  maybe.AddPaths( wxT( DEFAULT_INSTALL_PATH ) );
68 
69  // Add the directory for the user-dependent, program specific data files.
70  // According to wxWidgets documentation:
71  // Unix: ~/.appname
72  // Windows: C:\Documents and Settings\username\Application Data\appname
73  maybe.AddPaths( wxStandardPaths::Get().GetUserDataDir() );
74 
75  {
76  // Should be full path to this program executable.
77  wxString bin_dir = Pgm().GetExecutablePath();
78 
79 #if defined(__MINGW32__)
80  // bin_dir uses unix path separator. So to parse with wxFileName
81  // use windows separator, especially important for server inclusion:
82  // like: \\myserver\local_path .
83  bin_dir.Replace( wxFileName::GetPathSeparator( wxPATH_UNIX ),
84  wxFileName::GetPathSeparator( wxPATH_WIN ) );
85 #endif
86 
87  wxFileName bin_fn( bin_dir, wxEmptyString );
88 
89  // Dir of the global (not user-specific), application specific, data files.
90  // From wx docs:
91  // Unix: prefix/share/appname
92  // Windows: the directory where the executable file is located
93  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
94  wxString data_dir = wxStandardPaths::Get().GetDataDir();
95 
96  if( bin_fn.GetPath() != data_dir )
97  {
98  // add data_dir if it is different from the bin_dir
99  maybe.AddPaths( data_dir );
100  }
101 
102  // Up one level relative to binary path with "share" appended below.
103  bin_fn.RemoveLastDir();
104  maybe.AddPaths( bin_fn.GetPath() );
105  }
106 
107  /* The normal OS program file install paths allow for a binary to be
108  * installed in a different path from the library files. This is
109  * useful for development purposes so the library and documentation
110  * files do not need to be installed separately. If someone can
111  * figure out a way to implement this without #ifdef, please do.
112  */
113 #if defined(__MINGW32__)
114  maybe.AddPaths( wxGetenv( wxT( "PROGRAMFILES" ) ) );
115 #else
116  maybe.AddPaths( wxGetenv( wxT( "PATH" ) ) );
117 #endif
118 #endif
119 
120 #if defined(DEBUG) && 0
121  maybe.Show( "maybe wish list" );
122 #endif
123 
124  // Append 1) kicad, 2) kicad/share, 3) share, and 4) share/kicad to each
125  // possible base path in 'maybe'. Since SEARCH_STACK::AddPaths() will verify
126  // readability and existence of each directory, not all of these will be
127  // actually appended.
128  for( unsigned i = 0; i < maybe.GetCount(); ++i )
129  {
130  wxFileName fn( maybe[i], wxEmptyString );
131 
132 #ifndef __WXMAC__
133  if( fn.GetPath().AfterLast( fn.GetPathSeparator() ) == wxT( "bin" ) )
134  {
135  fn.RemoveLastDir();
136 
137  if( !fn.GetDirCount() )
138  continue; // at least on linux
139  }
140 #endif
141 
142  aSearchStack->AddPaths( fn.GetPath() );
143 
144 #ifndef __WXMAC__
145  fn.AppendDir( wxT( "kicad" ) );
146  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/kicad
147 
148  fn.AppendDir( wxT( "share" ) );
149  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/kicad/share
150 
151  fn.RemoveLastDir(); // ../ clear share
152  fn.RemoveLastDir(); // ../ clear kicad
153 
154  fn.AppendDir( wxT( "share" ) );
155  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/share
156 
157  fn.AppendDir( wxT( "kicad" ) );
158  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/share/kicad
159 #endif
160  }
161 
162 #if defined(DEBUG) && 0
163  // final results:
164  aSearchStack->Show( __func__ );
165 #endif
166 }
VTBL_ENTRY const wxString & GetExecutablePath() const
Definition: pgm_base.h:173
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:63
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
void AddPaths(const wxString &aPaths, int aIndex=-1)
Function AddPaths insert or append path(s)