resetting manifest requested domain to floor
[platform/upstream/ccache.git] / test / test_argument_processing.c
1 /*
2  * Copyright (C) 2010-2011 Joel Rosdahl
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 3 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /*
20  * This file contains tests for the processing of compiler arguments.
21  */
22
23 #include "ccache.h"
24 #include "test/framework.h"
25 #include "test/util.h"
26
27 TEST_SUITE(argument_processing)
28
29 TEST(dash_E_should_result_in_called_for_preprocessing)
30 {
31         struct args *orig = args_init_from_string("cc -c foo.c -E");
32         struct args *preprocessed, *compiler;
33
34         create_file("foo.c", "");
35         CHECK(!cc_process_args(orig, &preprocessed, &compiler));
36         CHECK_UNS_EQ(1, stats_get_pending(STATS_PREPROCESSING));
37
38         args_free(orig);
39 }
40
41 TEST(dash_M_should_be_unsupported)
42 {
43         struct args *orig = args_init_from_string("cc -c foo.c -M");
44         struct args *preprocessed, *compiler;
45
46         create_file("foo.c", "");
47         CHECK(!cc_process_args(orig, &preprocessed, &compiler));
48         CHECK_UNS_EQ(1, stats_get_pending(STATS_UNSUPPORTED));
49
50         args_free(orig);
51 }
52
53 TEST(dependency_flags_should_only_be_sent_to_the_preprocessor)
54 {
55 #define CMD \
56         "cc -c -MD -MMD -MP -MF foo.d -MT mt1 -MT mt2 -MQ mq1 -MQ mq2" \
57         " -Wp,-MD,wpmd -Wp,-MMD,wpmmd"
58         struct args *orig = args_init_from_string(CMD " foo.c -o foo.o");
59         struct args *exp_cpp = args_init_from_string(CMD);
60 #undef CMD
61         struct args *exp_cc = args_init_from_string("cc -c");
62         struct args *act_cpp = NULL, *act_cc = NULL;
63         create_file("foo.c", "");
64
65         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
66         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
67         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
68
69         args_free(orig);
70 }
71
72 TEST(dependency_flags_that_take_an_argument_should_not_require_space_delimiter)
73 {
74         struct args *orig = args_init_from_string(
75                 "cc -c -MMD -MFfoo.d -MT mt -MTmt -MQmq foo.c -o foo.o");
76         struct args *exp_cpp = args_init_from_string(
77                 "cc -c -MMD -MFfoo.d -MT mt -MTmt -MQmq");
78         struct args *exp_cc = args_init_from_string("cc -c");
79         struct args *act_cpp = NULL, *act_cc = NULL;
80         create_file("foo.c", "");
81
82         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
83         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
84         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
85
86         args_free(orig);
87 }
88
89 TEST(sysroot_should_be_rewritten_if_basedir_is_used)
90 {
91         extern char *base_dir;
92         extern char *current_working_dir;
93         struct args *orig =
94                 args_init_from_string("cc --sysroot=/some/directory -c foo.c");
95         struct args *act_cpp = NULL, *act_cc = NULL;
96         create_file("foo.c", "");
97
98         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
99         CHECK_STR_EQ(act_cpp->argv[1], "--sysroot=/some/directory");
100
101         cc_reset();
102         base_dir = "/some";
103         current_working_dir = get_cwd();
104
105         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
106         CHECK(str_startswith(act_cpp->argv[1], "--sysroot=../"));
107
108         args_free(orig);
109         base_dir = NULL;
110         current_working_dir = NULL;
111 }
112
113 TEST(MF_flag_with_immediate_argument_should_work_as_last_argument)
114 {
115         struct args *orig = args_init_from_string(
116                 "cc -c foo.c -o foo.o -MMD -MT bar -MFfoo.d");
117         struct args *exp_cpp = args_init_from_string(
118                 "cc -c -MMD -MT bar -MFfoo.d");
119         struct args *exp_cc = args_init_from_string("cc -c");
120         struct args *act_cpp = NULL, *act_cc = NULL;
121         create_file("foo.c", "");
122
123         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
124         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
125         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
126
127         args_free(orig);
128 }
129
130 TEST(MT_flag_with_immediate_argument_should_work_as_last_argument)
131 {
132         struct args *orig = args_init_from_string(
133                 "cc -c foo.c -o foo.o -MMD -MFfoo.d -MT foo -MTbar");
134         struct args *exp_cpp = args_init_from_string(
135                 "cc -c -MMD -MFfoo.d -MT foo -MTbar");
136         struct args *exp_cc = args_init_from_string("cc -c");
137         struct args *act_cpp = NULL, *act_cc = NULL;
138         create_file("foo.c", "");
139
140         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
141         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
142         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
143
144         args_free(orig);
145 }
146
147 TEST(MQ_flag_with_immediate_argument_should_work_as_last_argument)
148 {
149         struct args *orig = args_init_from_string(
150                 "cc -c foo.c -o foo.o -MMD -MFfoo.d -MQ foo -MQbar");
151         struct args *exp_cpp = args_init_from_string(
152                 "cc -c -MMD -MFfoo.d -MQ foo -MQbar");
153         struct args *exp_cc = args_init_from_string("cc -c");
154         struct args *act_cpp = NULL, *act_cc = NULL;
155         create_file("foo.c", "");
156
157         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
158         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
159         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
160
161         args_free(orig);
162 }
163
164 TEST(MQ_flag_without_immediate_argument_should_not_add_MQobj)
165 {
166         struct args *orig = args_init_from_string(
167                 "gcc -c -MD -MP -MFfoo.d -MQ foo.d foo.c");
168         struct args *exp_cpp = args_init_from_string(
169                 "gcc -c -MD -MP -MFfoo.d -MQ foo.d");
170         struct args *exp_cc = args_init_from_string(
171                 "gcc -c");
172         struct args *act_cpp = NULL, *act_cc = NULL;
173         create_file("foo.c", "");
174
175         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
176         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
177         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
178
179         args_free(orig);
180 }
181
182 TEST(MT_flag_without_immediate_argument_should_not_add_MTobj)
183 {
184         struct args *orig = args_init_from_string(
185                 "gcc -c -MD -MP -MFfoo.d -MT foo.d foo.c");
186         struct args *exp_cpp = args_init_from_string(
187                 "gcc -c -MD -MP -MFfoo.d -MT foo.d");
188         struct args *exp_cc = args_init_from_string(
189                 "gcc -c");
190         struct args *act_cpp = NULL, *act_cc = NULL;
191         create_file("foo.c", "");
192
193         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
194         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
195         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
196
197         args_free(orig);
198 }
199
200 TEST(MQ_flag_with_immediate_argument_should_add_MQobj)
201 {
202         struct args *orig = args_init_from_string(
203                 "gcc -c -MD -MP -MFfoo.d -MQfoo.d foo.c");
204         struct args *exp_cpp = args_init_from_string(
205                 "gcc -c -MD -MP -MFfoo.d -MQfoo.d -MQ foo.o");
206         struct args *exp_cc = args_init_from_string(
207                 "gcc -c");
208         struct args *act_cpp = NULL, *act_cc = NULL;
209         create_file("foo.c", "");
210
211         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
212         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
213         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
214
215         args_free(orig);
216 }
217
218 TEST(MT_flag_with_immediate_argument_should_add_MQobj)
219 {
220         struct args *orig = args_init_from_string(
221                 "gcc -c -MD -MP -MFfoo.d -MTfoo.d foo.c");
222         struct args *exp_cpp = args_init_from_string(
223                 "gcc -c -MD -MP -MFfoo.d -MTfoo.d -MQ foo.o");
224         struct args *exp_cc = args_init_from_string(
225                 "gcc -c");
226         struct args *act_cpp = NULL, *act_cc = NULL;
227         create_file("foo.c", "");
228
229         CHECK(cc_process_args(orig, &act_cpp, &act_cc));
230         CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
231         CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
232
233         args_free(orig);
234 }
235
236
237 TEST_SUITE_END