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;
69
70 tmp.AppendDir( wxT( "scripting" ) );
71
72 return tmp.GetPath();
73}
74
75
77{
78 wxFileName tmp;
80
81 tmp.AppendDir( wxT( "template" ) );
82
83 return tmp.GetPathWithSep();
84}
85
86
88{
89 wxFileName tmp;
91
92 tmp.AppendDir( wxT( "symbols" ) );
93
94 return tmp.GetPath();
95}
96
97
99{
100 wxFileName tmp;
101 getUserDocumentPath( tmp );
102
103 tmp.AppendDir( wxT( "footprints" ) );
104
105 return tmp.GetPath();
106}
107
108
110{
111 wxFileName tmp;
112 getUserDocumentPath( tmp );
113
114 tmp.AppendDir( wxT( "blocks" ) );
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 _WIN32
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( "/blocks" );
243
244 return path;
245}
246
247
249{
250 wxString path;
251
252 path = GetStockEDALibraryPath() + wxT( "/3dmodels" );
253
254 return path;
255}
256
257
259{
260 wxString path;
261
262 path = GetStockDataPath() + wxT( "/scripting" );
263
264 return path;
265}
266
267
269{
270 wxString path;
271
272 path = GetStockEDALibraryPath() + wxT( "/template" );
273
274 return path;
275}
276
277
279{
280 wxString path;
281
282 path = GetStockDataPath() + wxT( "/internat" );
283
284 return path;
285}
286
287
289{
290 wxFileName fn;
291
292#if defined( __WXMSW__ )
293 fn.AssignDir( GetExecutablePath() );
294 fn.AppendDir( wxT( "scripting" ) );
295#else
296 fn.AssignDir( PATHS::GetStockDataPath( false ) );
297#endif
298 fn.AppendDir( wxT( "plugins" ) );
299
300 return fn.GetPathWithSep();
301}
302
303
305{
306 wxFileName fn;
307
308#if defined( __WXMSW__ )
309 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
310 {
311 fn.AssignDir( getWindowsKiCadRoot() );
312 }
313 else
314 {
315 fn.AssignDir( GetExecutablePath() );
316 }
317
318 fn.AppendDir( wxT( "plugins" ) );
319#elif defined( __WXMAC__ )
320 fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
321
322 // This must be mapped to main bundle for everything but kicad.app
323 const wxArrayString dirs = fn.GetDirs();
324
325 // Check if we are the main kicad binary. in this case, the path will be
326 // /path/to/bundlename.app/Contents/PlugIns
327 // If we are an aux binary, the path will be something like
328 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/PlugIns
329 if( dirs.GetCount() >= 6 &&
330 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
331 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
332 {
333 fn.RemoveLastDir();
334 fn.RemoveLastDir();
335 fn.RemoveLastDir();
336 fn.RemoveLastDir();
337 fn.AppendDir( wxT( "PlugIns" ) );
338 }
339#else
340 // KICAD_PLUGINDIR = CMAKE_INSTALL_FULL_LIBDIR path is the absolute path
341 // corresponding to the install path used for constructing KICAD_USER_PLUGIN
342 wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
343 fn.Assign( tfname, "" );
344 fn.AppendDir( wxT( "kicad" ) );
345 fn.AppendDir( wxT( "plugins" ) );
346#endif
347
348 fn.AppendDir( wxT( "3d" ) );
349
350 return fn.GetPathWithSep();
351}
352
353
355{
356 wxFileName fn;
357
358 fn.AssignDir( PATHS::GetStockDataPath( false ) );
359 fn.AppendDir( wxT( "demos" ) );
360
361 return fn.GetPathWithSep();
362}
363
364
366{
367 wxString envPath;
368 wxFileName tmp;
369
370 tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
371
372 // Use KICAD_CACHE_HOME to allow the user to force a specific cache path.
373 if( wxGetEnv( wxT( "KICAD_CACHE_HOME" ), &envPath ) && !envPath.IsEmpty() )
374 {
375 // Override the assignment above with KICAD_CACHE_HOME
376 tmp.AssignDir( envPath );
377 }
378
379 tmp.AppendDir( KICAD_PATH_STR );
380 tmp.AppendDir( GetMajorMinorVersion().ToStdString() );
381
382 return tmp.GetPathWithSep();
383}
384
385
387{
388 wxString path;
389
390#if defined( __WXMAC__ )
391 path = GetOSXKicadDataDir();
392#elif defined( __WXMSW__ )
393 path = getWindowsKiCadRoot() + wxT( "share/doc/kicad" );
394#else
395 path = wxString::FromUTF8Unchecked( KICAD_DOCS );
396#endif
397
398 return path;
399}
400
401
403{
404 wxFileName path;
405 path.AssignDir( wxStandardPaths::Get().GetTempDir() );
406 path.AppendDir( "org.kicad.kicad" );
407 path.AppendDir( "instances" );
408 return path.GetPathWithSep();
409}
410
411
413{
414 wxFileName tmp;
415 getUserDocumentPath( tmp );
416
417 tmp.AppendDir( wxT( "logs" ) );
418
419 return tmp.GetPath();
420}
421
422
423bool PATHS::EnsurePathExists( const wxString& aPath )
424{
425 wxFileName path( aPath );
426 if( !path.MakeAbsolute() )
427 {
428 return false;
429 }
430
431 if( !wxFileName::DirExists( aPath ) )
432 {
433 if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
434 {
435 return false;
436 }
437 }
438
439 return true;
440}
441
442
444{
454}
455
456
457#ifdef __WXMAC__
458wxString PATHS::GetOSXKicadUserDataDir()
459{
460 // According to wxWidgets documentation for GetUserDataDir:
461 // Mac: ~/Library/Application Support/appname
462 wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
463
464 // Since appname is different if started via launcher or standalone binary
465 // map all to "kicad" here
466 udir.RemoveLastDir();
467 udir.AppendDir( wxT( "kicad" ) );
468
469 return udir.GetPath();
470}
471
472
473wxString PATHS::GetOSXKicadMachineDataDir()
474{
475 // 6.0 forward: Same as the main data dir
476 return GetOSXKicadDataDir();
477}
478
479
480wxString PATHS::GetOSXKicadDataDir()
481{
482 // According to wxWidgets documentation for GetDataDir:
483 // Mac: appname.app/Contents/SharedSupport bundle subdirectory
484 wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
485
486 // This must be mapped to main bundle for everything but kicad.app
487 const wxArrayString dirs = ddir.GetDirs();
488
489 // Check if we are the main kicad binary. in this case, the path will be
490 // /path/to/bundlename.app/Contents/SharedSupport
491 // If we are an aux binary, the path will be something like
492 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
493 if( dirs.GetCount() >= 6 &&
494 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
495 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
496 {
497 ddir.RemoveLastDir();
498 ddir.RemoveLastDir();
499 ddir.RemoveLastDir();
500 ddir.RemoveLastDir();
501 ddir.AppendDir( wxT( "SharedSupport" ) );
502 }
503
504 return ddir.GetPath();
505}
506#endif
507
508
509#ifdef _WIN32
510wxString PATHS::GetWindowsFontConfigDir()
511{
512 wxFileName fn;
513 fn.AssignDir( getWindowsKiCadRoot() );
514 fn.AppendDir( wxS( "etc" ) );
515 fn.AppendDir( wxS( "fonts" ) );
516
517 return fn.GetPathWithSep();
518}
519
520
521wxString PATHS::getWindowsKiCadRoot()
522{
523 wxFileName root( GetExecutablePath() + wxT( "/../" ) );
524 root.MakeAbsolute();
525
526 return root.GetPathWithSep();
527}
528#endif
529
530
532{
533 static wxString user_settings_path;
534
535 if( user_settings_path.empty() )
536 user_settings_path = CalculateUserSettingsPath();
537
538 return user_settings_path;
539}
540
541
542wxString PATHS::CalculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
543{
544 wxFileName cfgpath;
545
546 // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
547
548 wxString envstr;
549 if( aUseEnv && wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
550 {
551 // Override the assignment above with KICAD_CONFIG_HOME
552 cfgpath.AssignDir( envstr );
553 }
554 else
555 {
556 cfgpath.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
557
558 cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
559 }
560
561 if( aIncludeVer )
562 cfgpath.AppendDir( GetMajorMinorVersion().ToStdString() );
563
564 return cfgpath.GetPath();
565}
566
567
569{
570 static wxString exe_path;
571
572 if( exe_path.empty() )
573 {
574 wxString bin_dir = wxStandardPaths::Get().GetExecutablePath();
575
576#ifdef __WXMAC__
577 // On OSX GetExecutablePath() will always point to main
578 // bundle directory, e.g., /Applications/kicad.app/
579
580 wxFileName fn( bin_dir );
582
583 if( fn.GetName() == wxT( "kicad" ) || fn.GetName() == wxT( "kicad-cli" ) )
584 {
585 // kicad launcher, so just remove the Contents/MacOS part
586 fn.RemoveLastDir();
587 fn.RemoveLastDir();
588 }
589 else
590 {
591 // standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
592 fn.RemoveLastDir();
593 fn.RemoveLastDir();
594 fn.RemoveLastDir();
595 fn.RemoveLastDir();
596 fn.RemoveLastDir();
597 }
598
599 bin_dir = fn.GetPath() + wxT( "/" );
600#else
601 // Use unix notation for paths. I am not sure this is a good idea,
602 // but it simplifies compatibility between Windows and Unices.
603 // However it is a potential problem in path handling under Windows.
604 bin_dir.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
605
606 // Remove file name form command line:
607 while( bin_dir.Last() != '/' && !bin_dir.IsEmpty() )
608 bin_dir.RemoveLast();
609#endif
610 exe_path = bin_dir;
611 }
612
613 return exe_path;
614}
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:402
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:76
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:542
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:288
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
Definition: paths.cpp:412
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:443
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
Definition: paths.cpp:386
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:423
static wxString GetDefaultUserDesignBlocksPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:109
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:304
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:98
static wxString GetStock3dmodelsPath()
Gets the stock (install) 3dmodels path.
Definition: paths.cpp:248
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:268
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:365
static wxString GetUserScriptingPath()
Gets the user path for python scripts.
Definition: paths.cpp:65
static wxString GetDefaultUserSymbolsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:87
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition: paths.cpp:354
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:278
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: paths.cpp:531
static wxString GetStockDesignBlocksPath()
Gets the stock (install) footprints path.
Definition: paths.cpp:238
static wxString GetStockFootprintsPath()
Gets the stock (install) footprints path.
Definition: paths.cpp:228
static const wxString & GetExecutablePath()
Definition: paths.cpp:568
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
Definition: paths.cpp:258
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