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