Skip to content

Commit

Permalink
[NFC] Document the solution to pr61006 and a test for it
Browse files Browse the repository at this point in the history
Address #61006.

The actual reason for the issue is about the usage of clang-scan-deps
instead of its functionalities since clang-scan-deps has
already considered the problem before for clang modules.

So this patch tries to document the corresponding solution
and add a test case for it to address the issue.
  • Loading branch information
ChuanqiXu9 committed Aug 11, 2023
1 parent 63afb70 commit f6b94e0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
32 changes: 26 additions & 6 deletions clang/docs/StandardCPlusPlusModules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1072,12 +1072,6 @@ the user can choose to get the dependency information per file. For example:
$ clang-scan-deps -format=p1689 -- <path-to-compiler-executable>/clang++ -std=c++20 impl_part.cppm -c -o impl_part.o
.. warning::

The ``<path-to-compiler-executable>/clang++`` should point to the real
binary and not to a symlink. If it points to a symlink the include paths
will not be correctly resolved.

And we'll get:

.. code-block:: text
Expand Down Expand Up @@ -1134,6 +1128,32 @@ We will get:
When clang-scan-deps detects ``-MF`` option, clang-scan-deps will try to write the
dependency information for headers to the file specified by ``-MF``.

Possible Issues: Failed to find system headers
----------------------------------------------

In case the users encounter errors like ``fatal error: 'stddef.h' file not found``,
probably the specified ``<path-to-compiler-executable>/clang++`` refers to a symlink
instead a real binary. There are 4 potential solutions to the problem:

* (1) End users can resolve the issue by pointing the specified compiler executable to
the real binary instead of the symlink.
* (2) End users can invoke ``<path-to-compiler-executable>/clang++ -print-resource-dir``
to get the corresponding resource directory for your compiler and add that directory
to the include search paths manually in the build scripts.
* (3) Build systems that use a compilation database as the input for clang-scan-deps
scanner, the build system can add the flag ``--resource-dir-recipe invoke-compiler`` to
the clang-scan-deps scanner to calculate the resources directory dynamically.
The calculation happens only once for a unique ``<path-to-compiler-executable>/clang++``.
* (4) For build systems that invokes the clang-scan-deps scanner per file, repeatedly
calculating the resource directory may be inefficient. In such cases, the build
system can cache the resource directory by itself and pass ``-resource-dir <resource-dir>``
explicitly in the command line options:

.. code-block:: console
$ clang-scan-deps -format=p1689 -- <path-to-compiler-executable>/clang++ -std=c++20 -resource-dir <resource-dir> mod.cppm -c -o mod.o
Possible Questions
==================

Expand Down
44 changes: 44 additions & 0 deletions clang/test/ClangScanDeps/pr61006.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// The slash direction in linux and windows are different.
// Also the command to create symbolic link is different.
// UNSUPPORTED: system-windows
//
// RUN: rm -fr %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: EXPECTED_RESOURCE_DIR=`%clang -print-resource-dir`
// RUN: ln -s %clang++ %t/clang++
// RUN: sed "s|EXPECTED_RESOURCE_DIR|$EXPECTED_RESOURCE_DIR|g; s|DIR|%/t|g" %t/P1689.json.in > %t/P1689.json
// RUN: clang-scan-deps -compilation-database %t/P1689.json -format=p1689 | FileCheck %t/a.cpp -DPREFIX=%/t
// RUN: clang-scan-deps -format=p1689 \
// RUN: -- %t/clang++ -std=c++20 -c -fprebuilt-module-path=%t %t/a.cpp -o %t/a.o \
// RUN: -resource-dir $EXPECTED_RESOURCE_DIR | FileCheck %t/a.cpp -DPREFIX=%/t

//--- P1689.json.in
[
{
"directory": "DIR",
"command": "DIR/clang++ -std=c++20 -c -fprebuilt-module-path=DIR DIR/a.cpp -o DIR/a.o -resource-dir EXPECTED_RESOURCE_DIR",
"file": "DIR/a.cpp",
"output": "DIR/a.o"
}
]

//--- a.cpp
#include <stddef.h>
import b;

// CHECK: {
// CHECK-NEXT: "revision": 0,
// CHECK-NEXT: "rules": [
// CHECK-NEXT: {
// CHECK-NEXT: "primary-output": "[[PREFIX]]/a.o",
// CHECK-NEXT: "requires": [
// CHECK-NEXT: {
// CHECK-NEXT: "logical-name": "b"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: ],
// CHECK-NEXT: "version": 1
// CHECK-NEXT: }

0 comments on commit f6b94e0

Please sign in to comment.