21#include <wx/filename.h>
22#include <wx/stdpaths.h>
39 if( wxGetEnv( wxT(
"KICAD_DOCUMENTS_HOME" ), &envPath ) )
40 aPath.AssignDir( envPath );
54 tmp.AppendDir( wxT(
"plugins" ) );
65 tmp.AppendDir( wxT(
"scripting" ) );
76 tmp.AppendDir( wxT(
"template" ) );
78 return tmp.GetPathWithSep();
87 tmp.AppendDir( wxT(
"symbols" ) );
98 tmp.AppendDir( wxT(
"footprints" ) );
100 return tmp.GetPath();
109 tmp.AppendDir( wxT(
"blocks" ) );
111 return tmp.GetPath();
120 tmp.AppendDir( wxT(
"3dmodels" ) );
122 return tmp.GetPath();
131 tmp.AppendDir( wxT(
"3rdparty" ) );
133 return tmp.GetAbsolutePath();
142 tmp.AppendDir( wxT(
"projects" ) );
144 return tmp.GetPath();
148#if defined( __WXMAC__ )
154static wxString getOSXBundleRoot()
156 static wxString bundleRoot;
158 if( bundleRoot.empty() )
160 wxFileName fn( wxStandardPaths::Get().GetExecutablePath() );
162 fn.SetFullName( wxEmptyString );
166 if( fn.GetDirCount() >= 2 && fn.GetDirs().Last() == wxT(
"MacOS" ) )
175 const wxArrayString dirs = fn.GetDirs();
176 if( dirs.GetCount() >= 4 && dirs[dirs.GetCount() - 2] == wxT(
"Applications" )
177 && dirs[dirs.GetCount() - 4].Lower().EndsWith( wxT(
"app" ) ) )
184 bundleRoot = fn.GetPath();
199#if defined( __WXMAC__ )
201 fn.AssignDir( getOSXBundleRoot() );
205#elif defined( __WXMSW__ )
206 path = getWindowsKiCadRoot();
210 wxASSERT_MSG( !
path.IsEmpty(), wxString::Format( wxT(
"Could not find build root directory above %s" ),
214 else if( wxGetEnv( wxT(
"KICAD_STOCK_DATA_HOME" ), &
path ) && !
path.IsEmpty() )
220#if defined( __WXMAC__ )
221 path = GetOSXKicadDataDir();
222#elif defined( __WXMSW__ )
223 path = getWindowsKiCadRoot() + wxT(
"share/kicad" );
225 path = wxString::FromUTF8Unchecked( KICAD_DATA );
235wxString PATHS::GetWindowsBaseSharePath()
237 return getWindowsKiCadRoot() + wxT(
"share\\" );
247#if defined( __WXMAC__ )
248 path = GetOSXKicadMachineDataDir();
249#elif defined( __WXMSW__ )
252 if( wxGetEnv( wxT(
"APPDIR" ), &
path ) )
253 path += wxT(
"/share/kicad" );
255 path = wxString::FromUTF8Unchecked( KICAD_LIBRARY_DATA );
267 fn.AppendDir(
"symbols" );
269 return fn.GetPathWithSep();
278 fn.AppendDir(
"footprints" );
280 return fn.GetPathWithSep();
289 fn.AppendDir(
"blocks" );
291 return fn.GetPathWithSep();
300 fn.AppendDir(
"3dmodels" );
302 return fn.GetPathWithSep();
311 fn.AppendDir(
"scripting" );
313 return fn.GetPathWithSep();
322 fn.AppendDir(
"template" );
324 return fn.GetPathWithSep();
333 fn.AppendDir(
"internat" );
335 return fn.GetPathWithSep();
343#if defined( __WXMSW__ )
345 fn.AppendDir( wxT(
"scripting" ) );
349 fn.AppendDir( wxT(
"plugins" ) );
351 return fn.GetPathWithSep();
359#if defined( __WXMSW__ )
362 fn.AssignDir( getWindowsKiCadRoot() );
369 fn.AppendDir( wxT(
"plugins" ) );
370#elif defined( __WXMAC__ )
371 fn.AssignDir( getOSXBundleRoot() );
372 fn.AppendDir( wxT(
"Contents" ) );
373 fn.AppendDir( wxT(
"PlugIns" ) );
380 fn.AppendDir( wxT(
".." ) );
381 fn.AppendDir( wxT(
"plugins" ) );
385 else if( wxGetEnv( wxT(
"APPDIR" ), &envPath ) )
387 fn.Assign( envPath, wxEmptyString );
388 fn.AppendDir( wxT(
"usr" ) );
389 fn.AppendDir( wxT(
"lib" ) );
390 fn.AppendDir( wxT(
"x86_64-linux-gnu" ) );
391 fn.AppendDir( wxT(
"kicad" ) );
392 fn.AppendDir( wxT(
"plugins" ) );
398 wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
399 fn.Assign( tfname,
"" );
400 fn.AppendDir( wxT(
"kicad" ) );
401 fn.AppendDir( wxT(
"plugins" ) );
405 fn.AppendDir( wxT(
"3d" ) );
407 return fn.GetPathWithSep();
416 fn.AppendDir( wxT(
"demos" ) );
418 return fn.GetPathWithSep();
430 if( wxGetEnv( wxT(
"KICAD_CACHE_HOME" ), &envPath ) && !envPath.IsEmpty() )
433 tmp.AssignDir( envPath );
439 return tmp.GetPathWithSep();
447#if defined( __WXMAC__ )
448 path = GetOSXKicadDataDir();
449#elif defined( __WXMSW__ )
450 path = getWindowsKiCadRoot() + wxT(
"share/doc/kicad" );
452 path = wxString::FromUTF8Unchecked( KICAD_DOCS );
464 tmp.AppendDir( wxT(
"logs" ) );
466 return tmp.GetPath();
472 wxString pathString = aPath;
476 pathString += wxFileName::GetPathSeparator();
479 wxFileName
path( pathString );
480 if( !
path.MakeAbsolute() )
485 if( !wxFileName::DirExists(
path.GetPath() ) )
487 if( !wxFileName::Mkdir(
path.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
514 if( !tmp.DirExists() )
516 wxString msg = wxString::Format(
517 _(
"KiCad was unable to use '%s'.\n"
519 "1. Disable 'Controlled folder access' in Windows settings or Group Policy\n"
520 "2. Make sure no other antivirus software interferes with KiCad\n"
521 "3. Make sure you have correct permissions set up" ),
524 wxMessageBox( msg,
_(
"Warning" ), wxICON_WARNING );
531wxString PATHS::GetOSXKicadUserDataDir()
535 wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
539 udir.RemoveLastDir();
540 udir.AppendDir( wxT(
"kicad" ) );
542 return udir.GetPath();
546wxString PATHS::GetOSXKicadMachineDataDir()
549 return GetOSXKicadDataDir();
553wxString PATHS::GetOSXKicadDataDir()
556 ddir.AssignDir( getOSXBundleRoot() );
557 ddir.AppendDir( wxT(
"Contents" ) );
558 ddir.AppendDir( wxT(
"SharedSupport" ) );
559 return ddir.GetPath();
565wxString PATHS::GetWindowsFontConfigDir()
568 fn.AssignDir( getWindowsKiCadRoot() );
569 fn.AppendDir( wxS(
"etc" ) );
570 fn.AppendDir( wxS(
"fonts" ) );
572 return fn.GetPathWithSep();
576wxString PATHS::getWindowsKiCadRoot()
581 return root.GetPathWithSep();
588 static wxString user_settings_path;
590 if( user_settings_path.empty() )
593 return user_settings_path;
604 if( aUseEnv && wxGetEnv( wxT(
"KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
607 cfgpath.AssignDir( envstr );
613 cfgpath.AppendDir(
TO_STR( KICAD_CONFIG_DIR ) );
619 return cfgpath.GetPath();
625 static wxString exe_path;
627 if( exe_path.empty() )
631 exe_path = getOSXBundleRoot() + wxT(
"/" );
638 if( wxGetEnv( wxT(
"APPDIR" ), &envPath ) )
642 if( !envPath.EndsWith( wxT(
"/" ) ) )
643 envPath += wxT(
"/" );
645 exe_path = envPath + wxT(
"usr/bin/" );
649 wxString bin_dir = wxStandardPaths::Get().GetExecutablePath();
657 while( bin_dir.Last() !=
'/' && !bin_dir.IsEmpty() )
658 bin_dir.RemoveLast();
674 wxFileName sibling( wxFileName( wxStandardPaths::Get().
GetExecutablePath() ).GetPath(), aBaseName );
680 sibling.SetExt( wxT(
"exe" ) );
683 if( !sibling.FileExists() )
684 return wxEmptyString;
686 return sibling.GetFullPath();
700 return wxGetEnv( wxT(
"KICAD_RUN_FROM_BUILD_DIR" ),
nullptr ) || !
GetBuildRoot().IsEmpty();
706#if !defined( __WXMAC__ ) && !defined( __WXMSW__ )
707 if( aExecutablePath.IsEmpty() )
708 return wxEmptyString;
713 const auto looksLikeBuildDir = [](
const wxFileName& aPath ) ->
bool
715 const wxDir schemaDir( aPath.GetPathWithSep() + wxT(
"schemas" ) );
717 if( !schemaDir.IsOpened() )
721 return schemaDir.GetFirst( &filename, wxT(
"*.json" ), wxDIR_FILES );
726 if( aExecutablePath.EndsWith( wxT(
"/" ) ) || aExecutablePath.EndsWith( wxT(
"\\" ) ) )
727 fn.AssignDir( aExecutablePath );
729 fn.Assign( aExecutablePath );
731 while( fn.GetDirCount() > 0 && !looksLikeBuildDir( fn ) )
734 if( fn.GetDirCount() == 0 )
735 return wxEmptyString;
739 wxUnusedVar( aExecutablePath );
740 return wxEmptyString;
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
static bool RunningFromBuildDir()
True when KiCad runs from its build tree or KICAD_RUN_FROM_BUILD_DIR is set.
static wxString GetUserPluginsPath()
Gets the user path for plugins.
static wxString GetStockSymbolsPath()
Gets the stock (install) symbols path.
static wxString ResolveSiblingExecutable(const wxString &aBaseName)
Resolve a sibling executable alongside the running process.
static wxString GetDefaultUser3DModelsPath()
Gets the default path we point users to create projects.
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
static void getUserDocumentPath(wxFileName &aPath)
Gets the user path for the current kicad version which acts as the root for other user paths.
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
static wxString CalculateUserSettingsPath(bool aIncludeVer=true, bool aUseEnv=true)
Determines the base path for user settings files.
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
static void EnsureUserPathsExist()
Ensures/creates user default paths.
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
static wxString GetDefaultUserDesignBlocksPath()
Gets the default path we point users to create projects.
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
static const wxString & GetBuildRoot()
The build root of the running executable, or empty outside a KiCad build tree.
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer plugins path.
static wxString FindKiCadBuildRoot(const wxString &aExecutablePath)
Return the CMake build root for a KiCad executable path, or an empty string outside a build tree.
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
static wxString GetDefaultUserFootprintsPath()
Gets the default path we point users to create projects.
static wxString GetStock3dmodelsPath()
Gets the stock (install) 3dmodels path.
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
static wxString GetUserScriptingPath()
Gets the user path for python scripts.
static wxString GetDefaultUserSymbolsPath()
Gets the default path we point users to create projects.
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
static wxString GetLocaleDataPath()
Gets the locales translation data path.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
static wxString GetStockDesignBlocksPath()
Gets the stock (install) footprints path.
static wxString GetStockFootprintsPath()
Gets the stock (install) footprints path.
static const wxString & GetExecutablePath()
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
static void ResolvePossibleSymlinks(wxFileName &aFilename)
static std::string ToStdString(const wxString &aStr)
This file contains miscellaneous commonly used macros and functions.
#define WIN_STRING_DIR_SEP
#define UNIX_STRING_DIR_SEP