Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / build.c
blob467347af5adfce536f46eb01b859a3167f2cfca7
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
30 #include <string.h>
32 #include "atf-c/build.h"
33 #include "atf-c/config.h"
34 #include "atf-c/error.h"
35 #include "atf-c/sanity.h"
36 #include "atf-c/text.h"
38 /* ---------------------------------------------------------------------
39 * Auxiliary functions.
40 * --------------------------------------------------------------------- */
42 static
43 atf_error_t
44 append_config_var(const char *var, atf_list_t *argv)
46 atf_error_t err;
47 atf_list_t words;
49 err = atf_text_split(atf_config_get(var), " ", &words);
50 if (atf_is_error(err))
51 goto out;
53 atf_list_append_list(argv, &words);
55 out:
56 return err;
59 static
60 atf_error_t
61 append_arg1(const char *arg, atf_list_t *argv)
63 return atf_list_append(argv, strdup(arg), true);
66 static
67 atf_error_t
68 append_arg2(const char *flag, const char *arg, atf_list_t *argv)
70 atf_error_t err;
72 err = append_arg1(flag, argv);
73 if (!atf_is_error(err))
74 err = append_arg1(arg, argv);
76 return err;
79 static
80 atf_error_t
81 append_optargs(const char *const optargs[], atf_list_t *argv)
83 atf_error_t err;
85 err = atf_no_error();
86 while (*optargs != NULL && !atf_is_error(err)) {
87 err = append_arg1(strdup(*optargs), argv);
88 optargs++;
91 return err;
94 static
95 atf_error_t
96 append_src_out(const char *src, const char *obj, atf_list_t *argv)
98 atf_error_t err;
100 err = append_arg2("-o", obj, argv);
101 if (atf_is_error(err))
102 goto out;
104 err = append_arg1("-c", argv);
105 if (atf_is_error(err))
106 goto out;
108 err = append_arg1(src, argv);
110 out:
111 return err;
114 /* ---------------------------------------------------------------------
115 * Free functions.
116 * --------------------------------------------------------------------- */
118 atf_error_t
119 atf_build_c_o(const char *sfile,
120 const char *ofile,
121 const char *const optargs[],
122 atf_list_t *argv)
124 atf_error_t err;
126 err = atf_list_init(argv);
127 if (atf_is_error(err))
128 goto err;
130 err = append_config_var("atf_build_cc", argv);
131 if (atf_is_error(err))
132 goto err_list;
134 err = append_config_var("atf_build_cppflags", argv);
135 if (atf_is_error(err))
136 goto err_list;
138 err = append_config_var("atf_build_cflags", argv);
139 if (atf_is_error(err))
140 goto err_list;
142 if (optargs != NULL) {
143 err = append_optargs(optargs, argv);
144 if (atf_is_error(err))
145 goto err_list;
148 err = append_src_out(sfile, ofile, argv);
149 if (atf_is_error(err))
150 goto err_list;
152 INV(!atf_is_error(err));
153 return err;
155 err_list:
156 atf_list_fini(argv);
157 err:
158 return err;
161 atf_error_t
162 atf_build_cpp(const char *sfile,
163 const char *ofile,
164 const char *const optargs[],
165 atf_list_t *argv)
167 atf_error_t err;
169 err = atf_list_init(argv);
170 if (atf_is_error(err))
171 goto err;
173 err = append_config_var("atf_build_cpp", argv);
174 if (atf_is_error(err))
175 goto err_list;
177 err = append_config_var("atf_build_cppflags", argv);
178 if (atf_is_error(err))
179 goto err_list;
181 if (optargs != NULL) {
182 err = append_optargs(optargs, argv);
183 if (atf_is_error(err))
184 goto err_list;
187 err = append_arg2("-o", ofile, argv);
188 if (atf_is_error(err))
189 goto err_list;
191 err = append_arg1(sfile, argv);
192 if (atf_is_error(err))
193 goto err_list;
195 INV(!atf_is_error(err));
196 return err;
198 err_list:
199 atf_list_fini(argv);
200 err:
201 return err;
204 atf_error_t
205 atf_build_cxx_o(const char *sfile,
206 const char *ofile,
207 const char *const optargs[],
208 atf_list_t *argv)
210 atf_error_t err;
212 err = atf_list_init(argv);
213 if (atf_is_error(err))
214 goto err;
216 err = append_config_var("atf_build_cxx", argv);
217 if (atf_is_error(err))
218 goto err_list;
220 err = append_config_var("atf_build_cppflags", argv);
221 if (atf_is_error(err))
222 goto err_list;
224 err = append_config_var("atf_build_cxxflags", argv);
225 if (atf_is_error(err))
226 goto err_list;
228 if (optargs != NULL) {
229 err = append_optargs(optargs, argv);
230 if (atf_is_error(err))
231 goto err_list;
234 err = append_src_out(sfile, ofile, argv);
235 if (atf_is_error(err))
236 goto err_list;
238 INV(!atf_is_error(err));
239 return err;
241 err_list:
242 atf_list_fini(argv);
243 err:
244 return err;