1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc // documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc #include <cassert>
31*0a6a1f1dSLionel Sambuc #include <map>
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #include "config.hpp"
34*0a6a1f1dSLionel Sambuc #include "env.hpp"
35*0a6a1f1dSLionel Sambuc #include "text.hpp"
36*0a6a1f1dSLionel Sambuc
37*0a6a1f1dSLionel Sambuc static std::map< std::string, std::string > m_variables;
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc static struct var {
40*0a6a1f1dSLionel Sambuc const char *name;
41*0a6a1f1dSLionel Sambuc const char *default_value;
42*0a6a1f1dSLionel Sambuc bool can_be_empty;
43*0a6a1f1dSLionel Sambuc } vars[] = {
44*0a6a1f1dSLionel Sambuc { "ATF_ARCH", ATF_ARCH, false, },
45*0a6a1f1dSLionel Sambuc { "ATF_BUILD_CC", ATF_BUILD_CC, false, },
46*0a6a1f1dSLionel Sambuc { "ATF_BUILD_CFLAGS", ATF_BUILD_CFLAGS, true, },
47*0a6a1f1dSLionel Sambuc { "ATF_BUILD_CPP", ATF_BUILD_CPP, false, },
48*0a6a1f1dSLionel Sambuc { "ATF_BUILD_CPPFLAGS", ATF_BUILD_CPPFLAGS, true, },
49*0a6a1f1dSLionel Sambuc { "ATF_BUILD_CXX", ATF_BUILD_CXX, false, },
50*0a6a1f1dSLionel Sambuc { "ATF_BUILD_CXXFLAGS", ATF_BUILD_CXXFLAGS, true, },
51*0a6a1f1dSLionel Sambuc { "ATF_CONFDIR", ATF_CONFDIR, false, },
52*0a6a1f1dSLionel Sambuc { "ATF_INCLUDEDIR", ATF_INCLUDEDIR, false, },
53*0a6a1f1dSLionel Sambuc { "ATF_LIBDIR", ATF_LIBDIR, false, },
54*0a6a1f1dSLionel Sambuc { "ATF_LIBEXECDIR", ATF_LIBEXECDIR, false, },
55*0a6a1f1dSLionel Sambuc { "ATF_MACHINE", ATF_MACHINE, false, },
56*0a6a1f1dSLionel Sambuc { "ATF_PKGDATADIR", ATF_PKGDATADIR, false, },
57*0a6a1f1dSLionel Sambuc { "ATF_SHELL", ATF_SHELL, false, },
58*0a6a1f1dSLionel Sambuc { "ATF_WORKDIR", ATF_WORKDIR, false, },
59*0a6a1f1dSLionel Sambuc { NULL, NULL, false, },
60*0a6a1f1dSLionel Sambuc };
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc //
63*0a6a1f1dSLionel Sambuc // Adds all predefined standard build-time variables to the m_variables
64*0a6a1f1dSLionel Sambuc // map, considering the values a user may have provided in the environment.
65*0a6a1f1dSLionel Sambuc //
66*0a6a1f1dSLionel Sambuc // Can only be called once during the program's lifetime.
67*0a6a1f1dSLionel Sambuc //
68*0a6a1f1dSLionel Sambuc static
69*0a6a1f1dSLionel Sambuc void
init_variables(void)70*0a6a1f1dSLionel Sambuc init_variables(void)
71*0a6a1f1dSLionel Sambuc {
72*0a6a1f1dSLionel Sambuc assert(m_variables.empty());
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc for (struct var* v = vars; v->name != NULL; v++) {
75*0a6a1f1dSLionel Sambuc const std::string varname = tools::text::to_lower(v->name);
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambuc if (tools::env::has(v->name)) {
78*0a6a1f1dSLionel Sambuc const std::string envval = tools::env::get(v->name);
79*0a6a1f1dSLionel Sambuc if (envval.empty() && !v->can_be_empty)
80*0a6a1f1dSLionel Sambuc m_variables[varname] = v->default_value;
81*0a6a1f1dSLionel Sambuc else
82*0a6a1f1dSLionel Sambuc m_variables[varname] = envval;
83*0a6a1f1dSLionel Sambuc } else {
84*0a6a1f1dSLionel Sambuc m_variables[varname] = v->default_value;
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc }
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc assert(!m_variables.empty());
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc const std::string&
get(const std::string & varname)92*0a6a1f1dSLionel Sambuc tools::config::get(const std::string& varname)
93*0a6a1f1dSLionel Sambuc {
94*0a6a1f1dSLionel Sambuc if (m_variables.empty())
95*0a6a1f1dSLionel Sambuc init_variables();
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc assert(has(varname));
98*0a6a1f1dSLionel Sambuc return m_variables[varname];
99*0a6a1f1dSLionel Sambuc }
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc const std::map< std::string, std::string >&
get_all(void)102*0a6a1f1dSLionel Sambuc tools::config::get_all(void)
103*0a6a1f1dSLionel Sambuc {
104*0a6a1f1dSLionel Sambuc if (m_variables.empty())
105*0a6a1f1dSLionel Sambuc init_variables();
106*0a6a1f1dSLionel Sambuc
107*0a6a1f1dSLionel Sambuc return m_variables;
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc bool
has(const std::string & varname)111*0a6a1f1dSLionel Sambuc tools::config::has(const std::string& varname)
112*0a6a1f1dSLionel Sambuc {
113*0a6a1f1dSLionel Sambuc if (m_variables.empty())
114*0a6a1f1dSLionel Sambuc init_variables();
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambuc return m_variables.find(varname) != m_variables.end();
117*0a6a1f1dSLionel Sambuc }
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc namespace tools {
120*0a6a1f1dSLionel Sambuc namespace config {
121*0a6a1f1dSLionel Sambuc //
122*0a6a1f1dSLionel Sambuc // Auxiliary function for the t_config test program so that it can
123*0a6a1f1dSLionel Sambuc // revert the configuration's global status to an empty state and
124*0a6a1f1dSLionel Sambuc // do new tests from there on.
125*0a6a1f1dSLionel Sambuc //
126*0a6a1f1dSLionel Sambuc // Ideally this shouldn't be part of the production library... but
127*0a6a1f1dSLionel Sambuc // this is so small that it does not matter.
128*0a6a1f1dSLionel Sambuc //
129*0a6a1f1dSLionel Sambuc void
__reinit(void)130*0a6a1f1dSLionel Sambuc __reinit(void)
131*0a6a1f1dSLionel Sambuc {
132*0a6a1f1dSLionel Sambuc m_variables.clear();
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc } // namespace config
135*0a6a1f1dSLionel Sambuc } // namespace tools
136