Commit Graph

3143 Commits

Author SHA1 Message Date
James Widman
4e4ec4c8ab Introduce new tool: -t compdb-targets
Fixes #1544

Co-authored-by: Linkun Chen <lkchen@google.com>
Co-authored-by: csmoe <csmoe@msn.com>
Co-authored-by: James Widman <james.widman@gmail.com>
2024-10-13 01:31:01 -07:00
Jan Niklas Hasse
fc2f81e072 Merge pull request #2489 from digit-google/safer-depslog-parser
Make the deps_log parser report corrupted files instead of crashing.
2024-10-04 13:14:28 +02:00
Jan Niklas Hasse
4c59ee0654 Merge pull request #2503 from digit-google/fix-output-regression
Fix #2499: Status update regression.
2024-10-01 09:03:18 +02:00
David 'Digit' Turner
562cee194c Fix #2499: Status update regression.
PR #2487 introduced a regression, where a completed command without
an output would force a newline, preventing the next status update
to appear on the same line in smart terminals.

This fixes the issue by adding the missing `!outputs.empty()`
condition + adding a proper regression test to catch future
breaks.

Fixed: #2499
2024-09-30 09:24:55 +00:00
Jan Niklas Hasse
6a31acc06c Merge pull request #2494 from vaerksted/master
fix typos
2024-09-19 08:12:34 +02:00
Jan Niklas Hasse
41ecb09d36 Merge pull request #2485 from digit-google/fix-inputs-tool
Fix `inputs` tool logic and add new formatting options.
2024-09-19 08:11:23 +02:00
Nico Weber
fe834334ec Merge pull request #2492 from digit-google/create-depfile-directory
Ensure depfile's parent directory is created before running an action.
2024-09-10 11:26:14 -04:00
spaette
60838c11e7 typos 2024-09-09 09:36:10 -05:00
David 'Digit' Turner
197dbdba41 Make the deps_log parser report corrupted files instead of crashing.
The parser used asserts to verify the consistency of the
deps log, which resulted in crashes in production. Replace
them with correct checks and return conditions.

This allows the parser to survive and report corrupted
files, and Ninja to ignore the latter.

+ Add related unit-test.

Fixes #2472
Fixes #2309
2024-09-04 15:55:20 +02:00
David 'Digit' Turner
46f272da3b DiskInterface::WriteFile(): Ensure binary file mode.
Call fopen() with "wb" instead of "w" to avoid translating
\n into \r\n on Windows, based on the machine's configuration.

This prevents random test breakages on Windows/x64 CI builders
that do not happen on Windows/arm64 or other operating systems.
2024-09-04 15:52:48 +02:00
David 'Digit' Turner
2c13023b7e Ensure depfile's parent directory is created before running an action.
For most actions, the depfile will be in the same directory as one
of its outputs, and their  parent directory will be created by Ninja
before running the command. However, this is not always the case.

In particular, the GN build tool is changing its Ninja build plan
generation logic, switching from using stamp files to phony targets,
after issue #478 was fixed in Ninja. For additionnal context
see https://gn-review.googlesource.com/c/gn/+/11380.

The newly generated build plans trigger this condition more frequently,
which results in flaky build failures for large GN-based projects such
as Fuchsia.

This patch ensures the depfile's parent directory is always created
before the command is launched to get rid of the issue entirely.
2024-09-04 12:19:31 +02:00
Jan Niklas Hasse
b2ae865439 Merge pull request #2487 from digit-google/faster-elide-middle
Faster elide middle implementation
2024-09-02 19:50:16 +02:00
David 'Digit' Turner
1ba5570807 StatusPrinter: only strip ANSI sequences when needed.
Just check for the ESC character in the `output` string,
if none is present, or if the terminal supports color,
avoid unnecessary busy work (string allocations and
parsing).
2024-09-02 18:36:12 +02:00
David 'Digit' Turner
5dd9b78fad ElideMiddle: Get rid of std::regex usage and heap allocations.
Get rid of std::regex and std::vector usage in ElideMiddleInPlace()
function. These are replaced with custom iterator classes that do
not perform any memory allocation at runtime.

Instead the input string will be parsed twice (once to determine the
visible width, and another time to create the elided result string).

