KiCad PCB EDA Suite
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 wxFileName tmp;
324
325 tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
326 tmp.AppendDir( KICAD_PATH_STR );
327 tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
328
329 return tmp.GetPathWithSep();
330}
331
332
334{
335 wxString path;
336
337#if defined( __WXMAC__ )
338 path = GetOSXKicadDataDir();
339#elif defined( __WXMSW__ )
340 path = getWindowsKiCadRoot() + wxT( "share/doc/kicad" );
341#else
342 path = wxString::FromUTF8Unchecked( KICAD_DOCS );
343#endif
344
345 return path;
346}
347
348
349bool PATHS::EnsurePathExists( const wxString& aPath )
350{
351 wxFileName path( aPath );
352 if( !path.MakeAbsolute() )
353 {
354 return false;
355 }
356
357 if( !wxFileName::DirExists( aPath ) )
358 {
359 if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
360 {
361 return false;
362 }
363 }
364
365 return true;
366}
367
368
370{
381}
382
383
384#ifdef __WXMAC__
385wxString PATHS::GetOSXKicadUserDataDir()
386{
387 // According to wxWidgets documentation for GetUserDataDir:
388 // Mac: ~/Library/Application Support/appname
389 wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
390
391 // Since appname is different if started via launcher or standalone binary
392 // map all to "kicad" here
393 udir.RemoveLastDir();
394 udir.AppendDir( wxT( "kicad" ) );
395
396 return udir.GetPath();
397}
398
399
400wxString PATHS::GetOSXKicadMachineDataDir()
401{
402 // 6.0 forward: Same as the main data dir
403 return GetOSXKicadDataDir();
404}
405
406
407wxString PATHS::GetOSXKicadDataDir()
408{
409 // According to wxWidgets documentation for GetDataDir:
410 // Mac: appname.app/Contents/SharedSupport bundle subdirectory
411 wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
412
413 // This must be mapped to main bundle for everything but kicad.app
414 const wxArrayString dirs = ddir.GetDirs();
415
416 // Check if we are the main kicad binary. in this case, the path will be
417 // /path/to/bundlename.app/Contents/SharedSupport
418 // If we are an aux binary, the path will be something like
419 // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
420 if( dirs.GetCount() >= 6 &&
421 dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
422 dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
423 {
424 ddir.RemoveLastDir();
425 ddir.RemoveLastDir();
426 ddir.RemoveLastDir();
427 ddir.RemoveLastDir();
428 ddir.AppendDir( wxT( "SharedSupport" ) );
429 }
430
431 return ddir.GetPath();
432}
433#endif
434
435
436#ifdef __WXWINDOWS__
437wxString PATHS::getWindowsKiCadRoot()
438{
439 wxFileName root( Pgm().GetExecutablePath() + wxT( "/../" ) );
440 root.MakeAbsolute();
441
442 return root.GetPathWithSep();
443}
444#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:369
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
Definition: paths.cpp:333
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:349
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:111