Imported Upstream version 3.5.1
[platform/upstream/ccache.git] / src / ccache.h
1 // Copyright (C) 2002-2007 Andrew Tridgell
2 // Copyright (C) 2009-2018 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 #ifndef CCACHE_H
19 #define CCACHE_H
20
21 #include "system.h"
22 #include "conf.h"
23 #include "counters.h"
24
25 #ifdef __GNUC__
26 #define ATTR_FORMAT(x, y, z) __attribute__((format (x, y, z)))
27 #define ATTR_NORETURN __attribute__((noreturn))
28 #else
29 #define ATTR_FORMAT(x, y, z)
30 #define ATTR_NORETURN
31 #endif
32
33 #ifndef MYNAME
34 #define MYNAME "ccache"
35 #endif
36
37 extern const char CCACHE_VERSION[];
38
39 // Statistics fields in storage order.
40 enum stats {
41         STATS_NONE = 0,
42         STATS_STDOUT = 1,
43         STATS_STATUS = 2,
44         STATS_ERROR = 3,
45         STATS_TOCACHE = 4,
46         STATS_PREPROCESSOR = 5,
47         STATS_COMPILER = 6,
48         STATS_MISSING = 7,
49         STATS_CACHEHIT_CPP = 8,
50         STATS_ARGS = 9,
51         STATS_LINK = 10,
52         STATS_NUMFILES = 11,
53         STATS_TOTALSIZE = 12,
54         STATS_OBSOLETE_MAXFILES = 13,
55         STATS_OBSOLETE_MAXSIZE = 14,
56         STATS_SOURCELANG = 15,
57         STATS_DEVICE = 16,
58         STATS_NOINPUT = 17,
59         STATS_MULTIPLE = 18,
60         STATS_CONFTEST = 19,
61         STATS_UNSUPPORTED_OPTION = 20,
62         STATS_OUTSTDOUT = 21,
63         STATS_CACHEHIT_DIR = 22,
64         STATS_NOOUTPUT = 23,
65         STATS_EMPTYOUTPUT = 24,
66         STATS_BADEXTRAFILE = 25,
67         STATS_COMPCHECK = 26,
68         STATS_CANTUSEPCH = 27,
69         STATS_PREPROCESSING = 28,
70         STATS_NUMCLEANUPS = 29,
71         STATS_UNSUPPORTED_DIRECTIVE = 30,
72         STATS_ZEROTIMESTAMP = 31,
73
74         STATS_END
75 };
76
77 enum guessed_compiler {
78         GUESSED_CLANG,
79         GUESSED_GCC,
80         GUESSED_NVCC,
81         GUESSED_PUMP,
82         GUESSED_UNKNOWN
83 };
84
85 extern enum guessed_compiler guessed_compiler;
86
87 #define SLOPPY_INCLUDE_FILE_MTIME 1
88 #define SLOPPY_INCLUDE_FILE_CTIME 2
89 #define SLOPPY_FILE_MACRO 4
90 #define SLOPPY_TIME_MACROS 8
91 #define SLOPPY_PCH_DEFINES 16
92 // Allow us to match files based on their stats (size, mtime, ctime), without
93 // looking at their contents.
94 #define SLOPPY_FILE_STAT_MATCHES 32
95 // Allow us to not include any system headers in the manifest include files,
96 // similar to -MM versus -M for dependencies.
97 #define SLOPPY_NO_SYSTEM_HEADERS 64
98 // Allow us to ignore ctimes when comparing file stats, so we can fake mtimes
99 // if we want to (it is much harder to fake ctimes, requires changing clock)
100 #define SLOPPY_FILE_STAT_MATCHES_CTIME 128
101
102 #define str_eq(s1, s2) (strcmp((s1), (s2)) == 0)
103 #define str_startswith(s, prefix) \
104         (strncmp((s), (prefix), strlen((prefix))) == 0)
105 #define str_endswith(s, suffix) \
106         (strlen(s) >= strlen(suffix) \
107          && str_eq((s) + strlen(s) - strlen(suffix), (suffix)))
108 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
109
110 // Buffer size for I/O operations. Should be a multiple of 4 KiB.
111 #define READ_BUFFER_SIZE 65536
112
113 // ----------------------------------------------------------------------------
114 // args.c
115
116 struct args {
117         char **argv;
118         int argc;
119 };
120
121 struct args *args_init(int, char **);
122 struct args *args_init_from_string(const char *);
123 struct args *args_init_from_gcc_atfile(const char *filename);
124 struct args *args_copy(struct args *args);
125 void args_free(struct args *args);
126 void args_add(struct args *args, const char *s);
127 void args_add_prefix(struct args *args, const char *s);
128 void args_extend(struct args *args, struct args *to_append);
129 void args_insert(struct args *dest, int index, struct args *src, bool replace);
130 void args_pop(struct args *args, int n);
131 void args_set(struct args *args, int index, const char *value);
132 void args_strip(struct args *args, const char *prefix);
133 void args_remove_first(struct args *args);
134 char *args_to_string(struct args *args);
135 bool args_equal(struct args *args1, struct args *args2);
136
137 // ----------------------------------------------------------------------------
138 // util.c
139
140 void cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
141 void cc_bulklog(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
142 void cc_log_argv(const char *prefix, char **argv);
143 void cc_dump_log_buffer(const char *path);
144 void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
145 void warn(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
146
147 void copy_fd(int fd_in, int fd_out);
148 int copy_file(const char *src, const char *dest, int compress_level);
149 int move_file(const char *src, const char *dest, int compress_level);
150 int move_uncompressed_file(const char *src, const char *dest,
151                            int compress_level);
152 bool file_is_compressed(const char *filename);
153 int create_dir(const char *dir);
154 int create_parent_dirs(const char *path);
155 const char *get_hostname(void);
156 const char *tmp_string(void);
157 char *format_hash_as_string(const unsigned char *hash, int size);
158 int create_cachedirtag(const char *dir);
159 char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
160 void reformat(char **ptr, const char *format, ...) ATTR_FORMAT(printf, 2, 3);
161 char *x_strdup(const char *s);
162 char *x_strndup(const char *s, size_t n);
163 void *x_malloc(size_t size);
164 void *x_calloc(size_t nmemb, size_t size);
165 void *x_realloc(void *ptr, size_t size);
166 void x_unsetenv(const char *name);
167 int x_fstat(int fd, struct stat *buf);
168 int x_lstat(const char *pathname, struct stat *buf);
169 int x_stat(const char *pathname, struct stat *buf);
170 void traverse(const char *dir, void (*fn)(const char *, struct stat *));
171 char *basename(const char *path);
172 char *dirname(const char *path);
173 const char *get_extension(const char *path);
174 char *remove_extension(const char *path);
175 size_t file_size(struct stat *st);
176 char *format_human_readable_size(uint64_t size);
177 char *format_parsable_size_with_suffix(uint64_t size);
178 bool parse_size_with_suffix(const char *str, uint64_t *size);
179 char *x_realpath(const char *path);
180 char *gnu_getcwd(void);
181 #ifndef HAVE_STRTOK_R
182 char *strtok_r(char *str, const char *delim, char **saveptr);
183 #endif
184 int create_tmp_fd(char **fname);
185 FILE *create_tmp_file(char **fname, const char *mode);
186 const char *get_home_directory(void);
187 char *get_cwd(void);
188 bool same_executable_name(const char *s1, const char *s2);
189 size_t common_dir_prefix_length(const char *s1, const char *s2);
190 char *get_relative_path(const char *from, const char *to);
191 bool is_absolute_path(const char *path);
192 bool is_full_path(const char *path);
193 bool is_symlink(const char *path);
194 void update_mtime(const char *path);
195 void x_exit(int status) ATTR_NORETURN;
196 int x_rename(const char *oldpath, const char *newpath);
197 int tmp_unlink(const char *path);
198 int x_unlink(const char *path);
199 int x_try_unlink(const char *path);
200 #ifndef _WIN32
201 char *x_readlink(const char *path);
202 #endif
203 bool read_file(const char *path, size_t size_hint, char **data, size_t *size);
204 char *read_text_file(const char *path, size_t size_hint);
205 char *subst_env_in_string(const char *str, char **errmsg);
206 void set_cloexec_flag(int fd);
207
208 // ----------------------------------------------------------------------------
209 // stats.c
210
211 void stats_update(enum stats stat);
212 void stats_flush(void);
213 unsigned stats_get_pending(enum stats stat);
214 void stats_zero(void);
215 void stats_summary(void);
216 void stats_update_size(int64_t size, int files);
217 void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles,
218                                uint64_t *maxsize);
219 void stats_set_sizes(const char *dir, unsigned num_files, uint64_t total_size);
220 void stats_add_cleanup(const char *dir, unsigned count);
221 void stats_timestamp(time_t time, struct counters *counters);
222 void stats_read(const char *path, struct counters *counters);
223 void stats_write(const char *path, struct counters *counters);
224
225 // ----------------------------------------------------------------------------
226 // exitfn.c
227
228 void exitfn_init(void);
229 void exitfn_add_nullary(void (*function)(void));
230 void exitfn_add(void (*function)(void *), void *context);
231 void exitfn_add_last(void (*function)(void *), void *context);
232 void exitfn_call(void);
233
234 // ----------------------------------------------------------------------------
235 // cleanup.c
236
237 void clean_up_dir(struct conf *conf, const char *dir, double limit_multiple);
238 void clean_up_all(struct conf *conf);
239 void wipe_all(struct conf *conf);
240
241 // ----------------------------------------------------------------------------
242 // execute.c
243
244 int execute(char **argv, int fd_out, int fd_err, pid_t *pid);
245 char *find_executable(const char *name, const char *exclude_name);
246 void print_command(FILE *fp, char **argv);
247 char *format_command(char **argv);
248
249 // ----------------------------------------------------------------------------
250 // lockfile.c
251
252 bool lockfile_acquire(const char *path, unsigned staleness_limit);
253 void lockfile_release(const char *path);
254
255 // ----------------------------------------------------------------------------
256 // ccache.c
257
258 extern time_t time_of_compilation;
259 extern bool output_is_precompiled_header;
260 void block_signals(void);
261 void unblock_signals(void);
262 bool cc_process_args(struct args *args, struct args **preprocessor_args,
263                     struct args **compiler_args);
264 void cc_reset(void);
265 bool is_precompiled_header(const char *path);
266
267 // ----------------------------------------------------------------------------
268
269 #ifdef HAVE_COMPAR_FN_T
270 #define COMPAR_FN_T __compar_fn_t
271 #else
272 typedef int (*COMPAR_FN_T)(const void *, const void *);
273 #endif
274
275 // Work with silly DOS binary open.
276 #ifndef O_BINARY
277 #define O_BINARY 0
278 #endif
279
280 #ifdef _WIN32
281 char *win32argvtos(char *prefix, char **argv);
282 char *win32getshell(char *path);
283 int win32execute(char *path, char **argv, int doreturn,
284                  int fd_stdout, int fd_stderr);
285 void add_exe_ext_if_no_to_fullpath(char *full_path_win_ext, size_t max_size,
286                                    const char *ext, const char *path);
287 #    ifndef _WIN32_WINNT
288 #    define _WIN32_WINNT 0x0501
289 #    endif
290 #    include <windows.h>
291 #    define mkdir(a,b) mkdir(a)
292 #    define link(src,dst) (CreateHardLink(dst,src,NULL) ? 0 : -1)
293 #    define lstat(a,b) stat(a,b)
294 #    define execv(a,b) win32execute(a,b,0,-1,-1)
295 #    define execute(a,b,c,d) win32execute(*(a),a,1,b,c)
296 #    define DIR_DELIM_CH '\\'
297 #    define PATH_DELIM ";"
298 #    define F_RDLCK 0
299 #    define F_WRLCK 0
300 #else
301 #    define DIR_DELIM_CH '/'
302 #    define PATH_DELIM ":"
303 #endif
304
305 #ifndef MAX
306 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
307 #endif
308 #ifndef MIN
309 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
310 #endif
311
312 #endif // ifndef CCACHE_H