With this patch, elide_middle_perftest goes from 87.1ms to 15.0ms
on my machine (x5.8 faster!). This also removes 65 kiB from the
stripped Linux binary for Ninja (!)
2024-09-02 18:36:12 +02:00
David 'Digit' Turner
ee9a296228 Faster and more correct ElideMiddle with ANSI sequences
Add a fast-path for the case where there is no escape sequence
in the input, that avoids un-necessary string allocations.

Properly handle ANSI color sequences that appear in the elided
part of the input string. They *must* be preserved to ensure
that the right span is printed with the right color.

For example, consider the following input:

    |GREEN|aaaaaaaaaaaaa|RED|bbbbbbbbbbb|BLACK|

With different levels of elision:

    max_width=0   -> |GREEN|RED|BLACK| instead of ""

    max_width=5   -> |GREEN|a..|RED|b|BLACK|
      (this was |GREEN|a..b|BLACK| before this fix)

Moreoever, do not call std::regex_iterator::str() to avoid
un-necessary string allocations and concatenations.

With this patch, 'elide_middle_perftest' goes from 159.9ms
to 87.1 ms on my machine (x1.8 faster)
2024-09-02 18:36:12 +02:00
David 'Digit' Turner
2d7ee526c4 Move ElideMiddle() to its own source file.
Move the function to its own source file since it is
no longer trivial (and will get optimized in future
patches).

+ Add a new ElideMiddleInPlace() function that doesn't
  do anything if no elision is required + use it in
  status.cc.

+ Add a new elide_middle_perftest program to benchmark
  the performance of the function.

  On my machine, the 'avg' result is 159.9ms.
2024-09-02 18:36:12 +02:00
David 'Digit' Turner
60f901e4bc Rewrite ElideMiddle.ElideAnsiEscapeCodes test.
Use C string macros to hold ANSI escape sequences in order
to make the test much easier to understand and follow.

NOTE: This does not change the test in any way!
2024-09-02 18:36:00 +02:00
David 'Digit' Turner
284349311d Add new options to inputs tool.
Add new options to the `inputs` tool in order to change
the format of its output:

- `--no-shell-escape` to avoid shell-escaping the results.

- `--dependency-order` to return results in dependency order,
  instead of sorting them alphabetically.

- `--print0` to use \0 as the list separator in the list,
  useful to process the target paths with `xargs -0` and
  similar tools.
2024-09-02 17:23:05 +02:00
David 'Digit' Turner
5b94d3407f Fix inputs tool implementation.
This uses the InputsCollector class introduced in the
previous patch to implement the tool properly. Results
are still shell-escaped and sorted alphabetically.

