Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Default Asset File locator for clang docs #97505

Closed
wants to merge 9 commits into from

Conversation

bob80905
Copy link
Contributor

@bob80905 bob80905 commented Jul 3, 2024

In clang-tools-extra\clang-doc\tool\ClangDocMain.cpp, there is a function, getDefaultAssetFiles that tries to find the location of
<build dir>\share\clang-doc\index.js, but fails. This is because there are alternate configurations in setting up the llvm project, and executables in different locations may be run. In some cases, executables are built and run in this location:
<build dir>\bin\<executables>
and in other cases, this location:
<build dir>\[Debug | Release]\bin\<executables>

The locator assumed that the executable that is running was in the first example above, and only ascended one directory, and in the case that an executable under Debug / Release was run, the index.js file wouldn't be found.
This PR changes the way in which the asset files are copied over to the build output directory, by placing the directory containing the asset files directly under whichever specific build-output configuration directory is being processed by cmake.
In this way, regardless of which configuration's executable runs getDefaultAssetFiles, the asset files will be found directly underneath the current build directory.
Fixes #98056

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 3, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: Joshua Batista (bob80905)

Changes

In clang-tools-extra\clang-doc\tool\ClangDocMain.cpp, there is a function, getDefaultAssetFiles that tries to find the location of
&lt;build dir&gt;\share\clang-doc\index.js, but fails. This is because there are alternate configurations in setting up the llvm project, and executables in different locations may be run. In some cases, executables are built and run in this location:
&lt;build dir&gt;\bin\&lt;executables&gt;
and in other cases, this location:
&lt;build dir&gt;\[Debug | Release]\bin\&lt;executables&gt;

The locator assumed that the executable that is running was in the first example above, and in the case that an executable under Debug / Release was run, the index.js file wouldn't be found.
This PR adjusts the locator to be agnostic to either scenario described above, and ascends an extra directory if the Debug directory is detected.


Full diff: https://github.com/llvm/llvm-project/pull/97505.diff

1 Files Affected:

  • (modified) clang-tools-extra/clang-doc/tool/ClangDocMain.cpp (+7-1)
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 6198a6e0cdcc3..b97fa715f9e67 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -167,7 +167,13 @@ llvm::Error getDefaultAssetFiles(const char *Argv0,
 
   llvm::SmallString<128> AssetsPath;
   AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
-  llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
+  llvm::sys::path::append(AssetsPath, "..");
+  llvm::SmallString<128> tempCopyDbg = AssetsPath;
+  llvm::sys::path::append(tempCopyDbg, "Debug");
+  // index.js may be in the debug
+  if (!llvm::sys::fs::is_directory(tempCopyDbg))
+    llvm::sys::path::append(AssetsPath, "..");
+  llvm::sys::path::append(AssetsPath, "share", "clang-doc");
   llvm::SmallString<128> DefaultStylesheet;
   llvm::sys::path::native(AssetsPath, DefaultStylesheet);
   llvm::sys::path::append(DefaultStylesheet,

@bob80905
Copy link
Contributor Author

bob80905 commented Jul 3, 2024

An important assumption I'm making here is that the Debug directory cannot exist without the Release directory. If this assumption doesn't hold, then this fix will fail if only the release directory exists and debug is missing. I believe by the nature of how the llvm project is generated, both directories are generated at all times.

Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right fix. The Release/Debug directories only exist in the build tree, so this is incurring an unnecessary cost in an installed clang-doc and introducing a potentially very strange behaviour for weird install locations.

Probably the simplest approach to fixing the problem would be to duplicate the share/clang-doc directory per config. This is a bit annoying because of the redundancy, but it's probably better than pursuing something more elaborate.

@damyanp
Copy link
Contributor

damyanp commented Jul 3, 2024

I don't think that the current description matches the change?

Does this mean that there'll likely be 3 copies of each file. One per configuration, and another copy just in the binary directory? In a multi-config build do we still need the copy in the binary directory?

@bob80905
Copy link
Contributor Author

bob80905 commented Jul 3, 2024

Updated the description.
In my tests, there are at least 5 copies of this file in the overall build output directory:

<llvm-project-dir>/<MS-Build-Dir>/Debug/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/Release/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/MinSizeRel/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/RelWithDebInfo/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/share/clang-doc

Yes, one per configuration, and one underneath the binary directory.
I am unsure if we still need the copy in the binary directory, because I'm not sure which executable is run by default when running tests.

@bob80905 bob80905 self-assigned this Jul 8, 2024
@bogner
Copy link
Contributor

bogner commented Jul 8, 2024

Updated the description. In my tests, there are at least 5 copies of this file in the overall build output directory:

<llvm-project-dir>/<MS-Build-Dir>/Debug/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/Release/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/MinSizeRel/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/RelWithDebInfo/share/clang-doc
<llvm-project-dir>/<MS-Build-Dir>/share/clang-doc

Yes, one per configuration, and one underneath the binary directory. I am unsure if we still need the copy in the binary directory, because I'm not sure which executable is run by default when running tests.

We shouldn't need the one in the binary directory - IIUC one per configuration will be sufficient, and the one that's never used is potentially confusing.

Please also test locally that the install target does the right thing with these changes. You can use -DCMAKE_INSTALL_PREFIX=/some/temp/dir to make sure it doesn't install to some system location and is easy to spot check.

clang-tools-extra/clang-doc/tool/CMakeLists.txt Outdated Show resolved Hide resolved
Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind of unfortunate how this works dependency-wise - we end up creating "Debug/share/clang-doc/${f}" even when we only build "Release" and vice-versa, as the "copy-clang-doc-assets" explicitly depends on each version of this file. Also the actual install copies the file directly from the source directory, so if we did end up actually configuring these files per config for some reason there's some potential for confusion.

However, the cmake-fu to do this any better is beyond me, and the current state is that we actually have broken bots because of this, so I think the solution here is Good Enough to get in and get the bots unblocked.

set(out_files)

function(copy_files_to_dst src_dir dst_dir file)
set(src "${src_dir}/${file}")
set(dst "${dst_dir}/${file}")
if (NOT EXISTS ${dst_dir})
file(MAKE_DIRECTORY ${dst_dir})
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is necessary - copy_if_different will do the right thing.

@bob80905
Copy link
Contributor Author

bob80905 commented Jul 9, 2024

Closing this PR since #98099 resolves the issue and has already been merged.

@bob80905 bob80905 closed this Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restore functionality to clang-doc asset locator in multi-config builds
5 participants