KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The KiCad Developers, see AUTHORS.TXT for contributors.
6 * @author Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <wx/stdpaths.h>
23
24#include <systemdirsappend.h>
25#include <common.h>
27#include <search_stack.h>
28#include <pgm_base.h>
29#include <config.h> // to define DEFAULT_INSTALL_PATH
30#include <paths.h>
31
32// put your best guesses in here, send the computer on a wild goose chase, its
33// got nothing else to do.
34
35void SystemDirsAppend( SEARCH_STACK* aSearchStack )
36{
37 // No clearing is done here, the most general approach is NOT to assume that
38 // our appends will be the only thing in the stack. This function has no
39 // knowledge of caller's intentions.
40
41 // wxPathList::AddEnvList() is broken, use SEARCH_STACK::AddPaths().
42 // SEARCH_STACK::AddPaths() will verify readability and existence of
43 // each directory before adding.
44 SEARCH_STACK maybe;
45
46 // User environment variable path is the first search path. Chances are
47 // if the user is savvy enough to set an environment variable they know
48 // what they are doing. It should take precedence over anything else.
49 // Otherwise don't set it.
50 maybe.AddPaths( wxGetenv( wxT( "KICAD" ) ) );
51
52#ifdef __WXMAC__
53 // Add the directory for the user-dependent, program specific data files.
54 maybe.AddPaths( PATHS::GetOSXKicadUserDataDir() );
55
56 // Global machine specific application data
57 maybe.AddPaths( PATHS::GetOSXKicadMachineDataDir() );
58
59 // Global application specific data files inside bundle
60 maybe.AddPaths( PATHS::GetOSXKicadDataDir() );
61#else
62 // This is from CMAKE_INSTALL_PREFIX.
63 // Useful when KiCad is installed by `make install`.
64 // Use as second ranked place.
65 maybe.AddPaths( wxT( DEFAULT_INSTALL_PATH ) );
66
67#ifdef __WXGTK__
68 // On Linux, the stock EDA library data install path can be redefined via
69 // KICAD_LIBRARY_DATA, otherwise KICAD_DATA will be used.
70 // Useful when multiple versions of KiCad are installed in parallel.
72#endif
73
74 // Add the directory for the user-dependent, program specific data files.
75 // According to wxWidgets documentation:
76 // Unix: ~/.appname
77 // Windows: C:\Documents and Settings\username\Application Data\appname
79
80 {
81 // Should be full path to this program executable.
82 wxString bin_dir = Pgm().GetExecutablePath();
83
84#if defined(_WIN32)
85 // bin_dir uses unix path separator. So to parse with wxFileName
86 // use windows separator, especially important for server inclusion:
87 // like: \\myserver\local_path .
88 bin_dir.Replace( wxFileName::GetPathSeparator( wxPATH_UNIX ),
89 wxFileName::GetPathSeparator( wxPATH_WIN ) );
90#endif
91
92 wxFileName bin_fn( bin_dir, wxEmptyString );
93
94 // Dir of the global (not user-specific), application specific, data files.
95 // From wx docs:
96 // Unix: prefix/share/appname
97 // Windows: the directory where the executable file is located
98 // Mac: appname.app/Contents/SharedSupport bundle subdirectory
99 wxString data_dir = wxStandardPaths::Get().GetDataDir();
100
101 if( bin_fn.GetPath() != data_dir )
102 {
103 // add data_dir if it is different from the bin_dir
104 maybe.AddPaths( data_dir );
105 }
106
107 // Up one level relative to binary path with "share" appended below.
108 bin_fn.RemoveLastDir();
109 maybe.AddPaths( bin_fn.GetPath() );
110 }
111
112 /* The normal OS program file install paths allow for a binary to be
113 * installed in a different path from the library files. This is
114 * useful for development purposes so the library and documentation
115 * files do not need to be installed separately. If someone can
116 * figure out a way to implement this without #ifdef, please do.
117 */
118#if defined(_WIN32)
119 maybe.AddPaths( wxGetenv( wxT( "PROGRAMFILES" ) ) );
120#else
121 maybe.AddPaths( wxGetenv( wxT( "PATH" ) ) );
122#endif
123#endif
124
125#if defined(DEBUG) && 0
126 maybe.Show( "maybe wish list" );
127#endif
128
129 // Append 1) kicad, 2) kicad/share, 3) share, and 4) share/kicad to each
130 // possible base path in 'maybe'. Since SEARCH_STACK::AddPaths() will verify
131 // readability and existence of each directory, not all of these will be
132 // actually appended.
133 for( unsigned i = 0; i < maybe.GetCount(); ++i )
134 {
135 wxFileName fn( maybe[i], wxEmptyString );
136
137#ifndef __WXMAC__
138 if( fn.GetPath().AfterLast( fn.GetPathSeparator() ) == wxT( "bin" ) )
139 {
140 fn.RemoveLastDir();
141
142 if( !fn.GetDirCount() )
143 continue; // at least on linux
144 }
145#endif
146
147 aSearchStack->AddPaths( fn.GetPath() );
148
149#ifndef __WXMAC__
150 fn.AppendDir( wxT( "kicad" ) );
151 aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/kicad
152
153 fn.AppendDir( wxT( "share" ) );
154 aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/kicad/share
155
156 fn.RemoveLastDir(); // ../ clear share
157 fn.RemoveLastDir(); // ../ clear kicad
158
159 fn.AppendDir( wxT( "share" ) );
160 aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/share
161
162 fn.AppendDir( wxT( "kicad" ) );
163 aSearchStack->AddPaths( fn.GetPath() ); // add maybe[i]/share/kicad
164#endif
165 }
166
167#if defined(DEBUG) && 0
168 // final results:
169 aSearchStack->Show( __func__ );
170#endif
171}
172
173
175{
176 SEARCH_STACK bases;
177
178 SystemDirsAppend( &bases );
179 aDst->Clear();
180
181 for( unsigned i = 0; i < bases.GetCount(); ++i )
182 {
183 wxFileName fn( bases[i], wxEmptyString );
184
185 // Add schematic library file path to search path list.
186 // we must add <kicad path>/library and <kicad path>/library/doc
187 if( aId == KIWAY::FACE_SCH )
188 {
189 // Add schematic doc file path (library/doc) to search path list.
190
191 fn.AppendDir( wxT( "library" ) );
192 aDst->AddPaths( fn.GetPath() );
193
194 fn.AppendDir( wxT( "doc" ) );
195 aDst->AddPaths( fn.GetPath() );
196
197 fn.RemoveLastDir();
198 fn.RemoveLastDir(); // "../../" up twice, removing library/doc/
199
200 fn.AppendDir( wxT( "symbols" ) );
201 aDst->AddPaths( fn.GetPath() );
202
203 fn.AppendDir( wxT( "doc" ) );
204 aDst->AddPaths( fn.GetPath() );
205
206 fn.RemoveLastDir();
207 fn.RemoveLastDir(); // "../../" up twice, removing symbols/doc/
208 }
209
210 // Add PCB library file path to search path list.
211 if( aId == KIWAY::FACE_PCB || aId == KIWAY::FACE_CVPCB )
212 {
213 fn.AppendDir( wxT( "modules" ) );
214 aDst->AddPaths( fn.GetPath() );
215 fn.RemoveLastDir();
216
217 fn.AppendDir( wxT( "footprints" ) );
218 aDst->AddPaths( fn.GetPath() );
219 fn.RemoveLastDir();
220
221 // Add 3D module library file path to search path list.
222 fn.AppendDir( wxT( "3dmodels" ) );
223 aDst->AddPaths( fn.GetPath() );
224 fn.RemoveLastDir();
225 }
226
227 // Add KiCad template file path to search path list.
228 fn.AppendDir( wxT( "template" ) );
229 aDst->AddPaths( fn.GetPath() );
230 }
231
232#ifndef __WXMAC__
233 aDst->AddPaths( wxT( "/usr/local/share" ) );
234#endif
235}
FACE_T
Known KIFACE implementations.
Definition kiway.h:317
@ FACE_SCH
eeschema DSO
Definition kiway.h:318
@ FACE_PCB
pcbnew DSO
Definition kiway.h:319
@ FACE_CVPCB
Definition kiway.h:320
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition paths.cpp:281
virtual const wxString & GetExecutablePath() const
Definition pgm_base.cpp:854
Look for files in a number of paths.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).
The common library.
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
void GlobalPathsAppend(SEARCH_STACK *aDst, KIWAY::FACE_T aId)
Initialize aDst SEARCH_STACK with KIFACE (DSO) specific settings.
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Append system places to aSearchStack in a platform specific way and pertinent to KiCad programs.
System directories search utilities.