Fixed #2482
2024-09-02 17:23:05 +02:00
David 'Digit' Turner
7e03348fe3 Add InputsCollector class.
Add a new class to wrap the logic needed to implement the
`inputs` tool correctly (see Issue #2482 for details), and
provide a unit-test for it.
2024-09-02 17:23:05 +02:00
Jan Niklas Hasse
4b7d3997c7 Merge pull request #2486 from digit-google/remove-warnings
Remove warnings
2024-08-30 22:24:50 +02:00
David 'Digit' Turner
50ae55a142 build.cc: Add 'override' directives to virtual method overrides.
Technically, these are not required since Ninja is built with -std=c++11,
but using these directives is good practice to clarify the code and
avoid simple human errors.
2024-08-28 15:24:57 +02:00
David 'Digit' Turner
0f05540e8d metrics.cc: Remove unused GetFrequency() function.
It was declared in an anonymous namespace, and nothing
uses it anymore, removes a compiler warning.
2024-08-28 15:24:50 +02:00
David 'Digit' Turner
77d979b459 explanations.h: Remove Explanations:enabled_ field.
This field is unused. This patch removes compiler warnings when
building with recent compilers (e.g. Clang 16.0)
2024-08-28 15:24:18 +02:00
David 'Digit' Turner
7d671d2800 status_printer.h: Add 'override' statements to avoid warnings.
This adds `override` statements, and removes an un-necessary
destructor declaration from status_printer.h in order to remove
annoying compiler warnings when building Ninja tests with CMake
with recent compilers. For example, clang 16.0 complains with:

```
/path/to/ninja/src/status_printer.h:29:16: warning: 'EdgeAddedToPlan' overrides a member function but is not marked 'override' [
-Winconsistent-missing-override]
  virtual void EdgeAddedToPlan(const Edge* edge);
               ^
```

The root issue is that while libninja is compiled explicitly for C++11,
which disable the warning, the tests themselves are not, since they rely
on GTest which now requires C++14, and thus the standard being used is
determined by the compiler's default configuration.
2024-08-28 15:22:57 +02:00
Jan Niklas Hasse
f220dc5845 Merge pull request #2479 from mcprat/workflow-ctest-verbose
GitHub Actions: Prevent ctest invocation error on macOS
2024-08-28 13:47:45 +02:00
Michael Pratt
6e77cdf3a0 GitHub Actions: Remove usage of CTest
It seems that new versions of CTest now require case-sensitive arguments,
resulting in an "unknown argument" error for "-vv", while documentation
shows the argument for the full verbosity option as "-VV".

In CMakeLists.txt, there is currently only 1 test registered to CMake
with add_test(), and that is to run the ninja_test binary.

Since using CTest is not reducing the number of commands used for testing,
fix the testing invocation by replacing CTest with a direct run of the test.

Signed-off-by: Michael Pratt <mcpratt@pm.me>
2024-08-25 21:19:59 -04:00
Jan Niklas Hasse
dcefb83853 Merge pull request #2461 from atetubou/patch-1
Fix typo: Explaantions -> Explanations
2024-06-14 17:58:58 +02:00
Takuto Ikuta
2f19d3a071 Fix typo 2024-06-13 15:45:09 +09:00
Jan Niklas Hasse
0b4b43aa3e Merge pull request #2452 from mavit/emacs-metadata
Add Emacs package attributes, “Keywords” and “URL”
2024-05-28 21:45:49 +02:00
Jan Niklas Hasse
8aa51b2912 Merge pull request #2455 from hundeboll/nominmax
cmake: disable min() and max() macros in windows.h
2024-05-28 21:45:01 +02:00
Martin Hundebøll
b3324979ef cmake: disable min() and max() macros in windows.h
Using std::min() and/or std::max() in sources where windows.h has been
include fails with

    error C2589: '(': illegal token on right side of '::' [D:\a\ninja\ninja\build\libninja.vcxproj]
    error C2062: type 'unknown-type' unexpected [D:\a\ninja\ninja\build\libninja.vcxproj]
    error C2059: syntax error: ')' [D:\a\ninja\ninja\build\libninja.vcxproj]
    error C2589: '(': illegal token on right side of '::' [D:\a\ninja\ninja\build\libninja.vcxproj]
    error C2059: syntax error: ')' [D:\a\ninja\ninja\build\libninja.vcxproj]

Avoid this by defining NOMINMAX for windows builds, which causes the
windows.h header to skip defining those two macros.
2024-05-27 22:31:47 +02:00
Jan Niklas Hasse
a58bf701d6 Merge pull request #2453 from HampusAdolfsson/flush_before_setmode
Flush stdout before calling _setmode on windows
2024-05-27 22:21:49 +02:00
Jan Niklas Hasse
6b8eeb8e07 Merge pull request #2456 from hundeboll/codespell
codespell: fix taked => taken, and ignore false positives
2024-05-27 22:21:21 +02:00
Martin Hundebøll
2d17936e9b codespell: fix taked => taken, and ignore false positives 2024-05-27 09:58:37 +02:00
Jan Niklas Hasse
ee43260944 Merge pull request #2451 from digit-google/explanations-class
Remove global `explanations_` variable
2024-05-23 23:00:40 +02:00
Hampus Adolfsson
dd8166d899 Flush stdout before calling _setmode on windows
This fixes a regression of #773 which was caused by enabling
stdout buffering in #2085. Before calling _setmode, the stdout
buffer should be emptied to ensure that the new mode is not
applied to the buffered content. See the 'Remarks' section here:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode
2024-05-23 11:54:36 +02:00
Peter Oliver
7744530eaf Add Emacs package attributes, “Keywords” and “URL” 2024-05-20 16:54:52 +01:00
David 'Digit' Turner
214df86c13 Remove the global explanations_ variable.
Remove the global variable from debug_flags.cc, instead
make each Builder instance provide its own optional
Explanations class instance, if `-d explain` is used.

Adjust all Builder dependencies to use a pointer to
this instance, instead of writing to a global mutable
variable from random places.

- Add `Explanations*` argument do DependencyScanner()
  and DyndepLoader() constructor. The value can be null
  if nothing needs to be recorded (e.g. in unit tests).

+ Remove record_explanation() and print_explanations(),
  and EXPLAIN() macro.

+ Remove the deprecated code path from status_printer.cc.
2024-05-19 17:41:39 +02:00
David 'Digit' Turner
1cb6ef8492 Status: Add SetExplanations() method.
Allow the StatusPrinter::PrintStatus() method to
use an Explanations instance, instead of calling
the global `print_explanations()` method.

Since the rest of Ninja still uses `record_explanation()`
at the moment, keep the old code path until we can get
rid of it as well.
2024-05-19 17:31:50 +02:00
David 'Digit' Turner
986d8440ca Add Explanations class
This class will be used to replace the global
mutable variable |explanations_| currently defined
in debug_flags.cc, and updated from random locations
of the source code through the `record_explanation()`
function.
2024-05-19 17:04:12 +02:00
Jan Niklas Hasse
805cf31757 Merge pull request #2067 from drothlis/explain-later
Print "explain" debug just before each command is run
2024-05-17 21:14:57 +02:00
Jan Niklas Hasse
f14a949534 Merge pull request #2448 from digit-google/build-tests-in-configure.py
configure.py: Support --gtest-source-dir to build tests.
2024-05-14 17:55:48 +02:00
Jan Niklas Hasse
d4b6084f49 Merge pull request #2428 from jhasse/elide-middle-ansi-escape
Correctly handle ANSI Escape Codes in ElideMiddle, fix #713
2024-05-14 17:49:55 +02:00
David 'Digit' Turner
afcd4a146f configure.py: Support --gtest-source-dir to build tests.
Allow the Ninja build plan generated by configure.py to
build `ninja_test` by compiling GoogleTest from source if
the path to the library if passed through the new option
`--gtest-source-dir` or the GTEST_SOURCE_DIR environment
variable.

For simplicity, probing for an installed version of the
library, and linking to it, is not supported (use the
CMake build for this).

This also removes the obsolete `--gtest-dir` option.

+ Update README.md

Fixes #2447
2024-05-13 00:14:14 +02:00
Jan Niklas Hasse
c470bf76f4 Merge pull request #2443 from digit-google/critical-path-with-topo-sort
ComputeCriticalPath: Use topological sort to speed up function.
2024-05-11 13:28:25 +02:00
David 'Digit' Turner
ef89584d0c ComputeCriticalPath: Use topological sort to speed up function.
Use a topological sort to get a sorted list of edges to perform
the critical path weigth propagation as fast as possible.

The previous version used a data flow algorithm that forced
the values to be recomputed several times for far too many
edges on complex build plans.

For example, for a Fuchsia build plan with 93339 edges,
this reduces the ComputeCriticalPath metric from 1.9s to 80ms!

The unit-tests still pass, and manual testing shows that the
order of commands does not change before and after this change
for the example build plan above.
2024-05-09 10:26:43 +02:00
David Röthlisberger
808fab6cd5 Add test for "-d explain" output
Before #2067, ninja would have printed this instead for the second
`ninja` invocation:

    ninja explain: output .FORCE of phony edge with no inputs doesn't exist
    ninja explain: .FORCE is dirty
    ninja explain: input is dirty
    ninja explain: mid is dirty
    [1/3] [ -e input ] || touch input
2024-05-08 13:44:50 +01:00
David Röthlisberger
37e0870c1c misc/output_test.py: Refactor to allow calling ninja multiple times
So that we're able to test *re* running ninja to test the output of
no-op builds etc.

I'll use this in the next commit to test "-d explain" output.
2024-05-08 13:44:50 +01:00
David Röthlisberger
8e6c741a4b "explain" debug prints just before each command is run
The explanations are printed just before each command is run. Before, it
would potentially print pages & pages of explanations at the beginning,
and it was hard to tell which explanation caused a particular command to
run.

Edges that are pruned due to "restat" used to print all their
explanations at the beginning, cluttering up the output with
explanations that didn't actually cause any rebuilding. Now, those
explanations aren't printed if those edges aren't run.
2024-05-08 13:44:50 +01:00