KiCad PCB EDA Suite
Loading...
Searching...
No Matches
paths.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) 2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <wx/filename.h>
21#include <wx/stdpaths.h>
22#include <wx/string.h>
23#include <wx/utils.h>
24
26#include <paths.h>
27#include <pgm_base.h>
29#include <config.h>
30
31// lowercase or pretty case depending on platform
32#if defined( __WXMAC__ ) || defined( __WXMSW__ )
33#define KICAD_PATH_STR wxT( "KiCad" )
34#else
35#define KICAD_PATH_STR wxT( "kicad" )
36#endif
37
38
39void PATHS::getUserDocumentPath( wxFileName& aPath )
40{
41 wxString envPath;
42
43 if( wxGetEnv( wxT( "KICAD_DOCUMENTS_HOME" ), &envPath ) )
44 aPath.AssignDir( envPath );
45 else
46 aPath.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() );
47
48 aPath.AppendDir( KICAD_PATH_STR );
49 aPath.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
50}
51
52
54{
55 wxFileName tmp;
57
58 tmp.AppendDir( wxT( "plugins" ) );
59
60 return tmp.GetPath();
61}
62
63
65{
66 wxFileName tmp;
67
68 tmp.AssignDir( PATHS::GetUserPluginsPath() );
69 tmp.AppendDir( wxT( "3d" ) );
70
71 return tmp.GetPath();
72}
73
74
76{
77 wxFileName tmp;
79
80 tmp.AppendDir( wxT( "scripting" ) );
81
82 return tmp.GetPath();
83}
84
85
87{
88 wxFileName tmp;
90
91 tmp.AppendDir( wxT( "template" ) );
92
93 return tmp.GetPathWithSep();
94}
95
96
98{
99 wxFileName tmp;
100 getUserDocumentPath( tmp );
101
102 tmp.AppendDir( wxT( "symbols" ) );
103
104 return tmp.GetPath();
105}
106
107
109{
110 wxFileName tmp;
111 getUserDocumentPath( tmp );
112
113 tmp.AppendDir( wxT( "footprints" ) );
114
115 return tmp.GetPath();
116}
117
118
120{
121 wxFileName tmp;
122 getUserDocumentPath( tmp );
123
124 tmp.AppendDir( wxT( "3dmodels" ) );
125
126 return tmp.GetPath();
127}
128
130{
131 wxFileName tmp;
132 getUserDocumentPath( tmp );
133
134 tmp.AppendDir( wxT( "3rdparty" ) );
135
136 return tmp.GetPath();
137}
138
140{
141 wxFileName tmp;
142 getUserDocumentPath( tmp );
143
144 tmp.AppendDir( wxT( "projects" ) );
145
146 return tmp.GetPath();
147}
148
149
150wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir )
151{
152 wxString path;
153
154 if( aRespectRunFromBuildDir && wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
155 {
156 // Allow debugging from build dir by placing relevant files/folders in the build root
157#if defined( __WXMAC__ )
158 wxFileName fn = wxStandardPaths::Get().GetExecutablePath();
159
160 fn.RemoveLastDir();
161 fn.RemoveLastDir();
162 fn.RemoveLastDir();
163 fn.RemoveLastDir();
164 path = fn.GetPath();
165#elif defined( __WXMSW__ )
166 path = getWindowsKiCadRoot();
167#else
168 path = Pgm().GetExecutablePath() + wxT( ".." );
169#endif
170 }
171 else
172 {
173#if defined( __WXMAC__ )
174 path = GetOSXKicadDataDir();
175#elif defined( __WXMSW__ )
176 path = getWindowsKiCadRoot() + wxT( "share/kicad" );
177#else
178 path = wxString::FromUTF8Unchecked( KICAD_DATA );
179#endif
180 }
181
182 return path;
183}
184
185
187{
188 wxString path;
189
190#if defined( __WXMAC__ )
191 path = GetOSXKicadMachineDataDir();
192#elif defined( __WXMSW__ )
193 path = GetStockDataPath( false );
194#else
195 path = wxString::FromUTF8Unchecked( KICAD_LIBRARY_DATA );
196#endif
197
198 return path;
199}
200
201
203{
204 wxString path;
205
206 path = GetStockEDALibraryPath() + wxT( "/symbols" );
207
208 return path;
209}
210
211
213{
214 wxString path;
215
216 path = GetStockEDALibraryPath() + wxT( "/footprints" );
217
218 return path;
219}
220
221
223{
224 wxString path;
225
226 path = GetStockEDALibraryPath() + wxT( "/3dmodels" );
227
228 return path;
229}
230
231
233{
234 wxString path;
235
236 path = GetStockDataPath() + wxT( "/scripting" );
237
238 return path;
239}
240
241
243{
244 wxString path;
245
246 path = GetStockEDALibraryPath() + wxT( "/template" );
247
248 return path;
249}
250
251
253{
254 wxString path;
255
256 path = GetStockDataPath() + wxT( "/internat" );
257
258 return path;
259}
260
261
263{
264 wxFileName fn;
265
266#if defined( __WXMSW__ )
267 fn.AssignDir( Pgm().GetExecutablePath() );
268 fn.AppendDir( wxT( "scripting" ) );
269#else
270 fn.AssignDir( PATHS::GetStockDataPath( false ) );
271#endif
272 fn.AppendDir( wxT( "plugins" ) );
273
274 return fn.GetPathWithSep();
275}
276
277
279{
280 wxFileName fn;
281
282#ifdef __WXGTK__
283 // KICAD_PLUGINDIR = CMAKE_INSTALL_FULL_LIBDIR path is the absolute path
284 // corresponding to the install path used for constructing KICAD_USER_PLUGIN
285 wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
286 fn.Assign( tfname, "" );
287 fn.AppendDir( wxT( "kicad" ) );
288 fn.AppendDir( wxT( "plugins" ) );
289#elif defined( __WXMAC__ )
290 fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
291#else
292 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
293 {
294 fn.AssignDir( getWindowsKiCadRoot() );
295 }
296 else
297 {
298 fn.AssignDir( Pgm().GetExecutablePath() );
299 }
300
301 fn.AppendDir( wxT( "plugins" ) );
302#endif
303
304 fn.AppendDir( wxT( "3d" ) );
305
306 return fn.GetPathWithSep();
307}
308
309
311{
312 wxFileName fn;
313
314 fn.AssignDir( PATHS::GetStockDataPath( false ) );
315 fn.AppendDir( wxT( "demos" ) );
316
317 return fn.GetPathWithSep();
318}
319
320
322{
323 wxString envPath;
324 wxFileName tmp;
325
326 tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
327
328 // Use KICAD_CACHE_HOME to allow the user to force a specific cache path.
329 if( wxGetEnv( wxT( "KICAD_CACHE_HOME" ), &envPath ) && !envPath.IsEmpty() )
330 {
331 // Override the assignment above with KICAD_CACHE_HOME
332 tmp.AssignDir( envPath );
333 }
334
335 tmp.AppendDir( KICAD_PATH_STR );
336 tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
337
338 return tmp.GetPathWithSep();
339}
340
341
343{
344 wxString path;
345
346#if defined( __WXMAC__ )
347 path = GetOSXKicadDataDir();
348#elif defined( __WXMSW__ )
349 path = getWindowsKiCadRoot() + wxT( "share/doc/kicad" );
350#else
351 path = wxString::FromUTF8Unchecked( KICAD_DOCS );
352#endif
353
354 return path;
355}
356
357
358bool PATHS::EnsurePathExists( const wxString& aPath )
359{
360 wxFileName path( aPath );
361 if( !path.MakeAbsolute() )
362 {
363 return false;
364 }
365
366 if( !wxFileName::DirExists( aPath ) )
367 {
368 if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
369 {
370 return false;
371 }
372 }
373
374 return true;
375}
376
377
379{
390}
391
392
393#ifdef __WXMAC__
394wxString PATHS::GetOSXKicadUserDataDir()
395{
396 // According to wxWidgets documentation for GetUserDataDir:
397 // Mac: ~/Library/Application Support/appname
398 wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
399
400 // Since appname is different if started via launcher or standalone binary
401 // map all to "kicad" here
402 udir.RemoveLastDir();
403 udir.AppendDir( wxT( "kicad" ) );
404
405 return udir.GetPath();
406}
407
408
409wxString PATHS::GetOSXKicadMachineDataDir()
410{
411 // 6.0 forward: Same as the main data dir
412 return GetOSXKicadDataDir();
413}
414
415
416wxString PATHS::GetOSXKicadDataDir()
417{
418 // According to wxWidgets documentation for GetDataDir:
419 // Mac: appname.app/Contents/SharedSupport bundle subdirectory
420 wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
421
422 // This must be mapped to main bundle for everything but kicad.app
423 const wxArrayString dirs = ddir.GetDirs();
424
425 // Check if we are the main kicad binary. in this case, the path will be
426 // /path/to/bundlename.app/Contents/SharedSupport
427 // If we are an aux binary, the path will be something like
428 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
429 if( dirs.GetCount() >= 6 &&
430 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
431 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
432 {
433 ddir.RemoveLastDir();
434 ddir.RemoveLastDir();
435 ddir.RemoveLastDir();
436 ddir.RemoveLastDir();
437 ddir.AppendDir( wxT( "SharedSupport" ) );
438 }
439
440 return ddir.GetPath();
441}
442#endif
443
444
445#ifdef __WXWINDOWS__
446wxString PATHS::GetWindowsFontConfigDir()
447{
448 wxFileName fn;
449 fn.AssignDir( getWindowsKiCadRoot() );
450 fn.AppendDir( wxS( "etc" ) );
451 fn.AppendDir( wxS( "fonts" ) );
452
453 return fn.GetPathWithSep();
454}
455
456
457wxString PATHS::getWindowsKiCadRoot()
458{
459 wxFileName root( Pgm().GetExecutablePath() + wxT( "/../" ) );
460 root.MakeAbsolute();
461
462 return root.GetPathWithSep();
463}
464#endif
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:53
static wxString GetStockSymbolsPath()
Gets the stock (install) symbols path.
Definition: paths.cpp:202
static wxString GetDefaultUser3DModelsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:119
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:86
static void getUserDocumentPath(wxFileName &aPath)
Gets the user path for the current kicad version which acts as the root for other user paths.
Definition: paths.cpp:39
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:186
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:139
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:262
static wxString GetUserPlugins3DPath()
Gets the user path for 3d viewer plugin.
Definition: paths.cpp:64
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:378
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
Definition: paths.cpp:342
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:358
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:129
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:278
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:150
static wxString GetDefaultUserFootprintsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:108
static wxString GetStock3dmodelsPath()
Gets the stock (install) 3dmodels path.
Definition: paths.cpp:222
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:242
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:321
static wxString GetUserScriptingPath()
Gets the user path for python scripts.
Definition: paths.cpp:75
static wxString GetDefaultUserSymbolsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:97
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition: paths.cpp:310
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:252
static wxString GetStockFootprintsPath()
Gets the stock (install) footprints path.
Definition: paths.cpp:212
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
Definition: paths.cpp:232
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
wxString GetUserCachePath()
Retrieves the operating system specific path for user's application cache.
#define KICAD_PATH_STR
Definition: paths.cpp:35
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115