2018-09-28 22:08:31 +01:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2018-09-28 22:08:31 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-01-16 17:20:45 -05:00
|
|
|
#include <build_version.h>
|
2018-09-28 22:08:31 +01:00
|
|
|
#include <env_vars.h>
|
2024-01-16 17:20:45 -05:00
|
|
|
#include <settings/environment.h>
|
2018-09-28 22:08:31 +01:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2024-01-16 17:20:45 -05:00
|
|
|
#include <wx/regex.h>
|
2020-10-23 20:50:09 -04:00
|
|
|
#include <wx/translation.h>
|
2019-03-22 21:15:26 +00:00
|
|
|
#include <wx/utils.h>
|
|
|
|
|
2018-09-28 22:08:31 +01:00
|
|
|
using STRING_MAP = std::map<wxString, wxString>;
|
|
|
|
|
2025-01-12 11:27:18 -05:00
|
|
|
/**
|
|
|
|
* List of pre-defined environment variables.
|
2018-09-28 22:08:31 +01:00
|
|
|
*
|
2025-01-12 11:27:18 -05:00
|
|
|
* @todo Instead of defining these values here, extract them from elsewhere in the program
|
|
|
|
* (where they are originally defined).
|
2018-09-28 22:08:31 +01:00
|
|
|
*/
|
2021-06-21 20:35:11 -04:00
|
|
|
static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars = {
|
2023-01-17 07:42:30 -05:00
|
|
|
wxS( "KIPRJMOD" ),
|
2024-01-16 17:20:45 -05:00
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ),
|
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
|
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ),
|
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
|
2023-01-17 07:42:30 -05:00
|
|
|
wxS( "KICAD_USER_TEMPLATE_DIR" ),
|
|
|
|
wxS( "KICAD_PTEMPLATES" ),
|
2024-01-16 17:20:45 -05:00
|
|
|
ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
|
2018-09-28 22:08:31 +01:00
|
|
|
};
|
|
|
|
|
2025-01-12 11:27:18 -05:00
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
|
2018-09-28 22:08:31 +01:00
|
|
|
{
|
2022-07-29 22:02:35 +01:00
|
|
|
for( const wxString& s : predefinedEnvVars )
|
2018-09-28 22:08:31 +01:00
|
|
|
{
|
|
|
|
if( s == aEnvVar )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
const ENV_VAR::ENV_VAR_LIST& ENV_VAR::GetPredefinedEnvVars()
|
2018-09-28 22:08:31 +01:00
|
|
|
{
|
2021-06-21 20:35:11 -04:00
|
|
|
return predefinedEnvVars;
|
2018-09-28 22:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-16 17:20:45 -05:00
|
|
|
wxString ENV_VAR::GetVersionedEnvVarName( const wxString& aBaseName )
|
|
|
|
{
|
|
|
|
int version = 0;
|
|
|
|
std::tie(version, std::ignore, std::ignore) = GetMajorMinorPatchTuple();
|
|
|
|
|
|
|
|
return wxString::Format( "KICAD%d_%s", version, aBaseName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<wxString> ENV_VAR::GetVersionedEnvVarValue( const ENV_VAR_MAP& aMap,
|
|
|
|
const wxString& aBaseName )
|
|
|
|
{
|
|
|
|
wxString exactMatch = ENV_VAR::GetVersionedEnvVarName( aBaseName );
|
|
|
|
|
|
|
|
if( aMap.count( exactMatch ) )
|
|
|
|
return aMap.at( exactMatch ).GetValue();
|
|
|
|
|
|
|
|
wxString partialMatch = wxString::Format( "KICAD*_%s", aBaseName );
|
|
|
|
|
|
|
|
for( const auto& [k, v] : aMap )
|
|
|
|
{
|
|
|
|
if( k.Matches( partialMatch ) )
|
|
|
|
return v.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
static void initialiseEnvVarHelp( STRING_MAP& aMap )
|
2018-09-28 22:08:31 +01:00
|
|
|
{
|
|
|
|
// Set up dynamically, as we want to be able to use _() translations,
|
|
|
|
// which can't be done statically
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] =
|
2018-09-28 22:08:31 +01:00
|
|
|
_( "The base path of locally installed system "
|
|
|
|
"footprint libraries (.pretty folders).");
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) )] =
|
2018-09-28 22:08:31 +01:00
|
|
|
_( "The base path of system footprint 3D shapes (.3Dshapes folders).");
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] =
|
2018-09-28 22:08:31 +01:00
|
|
|
_( "The base path of the locally installed symbol libraries.");
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) )] =
|
2018-09-28 22:08:31 +01:00
|
|
|
_( "A directory containing project templates installed with KiCad.");
|
2023-01-17 07:42:30 -05:00
|
|
|
aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
|
2018-09-28 22:08:31 +01:00
|
|
|
_( "Optional. Can be defined if you want to create your own project "
|
|
|
|
"templates folder.");
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] =
|
2020-12-08 10:39:45 -08:00
|
|
|
_( "A directory containing 3rd party plugins, libraries and other "
|
|
|
|
"downloadable content.");
|
2023-01-17 07:42:30 -05:00
|
|
|
aMap[wxS( "KIPRJMOD" )] =
|
2018-09-28 22:08:31 +01:00
|
|
|
_("Internally defined by KiCad (cannot be edited) and is set "
|
|
|
|
"to the absolute path of the currently loaded project file. This environment "
|
|
|
|
"variable can be used to define files and paths relative to the currently loaded "
|
|
|
|
"project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
|
|
|
|
"folder containing a project specific footprint library named footprints.pretty." );
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SCRIPTING_DIR" ) )] =
|
2020-12-31 13:22:44 -08:00
|
|
|
_( "A directory containing system-wide scripts installed with KiCad" );
|
2024-01-16 17:20:45 -05:00
|
|
|
aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] =
|
2020-12-31 13:22:44 -08:00
|
|
|
_( "A directory containing user-specific scripts installed with KiCad" );
|
2018-09-28 22:08:31 +01:00
|
|
|
|
|
|
|
// Deprecated vars
|
2024-12-25 13:15:18 +00:00
|
|
|
#define DEP( var ) wxString::Format( _( "Deprecated version of %s." ), var )
|
|
|
|
|
2025-01-12 11:27:18 -05:00
|
|
|
aMap[wxS( "KICAD_PTEMPLATES" )] =
|
|
|
|
DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ) );
|
2024-12-25 13:15:18 +00:00
|
|
|
aMap[wxS( "KISYS3DMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
|
|
|
|
aMap[wxS( "KISYSMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ) );
|
|
|
|
aMap[wxS( "KICAD_SYMBOL_DIR" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ) );
|
|
|
|
|
|
|
|
#undef DEP
|
2018-09-28 22:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
|
2018-09-28 22:08:31 +01:00
|
|
|
{
|
2021-06-21 20:35:11 -04:00
|
|
|
static STRING_MAP envVarHelpText;
|
2018-09-28 22:08:31 +01:00
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
if( envVarHelpText.size() == 0 )
|
|
|
|
initialiseEnvVarHelp( envVarHelpText );
|
2018-09-28 22:08:31 +01:00
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
return envVarHelpText[ aEnvVar ];
|
2019-03-22 21:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
2022-08-25 15:50:47 -07:00
|
|
|
std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
|
2019-03-22 21:15:26 +00:00
|
|
|
{
|
|
|
|
wxString env;
|
2021-06-30 17:28:46 -04:00
|
|
|
|
2019-03-22 21:15:26 +00:00
|
|
|
if( wxGetEnv( aEnvVarName, &env ) )
|
|
|
|
{
|
|
|
|
double value;
|
2021-06-30 17:28:46 -04:00
|
|
|
|
2019-03-22 21:15:26 +00:00
|
|
|
if( env.ToDouble( &value ) )
|
2021-07-06 13:32:34 -04:00
|
|
|
return value;
|
2019-03-22 21:15:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:50:47 -07:00
|
|
|
return std::nullopt;
|
2019-03-22 21:15:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 17:28:46 -04:00
|
|
|
|
2019-03-22 21:15:26 +00:00
|
|
|
template<>
|
2022-08-25 15:50:47 -07:00
|
|
|
std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
|
2019-03-22 21:15:26 +00:00
|
|
|
{
|
2022-08-25 15:50:47 -07:00
|
|
|
std::optional<wxString> optValue;
|
2019-03-22 21:15:26 +00:00
|
|
|
|
|
|
|
wxString env;
|
2021-06-30 17:28:46 -04:00
|
|
|
|
2019-03-22 21:15:26 +00:00
|
|
|
if( wxGetEnv( aEnvVarName, &env ) )
|
|
|
|
{
|
2021-06-21 20:35:11 -04:00
|
|
|
optValue = env;
|
2019-03-22 21:15:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 20:35:11 -04:00
|
|
|
return optValue;
|
2020-10-29 16:43:34 -07:00
|
|
|
}
|