KiCad PCB EDA Suite
Loading...
Searching...
No Matches
env_vars.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 The 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 <build_version.h>
21#include <env_vars.h>
23#include <core/kicad_algo.h>
24
25#include <map>
26
27#include <wx/regex.h>
28#include <wx/translation.h>
29#include <wx/utils.h>
30
37static const std::vector<wxString> predefinedEnvVars = {
38 wxS( "KIPRJMOD" ),
39 ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ),
40 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ),
41 ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ),
42 ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
43 wxS( "KICAD_USER_TEMPLATE_DIR" ),
44 wxS( "KICAD_PTEMPLATES" ),
45 ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
46};
47
48
49bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
50{
51 for( const wxString& s : predefinedEnvVars )
52 {
53 if( s == aEnvVar )
54 return true;
55 }
56
57 return false;
58}
59
60
61const std::vector<wxString>& ENV_VAR::GetPredefinedEnvVars()
62{
63 return predefinedEnvVars;
64}
65
66
67void ENV_VAR::GetEnvVarAutocompleteTokens( wxArrayString* aVars )
68{
69 for( const wxString& var : ENV_VAR::GetPredefinedEnvVars() )
70 {
71 if( !alg::contains( *aVars, var ) )
72 aVars->push_back( var );
73 }
74}
75
76
77wxString ENV_VAR::GetVersionedEnvVarName( const wxString& aBaseName )
78{
79 int version = 0;
80 std::tie(version, std::ignore, std::ignore) = GetMajorMinorPatchTuple();
81
82 return wxString::Format( "KICAD%d_%s", version, aBaseName );
83}
84
85
86std::optional<wxString> ENV_VAR::GetVersionedEnvVarValue( const ENV_VAR_MAP& aMap,
87 const wxString& aBaseName )
88{
89 wxString exactMatch = ENV_VAR::GetVersionedEnvVarName( aBaseName );
90
91 if( aMap.count( exactMatch ) )
92 return aMap.at( exactMatch ).GetValue();
93
94 wxString partialMatch = wxString::Format( "KICAD*_%s", aBaseName );
95
96 for( const auto& [k, v] : aMap )
97 {
98 if( k.Matches( partialMatch ) )
99 return v.GetValue();
100 }
101
102 return std::nullopt;
103}
104
105
106static void initialiseEnvVarHelp( std::map<wxString, wxString>& aMap )
107{
108 // Set up dynamically, as we want to be able to use _() translations,
109 // which can't be done statically
110 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) )] =
111 _( "The base path of locally installed system footprint libraries (.pretty folders)." );
112
113 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) )] =
114 _( "The base path of system footprint 3D shapes (.3Dshapes folders)." );
115
116 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) )] =
117 _( "The base path of the locally installed symbol libraries." );
118
119 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) )] =
120 _( "A directory containing project templates installed with KiCad." );
121
122 aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
123 _( "Optional. Can be defined if you want to create your own project templates folder." );
124
125 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) )] =
126 _( "A directory containing 3rd party plugins, libraries and other downloadable content." );
127
128 aMap[wxS( "KIPRJMOD" )] =
129 _( "Internally defined by KiCad (cannot be edited) and is set to the absolute path of the currently "
130 "loaded project file. This environment variable can be used to define files and paths relative "
131 "to the currently loaded project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be "
132 "defined as a folder containing a project specific footprint library named footprints.pretty." );
133
134 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "SCRIPTING_DIR" ) )] =
135 _( "A directory containing system-wide scripts installed with KiCad." );
136
137 aMap[ENV_VAR::GetVersionedEnvVarName( wxS( "USER_SCRIPTING_DIR" ) )] =
138 _( "A directory containing user-specific scripts installed with KiCad." );
139
140 // Deprecated vars
141#define DEP( var ) wxString::Format( _( "Deprecated version of %s." ), var )
142
143 aMap[wxS( "KICAD_PTEMPLATES" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ) );
144 aMap[wxS( "KISYS3DMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
145 aMap[wxS( "KISYSMOD" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ) );
146 aMap[wxS( "KICAD_SYMBOL_DIR" )] = DEP( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ) );
147
148#undef DEP
149}
150
151
152wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
153{
154 static std::map<wxString, wxString> envVarHelpText;
155
156 if( envVarHelpText.size() == 0 )
157 initialiseEnvVarHelp( envVarHelpText );
158
159 return envVarHelpText[ aEnvVar ];
160}
161
162
163template<>
164std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
165{
166 wxString env;
167
168 if( wxGetEnv( aEnvVarName, &env ) )
169 {
170 double value;
171
172 if( env.ToDouble( &value ) )
173 return value;
174 }
175
176 return std::nullopt;
177}
178
179
180template<>
181std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
182{
183 std::optional<wxString> optValue;
184 wxString env;
185
186 if( wxGetEnv( aEnvVarName, &env ) )
187 optValue = env;
188
189 return optValue;
190}
const std::tuple< int, int, int > & GetMajorMinorPatchTuple()
Get the build version numbers as a tuple.
#define _(s)
static void initialiseEnvVarHelp(std::map< wxString, wxString > &aMap)
Definition: env_vars.cpp:106
#define DEP(var)
static const std::vector< wxString > predefinedEnvVars
List of pre-defined environment variables.
Definition: env_vars.cpp:37
Functions related to environment variables, including help functions.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
KICOMMON_API wxString LookUpEnvVarHelp(const wxString &aEnvVar)
Look up long-form help text for a given environment variable.
Definition: env_vars.cpp:152
KICOMMON_API void GetEnvVarAutocompleteTokens(wxArrayString *aVars)
Return autocomplete tokens for environment variables for Scintilla.
Definition: env_vars.cpp:67
KICOMMON_API std::optional< VAL_TYPE > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
Definition: env_vars.cpp:86
KICOMMON_API const std::vector< wxString > & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition: env_vars.cpp:61
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition: env_vars.cpp:77
KICOMMON_API bool IsEnvVarImmutable(const wxString &aEnvVar)
Determine if an environment variable is "predefined", i.e.
Definition: env_vars.cpp:49
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100