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