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

Go to the source code of this file.

Functions

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

Function Documentation

◆ SystemDirsAppend()

void SystemDirsAppend ( SEARCH_STACK aSearchStack)

Append system places to aSearchStack in a platform specific way and pertinent to KiCad programs.

Definition at line 38 of file systemdirsappend.cpp.

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

References SEARCH_STACK::AddPaths(), KIPLATFORM::ENV::GetDocumentsPath(), and Pgm().

Referenced by PGM_BASE::InitPgm(), FP_LIB_TABLE::LoadGlobalTable(), SYMBOL_LIB_TABLE::LoadGlobalTable(), PGM_KICAD::OnPgmInit(), PGM_BASE::SetLanguagePath(), setSearchPaths(), and DIALOG_GLOBAL_LIB_TABLE_CONFIG::TransferDataToWindow().