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 
25 #include <kiplatform/environment.h>
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 
39 void 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;
56  getUserDocumentPath( 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;
78  getUserDocumentPath( tmp );
79 
80  tmp.AppendDir( wxT( "scripting" ) );
81 
82  return tmp.GetPath();
83 }
84 
85 
87 {
88  wxFileName tmp;
89  getUserDocumentPath( 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 
150 wxString 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, wxEmptyString );
287  fn.AppendDir( wxT( "kicad" ) );
288  fn.AppendDir( wxT( "plugins" ) );
289 #elif defined( __WXMAC__ )
290  fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
291 #else
292  fn.AssignDir( Pgm().GetExecutablePath() );
293  fn.AppendDir( wxT( "plugins" ) );
294 #endif
295 
296  fn.AppendDir( wxT( "3d" ) );
297 
298  return fn.GetPathWithSep();
299 }
300 
301 
303 {
304  wxFileName fn;
305 
306  fn.AssignDir( PATHS::GetStockDataPath( false ) );
307  fn.AppendDir( wxT( "demos" ) );
308 
309  return fn.GetPathWithSep();
310 }
311 
312 
314 {
315  wxFileName tmp;
316 
317  tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
318  tmp.AppendDir( KICAD_PATH_STR );
319  tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
320 
321  return tmp.GetPathWithSep();
322 }
323 
324 
326 {
327  wxString path;
328 
329 #if defined( __WXMAC__ )
330  path = GetOSXKicadDataDir();
331 #elif defined( __WXMSW__ )
332  path = getWindowsKiCadRoot() + wxT( "share/doc/kicad" );
333 #else
334  path = wxString::FromUTF8Unchecked( KICAD_DOCS );
335 #endif
336 
337  return path;
338 }
339 
340 
341 bool PATHS::EnsurePathExists( const wxString& aPath )
342 {
343  wxFileName path( aPath );
344  if( !path.Normalize() )
345  {
346  return false;
347  }
348 
349  if( !wxFileName::DirExists( aPath ) )
350  {
351  if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
352  {
353  return false;
354  }
355  }
356 
357  return true;
358 }
359 
360 
362 {
372 }
373 
374 
375 #ifdef __WXMAC__
376 wxString PATHS::GetOSXKicadUserDataDir()
377 {
378  // According to wxWidgets documentation for GetUserDataDir:
379  // Mac: ~/Library/Application Support/appname
380  wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
381 
382  // Since appname is different if started via launcher or standalone binary
383  // map all to wxT( "kicad" ) here
384  udir.RemoveLastDir();
385  udir.AppendDir( wxT( "kicad" ) );
386 
387  return udir.GetPath();
388 }
389 
390 
391 wxString PATHS::GetOSXKicadMachineDataDir()
392 {
393  // 6.0 forward: Same as the main data dir
394  return GetOSXKicadDataDir();
395 }
396 
397 
398 wxString PATHS::GetOSXKicadDataDir()
399 {
400  // According to wxWidgets documentation for GetDataDir:
401  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
402  wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
403 
404  // This must be mapped to main bundle for everything but kicad.app
405  const wxArrayString dirs = ddir.GetDirs();
406 
407  // Check if we are the main kicad binary. in this case, the path will be
408  // /path/to/bundlename.app/Contents/SharedSupport
409  // If we are an aux binary, the path will be something like
410  // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
411  if( dirs.GetCount() >= 6 &&
412  dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
413  dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
414  {
415  ddir.RemoveLastDir();
416  ddir.RemoveLastDir();
417  ddir.RemoveLastDir();
418  ddir.RemoveLastDir();
419  ddir.AppendDir( wxT( "SharedSupport" ) );
420  }
421 
422  return ddir.GetPath();
423 }
424 #endif
425 
426 
427 #ifdef __WXWINDOWS__
428 wxString PATHS::getWindowsKiCadRoot()
429 {
430  wxFileName root( Pgm().GetExecutablePath() + wxT( "/../" ) );
431  root.Normalize();
432 
433  return root.GetPathWithSep();
434 }
435 #endif
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:341
static wxString GetDefaultUser3DModelsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:119
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
static wxString GetUserScriptingPath()
Gets the user path for python scripts.
Definition: paths.cpp:75
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
Definition: paths.cpp:232
wxString GetUserCachePath()
Retrieves the operating system specific path for user's application cache.
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:278
static wxString GetStockSymbolsPath()
Gets the stock (install) symbols path.
Definition: paths.cpp:202
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:313
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
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
Definition: paths.cpp:325
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 GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:242
static wxString GetUserPlugins3DPath()
Gets the user path for 3d viewer plugin.
Definition: paths.cpp:64
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:53
#define KICAD_PATH_STR
Definition: paths.cpp:35
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:86
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:361
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:139
static wxString GetDefaultUserSymbolsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:97
static wxString GetDefaultUserFootprintsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:108
see class PGM_BASE
static wxString GetStockFootprintsPath()
Gets the stock (install) footprints path.
Definition: paths.cpp:212
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition: paths.cpp:302
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:186
static wxString GetStock3dmodelsPath()
Gets the stock (install) 3dmodels path.
Definition: paths.cpp:222
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:129
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:252
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:262