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

Infer executable path #15

Closed
japaric opened this issue Aug 2, 2018 · 3 comments · Fixed by #20
Closed

Infer executable path #15

japaric opened this issue Aug 2, 2018 · 3 comments · Fixed by #20
Assignees
Labels
Blocks Rust 2018 help wanted Extra attention is needed
Milestone

Comments

@japaric
Copy link
Member

japaric commented Aug 2, 2018

cargo objdump -- -d -no-show-raw-insn target/thumbv7m-none-eabi/debug/app is too long to write and redundant if you working on a binary crate. Let's have cargo-binutils infer the path of the executable. At the end we should be able to just write cargo objdump -- -d -no-show-raw-insn.

Here's what the logic should do (this may contain some errors / holes):

  • If the user passed a path after -- use that and don't try to infer the path
  • Else
    • Figure out where's the build directory; this will be the first part of our path
      • If CARGO_TARGET_DIR is set use that
      • If we are in a workspace use the $workspace_root/target
      • Else use $crate_root/target (XXX what about --manifest-path?)
    • Append the compilation target (e.g. thumbv7m-none-eabi) to the path.
    • If the user passed --release before -- use release as the next path segment; otherwise use debug as the next path segment
    • Figure out which executable we are dealing with
      • If the user passed --example $E before -- append examples/$E to the path.
      • else if the user passed --bin $B before -- append $B to the path
      • else if we are dealing with a binary crate (src/main.rs exists) append the crate name to the path
      • else (we are dealing with a library crate; src/main.rs is missing) error out (XXX we could check this earlier)
  • Append our inferred path to tools_args
@japaric japaric added help wanted Extra attention is needed Blocks Rust 2018 labels Aug 2, 2018
@japaric japaric added this to the RC1 milestone Aug 2, 2018
@japaric
Copy link
Member Author

japaric commented Aug 2, 2018

Inferring the path should be done for these tools: nm, objdump, size and strip. And maybe objcopy; not sure about the last one as I haven't used it before via cargo-binutils.

@japaric japaric self-assigned this Aug 7, 2018
@japaric
Copy link
Member Author

japaric commented Aug 25, 2018

Working on this because having to type the full path annoys me to no end.

japaric added a commit that referenced this issue Aug 26, 2018
this commits extends the UI with the following flag:

- `--bin`
- `--example`
- `--lib`
- `--release`

if one of `--bin`, `--example` or `--lib` is used then cargo-$tool will first
build the Cargo project and then proceed to run the llvm tool on the output
artifact -- that is the output artifact will be the first argument passed to the
llvm tool.

`--release` controls whether the project is compiled with optimizations or not.

if none of these three flags is passed then the old behavior is used. No path to
an artifact is passed to the llvm tool and the user is expected to provide it.

this new logic supports workspaces and the CARGO_TARGET_DIR env variable.

closes #15
@japaric
Copy link
Member Author

japaric commented Aug 26, 2018

PR #20 implements this

bors bot added a commit that referenced this issue Aug 28, 2018
20: build and inspect r=therealprof a=japaric

build and inspect

this commits extends the UI with the following flag:

- `--bin`
- `--example`
- `--lib`
- `--release`

if one of `--bin`, `--example` or `--lib` is used then cargo-$tool will first
build the Cargo project and then proceed to run the llvm tool on the output
artifact -- that is the output artifact will be the first argument passed to the
llvm tool.

`--release` controls whether the project is compiled with optimizations or not.

if none of these three flags is passed then the old behavior is used. No path to
an artifact is passed to the llvm tool and the user is expected to provide it.

this new logic supports workspaces and the CARGO_TARGET_DIR env variable.

closes #15

r? @rust-embedded/tools (anyone)
This builds on top on #19 so that should be merged first

---

### Sample outputs:

``` rust
$ cargo new --lib foo && cd $_

$ cat >src/lib.rs <<'EOF'
#![no_std]
#[no_mangle]
pub fn foo(x: u32, y: u32) -> u32 {
    x + y
}
EOF

$ cargo objdump --lib --release -- -disassemble 2>/dev/null

/home/japaric/tmp/foo/target/release/libfoo.rlib(foo-136cd7af94a84b98.foo.awvzjwiu-cgu.0.rcgu.o):       file format ELF64-x86-64

Disassembly of section .text.foo:
foo:
       0:       8d 04 37        leal    (%rdi,%rsi), %eax
       3:       c3      retq

$ cargo objdump --target aarch64-unknown-linux-gnu --lib --release -- -disassemble 2>/dev/null

/home/japaric/tmp/foo/target/aarch64-unknown-linux-gnu/release/libfoo.rlib(foo-05ebc283581f4ba3.foo.9kf7hsz7-cgu.0.rcgu.o):     file format ELF64-aarch64-little

Disassembly of section .text.foo:
foo:
       0:       20 00 00 0b     add     w0, w1, w0
       4:       c0 03 5f d6     ret

$ cargo objdump --target riscv32imac-unknown-none-elf --lib --release -- -disassemble 2>/dev/null

/home/japaric/tmp/foo/target/riscv32imac-unknown-none-elf/release/libfoo.rlib(foo-05ebc283581f4ba3.foo.9kf7hsz7-cgu.0.rcgu.o):  file format ELF32-riscv

Disassembly of section .text.foo:
foo:
       0:       33 85 a5 00     add     a0, a1, a0
       4:       67 80 00 00     ret

$ cargo objdump --target thumbv7m-none-eabi --lib --release -- -disassemble 2>/dev/null

/home/japaric/tmp/foo/target/thumbv7m-none-eabi/release/libfoo.rlib(foo-05ebc283581f4ba3.foo.9kf7hsz7-cgu.0.rcgu.o):    file format ELF32-arm-little

Disassembly of section .text.foo:
foo:
       0:       08 44   add     r0, r1
       2:       70 47   bx      lr
```

Help text:

``` console
$ cargo nm -h
cargo-nm 0.1.2
Proxy for the `llvm-nm` tool shipped with the Rust toolchain.

USAGE:
    cargo-nm [FLAGS] [OPTIONS] [--] [args]...

FLAGS:
    -h, --help       Prints help information
        --lib        Build only this package's library
        --release    Build artifacts in release mode, with optimizations
    -V, --version    Prints version information
    -v, --verbose    Use verbose output

OPTIONS:
        --bin <NAME>         Build only the specified binary
        --example <NAME>     Build only the specified example
        --target <TRIPLE>    Target triple for which the code is compiled

ARGS:
    <args>...

The specified <args>... will all be passed to the final tool invocation.
```


Co-authored-by: Jorge Aparicio <[email protected]>
@bors bors bot closed this as completed in #20 Aug 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocks Rust 2018 help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant