xref: /freebsd/contrib/atf/atf-c++/build.cpp (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2009 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 extern "C" {
31 #include "atf-c/build.h"
32 #include "atf-c/error.h"
33 #include "atf-c/utils.h"
34 }
35 
36 #include "build.hpp"
37 
38 #include "detail/exceptions.hpp"
39 #include "detail/process.hpp"
40 
41 namespace impl = atf::build;
42 #define IMPL_NAME "atf::build"
43 
44 // ------------------------------------------------------------------------
45 // Auxiliary functions.
46 // ------------------------------------------------------------------------
47 
48 inline
49 atf::process::argv_array
50 cargv_to_argv(const atf_list_t* l)
51 {
52     std::vector< const char* > aux;
53 
54     atf_list_citer_t iter;
55     atf_list_for_each_c(iter, l)
56         aux.push_back(static_cast< const char* >(atf_list_citer_data(iter)));
57 
58     return atf::process::argv_array(aux);
59 }
60 
61 inline
62 atf::process::argv_array
63 cargv_to_argv_and_free(char** l)
64 {
65     try {
66         atf::process::argv_array argv((const char* const*)l);
67         atf_utils_free_charpp(l);
68         return argv;
69     } catch (...) {
70         atf_utils_free_charpp(l);
71         throw;
72     }
73 }
74 
75 // ------------------------------------------------------------------------
76 // Free functions.
77 // ------------------------------------------------------------------------
78 
79 atf::process::argv_array
80 impl::c_o(const std::string& sfile, const std::string& ofile,
81           const atf::process::argv_array& optargs)
82 {
83     char** l;
84 
85     atf_error_t err = atf_build_c_o(sfile.c_str(), ofile.c_str(),
86                                     optargs.exec_argv(), &l);
87     if (atf_is_error(err))
88         throw_atf_error(err);
89 
90     return cargv_to_argv_and_free(l);
91 }
92 
93 atf::process::argv_array
94 impl::cpp(const std::string& sfile, const std::string& ofile,
95           const atf::process::argv_array& optargs)
96 {
97     char** l;
98 
99     atf_error_t err = atf_build_cpp(sfile.c_str(), ofile.c_str(),
100                                     optargs.exec_argv(), &l);
101     if (atf_is_error(err))
102         throw_atf_error(err);
103 
104     return cargv_to_argv_and_free(l);
105 }
106 
107 atf::process::argv_array
108 impl::cxx_o(const std::string& sfile, const std::string& ofile,
109             const atf::process::argv_array& optargs)
110 {
111     char** l;
112 
113     atf_error_t err = atf_build_cxx_o(sfile.c_str(), ofile.c_str(),
114                                       optargs.exec_argv(), &l);
115     if (atf_is_error(err))
116         throw_atf_error(err);
117 
118     return cargv_to_argv_and_free(l);
119 }
120