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( "3dmodels" ) );
115
116 return tmp.GetPath();
117}
118
120{
121 wxFileName tmp;
122 getUserDocumentPath( tmp );
123
124 tmp.AppendDir( wxT( "3rdparty" ) );
125
126 return tmp.GetPath();
127}
128
130{
131 wxFileName tmp;
132 getUserDocumentPath( tmp );
133
134 tmp.AppendDir( wxT( "projects" ) );
135
136 return tmp.GetPath();
137}
138
139
140wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir )
141{
142 wxString path;
143
144 if( aRespectRunFromBuildDir && wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
145 {
146 // Allow debugging from build dir by placing relevant files/folders in the build root
147#if defined( __WXMAC__ )
148 wxFileName fn = wxStandardPaths::Get().GetExecutablePath();
149
150 fn.RemoveLastDir();
151 fn.RemoveLastDir();
152 fn.RemoveLastDir();
153 fn.RemoveLastDir();
154 path = fn.GetPath();
155#elif defined( __WXMSW__ )
156 path = getWindowsKiCadRoot();
157#else
158 path = GetExecutablePath() + wxT( ".." );
159#endif
160 }
161 else if( wxGetEnv( wxT( "KICAD_STOCK_DATA_HOME" ), &path ) && !path.IsEmpty() )
162 {
163 return path;
164 }
165 else
166 {
167#if defined( __WXMAC__ )
168 path = GetOSXKicadDataDir();
169#elif defined( __WXMSW__ )
170 path = getWindowsKiCadRoot() + wxT( "share/kicad" );
171#else
172 path = wxString::FromUTF8Unchecked( KICAD_DATA );
173#endif
174 }
175
176 return path;
177}
178
179
180#ifdef __WXMSW__
184wxString PATHS::GetWindowsBaseSharePath()
185{
186 return getWindowsKiCadRoot() + wxT( "share\\" );
187}
188#endif
189
190
192{
193 wxString path;
194
195#if defined( __WXMAC__ )
196 path = GetOSXKicadMachineDataDir();
197#elif defined( __WXMSW__ )
198 path = GetStockDataPath( false );
199#else
200 path = wxString::FromUTF8Unchecked( KICAD_LIBRARY_DATA );
201#endif
202
203 return path;
204}
205
206
208{
209 wxString path;
210
211 path = GetStockEDALibraryPath() + wxT( "/symbols" );
212
213 return path;
214}
215
216
218{
219 wxString path;
220
221 path = GetStockEDALibraryPath() + wxT( "/footprints" );
222
223 return path;
224}
225
226
228{
229 wxString path;
230
231 path = GetStockEDALibraryPath() + wxT( "/3dmodels" );
232
233 return path;
234}
235
236
238{
239 wxString path;
240
241 path = GetStockDataPath() + wxT( "/scripting" );
242
243 return path;
244}
245
246
248{
249 wxString path;
250
251 path = GetStockEDALibraryPath() + wxT( "/template" );
252
253 return path;
254}
255
256
258{
259 wxString path;
260
261 path = GetStockDataPath() + wxT( "/internat" );
262
263 return path;
264}
265
266
268{
269 wxFileName fn;
270
271#if defined( __WXMSW__ )
272 fn.AssignDir( GetExecutablePath() );
273 fn.AppendDir( wxT( "scripting" ) );
274#else
275 fn.AssignDir( PATHS::GetStockDataPath( false ) );
276#endif
277 fn.AppendDir( wxT( "plugins" ) );
278
279 return fn.GetPathWithSep();
280}
281
282
284{
285 wxFileName fn;
286
287#if defined( __WXMSW__ )
288 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
289 {
290 fn.AssignDir( getWindowsKiCadRoot() );
291 }
292 else
293 {
294 fn.AssignDir( GetExecutablePath() );
295 }
296
297 fn.AppendDir( wxT( "plugins" ) );
298#elif defined( __WXMAC__ )
299 fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
300
301 // This must be mapped to main bundle for everything but kicad.app
302 const wxArrayString dirs = fn.GetDirs();
303
304 // Check if we are the main kicad binary. in this case, the path will be
305 // /path/to/bundlename.app/Contents/PlugIns
306 // If we are an aux binary, the path will be something like
307 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/PlugIns
308 if( dirs.GetCount() >= 6 &&
309 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
310 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
311 {
312 fn.RemoveLastDir();
313 fn.RemoveLastDir();
314 fn.RemoveLastDir();
315 fn.RemoveLastDir();
316 fn.AppendDir( wxT( "PlugIns" ) );
317 }
318#else
319 // KICAD_PLUGINDIR = CMAKE_INSTALL_FULL_LIBDIR path is the absolute path
320 // corresponding to the install path used for constructing KICAD_USER_PLUGIN
321 wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
322 fn.Assign( tfname, "" );
323 fn.AppendDir( wxT( "kicad" ) );
324 fn.AppendDir( wxT( "plugins" ) );
325#endif
326
327 fn.AppendDir( wxT( "3d" ) );
328
329 return fn.GetPathWithSep();
330}
331
332
334{
335 wxFileName fn;
336
337 fn.AssignDir( PATHS::GetStockDataPath( false ) );
338 fn.AppendDir( wxT( "demos" ) );
339
340 return fn.GetPathWithSep();
341}
342
343
345{
346 wxString envPath;
347 wxFileName tmp;
348
349 tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
350
351 // Use KICAD_CACHE_HOME to allow the user to force a specific cache path.
352 if( wxGetEnv( wxT( "KICAD_CACHE_HOME" ), &envPath ) && !envPath.IsEmpty() )
353 {
354 // Override the assignment above with KICAD_CACHE_HOME
355 tmp.AssignDir( envPath );
356 }
357
358 tmp.AppendDir( KICAD_PATH_STR );
359 tmp.AppendDir( GetMajorMinorVersion().ToStdString() );
360
361 return tmp.GetPathWithSep();
362}
363
364
366{
367 wxString path;
368
369#if defined( __WXMAC__ )
370 path = GetOSXKicadDataDir();
371#elif defined( __WXMSW__ )
372 path = getWindowsKiCadRoot() + wxT( "share/doc/kicad" );
373#else
374 path = wxString::FromUTF8Unchecked( KICAD_DOCS );
375#endif
376
377 return path;
378}
379
380
382{
383 wxFileName path;
384 path.AssignDir( wxStandardPaths::Get().GetTempDir() );
385 path.AppendDir( "org.kicad.kicad" );
386 path.AppendDir( "instances" );
387 return path.GetPathWithSep();
388}
389
390
392{
393 wxFileName tmp;
394 getUserDocumentPath( tmp );
395
396 tmp.AppendDir( wxT( "logs" ) );
397
398 return tmp.GetPath();
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:207
static wxString GetDefaultUser3DModelsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:109
static wxString GetInstanceCheckerPath()
Gets the path used for wxSingleInstanceChecker lock files.
Definition: paths.cpp:381
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:191
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:129
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:267
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
Definition: paths.cpp:391
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:365
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:119
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:283
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:140
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:227
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:247
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:344
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:333
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:257
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:217
static const wxString & GetExecutablePath()
Definition: paths.cpp:547
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
Definition: paths.cpp:237
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