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 <config.h>
28#include <build_version.h>
29#include <macros.h>
30#include <wx_filename.h>
31
32// lowercase or pretty case depending on platform
33#if defined( __WXMAC__ ) || defined( __WXMSW__ )
34#define KICAD_PATH_STR wxT( "KiCad" )
35#else
36#define KICAD_PATH_STR wxT( "kicad" )
37#endif
38
39
40void PATHS::getUserDocumentPath( wxFileName& aPath )
41{
42 wxString envPath;
43
44 if( wxGetEnv( wxT( "KICAD_DOCUMENTS_HOME" ), &envPath ) )
45 aPath.AssignDir( envPath );
46 else
47 aPath.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() );
48
49 aPath.AppendDir( KICAD_PATH_STR );
50 aPath.AppendDir( GetMajorMinorVersion().ToStdString() );
51}
52
53
55{
56 wxFileName tmp;
58
59 tmp.AppendDir( wxT( "plugins" ) );
60
61 return tmp.GetPath();
62}
63
64
66{
67 wxFileName tmp;
68
69 tmp.AssignDir( PATHS::GetUserPluginsPath() );
70 tmp.AppendDir( wxT( "3d" ) );
71
72 return tmp.GetPath();
73}
74
75
77{
78 wxFileName tmp;
80
81 tmp.AppendDir( wxT( "scripting" ) );
82
83 return tmp.GetPath();
84}
85
86
88{
89 wxFileName tmp;
91
92 tmp.AppendDir( wxT( "template" ) );
93
94 return tmp.GetPathWithSep();
95}
96
97
99{
100 wxFileName tmp;
101 getUserDocumentPath( tmp );
102
103 tmp.AppendDir( wxT( "symbols" ) );
104
105 return tmp.GetPath();
106}
107
108
110{
111 wxFileName tmp;
112 getUserDocumentPath( tmp );
113
114 tmp.AppendDir( wxT( "footprints" ) );
115
116 return tmp.GetPath();
117}
118
119
121{
122 wxFileName tmp;
123 getUserDocumentPath( tmp );
124
125 tmp.AppendDir( wxT( "3dmodels" ) );
126
127 return tmp.GetPath();
128}
129
131{
132 wxFileName tmp;
133 getUserDocumentPath( tmp );
134
135 tmp.AppendDir( wxT( "3rdparty" ) );
136
137 return tmp.GetPath();
138}
139
141{
142 wxFileName tmp;
143 getUserDocumentPath( tmp );
144
145 tmp.AppendDir( wxT( "projects" ) );
146
147 return tmp.GetPath();
148}
149
150
151wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir )
152{
153 wxString path;
154
155 if( aRespectRunFromBuildDir && wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
156 {
157 // Allow debugging from build dir by placing relevant files/folders in the build root
158#if defined( __WXMAC__ )
159 wxFileName fn = wxStandardPaths::Get().GetExecutablePath();
160
161 fn.RemoveLastDir();
162 fn.RemoveLastDir();
163 fn.RemoveLastDir();
164 fn.RemoveLastDir();
165 path = fn.GetPath();
166#elif defined( __WXMSW__ )
167 path = getWindowsKiCadRoot();
168#else
169 path = GetExecutablePath() + wxT( ".." );
170#endif
171 }
172 else if( wxGetEnv( wxT( "KICAD_STOCK_DATA_HOME" ), &path ) && !path.IsEmpty() )
173 {
174 return path;
175 }
176 else
177 {
178#if defined( __WXMAC__ )
179 path = GetOSXKicadDataDir();
180#elif defined( __WXMSW__ )
181 path = getWindowsKiCadRoot() + wxT( "share/kicad" );
182#else
183 path = wxString::FromUTF8Unchecked( KICAD_DATA );
184#endif
185 }
186
187 return path;
188}
189
190
191#ifdef __WXMSW__
195wxString PATHS::GetWindowsBaseSharePath()
196{
197 return getWindowsKiCadRoot() + wxT( "share\\" );
198}
199#endif
200
201
203{
204 wxString path;
205
206#if defined( __WXMAC__ )
207 path = GetOSXKicadMachineDataDir();
208#elif defined( __WXMSW__ )
209 path = GetStockDataPath( false );
210#else
211 path = wxString::FromUTF8Unchecked( KICAD_LIBRARY_DATA );
212#endif
213
214 return path;
215}
216
217
219{
220 wxString path;
221
222 path = GetStockEDALibraryPath() + wxT( "/symbols" );
223
224 return path;
225}
226
227
229{
230 wxString path;
231
232 path = GetStockEDALibraryPath() + wxT( "/footprints" );
233
234 return path;
235}
236
237
239{
240 wxString path;
241
242 path = GetStockEDALibraryPath() + wxT( "/3dmodels" );
243
244 return path;
245}
246
247
249{
250 wxString path;
251
252 path = GetStockDataPath() + wxT( "/scripting" );
253
254 return path;
255}
256
257
259{
260 wxString path;
261
262 path = GetStockEDALibraryPath() + wxT( "/template" );
263
264 return path;
265}
266
267
269{
270 wxString path;
271
272 path = GetStockDataPath() + wxT( "/internat" );
273
274 return path;
275}
276
277
279{
280 wxFileName fn;
281
282#if defined( __WXMSW__ )
283 fn.AssignDir( GetExecutablePath() );
284 fn.AppendDir( wxT( "scripting" ) );
285#else
286 fn.AssignDir( PATHS::GetStockDataPath( false ) );
287#endif
288 fn.AppendDir( wxT( "plugins" ) );
289
290 return fn.GetPathWithSep();
291}
292
293
295{
296 wxFileName fn;
297
298#if defined( __WXMSW__ )
299 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
300 {
301 fn.AssignDir( getWindowsKiCadRoot() );
302 }
303 else
304 {
305 fn.AssignDir( GetExecutablePath() );
306 }
307
308 fn.AppendDir( wxT( "plugins" ) );
309#elif defined( __WXMAC__ )
310 fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
311
312 // This must be mapped to main bundle for everything but kicad.app
313 const wxArrayString dirs = fn.GetDirs();
314
315 // Check if we are the main kicad binary. in this case, the path will be
316 // /path/to/bundlename.app/Contents/PlugIns
317 // If we are an aux binary, the path will be something like
318 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/PlugIns
319 if( dirs.GetCount() >= 6 &&
320 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
321 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
322 {
323 fn.RemoveLastDir();
324 fn.RemoveLastDir();
325 fn.RemoveLastDir();
326 fn.RemoveLastDir();
327 fn.AppendDir( wxT( "PlugIns" ) );
328 }
329#else
330 // KICAD_PLUGINDIR = CMAKE_INSTALL_FULL_LIBDIR path is the absolute path
331 // corresponding to the install path used for constructing KICAD_USER_PLUGIN
332 wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
333 fn.Assign( tfname, "" );
334 fn.AppendDir( wxT( "kicad" ) );
335 fn.AppendDir( wxT( "plugins" ) );
336#endif
337
338 fn.AppendDir( wxT( "3d" ) );
339
340 return fn.GetPathWithSep();
341}
342
343
345{
346 wxFileName fn;
347
348 fn.AssignDir( PATHS::GetStockDataPath( false ) );
349 fn.AppendDir( wxT( "demos" ) );
350
351 return fn.GetPathWithSep();
352}
353
354
356{
357 wxString envPath;
358 wxFileName tmp;
359
360 tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
361
362 // Use KICAD_CACHE_HOME to allow the user to force a specific cache path.
363 if( wxGetEnv( wxT( "KICAD_CACHE_HOME" ), &envPath ) && !envPath.IsEmpty() )
364 {
365 // Override the assignment above with KICAD_CACHE_HOME
366 tmp.AssignDir( envPath );
367 }
368
369 tmp.AppendDir( KICAD_PATH_STR );
370 tmp.AppendDir( GetMajorMinorVersion().ToStdString() );
371
372 return tmp.GetPathWithSep();
373}
374
375
377{
378 wxString path;
379
380#if defined( __WXMAC__ )
381 path = GetOSXKicadDataDir();
382#elif defined( __WXMSW__ )
383 path = getWindowsKiCadRoot() + wxT( "share/doc/kicad" );
384#else
385 path = wxString::FromUTF8Unchecked( KICAD_DOCS );
386#endif
387
388 return path;
389}
390
391
393{
394 wxFileName path;
395 path.AssignDir( wxStandardPaths::Get().GetTempDir() );
396 path.AppendDir( "org.kicad.kicad" );
397 path.AppendDir( "instances" );
398 return path.GetPathWithSep();
399}
400
401
402bool PATHS::EnsurePathExists( const wxString& aPath )
403{
404 wxFileName path( aPath );
405 if( !path.MakeAbsolute() )
406 {
407 return false;
408 }
409
410 if( !wxFileName::DirExists( aPath ) )
411 {
412 if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
413 {
414 return false;
415 }
416 }
417
418 return true;
419}
420
421
423{
433}
434
435
436#ifdef __WXMAC__
437wxString PATHS::GetOSXKicadUserDataDir()
438{
439 // According to wxWidgets documentation for GetUserDataDir:
440 // Mac: ~/Library/Application Support/appname
441 wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
442
443 // Since appname is different if started via launcher or standalone binary
444 // map all to "kicad" here
445 udir.RemoveLastDir();
446 udir.AppendDir( wxT( "kicad" ) );
447
448 return udir.GetPath();
449}
450
451
452wxString PATHS::GetOSXKicadMachineDataDir()
453{
454 // 6.0 forward: Same as the main data dir
455 return GetOSXKicadDataDir();
456}
457
458
459wxString PATHS::GetOSXKicadDataDir()
460{
461 // According to wxWidgets documentation for GetDataDir:
462 // Mac: appname.app/Contents/SharedSupport bundle subdirectory
463 wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
464
465 // This must be mapped to main bundle for everything but kicad.app
466 const wxArrayString dirs = ddir.GetDirs();
467
468 // Check if we are the main kicad binary. in this case, the path will be
469 // /path/to/bundlename.app/Contents/SharedSupport
470 // If we are an aux binary, the path will be something like
471 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
472 if( dirs.GetCount() >= 6 &&
473 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
474 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
475 {
476 ddir.RemoveLastDir();
477 ddir.RemoveLastDir();
478 ddir.RemoveLastDir();
479 ddir.RemoveLastDir();
480 ddir.AppendDir( wxT( "SharedSupport" ) );
481 }
482
483 return ddir.GetPath();
484}
485#endif
486
487
488#ifdef __WXMSW__
489wxString PATHS::GetWindowsFontConfigDir()
490{
491 wxFileName fn;
492 fn.AssignDir( getWindowsKiCadRoot() );
493 fn.AppendDir( wxS( "etc" ) );
494 fn.AppendDir( wxS( "fonts" ) );
495
496 return fn.GetPathWithSep();
497}
498
499
500wxString PATHS::getWindowsKiCadRoot()
501{
502 wxFileName root( GetExecutablePath() + wxT( "/../" ) );
503 root.MakeAbsolute();
504
505 return root.GetPathWithSep();
506}
507#endif
508
509
511{
512 static wxString user_settings_path;
513
514 if( user_settings_path.empty() )
515 user_settings_path = CalculateUserSettingsPath();
516
517 return user_settings_path;
518}
519
520
521wxString PATHS::CalculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
522{
523 wxFileName cfgpath;
524
525 // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
526
527 wxString envstr;
528 if( aUseEnv && wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
529 {
530 // Override the assignment above with KICAD_CONFIG_HOME
531 cfgpath.AssignDir( envstr );
532 }
533 else
534 {
535 cfgpath.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
536
537 cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
538 }
539
540 if( aIncludeVer )
541 cfgpath.AppendDir( GetMajorMinorVersion().ToStdString() );
542
543 return cfgpath.GetPath();
544}
545
546
548{
549 static wxString exe_path;
550
551 if( exe_path.empty() )
552 {
553 wxString bin_dir = wxStandardPaths::Get().GetExecutablePath();
554
555#ifdef __WXMAC__
556 // On OSX GetExecutablePath() will always point to main
557 // bundle directory, e.g., /Applications/kicad.app/
558
559 wxFileName fn( bin_dir );
561
562 if( fn.GetName() == wxT( "kicad" ) || fn.GetName() == wxT( "kicad-cli" ) )
563 {
564 // kicad launcher, so just remove the Contents/MacOS part
565 fn.RemoveLastDir();
566 fn.RemoveLastDir();
567 }
568 else
569 {
570 // standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
571 fn.RemoveLastDir();
572 fn.RemoveLastDir();
573 fn.RemoveLastDir();
574 fn.RemoveLastDir();
575 fn.RemoveLastDir();
576 }
577
578 bin_dir = fn.GetPath() + wxT( "/" );
579#else
580 // Use unix notation for paths. I am not sure this is a good idea,
581 // but it simplifies compatibility between Windows and Unices.
582 // However it is a potential problem in path handling under Windows.
583 bin_dir.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
584
585 // Remove file name form command line:
586 while( bin_dir.Last() != '/' && !bin_dir.IsEmpty() )
587 bin_dir.RemoveLast();
588#endif
589 exe_path = bin_dir;
590 }
591
592 return exe_path;
593}
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:54
static wxString GetStockSymbolsPath()
Gets the stock (install) symbols path.
Definition: paths.cpp:218
static wxString GetDefaultUser3DModelsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:120
static wxString GetInstanceCheckerPath()
Gets the path used for wxSingleInstanceChecker lock files.
Definition: paths.cpp:392
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:87
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:40
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:202
static wxString CalculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
Definition: paths.cpp:521
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:140
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:278
static wxString GetUserPlugins3DPath()
Gets the user path for 3d viewer plugin.
Definition: paths.cpp:65
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:422
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
Definition: paths.cpp:376
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:402
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:130
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:294
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:151
static wxString GetDefaultUserFootprintsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:109
static wxString GetStock3dmodelsPath()
Gets the stock (install) 3dmodels path.
Definition: paths.cpp:238
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:258
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:355
static wxString GetUserScriptingPath()
Gets the user path for python scripts.
Definition: paths.cpp:76
static wxString GetDefaultUserSymbolsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:98
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition: paths.cpp:344
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:268
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: paths.cpp:510
static wxString GetStockFootprintsPath()
Gets the stock (install) footprints path.
Definition: paths.cpp:228
static const wxString & GetExecutablePath()
Definition: paths.cpp:547
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
Definition: paths.cpp:248
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:92
This file contains miscellaneous commonly used macros and functions.
#define TO_STR(x)
Definition: macros.h:94
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
wxString GetUserConfigPath()
Retrieves the operating system specific path for a user's configuration store.
wxString GetUserCachePath()
Retrieves the operating system specific path for user's application cache.
#define KICAD_PATH_STR
Definition: paths.cpp:36
#define WIN_STRING_DIR_SEP
Definition: paths.h:31
#define UNIX_STRING_DIR_SEP
Definition: paths.h:30