1240116Smarcel// Copyright (c) 2009 The NetBSD Foundation, Inc.
2240116Smarcel// All rights reserved.
3240116Smarcel//
4240116Smarcel// Redistribution and use in source and binary forms, with or without
5240116Smarcel// modification, are permitted provided that the following conditions
6240116Smarcel// are met:
7240116Smarcel// 1. Redistributions of source code must retain the above copyright
8240116Smarcel//    notice, this list of conditions and the following disclaimer.
9240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel//    notice, this list of conditions and the following disclaimer in the
11240116Smarcel//    documentation and/or other materials provided with the distribution.
12240116Smarcel//
13240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25240116Smarcel
26273929Sjmmv#include "atf-c++/build.hpp"
27273929Sjmmv
28240116Smarcelextern "C" {
29240116Smarcel#include "atf-c/build.h"
30240116Smarcel#include "atf-c/error.h"
31240116Smarcel#include "atf-c/utils.h"
32240116Smarcel}
33240116Smarcel
34273929Sjmmv#include "atf-c++/detail/exceptions.hpp"
35273929Sjmmv#include "atf-c++/detail/process.hpp"
36240116Smarcel
37240116Smarcelnamespace impl = atf::build;
38240116Smarcel#define IMPL_NAME "atf::build"
39240116Smarcel
40240116Smarcel// ------------------------------------------------------------------------
41240116Smarcel// Auxiliary functions.
42240116Smarcel// ------------------------------------------------------------------------
43240116Smarcel
44240116Smarcelinline
45240116Smarcelatf::process::argv_array
46240116Smarcelcargv_to_argv(const atf_list_t* l)
47240116Smarcel{
48240116Smarcel    std::vector< const char* > aux;
49240116Smarcel
50240116Smarcel    atf_list_citer_t iter;
51240116Smarcel    atf_list_for_each_c(iter, l)
52240116Smarcel        aux.push_back(static_cast< const char* >(atf_list_citer_data(iter)));
53240116Smarcel
54240116Smarcel    return atf::process::argv_array(aux);
55240116Smarcel}
56240116Smarcel
57240116Smarcelinline
58240116Smarcelatf::process::argv_array
59240116Smarcelcargv_to_argv_and_free(char** l)
60240116Smarcel{
61240116Smarcel    try {
62240116Smarcel        atf::process::argv_array argv((const char* const*)l);
63240116Smarcel        atf_utils_free_charpp(l);
64240116Smarcel        return argv;
65240116Smarcel    } catch (...) {
66240116Smarcel        atf_utils_free_charpp(l);
67240116Smarcel        throw;
68240116Smarcel    }
69240116Smarcel}
70240116Smarcel
71240116Smarcel// ------------------------------------------------------------------------
72240116Smarcel// Free functions.
73240116Smarcel// ------------------------------------------------------------------------
74240116Smarcel
75240116Smarcelatf::process::argv_array
76240116Smarcelimpl::c_o(const std::string& sfile, const std::string& ofile,
77240116Smarcel          const atf::process::argv_array& optargs)
78240116Smarcel{
79240116Smarcel    char** l;
80240116Smarcel
81240116Smarcel    atf_error_t err = atf_build_c_o(sfile.c_str(), ofile.c_str(),
82240116Smarcel                                    optargs.exec_argv(), &l);
83240116Smarcel    if (atf_is_error(err))
84240116Smarcel        throw_atf_error(err);
85240116Smarcel
86240116Smarcel    return cargv_to_argv_and_free(l);
87240116Smarcel}
88240116Smarcel
89240116Smarcelatf::process::argv_array
90240116Smarcelimpl::cpp(const std::string& sfile, const std::string& ofile,
91240116Smarcel          const atf::process::argv_array& optargs)
92240116Smarcel{
93240116Smarcel    char** l;
94240116Smarcel
95240116Smarcel    atf_error_t err = atf_build_cpp(sfile.c_str(), ofile.c_str(),
96240116Smarcel                                    optargs.exec_argv(), &l);
97240116Smarcel    if (atf_is_error(err))
98240116Smarcel        throw_atf_error(err);
99240116Smarcel
100240116Smarcel    return cargv_to_argv_and_free(l);
101240116Smarcel}
102240116Smarcel
103240116Smarcelatf::process::argv_array
104240116Smarcelimpl::cxx_o(const std::string& sfile, const std::string& ofile,
105240116Smarcel            const atf::process::argv_array& optargs)
106240116Smarcel{
107240116Smarcel    char** l;
108240116Smarcel
109240116Smarcel    atf_error_t err = atf_build_cxx_o(sfile.c_str(), ofile.c_str(),
110240116Smarcel                                      optargs.exec_argv(), &l);
111240116Smarcel    if (atf_is_error(err))
112240116Smarcel        throw_atf_error(err);
113240116Smarcel
114240116Smarcel    return cargv_to_argv_and_free(l);
115240116Smarcel}
116