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

Adding support to generate rust-project.json #2

Closed

Conversation

thepacketgeek
Copy link
Contributor

@thepacketgeek thepacketgeek commented Dec 10, 2022

Using the linux/scripts/generate_rust_analyzer.py script as inspiration to add a make rust-analyzer target for an out-of-tree module.

Process:

  • Make the $KDIR rust-project by issuing make rust-analyzer
  • Extend the crates config with the out-of-tree module
  • Write to rust-project.json in the module directory

Here's an example of the added module crate config:

$ more rust-project.json| tail -n 20
        {
            "cfg": [],
            "deps": [
                {
                    "crate": 6,
                    "name": "kernel"
                }
            ],
            "display_name": "rust_out_of_tree",
            "edition": "2021",
            "env": {
                "RUST_MODFILE": "This is only for rust-analyzer"
            },
            "is_proc_macro": false,
            "is_workspace_member": false,
            "root_module": "rust_out_of_tree.rs"
        }
    ],
    "sysroot_src": "/home/mat/.rustup/toolchains/1.62.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library"
}

And the updated kernel crate config (pointing at my KDIR of ../rust_for_linux):

more rust-project.json| jq 'del(.crates[].cfg) | .crates[]| select(.display_name=="kernel") '
{
  "deps": [
    {
      "crate": 0,
      "name": "core"
    },
    {
      "crate": 2,
      "name": "alloc"
    },
    {
      "crate": 3,
      "name": "macros"
    },
    {
      "crate": 4,
      "name": "build_error"
    },
    {
      "crate": 5,
      "name": "bindings"
    }
  ],
  "display_name": "kernel",
  "edition": "2021",
  "env": {
    "RUST_MODFILE": "This is only for rust-analyzer"
  },
  "is_proc_macro": false,
  "is_workspace_member": true,
  "root_module": "../rust_for_linux/rust/kernel/lib.rs",
  "source": {
    "exclude_dirs": [],
    "include_dirs": [
      "rust/kernel",
      "rust"
    ]
  }
}

===

One janky thing I'm not currently a fan of is getting the root_module and rust source file name. This script currently only supports one file/module. I'd prefer to get it from the Kbuild file by reading obj-m but I'm not sure how to do that, any suggestions here are welcome.

@jordanisaacs
Copy link

I am using the script for out of tree modules and it works fantastic. However, I am encountering a proc-macro error for the module! macro:

proc macro `module` not expanded: Proc-macro dylib loading failed: No such file or directory (OS Error 2)

Curious if you have seen it before?

@thepacketgeek
Copy link
Contributor Author

@jordanisaacs I haven't seen that before. Do you see that when running make rust-analyzer? Or is that a rust-analyzer error message?

@jordanisaacs
Copy link

It is a rust-analyzer diagnostic error. Everything else works great (docs, go to, completion, etc). Only difference from how this PR does it is rather than calling make -C (KDIR) every time, I built the kernel rust-project.json once and just reference it.

@thepacketgeek
Copy link
Contributor Author

Ah, you found a bug (well two), thank you @jordanisaacs. Updated the script to not typo "include_dirs", and also actually updating the list with the new fully-qualified paths.

@jordanisaacs
Copy link

Can confirm that fixed it @thepacketgeek thanks, this script is fantastic!

@ojeda
Copy link
Member

ojeda commented Jan 19, 2023

Related: Rust-for-Linux/linux#914

@thepacketgeek
Copy link
Contributor Author

Related: Rust-for-Linux/linux#914

Even better, nicer that module writers don't need to duplicate this effort for every module. I'll close this PR, Thank you!

@ojeda
Copy link
Member

ojeda commented Jan 19, 2023

Thanks to you, Mat, for sending the PR! :)

If you have time, please take a look at the other PR, and perhaps give a Tested-by: ... <...> tag there (or in the mailing list) if that one worked for you (not sure if you have done kernel work upstream; if not: the tags get added to the commit message so that people get credit!).

@ojeda
Copy link
Member

ojeda commented Jan 19, 2023

(And of course you can review it too: Reviewed-by: ... <...>)

vvarma added a commit to vvarma/rusty-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 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 to Rust-for-Linux/linux 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 to Rust-for-Linux/linux 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.

3 participants