1 // Copyright (C) 2002-2007 Andrew Tridgell
2 // Copyright (C) 2009-2018 Joel Rosdahl
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)
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
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
26 #define ATTR_FORMAT(x, y, z) __attribute__((format (x, y, z)))
27 #define ATTR_NORETURN __attribute__((noreturn))
29 #define ATTR_FORMAT(x, y, z)
34 #define MYNAME "ccache"
37 extern const char CCACHE_VERSION[];
39 // Statistics fields in storage order.
46 STATS_PREPROCESSOR = 5,
49 STATS_CACHEHIT_CPP = 8,
54 STATS_OBSOLETE_MAXFILES = 13,
55 STATS_OBSOLETE_MAXSIZE = 14,
56 STATS_SOURCELANG = 15,
61 STATS_UNSUPPORTED_OPTION = 20,
63 STATS_CACHEHIT_DIR = 22,
65 STATS_EMPTYOUTPUT = 24,
66 STATS_BADEXTRAFILE = 25,
68 STATS_CANTUSEPCH = 27,
69 STATS_PREPROCESSING = 28,
70 STATS_NUMCLEANUPS = 29,
71 STATS_UNSUPPORTED_DIRECTIVE = 30,
72 STATS_ZEROTIMESTAMP = 31,
77 enum guessed_compiler {
85 extern enum guessed_compiler guessed_compiler;
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
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]))
110 // Buffer size for I/O operations. Should be a multiple of 4 KiB.
111 #define READ_BUFFER_SIZE 65536
113 // ----------------------------------------------------------------------------
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);
137 // ----------------------------------------------------------------------------
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);
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,
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);
184 int create_tmp_fd(char **fname);
185 FILE *create_tmp_file(char **fname, const char *mode);
186 const char *get_home_directory(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);
201 char *x_readlink(const char *path);
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);
208 // ----------------------------------------------------------------------------
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,
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);
225 // ----------------------------------------------------------------------------
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);
234 // ----------------------------------------------------------------------------
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);
241 // ----------------------------------------------------------------------------
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);
249 // ----------------------------------------------------------------------------
252 bool lockfile_acquire(const char *path, unsigned staleness_limit);
253 void lockfile_release(const char *path);
255 // ----------------------------------------------------------------------------
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);
265 bool is_precompiled_header(const char *path);
267 // ----------------------------------------------------------------------------
269 #ifdef HAVE_COMPAR_FN_T
270 #define COMPAR_FN_T __compar_fn_t
272 typedef int (*COMPAR_FN_T)(const void *, const void *);
275 // Work with silly DOS binary open.
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
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 ";"
301 # define DIR_DELIM_CH '/'
302 # define PATH_DELIM ":"
306 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
309 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
312 #endif // ifndef CCACHE_H