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