Imported Upstream version 3.2.6
[platform/upstream/ccache.git] / 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 = 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
55         STATS_END
56 };
57
58 #define SLOPPY_INCLUDE_FILE_MTIME 1
59 #define SLOPPY_INCLUDE_FILE_CTIME 2
60 #define SLOPPY_FILE_MACRO 4
61 #define SLOPPY_TIME_MACROS 8
62 #define SLOPPY_PCH_DEFINES 16
63 /*
64  * Allow us to match files based on their stats (size, mtime, ctime), without
65  * looking at their contents.
66  */
67 #define SLOPPY_FILE_STAT_MATCHES 32
68
69 #define str_eq(s1, s2) (strcmp((s1), (s2)) == 0)
70 #define str_startswith(s, p) (strncmp((s), (p), strlen((p))) == 0)
71
72 /* ------------------------------------------------------------------------- */
73 /* args.c */
74
75 struct args {
76         char **argv;
77         int argc;
78 };
79
80 struct args *args_init(int, char **);
81 struct args *args_init_from_string(const char *);
82 struct args *args_init_from_gcc_atfile(const char *filename);
83 struct args *args_copy(struct args *args);
84 void args_free(struct args *args);
85 void args_add(struct args *args, const char *s);
86 void args_add_prefix(struct args *args, const char *s);
87 void args_extend(struct args *args, struct args *to_append);
88 void args_insert(struct args *dest, int index, struct args *src, bool replace);
89 void args_pop(struct args *args, int n);
90 void args_set(struct args *args, int index, const char *value);
91 void args_strip(struct args *args, const char *prefix);
92 void args_remove_first(struct args *args);
93 char *args_to_string(struct args *args);
94 bool args_equal(struct args *args1, struct args *args2);
95
96 /* ------------------------------------------------------------------------- */
97 /* hash.c */
98
99 void hash_start(struct mdfour *md);
100 void hash_buffer(struct mdfour *md, const void *s, size_t len);
101 char *hash_result(struct mdfour *md);
102 void hash_result_as_bytes(struct mdfour *md, unsigned char *out);
103 bool hash_equal(struct mdfour *md1, struct mdfour *md2);
104 void hash_delimiter(struct mdfour *md, const char *type);
105 void hash_string(struct mdfour *md, const char *s);
106 void hash_string_length(struct mdfour *md, const char *s, int length);
107 void hash_int(struct mdfour *md, int x);
108 bool hash_fd(struct mdfour *md, int fd);
109 bool hash_file(struct mdfour *md, const char *fname);
110
111 /* ------------------------------------------------------------------------- */
112 /* util.c */
113
114 void cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
115 void cc_bulklog(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
116 void cc_log_argv(const char *prefix, char **argv);
117 void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
118
119 void copy_fd(int fd_in, int fd_out);
120 int copy_file(const char *src, const char *dest, int compress_level);
121 int move_file(const char *src, const char *dest, int compress_level);
122 int move_uncompressed_file(const char *src, const char *dest,
123                            int compress_level);
124 bool file_is_compressed(const char *filename);
125 int create_dir(const char *dir);
126 int create_parent_dirs(const char *path);
127 const char *get_hostname(void);
128 const char *tmp_string(void);
129 char *format_hash_as_string(const unsigned char *hash, int size);
130 int create_cachedirtag(const char *dir);
131 char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
132 void reformat(char **ptr, const char *format, ...) ATTR_FORMAT(printf, 2, 3);
133 char *x_strdup(const char *s);
134 char *x_strndup(const char *s, size_t n);
135 void *x_malloc(size_t size);
136 void *x_calloc(size_t nmemb, size_t size);
137 void *x_realloc(void *ptr, size_t size);
138 void x_unsetenv(const char *name);
139 int x_fstat(int fd, struct stat *buf);
140 int x_lstat(const char *pathname, struct stat *buf);
141 int x_stat(const char *pathname, struct stat *buf);
142 void traverse(const char *dir, void (*fn)(const char *, struct stat *));
143 char *basename(const char *path);
144 char *dirname(const char *path);
145 const char *get_extension(const char *path);
146 char *remove_extension(const char *path);
147 size_t file_size(struct stat *st);
148 char *format_human_readable_size(uint64_t size);
149 char *format_parsable_size_with_suffix(uint64_t size);
150 bool parse_size_with_suffix(const char *str, uint64_t *size);
151 char *x_realpath(const char *path);
152 char *gnu_getcwd(void);
153 #ifndef HAVE_STRTOK_R
154 char *strtok_r(char *str, const char *delim, char **saveptr);
155 #endif
156 int create_tmp_fd(char **fname);
157 FILE *create_tmp_file(char **fname, const char *mode);
158 const char *get_home_directory(void);
159 char *get_cwd(void);
160 bool same_executable_name(const char *s1, const char *s2);
161 size_t common_dir_prefix_length(const char *s1, const char *s2);
162 char *get_relative_path(const char *from, const char *to);
163 bool is_absolute_path(const char *path);
164 bool is_full_path(const char *path);
165 void update_mtime(const char *path);
166 void x_exit(int status) ATTR_NORETURN;
167 int x_rename(const char *oldpath, const char *newpath);
168 int tmp_unlink(const char *path);
169 int x_unlink(const char *path);
170 #ifndef _WIN32
171 char *x_readlink(const char *path);
172 #endif
173 bool read_file(const char *path, size_t size_hint, char **data, size_t *size);
174 char *read_text_file(const char *path, size_t size_hint);
175 char *subst_env_in_string(const char *str, char **errmsg);
176
177 /* ------------------------------------------------------------------------- */
178 /* stats.c */
179
180 void stats_update(enum stats stat);
181 void stats_flush(void);
182 unsigned stats_get_pending(enum stats stat);
183 void stats_zero(void);
184 void stats_summary(struct conf *conf);
185 void stats_update_size(uint64_t size, unsigned files);
186 void stats_get_obsolete_limits(const char *dir, unsigned *maxfiles,
187                                uint64_t *maxsize);
188 void stats_set_sizes(const char *dir, unsigned num_files, uint64_t total_size);
189 void stats_read(const char *path, struct counters *counters);
190 void stats_write(const char *path, struct counters *counters);
191
192 /* ------------------------------------------------------------------------- */
193 /* unify.c */
194
195 int unify_hash(struct mdfour *hash, const char *fname);
196
197 /* ------------------------------------------------------------------------- */
198 /* exitfn.c */
199
200 void exitfn_init(void);
201 void exitfn_add_nullary(void (*function)(void));
202 void exitfn_add(void (*function)(void *), void *context);
203 void exitfn_call(void);
204
205 /* ------------------------------------------------------------------------- */
206 /* cleanup.c */
207
208 void cleanup_dir(struct conf *conf, const char *dir);
209 void cleanup_all(struct conf *conf);
210 void wipe_all(struct conf *conf);
211
212 /* ------------------------------------------------------------------------- */
213 /* execute.c */
214
215 int execute(char **argv, int fd_out, int fd_err, pid_t *pid);
216 char *find_executable(const char *name, const char *exclude_name);
217 void print_command(FILE *fp, char **argv);
218
219 /* ------------------------------------------------------------------------- */
220 /* lockfile.c */
221
222 bool lockfile_acquire(const char *path, unsigned staleness_limit);
223 void lockfile_release(const char *path);
224
225 /* ------------------------------------------------------------------------- */
226 /* ccache.c */
227
228 extern time_t time_of_compilation;
229 void block_signals(void);
230 void unblock_signals(void);
231 bool cc_process_args(struct args *args, struct args **preprocessor_args,
232                     struct args **compiler_args);
233 void cc_reset(void);
234 bool is_precompiled_header(const char *path);
235
236 /* ------------------------------------------------------------------------- */
237
238 #if HAVE_COMPAR_FN_T
239 #define COMPAR_FN_T __compar_fn_t
240 #else
241 typedef int (*COMPAR_FN_T)(const void *, const void *);
242 #endif
243
244 /* work with silly DOS binary open */
245 #ifndef O_BINARY
246 #define O_BINARY 0
247 #endif
248
249 /* mkstemp() on some versions of cygwin don't handle binary files, so
250    override */
251 #ifdef __CYGWIN__
252 #undef HAVE_MKSTEMP
253 #endif
254
255 #ifdef _WIN32
256 char *win32argvtos(char *prefix, char **argv);
257 char *win32getshell(char *path);
258 int win32execute(char *path, char **argv, int doreturn,
259                  int fd_stdout, int fd_stderr);
260 void add_exe_ext_if_no_to_fullpath(char *full_path_win_ext, size_t max_size,
261                                    const char *ext, const char *path);
262 #    ifndef _WIN32_WINNT
263 #    define _WIN32_WINNT 0x0501
264 #    endif
265 #    include <windows.h>
266 #    define mkdir(a,b) mkdir(a)
267 #    define link(src,dst) (CreateHardLink(dst,src,NULL) ? 0 : -1)
268 #    define lstat(a,b) stat(a,b)
269 #    define execv(a,b) win32execute(a,b,0,-1,-1)
270 #    define execute(a,b,c) win32execute(*(a),a,1,b,c)
271 #    define PATH_DELIM ";"
272 #    define F_RDLCK 0
273 #    define F_WRLCK 0
274 #else
275 #    define PATH_DELIM ":"
276 #endif
277
278 #ifndef MAX
279 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
280 #endif
281
282 #endif /* ifndef CCACHE_H */