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

rust-analyzer for out-of-tree modules #914

Open
wants to merge 1 commit into
base: rust
Choose a base branch
from

Conversation

vvarma
Copy link

@vvarma vvarma commented Oct 17, 2022

adding support for running make rust-analyzer from an out-of-tree module.

make LLVM=1 -C <path_to_kdir> M=`pwd` rust-analyzer

@ojeda
Copy link
Member

ojeda commented Oct 18, 2022

Thanks!

Please read Documentation/process/submitting-patches.rst. In particular, you will need to reword your commit messages to explain why each patch is needed, fix the title with a prefix (see previous commits to get a feeling for what is the right one), and also sign your work with your real name to certify you that you wrote it or otherwise have the right to pass it on as an open-source patch (Developer's Certificate of Origin).

rust/Makefile Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
rust/Makefile Outdated
@@ -392,6 +392,10 @@ rust-analyzer:
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
$(RUST_LIB_SRC) > $(objtree)/rust-project.json

rust-analyzer-extmod:
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(abs_srctree) $(abs_objtree) \
Copy link
Member

@ojeda ojeda Dec 2, 2022

Choose a reason for hiding this comment

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

Why are absolute paths required compared to the existing target? It would be good to mention the reason in a comment -- I did something similar in our other rustfmt target for find:

# We match using absolute paths since `find` does not resolve them
# when matching, which is a problem when e.g. `srctree` is `..`.

If it is due to some detail in the Python script, then it may make sense to resolve it / expand them / explain it in the Python script. Or, if the targets are merged (please see my other comment on that), then maybe we can use absolute paths for both here.

rust/Makefile Outdated Show resolved Hide resolved
@vvarma vvarma force-pushed the extmod/rustanalyzer branch from c688922 to 96f2342 Compare December 3, 2022 11:39
@ojeda
Copy link
Member

ojeda commented Jan 4, 2023

@vvarma
Copy link
Author

vvarma commented Jan 16, 2023

Hi @ojeda ! Is there anything else that I need to do to get this PR merged?

@ojeda
Copy link
Member

ojeda commented Jan 16, 2023

Now that the targets are merged, would the existing target work for both cases (in-tree and out-of-tree) with abs_ paths, so that we simplify the code? Or not? i.e. this part of my comment above:

Or, if the targets are merged (please see my other comment on that), then maybe we can use absolute paths for both here.

Also, could you please submit this as a patch to the mailing list? Thanks!

@vvarma vvarma force-pushed the extmod/rustanalyzer branch 2 times, most recently from 23d263e to 2fca591 Compare January 18, 2023 15:12
@ojeda
Copy link
Member

ojeda commented Jan 19, 2023

@ojeda
Copy link
Member

ojeda commented Jan 19, 2023

@bjorn3 @aliceinwire Since you reviewed this patch previously, you may want to send the Reviewed-bys into the mailing list (the patch changed a fair bit since then, though). Most importantly, if you have time to give it a try and confirm it works in your setup (in- and/or out-of-tree) to give a Tested-by, it would be great. Thanks!

for path in (srctree / folder).rglob("*.rs"):
logging.info("Checking %s", path)
name = path.name.replace(".rs", "")

# Skip those that are not crate roots.
if f"{name}.o" not in open(path.parent / "Makefile").read():
if os.path.exists(path.parent / "Makefile") and f"{name}.o" not in open(path.parent / "Makefile").read():

Choose a reason for hiding this comment

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

As I was testing this with the rust-out-of-tree-module example, I found that the crate was getting skipped because of this logic. I believe this will also need to search a possible Kbuild file (E.g. the example)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 1546b80db554..f5567825c94c 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -107,7 +107,15 @@ def generate_crates(srctree, objtree, sysroot_src, external_src):
             name = path.name.replace(".rs", "")
 
             # Skip those that are not crate roots.
-            if os.path.exists(path.parent / "Makefile") and f"{name}.o" not in open(path.parent / "Makefile").read():
+            def is_crate_root(*filenames):+                for filename in filenames:
+                    filepath = path.parent / filename
+                    if os.path.exists(filepath) and f"{name}.o" in open(filepath).read():
+                        return True
+                return False
+
+            if not is_crate_root("Makefile", "Kbuild"):
+                logging.warning(f"skipping")
                 continue
 
             logging.info("Adding %s", name)

Copy link

@thepacketgeek thepacketgeek left a comment

Choose a reason for hiding this comment

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

Found an issue where .o files referenced in Kbuild files are causing the script to skip the crate addition in rust-project.json

@ojeda
Copy link
Member

ojeda commented Jan 21, 2023

Indeed, @bjorn3 also reported it in the mailing list today, and I agree it should be supported (and potentially a smarter way provided) -- please see the thread at https://lore.kernel.org/rust-for-linux/20230118160220.776302-1-varmavinaym@gmail.com/

@thepacketgeek
Copy link

Indeed, @bjorn3 also reported it in the mailing list today, and I agree it should be supported (and potentially a smarter way provided) -- please see the thread at https://lore.kernel.org/rust-for-linux/20230118160220.776302-1-varmavinaym@gmail.com/

Ah, sorry I missed it in the mailing list, will follow along there now (Y)

@ojeda
Copy link
Member

ojeda commented Jan 21, 2023

No need to apologize! :)

Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2

Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
@vvarma vvarma force-pushed the extmod/rustanalyzer branch from 2fca591 to 33102ff Compare January 21, 2023 04:59
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Jan 21, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2

Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Mar 7, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2

Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Mar 7, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2

Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Apr 11, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2

Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
ojeda pushed a commit that referenced this pull request Aug 2, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: #914
Link: Rust-for-Linux/rust-out-of-tree-module#2
Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
Link: https://lore.kernel.org/r/20230411091714.130525-1-varmavinaym@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
ojeda pushed a commit that referenced this pull request Aug 7, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: #914
Link: Rust-for-Linux/rust-out-of-tree-module#2
Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
Link: https://lore.kernel.org/r/20230411091714.130525-1-varmavinaym@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
vtta pushed a commit to vtta/linux-archive that referenced this pull request Sep 29, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2
Signed-off-by: Vinay Varma <varmavinaym@gmail.com>
Link: https://lore.kernel.org/r/20230411091714.130525-1-varmavinaym@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
plorefice added a commit to plorefice/linux that referenced this pull request Nov 11, 2023
Adds support for out-of-tree rust modules to use the `rust-analyzer`
make target to generate the rust-project.json file.

The change involves adding an optional parameter `external_src` to the
`generate_rust_analyzer.py` which expects the path to the out-of-tree
module's source directory. When this parameter is passed, I have chosen
not to add the non-core modules (samples and drivers) into the result
since these are not expected to be used in third party modules. Related
changes are also made to the Makefile and rust/Makefile allowing the
`rust-analyzer` target to be used for out-of-tree modules as well.

Link: Rust-for-Linux#914
Link: Rust-for-Linux/rust-out-of-tree-module#2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants