xref: /netbsd-src/external/bsd/atf/dist/atf-c++/config.cpp (revision d780102efefa02003390cc43ea410dbd0ebb4a85)
1e2207522Sjmmv //
2e2207522Sjmmv // Automated Testing Framework (atf)
3e2207522Sjmmv //
4*a551a20fSjmmv // Copyright (c) 2007 The NetBSD Foundation, Inc.
5e2207522Sjmmv // All rights reserved.
6e2207522Sjmmv //
7e2207522Sjmmv // Redistribution and use in source and binary forms, with or without
8e2207522Sjmmv // modification, are permitted provided that the following conditions
9e2207522Sjmmv // are met:
10e2207522Sjmmv // 1. Redistributions of source code must retain the above copyright
11e2207522Sjmmv //    notice, this list of conditions and the following disclaimer.
12e2207522Sjmmv // 2. Redistributions in binary form must reproduce the above copyright
13e2207522Sjmmv //    notice, this list of conditions and the following disclaimer in the
14e2207522Sjmmv //    documentation and/or other materials provided with the distribution.
15e2207522Sjmmv //
16e2207522Sjmmv // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17e2207522Sjmmv // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18e2207522Sjmmv // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19e2207522Sjmmv // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20e2207522Sjmmv // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21e2207522Sjmmv // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e2207522Sjmmv // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23e2207522Sjmmv // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24e2207522Sjmmv // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25e2207522Sjmmv // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26e2207522Sjmmv // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27e2207522Sjmmv // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28e2207522Sjmmv //
29e2207522Sjmmv 
30e2207522Sjmmv #include <map>
31e2207522Sjmmv 
32e2207522Sjmmv extern "C" {
33e2207522Sjmmv #include "atf-c/config.h"
34e2207522Sjmmv }
35e2207522Sjmmv 
36895f502bSjmmv #include "config.hpp"
37895f502bSjmmv 
38895f502bSjmmv #include "detail/env.hpp"
39895f502bSjmmv #include "detail/sanity.hpp"
40e2207522Sjmmv 
41e2207522Sjmmv static std::map< std::string, std::string > m_variables;
42e2207522Sjmmv 
43e2207522Sjmmv //
44e2207522Sjmmv // Adds all predefined standard build-time variables to the m_variables
45e2207522Sjmmv // map, considering the values a user may have provided in the environment.
46e2207522Sjmmv //
47e2207522Sjmmv // Can only be called once during the program's lifetime.
48e2207522Sjmmv //
49e2207522Sjmmv static
50e2207522Sjmmv void
init_variables(void)51e2207522Sjmmv init_variables(void)
52e2207522Sjmmv {
53e2207522Sjmmv     PRE(m_variables.empty());
54e2207522Sjmmv 
5512aa0b5aSjmmv     m_variables["atf_build_cc"] = atf_config_get("atf_build_cc");
5612aa0b5aSjmmv     m_variables["atf_build_cflags"] = atf_config_get("atf_build_cflags");
5712aa0b5aSjmmv     m_variables["atf_build_cpp"] = atf_config_get("atf_build_cpp");
5812aa0b5aSjmmv     m_variables["atf_build_cppflags"] = atf_config_get("atf_build_cppflags");
5912aa0b5aSjmmv     m_variables["atf_build_cxx"] = atf_config_get("atf_build_cxx");
6012aa0b5aSjmmv     m_variables["atf_build_cxxflags"] = atf_config_get("atf_build_cxxflags");
6112aa0b5aSjmmv     m_variables["atf_includedir"] = atf_config_get("atf_includedir");
62895f502bSjmmv     m_variables["atf_libexecdir"] = atf_config_get("atf_libexecdir");
63e2207522Sjmmv     m_variables["atf_pkgdatadir"] = atf_config_get("atf_pkgdatadir");
64e2207522Sjmmv     m_variables["atf_shell"] = atf_config_get("atf_shell");
65e2207522Sjmmv     m_variables["atf_workdir"] = atf_config_get("atf_workdir");
66e2207522Sjmmv 
67e2207522Sjmmv     POST(!m_variables.empty());
68e2207522Sjmmv }
69e2207522Sjmmv 
70e2207522Sjmmv const std::string&
get(const std::string & varname)71e2207522Sjmmv atf::config::get(const std::string& varname)
72e2207522Sjmmv {
73e2207522Sjmmv     if (m_variables.empty())
74e2207522Sjmmv         init_variables();
75e2207522Sjmmv 
76e2207522Sjmmv     PRE(has(varname));
77e2207522Sjmmv     return m_variables[varname];
78e2207522Sjmmv }
79e2207522Sjmmv 
80e2207522Sjmmv const std::map< std::string, std::string >&
get_all(void)81e2207522Sjmmv atf::config::get_all(void)
82e2207522Sjmmv {
83e2207522Sjmmv     if (m_variables.empty())
84e2207522Sjmmv         init_variables();
85e2207522Sjmmv 
86e2207522Sjmmv     return m_variables;
87e2207522Sjmmv }
88e2207522Sjmmv 
89e2207522Sjmmv bool
has(const std::string & varname)90e2207522Sjmmv atf::config::has(const std::string& varname)
91e2207522Sjmmv {
92e2207522Sjmmv     if (m_variables.empty())
93e2207522Sjmmv         init_variables();
94e2207522Sjmmv 
95e2207522Sjmmv     return m_variables.find(varname) != m_variables.end();
96e2207522Sjmmv }
97e2207522Sjmmv 
98e2207522Sjmmv extern "C" {
99e2207522Sjmmv void __atf_config_reinit(void);
100e2207522Sjmmv }
101e2207522Sjmmv 
102e2207522Sjmmv namespace atf {
103e2207522Sjmmv namespace config {
104e2207522Sjmmv //
105e2207522Sjmmv // Auxiliary function for the t_config test program so that it can
106e2207522Sjmmv // revert the configuration's global status to an empty state and
107e2207522Sjmmv // do new tests from there on.
108e2207522Sjmmv //
109e2207522Sjmmv // Ideally this shouldn't be part of the production library... but
110e2207522Sjmmv // this is so small that it does not matter.
111e2207522Sjmmv //
112e2207522Sjmmv void
__reinit(void)113e2207522Sjmmv __reinit(void)
114e2207522Sjmmv {
115e2207522Sjmmv     __atf_config_reinit();
116e2207522Sjmmv     m_variables.clear();
117e2207522Sjmmv }
118e2207522Sjmmv } // namespace config
119e2207522Sjmmv } // namespace atf
120