Erik Johansson [Sun, 23 Feb 2020 20:56:59 +0000 (21:56 +0100)]
Fix four minor cppcheck warnings
- uninitvar:src/ccache.cpp:3422: Uninitialized variable: ctx
- redundantInitialization:src/legacy_util.cpp:1067: Redundant initialization
for 'q'. The initialized value is overwritten before it is read.
- constVariable:src/stats.cpp:429: Variable 'ctx' can be declared with const
- variableScope:src/Util.cpp:404: The scope of the variable 'right' can be
reduced.
Joel Rosdahl [Thu, 20 Feb 2020 20:24:15 +0000 (21:24 +0100)]
Don’t use realpath(3) for normalization when computing relative paths
The current working directory (CWD) can come from two sources: Either
the return value of getcwd(3) (“actual CWD” below) or the environment
variable $PWD (“apparent CWD” below). The former is returned by e.g.
$(CURDIR) in Makefiles and by “pwd -P” and is always in normalized form
(no “.” or “..” parts or extra slashes). The latter is returned by “echo
$PWD” or “pwd” and can potentially be in unnormalized form on some
systems.
The actual CWD and apparent CWD may also differ if there are symlinks in
the path. Absolute paths to files given to ccache can therefore be based
on either of these CWD forms. When computing relative paths under the
base directory the CWD needs be in normalized form for the algorithm to
be reasonably simple.
2df269a3 solved a bug with an unnormalized apparent CWD by using
realpath(3) for normalization. Using realpath also makes the algorithm
correct in the presence of symlinks. It however also means that all
symlinks (both in CWD and in command line arguments) are dereferenced.
The downside of this is that if either of the symlink targets contain
specific names (such as build ID, date, username or similar) then the
relative paths will also contain those specific path names, leading to
cache misses.
Solve this by:
- Performing normalization without using realpath, i.e. without
expanding symlinks.
- Computing a relative path based on normalized CWD and normalized path.
- Checking whether the relative path resolves to the same i-node as the
original path. If it does, use it, otherwise just use the original
path (and take a potential cache miss).
- Doing the above calculation both for the actual and the apparent CWD
and choose the best one.
This solves the problem that PR #491 intended to address in a better
way.
Joel Rosdahl [Wed, 19 Feb 2020 10:00:24 +0000 (11:00 +0100)]
Implement Util::normalize_absolute_path
Normalization here means syntactically removing redundant slashes and
resolving "." and ".." parts. The algorithm does however *not* follow
symlinks, so the result may not actually resolve to `path`.
Joel Rosdahl [Sat, 22 Feb 2020 12:46:30 +0000 (13:46 +0100)]
Use find_executable_in_path to find executable in hash_command_output
This makes the Windows version behave similar to the non-Windows
version: just search in $PATH, not potentially in the path specified by
the “path” configuration setting.
Joel Rosdahl [Sat, 22 Feb 2020 12:12:16 +0000 (13:12 +0100)]
Make failure to run compiler_check command a soft error
No need to make a failure when running the compiler check fatal since
other misconfigured settings can have a similar effect without resulting
in fatal errors.
Joel Rosdahl [Sun, 16 Feb 2020 12:06:24 +0000 (13:06 +0100)]
Improve functions related to CWD
The different functions related to current working directory (CWD) have
become messy during the years:
- gnu_getcwd is a simple wrapper around getcwd(3), thus returning the
actual canonical path.
- get_cwd returns $PWD, falling back to getcwd(3) if $PWD is not sane.
- get_current_working_dir (local function in ccache.cpp) memoizes
x_realpath(get_cwd()) (i.e., getcwd(3) in essence...) in the global
current_working_dir variable. Unit tests may manipulate
current_working_dir.
Improve this by:
- Replacing gnu_getcwd with Util::get_actual_cwd.
- Replacing get_cwd with Util::get_apparent_cwd.
- Removing get_current_working_dir and placing both actual and apparent
CWD in the context object.
Thomas Otto [Thu, 13 Feb 2020 20:54:19 +0000 (21:54 +0100)]
Added 'make check_format' to makefile rules (#525)
'make format' formats all source files with clang-format, and
'make check_format' only checks and on failure prints a diff,
then exits with the appropriate status.
Because quick interactive formatting is one use case, xargs is
used for parallelism. Proper make-parallelism would require
-k and -j arguments to work.
Joel Rosdahl [Sat, 8 Feb 2020 22:19:18 +0000 (23:19 +0100)]
Improve the failed() and fatal() mechanisms
The failure() and fatal() functions now exit by throwing exceptions that
are caught by the top level functions. This makes it possible to “throw
Failure” in functions that don’t have access to orig_args (or the future
context object).
While at it, renamed top-level functions to better reflect their
purpose.
Thomas Otto [Sat, 8 Feb 2020 16:12:51 +0000 (17:12 +0100)]
Extract setup_config() from initialize() (#534)
Repeated initialize-calls in ccache_main_options() added duplicate exit
functions. These calls had the side effect of passing a config relevant
changes from one command line switch to the next. Instead setup_config()
is now called afterwards.