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