Move executable path to PATHS to avoid a circular dependency

This commit is contained in:
Marek Roszko 2023-09-23 20:21:37 -04:00
parent 3c18e216b1
commit e3285c234a
4 changed files with 66 additions and 57 deletions

View File

@ -24,7 +24,6 @@
#include <kiplatform/environment.h>
#include <paths.h>
#include <pgm_base.h>
#include <config.h>
#include <build_version.h>
#include <macros.h>
@ -166,7 +165,7 @@ wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir )
#elif defined( __WXMSW__ )
path = getWindowsKiCadRoot();
#else
path = Pgm().GetExecutablePath() + wxT( ".." );
path = GetExecutablePath() + wxT( ".." );
#endif
}
else
@ -265,7 +264,7 @@ wxString PATHS::GetStockPluginsPath()
wxFileName fn;
#if defined( __WXMSW__ )
fn.AssignDir( Pgm().GetExecutablePath() );
fn.AssignDir( GetExecutablePath() );
fn.AppendDir( wxT( "scripting" ) );
#else
fn.AssignDir( PATHS::GetStockDataPath( false ) );
@ -296,7 +295,7 @@ wxString PATHS::GetStockPlugins3DPath()
}
else
{
fn.AssignDir( Pgm().GetExecutablePath() );
fn.AssignDir( GetExecutablePath() );
}
fn.AppendDir( wxT( "plugins" ) );
@ -457,7 +456,7 @@ wxString PATHS::GetWindowsFontConfigDir()
wxString PATHS::getWindowsKiCadRoot()
{
wxFileName root( Pgm().GetExecutablePath() + wxT( "/../" ) );
wxFileName root( GetExecutablePath() + wxT( "/../" ) );
root.MakeAbsolute();
return root.GetPathWithSep();
@ -499,4 +498,52 @@ wxString PATHS::CalculateUserSettingsPath( bool aIncludeVer, bool aUseEnv )
cfgpath.AppendDir( GetMajorMinorVersion().ToStdString() );
return cfgpath.GetPath();
}
const wxString& PATHS::GetExecutablePath()
{
static wxString exe_path;
if( exe_path.empty() )
{
wxString bin_dir = wxStandardPaths::Get().GetExecutablePath();
#ifdef __WXMAC__
// On OSX GetExecutablePath() will always point to main
// bundle directory, e.g., /Applications/kicad.app/
wxFileName fn( bin_dir );
if( fn.GetName() == wxT( "kicad" ) || fn.GetName() == wxT( "kicad-cli" ) )
{
// kicad launcher, so just remove the Contents/MacOS part
fn.RemoveLastDir();
fn.RemoveLastDir();
}
else
{
// standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
fn.RemoveLastDir();
fn.RemoveLastDir();
fn.RemoveLastDir();
fn.RemoveLastDir();
fn.RemoveLastDir();
}
bin_dir = fn.GetPath() + wxT( "/" );
#else
// Use unix notation for paths. I am not sure this is a good idea,
// but it simplifies compatibility between Windows and Unices.
// However it is a potential problem in path handling under Windows.
bin_dir.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
// Remove file name form command line:
while( bin_dir.Last() != '/' && !bin_dir.IsEmpty() )
bin_dir.RemoveLast();
#endif
exe_path = bin_dir;
}
return exe_path;
}

View File

@ -514,7 +514,6 @@ bool PGM_BASE::InitPgm( bool aHeadless, bool aSkipPyInit, bool aIsUnitTest )
// Analyze the command line & initialize the binary path
wxString tmp;
setExecutablePath();
SetLanguagePath();
SetDefaultLanguage( tmp );
@ -594,48 +593,6 @@ bool PGM_BASE::InitPgm( bool aHeadless, bool aSkipPyInit, bool aIsUnitTest )
}
bool PGM_BASE::setExecutablePath()
{
m_bin_dir = wxStandardPaths::Get().GetExecutablePath();
#ifdef __WXMAC__
// On OSX Pgm().GetExecutablePath() will always point to main
// bundle directory, e.g., /Applications/kicad.app/
wxFileName fn( m_bin_dir );
if( fn.GetName() == wxT( "kicad" ) || fn.GetName() == wxT( "kicad-cli" ) )
{
// kicad launcher, so just remove the Contents/MacOS part
fn.RemoveLastDir();
fn.RemoveLastDir();
}
else
{
// standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
fn.RemoveLastDir();
fn.RemoveLastDir();
fn.RemoveLastDir();
fn.RemoveLastDir();
fn.RemoveLastDir();
}
m_bin_dir = fn.GetPath() + wxT( "/" );
#else
// Use unix notation for paths. I am not sure this is a good idea,
// but it simplifies compatibility between Windows and Unices.
// However it is a potential problem in path handling under Windows.
m_bin_dir.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
// Remove file name form command line:
while( m_bin_dir.Last() != '/' && !m_bin_dir.IsEmpty() )
m_bin_dir.RemoveLast();
#endif
return true;
}
void PGM_BASE::loadCommonSettings()
{
m_text_editor = GetCommonSettings()->m_System.text_editor;
@ -991,3 +948,9 @@ void PGM_BASE::HandleAssert( const wxString& aFile, int aLine, const wxString& a
}
#endif
}
const wxString& PGM_BASE::GetExecutablePath() const
{
return PATHS::GetExecutablePath();
}

View File

@ -23,6 +23,12 @@
#include <wx/filename.h>
#include <wx/string.h>
/**
* @note Do we really need these defined?
*/
#define UNIX_STRING_DIR_SEP wxT( "/" )
#define WIN_STRING_DIR_SEP wxT( "\\" )
/**
* Helper class to centralize the paths used throughout kicad
*/
@ -204,6 +210,7 @@ public:
*/
static wxString CalculateUserSettingsPath( bool aIncludeVer = true, bool aUseEnv = true );
static const wxString& GetExecutablePath();
private:
// we are a static helper
PATHS() {}

View File

@ -171,7 +171,7 @@ public:
virtual const wxString& GetKicadEnvVariable() const { return m_kicad_env; }
virtual const wxString& GetExecutablePath() const { return m_bin_dir; }
virtual const wxString& GetExecutablePath() const;
virtual wxLocale* GetLocale() { return m_locale; }
@ -387,13 +387,6 @@ protected:
/// Trap all changes in here, simplifies debugging
void setLanguageId( int aId ) { m_language_id = aId; }
/**
* Find the path to the executable and stores it in PGM_BASE::m_bin_dir.
*
* @return true if success, else false.
*/
bool setExecutablePath();
#ifdef KICAD_USE_SENTRY
void sentryInit();
void sentryPrompt();
@ -411,7 +404,6 @@ protected:
std::unique_ptr<wxSingleInstanceChecker> m_pgm_checker;
wxString m_bin_dir; /// full path to this program
wxString m_kicad_env; /// The KICAD system environment variable.
wxLocale* m_locale;