Fix Util::read_file truncating files if size_hint is an underestimate (#808)
If the size_hint passed to read_file was an underestimate, or the
platform's `stat()` implementation gives an inaccurate file size (e.g.
MinGW) then `Util::read_file()` would only issue a single `read()` call
instead of reading the entire file.
Joel Rosdahl [Tue, 23 Feb 2021 20:11:58 +0000 (21:11 +0100)]
Handle -frecord-gcc-switches correctly
-frecord-gcc-switches records the full command line in the object file,
so include the original command line in the hash as suggested by
Lawrence Chan.
StdAtomic.cmake: Probe atomic increment as well (#800)
On sparc there are 8-byte atomic loads and stores available in ISA
but not atomic increments. As a result linking fails as:
```
ld: src/libccache_lib.a(InodeCache.cpp.o):
undefined reference to symbol '__atomic_fetch_add_8@@LIBATOMIC_1.0'
ld: sparc-unknown-linux-gnu/8.2.0/libatomic.so.1:
error adding symbols: DSO missing from command line
```
The fix is to add increment into libatomic test.
tested on `sparc-unknown-linux-gnu` target.
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
Joel Rosdahl [Sun, 7 Feb 2021 12:32:26 +0000 (13:32 +0100)]
Only dup2 stderr into $UNCACHED_ERR_FD for preprocessor/compiler
Ccache dup2s the stderr FD and publishes the resulting FD number in
$UNCACHED_ERR_FD for usage by e.g. distcc. This is done any executed
child process.
As noted by Sam Varshavchik on the ccache mailing list, this leads to an
unfortunate and complex problem in combination with GNU Make, LTO
linking and the Linux PTY driver:
Since UNCACHED_ERR_FD is only relevant when running the preprocessor or
compiler, let’s only dup2 stderr when executing those, i.e. not when
falling back to just running the wrapped command such as the linker.
Joel Rosdahl [Sat, 30 Jan 2021 18:35:54 +0000 (19:35 +0100)]
Improve handling of SOURCE_DATE_EPOCH
PR #755 (be1ed774) made it so that the value of SOURCE_DATE_EPOCH is
ignored if time_macros sloppiness is set. A downside of this is
naturally that the user has to set sloppiness if SOURCE_DATE_EPOCH is
set in the environment.
Insight: SOURCE_DATE_EPOCH actually only ever changes the result if the
source code contains __DATE__. __TIME__ is not an issue since ccache
disables the direct mode if __TIME__is present and the preprocessor mode
will then see the actual expansion regardless of any SOURCE_DATE_EPOCH
value. Finally, the __TIMESTAMP__ case is not applicable at all since
it’s not affected by SOURCE_DATE_EPOCH.
Therefore, make sure to only hash SOURCE_DATE_EPOCH if we find __DATE__.
The user then does not have to set sloppiness to get direct mode hits
for files that don’t contain __DATE__.
Joel Rosdahl [Sat, 30 Jan 2021 18:17:33 +0000 (19:17 +0100)]
Improve speed of compiler launcher command in UseCcache.cmake
I noticed that the overhead of “cmake -E env” is around 9 ms on my
system. This means that ccache direct mode hits on average have become
twice as slow when building ccache itself on my system.
Improve this by using the standard “env” program if available. Its
overhead is around 1 ms.
Make `is_equal_object_files` more lenient when comparing COFF object
files.
COFF object files contain the original source file name, which was
breaking the cpp1 test. They often contain a timestamp used by the
incremental linker, unless this is explicitly disabled via `/Brepro`
(MSVC) or `-mno-incremental-linker-compatible` (clang).
Test suite fixes and improvements for Windows (#780)
* Tests: properly handle compiler arguments from CC environment variable
* Tests: don't pass test names directly to printf
In some cases test names would be interpreted as invalid arguments to
`printf` instead of a string to be printed, and this resulted in
confusing output on test failure.
* Tests: enable symlink support on Windows
git-bash's `ln -s` defaults to making a copy instead of making a symlink
for compatibility, but it is possible to ask for native Windows symlink
support instead.
Creating symlinks on Windows requires suitable permissions, or that
"Developer Mode" is enabled. (This is true for the Github Actions
Windows runners.)
* Tests: performance fixes for Windows
On Windows, git-bash's emulation of fork/exec is exteremely slow -- on
my machine it's typically around 30ms to spawn /usr/bin/true from a bash
script compared to 2ms on my macOS machine.
This is really noticeable when running ccache tests. This patch fixes
some of the hot code (i.e. code invoked for every test case) to avoid
spawning external commands or creating as many subshells.
* Tests: get more tests passing on Windows
- account for \r\n line endings in --version test
- skip tests that can never succeed on Windows
Joel Rosdahl [Sun, 17 Jan 2021 12:07:01 +0000 (13:07 +0100)]
Don’t cache result if a preprocessed header file is too new
Unless sloppiness is set to ignore mtime/ctime, ccache classifies a
newly created header file as “too new” and then skips it in
do_remember_include_file and returns false, which makes
remember_include_file disable the direct mode. This works as intended
for normal header files whose content is included in the preprocessed
output. However, for a preprocessed header file this doesn’t work well
since its content then isn’t included in the hash, which can lead to
false positive cache hits.
Fix this by not caching the result if a preprocessed header file is too
new, i.e. increment the “can’t use preprocessed header” statistics
counter and fall back to just running the compiler.
doc/MANUAL.adoc: Don't use non-ASCII quotes (#761)
Some locales like "LANG=fr_FR.iso885915@euro make" can't
handle UTF-8 single- and double-quotes:
$ LANG=fr_FR.iso885915@euro make
...
asciidoc: FAILED: MANUAL.adoc: line 529: unexpected error:
...
File "/usr/lib/python3.8/encodings/iso8859_15.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u201c'
in position 54: character maps to <undefined>
To avoid it the patch uses ASCII equivalents of symbols.
Azat Khuzhin [Sat, 9 Jan 2021 18:44:30 +0000 (21:44 +0300)]
Ignore SOURCE_DATE_EPOCH under time_macros sloppiness (#755)
SOURCE_DATE_EPOCH will be passed from debhelpers, by extracting last
entry from d/changelog (or current time if there is entries)
And this will not allow to use cache.
Alexander Lanin [Wed, 6 Jan 2021 20:50:41 +0000 (21:50 +0100)]
Fix scanning of headers with Clang-Tidy (#758)
By adding . as an include directory, CMake actually took it literally and files
are included from e.g. src/./AtomicFile.h. However in .clang-tidy headers with
an additional slash (headers from subdirectory third_party) are excluded from
reports.
This commit:
- gets rid of include via .
- fixes some warnings
- disables the rest
Joel Rosdahl [Wed, 6 Jan 2021 20:29:37 +0000 (21:29 +0100)]
Suppress Clang-Tidy warning about including signal.h
sigaddset and similar functions are specified by POSIX to be in signal.h
and the C++ csignal header only contains a subset of the signal.h
declarations.
Add VS2019 build jobs that use clang for the test suite. There are many
test failures on Windows, but these are ignored for now.
Tweak CMake build scripts:
- Fix CI build type handling for MSVC (recognise `/NDEBUG` and not just
`-DNDEBUG`)
- Fix incorrect warnings-as-errors flag for MSVC
- Suppress an additional conversion warning on MSVC
Joel Rosdahl [Mon, 4 Jan 2021 13:30:32 +0000 (14:30 +0100)]
Add debug_dir setting for specifying a directory for debug files
This makes it possible to store debug files outside a transient build
directory. As a bonus, it also allows for getting debug files when the
object file is /dev/null.
Joel Rosdahl [Wed, 6 Jan 2021 18:23:03 +0000 (19:23 +0100)]
Make Util::make_relative_path able to find matches for canonical path (#760)
Scenario:
- /tmp is a symlink to /private/tmp.
- Both apparent and actual CWD is /private/tmp/dir.
- A path (e.g. the object file) on the command line is /tmp/dir/file.o.
- Base directory is set up to match /tmp/dir/file.o.
Ccache then rewrites /tmp/dir/file.o into ../../private/tmp/dir/file.o,
which is correct but not optimal since ./file.o would be a better
relative path. Especially on macOS where, for unknown reasons, the
kernel sometimes disallows opening a file like
../../private/tmp/dir/file.o for writing.
Improve this by letting Util::make_relative_path try to match
real_path(path) against the CWDs and choose the best match.
Joel Rosdahl [Sun, 3 Jan 2021 12:39:57 +0000 (13:39 +0100)]
Fix retrieval of object file when destination is /dev/null
ResultRetriever::on_entry_data assumes that a destination file has been
opened if the entry type is not stderr_output, but that’s incorrect
since on_entry_start doesn’t open a destination file if it’s /dev/null.
An assertion is triggered:
Joel Rosdahl [Wed, 30 Dec 2020 20:22:26 +0000 (21:22 +0100)]
Only accept -f(no-)color-diagnostics for Clang
If a non-Clang compiler gets -f(no-)color-diagnostics then bail out and
just execute the compiler. The reason is that we don't include
-f(no-)color-diagnostics in the hash so there can be a false cache hit
in the following scenario:
Joel Rosdahl [Tue, 29 Dec 2020 18:33:54 +0000 (19:33 +0100)]
Remove obsolete (and now incorrect) fallback replacement of realpath(3)
The fallback replacement of realpath(3) (from 8e918ccc) uses readlink(3)
under the assumption that we’re only interested about symlinks, but
that’s no longer the case: we’re using it for normalization purposes as
well. Let’s just remove it. If it turns out that there still are
non-Windows systems that don’t have realpath(3) and that we care about
we’ll figure out something else.
Bash tests were not actually being run on the macOS CI agents because
the version of sed installed there does not understand the `-r` flag:
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
- use `sed -E` instead of `sed -r` as the latter isn't supported by BSD sed.
- export `SDKROOT` in `test/run`. Otherwise it appears at least some
some Apple toolchains (e.g. Xcode 10.3) will pick the _latest_ SDK
installed on the host (e.g.
`/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk`)
instead of using the SDK bundled with the toolchain (e.g.
`/Applications/Xcode_10.3.app/.../MacOSX10.14.sdk`).
The 10.15 SDK is not compatible with Xcode 10.3:
ld: unsupported tapi file type '!tapi-tbd' in YAML file
'/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib/libSystem.tbd'
for architecture x86_64
clang: error: linker command failed with exit code 1
Improve TemporaryFile implementation for Windows (#736)
On Windows, multiple ccache process could race each other to create,
rename and delete temporary files, because they would attempt to
generate the same sequence of temporary file names
(`tmp.cpp_stdout.iG2Kb7`, `tmp.cpp_stdout.P1kAlM`,
`tmp.cpp_stdout.FzP5tM`, ...).
This is because ccache used mingw-w64's [implementation of mkstemp][1],
which uses `rand()` to generate temporary file names, and ccache was
never seeding the thread-local PRNG used by `rand()`.
Replace ccache's use of `mkstemp()` on Windows with an implementation
based on OpenBSD. This allows us to sidestep mingw-w64's problematic
implementation, and allows us to build using MSVC again. (MSVC's C
standard library does not provide `mkstemp()`.)
Example errors:
- Some ccache process is in the process of deleting a temporary file:
ccache: error: Failed to create temporary file for C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.FzP5tM: Access is denied.
- Some ccache process has destination file open, so it can't be overwritten:
ccache: error: failed to rename C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.iG2Kb7 to C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.iG2Kb7.ii: Access is denied.
- Source file has been deleted by some other ccache process:
ccache: error: failed to rename C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.P1kAlM to C:\Users\someuser/.ccache/tmp/tmp.cpp_stdout.P1kAlM.ii: The system cannot find the file specified.
Joel Rosdahl [Mon, 21 Dec 2020 18:02:19 +0000 (19:02 +0100)]
Add preprocessed file extension to cpp stdout early
Unless when compiling a preprocessed file directly, ccache creates a
temporary file to store the output of the preprocessor, registers the
file for removal at program exit, renames the file to one with a .i/.ii
extension and then registers that file for removal as well. This works
by chance in practice as long as mkstemp() returns something with low
probability of being reused, but as discussed in #736 it risks failing
when mkstemp() doesn’t behave that way.
Fix this by creating the new name (with the needed extension) using a
hard link so that the original file will outlive the new file, thus
blocking another ccache process from creating a file with the same name
again. To make the new temporary file outlive the old file, also delete
pending temporary files in LIFO order.
Joel Rosdahl [Tue, 8 Dec 2020 20:19:19 +0000 (21:19 +0100)]
Improve incorrect documentation on what cache_dir does
The manual says that cache_dir only takes effect if set by $CCACHE_DIR
or by cache_dir in the secondary (system-wide) configuration file, which
is incorrect. It’s kind of correct for how the primary configuration
file is found, though.
Erik Flodin [Mon, 7 Dec 2020 18:20:31 +0000 (19:20 +0100)]
Improve SIMD detection (#735)
* Try to compile code to detect SSE/AVX support. Just checking if the compiler
supports the flag isn't enough as e.g. Clang on Apple's new ARM silicon seems
to accept the flag but then fails when building.
* Try to detect and enable BLAKE3's Neon support.
* Improve detection of AVX2 target attribute support and remove the explicit
compiler version check that hopefully shouldn't be needed.
Joel Rosdahl [Mon, 7 Dec 2020 06:19:52 +0000 (07:19 +0100)]
Improve mutex handling code structure in InodeCache
InodeCache’s acquire_bucket and release_bucket functions need to be used
together. There is no problem with this currently, but it’s possible
that one may forget to call release_bucket in some code path in the
future.
Improve this by introducing a single InodeCache::with_bucket function
that makes it impossible to misuse the API. This should also make
thread-safety analysis algorithms, e.g. Clang’s
-Wthread-safety-analysis, happy.
Joel Rosdahl [Sat, 5 Dec 2020 18:17:32 +0000 (19:17 +0100)]
Reintroduce dev mode and disable problematic build flags in user mode
In version 3.x, ccache was in “user mode” when building from release
archive sources and “dev mode” otherwise. In “dev mode”, additional
compiler flags like “-Wextra -Wpedantic -Werror” were added, but they
were not present in “user mode” in order not to break end users’ builds.
This behavior was partially lost in the conversion to CMake.
This commit tries to imitate the previous behavior by introducing a
CCACHE_DEV_MODE CMake variable and only enable potentially problematic
compiler flags when it’s set to ON.