xref: /minix3/external/bsd/atf/dist/atf-c/build.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc  * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc  *
4*11be35a1SLionel Sambuc  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc  */
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <stdlib.h>
31*11be35a1SLionel Sambuc #include <string.h>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include "atf-c/build.h"
34*11be35a1SLionel Sambuc #include "atf-c/config.h"
35*11be35a1SLionel Sambuc #include "atf-c/error.h"
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "detail/sanity.h"
38*11be35a1SLionel Sambuc #include "detail/text.h"
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
41*11be35a1SLionel Sambuc  * Auxiliary functions.
42*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc static
45*11be35a1SLionel Sambuc atf_error_t
append_config_var(const char * var,atf_list_t * argv)46*11be35a1SLionel Sambuc append_config_var(const char *var, atf_list_t *argv)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc     atf_error_t err;
49*11be35a1SLionel Sambuc     atf_list_t words;
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc     err = atf_text_split(atf_config_get(var), " ", &words);
52*11be35a1SLionel Sambuc     if (atf_is_error(err))
53*11be35a1SLionel Sambuc         goto out;
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc     atf_list_append_list(argv, &words);
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc out:
58*11be35a1SLionel Sambuc     return err;
59*11be35a1SLionel Sambuc }
60*11be35a1SLionel Sambuc 
61*11be35a1SLionel Sambuc static
62*11be35a1SLionel Sambuc atf_error_t
append_arg1(const char * arg,atf_list_t * argv)63*11be35a1SLionel Sambuc append_arg1(const char *arg, atf_list_t *argv)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc     return atf_list_append(argv, strdup(arg), true);
66*11be35a1SLionel Sambuc }
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc static
69*11be35a1SLionel Sambuc atf_error_t
append_arg2(const char * flag,const char * arg,atf_list_t * argv)70*11be35a1SLionel Sambuc append_arg2(const char *flag, const char *arg, atf_list_t *argv)
71*11be35a1SLionel Sambuc {
72*11be35a1SLionel Sambuc     atf_error_t err;
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc     err = append_arg1(flag, argv);
75*11be35a1SLionel Sambuc     if (!atf_is_error(err))
76*11be35a1SLionel Sambuc         err = append_arg1(arg, argv);
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc     return err;
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc static
82*11be35a1SLionel Sambuc atf_error_t
append_optargs(const char * const optargs[],atf_list_t * argv)83*11be35a1SLionel Sambuc append_optargs(const char *const optargs[], atf_list_t *argv)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc     atf_error_t err;
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc     err = atf_no_error();
88*11be35a1SLionel Sambuc     while (*optargs != NULL && !atf_is_error(err)) {
89*11be35a1SLionel Sambuc         err = append_arg1(strdup(*optargs), argv);
90*11be35a1SLionel Sambuc         optargs++;
91*11be35a1SLionel Sambuc     }
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc     return err;
94*11be35a1SLionel Sambuc }
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc static
97*11be35a1SLionel Sambuc atf_error_t
append_src_out(const char * src,const char * obj,atf_list_t * argv)98*11be35a1SLionel Sambuc append_src_out(const char *src, const char *obj, atf_list_t *argv)
99*11be35a1SLionel Sambuc {
100*11be35a1SLionel Sambuc     atf_error_t err;
101*11be35a1SLionel Sambuc 
102*11be35a1SLionel Sambuc     err = append_arg2("-o", obj, argv);
103*11be35a1SLionel Sambuc     if (atf_is_error(err))
104*11be35a1SLionel Sambuc         goto out;
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc     err = append_arg1("-c", argv);
107*11be35a1SLionel Sambuc     if (atf_is_error(err))
108*11be35a1SLionel Sambuc         goto out;
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc     err = append_arg1(src, argv);
111*11be35a1SLionel Sambuc 
112*11be35a1SLionel Sambuc out:
113*11be35a1SLionel Sambuc     return err;
114*11be35a1SLionel Sambuc }
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc static
117*11be35a1SLionel Sambuc atf_error_t
list_to_array(const atf_list_t * l,char *** ap)118*11be35a1SLionel Sambuc list_to_array(const atf_list_t *l, char ***ap)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc     atf_error_t err;
121*11be35a1SLionel Sambuc     char **a;
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc     a = (char **)malloc((atf_list_size(l) + 1) * sizeof(char *));
124*11be35a1SLionel Sambuc     if (a == NULL)
125*11be35a1SLionel Sambuc         err = atf_no_memory_error();
126*11be35a1SLionel Sambuc     else {
127*11be35a1SLionel Sambuc         char **aiter;
128*11be35a1SLionel Sambuc         atf_list_citer_t liter;
129*11be35a1SLionel Sambuc 
130*11be35a1SLionel Sambuc         aiter = a;
131*11be35a1SLionel Sambuc         atf_list_for_each_c(liter, l) {
132*11be35a1SLionel Sambuc             *aiter = strdup((const char *)atf_list_citer_data(liter));
133*11be35a1SLionel Sambuc             aiter++;
134*11be35a1SLionel Sambuc         }
135*11be35a1SLionel Sambuc         *aiter = NULL;
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc         err = atf_no_error();
138*11be35a1SLionel Sambuc     }
139*11be35a1SLionel Sambuc     *ap = a; /* Shut up warnings in the caller about uninitialized *ap. */
140*11be35a1SLionel Sambuc 
141*11be35a1SLionel Sambuc     return err;
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc /* ---------------------------------------------------------------------
145*11be35a1SLionel Sambuc  * Free functions.
146*11be35a1SLionel Sambuc  * --------------------------------------------------------------------- */
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc atf_error_t
atf_build_c_o(const char * sfile,const char * ofile,const char * const optargs[],char *** argv)149*11be35a1SLionel Sambuc atf_build_c_o(const char *sfile,
150*11be35a1SLionel Sambuc               const char *ofile,
151*11be35a1SLionel Sambuc               const char *const optargs[],
152*11be35a1SLionel Sambuc               char ***argv)
153*11be35a1SLionel Sambuc {
154*11be35a1SLionel Sambuc     atf_error_t err;
155*11be35a1SLionel Sambuc     atf_list_t argv_list;
156*11be35a1SLionel Sambuc 
157*11be35a1SLionel Sambuc     err = atf_list_init(&argv_list);
158*11be35a1SLionel Sambuc     if (atf_is_error(err))
159*11be35a1SLionel Sambuc         goto out;
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cc", &argv_list);
162*11be35a1SLionel Sambuc     if (atf_is_error(err))
163*11be35a1SLionel Sambuc         goto out_list;
164*11be35a1SLionel Sambuc 
165*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cppflags", &argv_list);
166*11be35a1SLionel Sambuc     if (atf_is_error(err))
167*11be35a1SLionel Sambuc         goto out_list;
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cflags", &argv_list);
170*11be35a1SLionel Sambuc     if (atf_is_error(err))
171*11be35a1SLionel Sambuc         goto out_list;
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc     if (optargs != NULL) {
174*11be35a1SLionel Sambuc         err = append_optargs(optargs, &argv_list);
175*11be35a1SLionel Sambuc         if (atf_is_error(err))
176*11be35a1SLionel Sambuc             goto out_list;
177*11be35a1SLionel Sambuc     }
178*11be35a1SLionel Sambuc 
179*11be35a1SLionel Sambuc     err = append_src_out(sfile, ofile, &argv_list);
180*11be35a1SLionel Sambuc     if (atf_is_error(err))
181*11be35a1SLionel Sambuc         goto out_list;
182*11be35a1SLionel Sambuc 
183*11be35a1SLionel Sambuc     err = list_to_array(&argv_list, argv);
184*11be35a1SLionel Sambuc     if (atf_is_error(err))
185*11be35a1SLionel Sambuc         goto out_list;
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc out_list:
188*11be35a1SLionel Sambuc     atf_list_fini(&argv_list);
189*11be35a1SLionel Sambuc out:
190*11be35a1SLionel Sambuc     return err;
191*11be35a1SLionel Sambuc }
192*11be35a1SLionel Sambuc 
193*11be35a1SLionel Sambuc atf_error_t
atf_build_cpp(const char * sfile,const char * ofile,const char * const optargs[],char *** argv)194*11be35a1SLionel Sambuc atf_build_cpp(const char *sfile,
195*11be35a1SLionel Sambuc               const char *ofile,
196*11be35a1SLionel Sambuc               const char *const optargs[],
197*11be35a1SLionel Sambuc               char ***argv)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc     atf_error_t err;
200*11be35a1SLionel Sambuc     atf_list_t argv_list;
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc     err = atf_list_init(&argv_list);
203*11be35a1SLionel Sambuc     if (atf_is_error(err))
204*11be35a1SLionel Sambuc         goto out;
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cpp", &argv_list);
207*11be35a1SLionel Sambuc     if (atf_is_error(err))
208*11be35a1SLionel Sambuc         goto out_list;
209*11be35a1SLionel Sambuc 
210*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cppflags", &argv_list);
211*11be35a1SLionel Sambuc     if (atf_is_error(err))
212*11be35a1SLionel Sambuc         goto out_list;
213*11be35a1SLionel Sambuc 
214*11be35a1SLionel Sambuc     if (optargs != NULL) {
215*11be35a1SLionel Sambuc         err = append_optargs(optargs, &argv_list);
216*11be35a1SLionel Sambuc         if (atf_is_error(err))
217*11be35a1SLionel Sambuc             goto out_list;
218*11be35a1SLionel Sambuc     }
219*11be35a1SLionel Sambuc 
220*11be35a1SLionel Sambuc     err = append_arg2("-o", ofile, &argv_list);
221*11be35a1SLionel Sambuc     if (atf_is_error(err))
222*11be35a1SLionel Sambuc         goto out_list;
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc     err = append_arg1(sfile, &argv_list);
225*11be35a1SLionel Sambuc     if (atf_is_error(err))
226*11be35a1SLionel Sambuc         goto out_list;
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc     err = list_to_array(&argv_list, argv);
229*11be35a1SLionel Sambuc     if (atf_is_error(err))
230*11be35a1SLionel Sambuc         goto out_list;
231*11be35a1SLionel Sambuc 
232*11be35a1SLionel Sambuc out_list:
233*11be35a1SLionel Sambuc     atf_list_fini(&argv_list);
234*11be35a1SLionel Sambuc out:
235*11be35a1SLionel Sambuc     return err;
236*11be35a1SLionel Sambuc }
237*11be35a1SLionel Sambuc 
238*11be35a1SLionel Sambuc atf_error_t
atf_build_cxx_o(const char * sfile,const char * ofile,const char * const optargs[],char *** argv)239*11be35a1SLionel Sambuc atf_build_cxx_o(const char *sfile,
240*11be35a1SLionel Sambuc                 const char *ofile,
241*11be35a1SLionel Sambuc                 const char *const optargs[],
242*11be35a1SLionel Sambuc                 char ***argv)
243*11be35a1SLionel Sambuc {
244*11be35a1SLionel Sambuc     atf_error_t err;
245*11be35a1SLionel Sambuc     atf_list_t argv_list;
246*11be35a1SLionel Sambuc 
247*11be35a1SLionel Sambuc     err = atf_list_init(&argv_list);
248*11be35a1SLionel Sambuc     if (atf_is_error(err))
249*11be35a1SLionel Sambuc         goto out;
250*11be35a1SLionel Sambuc 
251*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cxx", &argv_list);
252*11be35a1SLionel Sambuc     if (atf_is_error(err))
253*11be35a1SLionel Sambuc         goto out_list;
254*11be35a1SLionel Sambuc 
255*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cppflags", &argv_list);
256*11be35a1SLionel Sambuc     if (atf_is_error(err))
257*11be35a1SLionel Sambuc         goto out_list;
258*11be35a1SLionel Sambuc 
259*11be35a1SLionel Sambuc     err = append_config_var("atf_build_cxxflags", &argv_list);
260*11be35a1SLionel Sambuc     if (atf_is_error(err))
261*11be35a1SLionel Sambuc         goto out_list;
262*11be35a1SLionel Sambuc 
263*11be35a1SLionel Sambuc     if (optargs != NULL) {
264*11be35a1SLionel Sambuc         err = append_optargs(optargs, &argv_list);
265*11be35a1SLionel Sambuc         if (atf_is_error(err))
266*11be35a1SLionel Sambuc             goto out_list;
267*11be35a1SLionel Sambuc     }
268*11be35a1SLionel Sambuc 
269*11be35a1SLionel Sambuc     err = append_src_out(sfile, ofile, &argv_list);
270*11be35a1SLionel Sambuc     if (atf_is_error(err))
271*11be35a1SLionel Sambuc         goto out_list;
272*11be35a1SLionel Sambuc 
273*11be35a1SLionel Sambuc     err = list_to_array(&argv_list, argv);
274*11be35a1SLionel Sambuc     if (atf_is_error(err))
275*11be35a1SLionel Sambuc         goto out_list;
276*11be35a1SLionel Sambuc 
277*11be35a1SLionel Sambuc out_list:
278*11be35a1SLionel Sambuc     atf_list_fini(&argv_list);
279*11be35a1SLionel Sambuc out:
280*11be35a1SLionel Sambuc     return err;
281*11be35a1SLionel Sambuc }
282