KiCad PCB EDA Suite
systemdirsappend.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014 CERN
5  * Copyright (C) 2014 KiCad Developers, see CHANGELOG.TXT for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <wx/stdpaths.h>
27 
28 #include <common.h>
29 #include <kiplatform/environment.h>
30 #include <search_stack.h>
31 #include <pgm_base.h>
32 #include <config.h> // to define DEFAULT_INSTALL_PATH
33 #include <paths.h>
34 
35 // put your best guesses in here, send the computer on a wild goose chase, its
36 // got nothing else to do.
37 
38 void SystemDirsAppend( SEARCH_STACK* aSearchStack )
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 #ifdef __WXGTK__
71  // On Linux, the stock EDA library data install path can be redefined via
72  // KICAD_LIBRARY_DATA, otherwise KICAD_DATA will be used.
73  // Useful when multiple versions of KiCad are installed in parallel.
75 #endif
76 
77  // Add the directory for the user-dependent, program specific data files.
78  // According to wxWidgets documentation:
79  // Unix: ~/.appname
80  // Windows: C:\Documents and Settings\username\Application Data\appname
82 
83  {
84  // Should be full path to this program executable.
85  wxString bin_dir = Pgm().GetExecutablePath();
86 
87 #if defined(_WIN32)
88  // bin_dir uses unix path separator. So to parse with wxFileName
89  // use windows separator, especially important for server inclusion:
90  // like: \\myserver\local_path .
91  bin_dir.Replace( wxFileName::GetPathSeparator( wxPATH_UNIX ),
92  wxFileName::GetPathSeparator( wxPATH_WIN ) );
93 #endif
94 
95  wxFileName bin_fn( bin_dir, wxEmptyString );
96 
97  // Dir of the global (not user-specific), application specific, data files.
98  // From wx docs:
99  // Unix: prefix/share/appname
100  // Windows: the directory where the executable file is located
101  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
102  wxString data_dir = wxStandardPaths::Get().GetDataDir();
103 
104  if( bin_fn.GetPath() != data_dir )
105  {
106  // add data_dir if it is different from the bin_dir
107  maybe.AddPaths( data_dir );
108  }
109 
110  // Up one level relative to binary path with "share" appended below.
111  bin_fn.RemoveLastDir();
112  maybe.AddPaths( bin_fn.GetPath() );
113  }
114 
115  /* The normal OS program file install paths allow for a binary to be
116  * installed in a different path from the library files. This is
117  * useful for development purposes so the library and documentation
118  * files do not need to be installed separately. If someone can
119  * figure out a way to implement this without #ifdef, please do.
120  */
121 #if defined(_WIN32)
122  maybe.AddPaths( wxGetenv( wxT( "PROGRAMFILES" ) ) );
123 #else
124  maybe.AddPaths( wxGetenv( wxT( "PATH" ) ) );
125 #endif
126 #endif
127 
128 #if defined(DEBUG) && 0
129  maybe.Show( "maybe wish list" );
130 #endif
131 
132  // Append 1) kicad, 2) kicad/share, 3) share, and 4) share/kicad to each
133  // possible base path in 'maybe'. Since SEARCH_STACK::AddPaths() will verify
134  // readability and existence of each directory, not all of these will be
135  // actually appended.
136  for( unsigned i = 0; i < maybe.GetCount(); ++i )
137  {
138  wxFileName fn( maybe[i], wxEmptyString );
139 
140 #ifndef __WXMAC__
141  if( fn.GetPath().AfterLast( fn.GetPathSeparator() ) == wxT( "bin" ) )
142  {
143  fn.RemoveLastDir();
144 
145  if( !fn.GetDirCount() )
146  continue; // at least on linux
147  }
148 #endif
149 
150  aSearchStack->AddPaths( fn.GetPath() );
151 
152 #ifndef __WXMAC__
153  fn.AppendDir( wxT( "kicad" ) );
154  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/kicad
155 
156  fn.AppendDir( wxT( "share" ) );
157  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/kicad/share
158 
159  fn.RemoveLastDir(); // ../ clear share
160  fn.RemoveLastDir(); // ../ clear kicad
161 
162  fn.AppendDir( wxT( "share" ) );
163  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/share
164 
165  fn.AppendDir( wxT( "kicad" ) );
166  aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/share/kicad
167 #endif
168  }
169 
170 #if defined(DEBUG) && 0
171  // final results:
172  aSearchStack->Show( __func__ );
173 #endif
174 }
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 SystemDirsAppend(SEARCH_STACK *aSearchStack)
Append system places to aSearchStack in a platform specific way and pertinent to KiCad programs.
see class PGM_BASE
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:186
The common library.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).