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 <env_vars.h>
21
22#include <map>
23
24#include <wx/translation.h>
25#include <wx/utils.h>
26
27using STRING_MAP = std::map<wxString, wxString>;
28
29/*
30 * List of pre-defined environment variables
31 *
32 * TODO - Instead of defining these values here,
33 * extract them from elsewhere in the program
34 * (where they are originally defined)
35 */
37 wxS( "KIPRJMOD" ),
38 wxS( "KICAD7_SYMBOL_DIR" ),
39 wxS( "KICAD7_3DMODEL_DIR" ),
40 wxS( "KICAD7_FOOTPRINT_DIR" ),
41 wxS( "KICAD7_TEMPLATE_DIR" ),
42 wxS( "KICAD_USER_TEMPLATE_DIR" ),
43 wxS( "KICAD_PTEMPLATES" ),
44 wxS( "KICAD7_3RD_PARTY" ),
45};
46
47
48bool ENV_VAR::IsEnvVarImmutable( const wxString& aEnvVar )
49{
50 for( const wxString& s : predefinedEnvVars )
51 {
52 if( s == aEnvVar )
53 return true;
54 }
55
56 return false;
57}
58
59
61{
62 return predefinedEnvVars;
63}
64
65
66static void initialiseEnvVarHelp( STRING_MAP& aMap )
67{
68 // Set up dynamically, as we want to be able to use _() translations,
69 // which can't be done statically
70 aMap[wxS( "KICAD7_FOOTPRINT_DIR" )] =
71 _( "The base path of locally installed system "
72 "footprint libraries (.pretty folders).");
73 aMap[wxS( "KICAD7_3DMODEL_DIR" )] =
74 _( "The base path of system footprint 3D shapes (.3Dshapes folders).");
75 aMap[wxS( "KICAD7_SYMBOL_DIR" )] =
76 _( "The base path of the locally installed symbol libraries.");
77 aMap[wxS( "KICAD7_TEMPLATE_DIR" )] =
78 _( "A directory containing project templates installed with KiCad.");
79 aMap[wxS( "KICAD_USER_TEMPLATE_DIR" )] =
80 _( "Optional. Can be defined if you want to create your own project "
81 "templates folder.");
82 aMap[wxS( "KICAD7_3RD_PARTY" )] =
83 _( "A directory containing 3rd party plugins, libraries and other "
84 "downloadable content.");
85 aMap[wxS( "KIPRJMOD" )] =
86 _("Internally defined by KiCad (cannot be edited) and is set "
87 "to the absolute path of the currently loaded project file. This environment "
88 "variable can be used to define files and paths relative to the currently loaded "
89 "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
90 "folder containing a project specific footprint library named footprints.pretty." );
91 aMap[wxS( "KICAD7_SCRIPTING_DIR" )] =
92 _( "A directory containing system-wide scripts installed with KiCad" );
93 aMap[wxS( "KICAD7_USER_SCRIPTING_DIR" )] =
94 _( "A directory containing user-specific scripts installed with KiCad" );
95
96 // Deprecated vars
97 aMap[wxS( "KICAD_PTEMPLATES" )] =
98 _( "Deprecated version of KICAD_TEMPLATE_DIR.");
99 aMap[wxS( "KISYS3DMOD" )] =
100 _( "Deprecated version of KICAD7_3DMODEL_DIR." );
101 aMap[wxS( "KISYSMOD" )] =
102 _( "Deprecated version of KICAD7_FOOTPRINT_DIR." );
103 aMap[wxS( "KICAD_SYMBOL_DIR" )] =
104 _( "Deprecated version of KICAD_SYMBOL_DIR.");
105}
106
107
108wxString ENV_VAR::LookUpEnvVarHelp( const wxString& aEnvVar )
109{
110 static STRING_MAP envVarHelpText;
111
112 if( envVarHelpText.size() == 0 )
113 initialiseEnvVarHelp( envVarHelpText );
114
115 return envVarHelpText[ aEnvVar ];
116}
117
118
119template<>
120std::optional<double> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
121{
122 wxString env;
123
124 if( wxGetEnv( aEnvVarName, &env ) )
125 {
126 double value;
127
128 if( env.ToDouble( &value ) )
129 return value;
130 }
131
132 return std::nullopt;
133}
134
135
136template<>
137std::optional<wxString> ENV_VAR::GetEnvVar( const wxString& aEnvVarName )
138{
139 std::optional<wxString> optValue;
140
141 wxString env;
142
143 if( wxGetEnv( aEnvVarName, &env ) )
144 {
145 optValue = env;
146 }
147
148 return optValue;
149}
#define _(s)
static const ENV_VAR::ENV_VAR_LIST predefinedEnvVars
Definition: env_vars.cpp:36
static void initialiseEnvVarHelp(STRING_MAP &aMap)
Definition: env_vars.cpp:66
std::map< wxString, wxString > STRING_MAP
Definition: env_vars.cpp:27
Functions related to environment variables, including help functions.
std::optional< VAL_TYPE > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.
wxString LookUpEnvVarHelp(const wxString &aEnvVar)
Look up long-form help text for a given environment variable.
Definition: env_vars.cpp:108
const ENV_VAR_LIST & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition: env_vars.cpp:60
bool IsEnvVarImmutable(const wxString &aEnvVar)
Determine if an environment variable is "predefined", i.e.
Definition: env_vars.cpp:48
std::vector< wxString > ENV_VAR_LIST
Definition: env_vars.h:34