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 <search_stack.h>
30 #include <pgm_base.h>
31 #include <config.h> // to define DEFAULT_INSTALL_PATH
32 
33 
34 // put your best guesses in here, send the computer on a wild goose chase, its
35 // got nothing else to do.
36 
37 void SystemDirsAppend( SEARCH_STACK* aSearchStack )
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:65
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Function SystemDirsAppend appends system places to aSearchStack in a platform specific way...
see class PGM_BASE
The common library.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Function AddPaths insert or append path(s)