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

Allow examples to be library #3556

Merged
merged 8 commits into from
Jan 20, 2017

Conversation

KalitaAlexey
Copy link
Contributor

@KalitaAlexey KalitaAlexey commented Jan 17, 2017

This PR allows to specify crate-type to an example in Cargo.toml.
After this PR an example's crate-type can be:

  • lib
  • rlib
  • dylib
  • proc-macro

Please look at src/cargo/core/manifest.rs:116 because I am not sure whether I done it right.

I haven't added any tests.
I'd like to add them if someone says me how to do that.

Fixes #2358.

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@alexcrichton
Copy link
Member

Looks good to me, thanks! The line with TODO looks ok to me and the failure on Travis seems relevant (just a style issue).

If you take a look at tests/*.rs you can probably just add a new tests to tests/test.rs

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
I compiled cargo using cargo build.
Which command should I run to check styles?

I will add some tests.

@KalitaAlexey
Copy link
Contributor Author

KalitaAlexey commented Jan 18, 2017

I allowed an example to be built as a library.
Should cargo test --example work?

EDIT:
I think it should.
I will make it.

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
What should cargo do if I run test --example <some_example_name>?
I checked it with file:

fn main() {
    panic!("");
}

#[test]
fn f() {
    panic!("");
}

It does nothing.
It writes that it compiles and then nothing.

@matklad
Copy link
Member

matklad commented Jan 18, 2017

Which command should I run to check styles?

./tests/check-style.sh

This just checks for lines longer than 100 characters, nothing fancy.

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
Bot failed because of timeout.

@alexcrichton
Copy link
Member

Thanks @KalitaAlexey! Could you also add assertions that the relevant files exist (e.g. dylibs and such) and also remove the TODO?

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
I will try.

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
Should cargo test also work?

@KalitaAlexey
Copy link
Contributor Author

KalitaAlexey commented Jan 19, 2017

I don't know how to check whether a library exists.
I need help of someone who knows.

@KalitaAlexey
Copy link
Contributor Author

KalitaAlexey commented Jan 19, 2017

I have found out that cargo uses rustc to determine the prefix and the suffix of a library.
I have no idea how I should expose that.

@alexcrichton
Copy link
Member

@KalitaAlexey you can use the existing_file function to test that a file exists or you could just read the directory and assert some file exists there. I wouldn't worry too much about platform-specific extensions and such, the test can be fairly liberal (e.g. check for any dll, not just the one platform dll)

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
Should cargo test --example <example> work if is a library?

Am I right about library prefixes and extensions?

  • lib, rlib:
    • Any platform: prefix: lib, extension: rlib
  • staticlib:
    • Windows: prefix: none, extension: lib
    • Not Windows: prefix: lib, extension: a
  • dylib:
    • Windows: prefix: none, extension: dll
    • Not Windows: prefix: lib, extension: so

@alexcrichton
Copy link
Member

It should do whatever it does today. If it runs unit tests then it should run unit test. If it just builds the artifact then it should just build the artifact.

On OSX the dylib extension is "dylib", but other than that the info you have I believe is correct.

@KalitaAlexey
Copy link
Contributor Author

KalitaAlexey commented Jan 19, 2017

In the PR cargo test --example <example> where <example> points to an example of some kind of library doesn't compile.

Today it doesn't compile because it is treated as a binary.

@alexcrichton
Copy link
Member

What happens if you point it to an example that's a binary? Libraries should match the same behavior.

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
Okay.
It requires changes.
I will update the PR.

@KalitaAlexey
Copy link
Contributor Author

It works.

TargetKind::Lib(ref kinds) => {
kinds.iter().map(|k| k.crate_type()).collect()
TargetKind::Lib(ref kinds) |
// TODO: I am not sure whether it should be encoded like a library or like an example
Copy link
Member

Choose a reason for hiding this comment

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

(it's ok to remove this now)

@alexcrichton
Copy link
Member

Looks like there's a failure on Travis as well?

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
Why did it fail?
On my machine all tests had completed without failures.

@KalitaAlexey
Copy link
Contributor Author

I hope it won't fail again.

@alexcrichton
Copy link
Member

@bors: r+

Looks good, thanks! I think that rollup PR is for rust-lang/rust, not Cargo, so this can't be included.

@bors
Copy link
Collaborator

bors commented Jan 20, 2017

📌 Commit e73e270 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Jan 20, 2017

⌛ Testing commit e73e270 with merge de6bce9...

bors added a commit that referenced this pull request Jan 20, 2017
…excrichton

Allow examples to be library

This PR allows to specify **crate-type** to an example in **Cargo.toml**.
After this PR an example's **crate-type** can be:

* lib
* rlib
* dylib
* proc-macro

Please look at src/cargo/core/manifest.rs:116 because I am not sure whether I done it right.

I haven't added any tests.
I'd like to add them if someone says me how to do that.

Fixes #2358.
@bors
Copy link
Collaborator

bors commented Jan 20, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing de6bce9 to master...

@bors bors merged commit e73e270 into rust-lang:master Jan 20, 2017
@jethrogb
Copy link
Contributor

Contrary to what's in OP, cdylib and staticlib are now also supported with this PR. Yay!

@jethrogb
Copy link
Contributor

Follow up issue in #3572

@KalitaAlexey KalitaAlexey deleted the allow-examples-to-be-library branch January 21, 2017 07:39
@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
I want to change http://doc.crates.io/guide.html
because it contains "Example executable files go in the examples directory.".
It should be "Example executable or library files go in the examples directory".
How can I change that?

@alexcrichton
Copy link
Member

@KalitaAlexey the docs are in src/doc in this repo, as I think you've found!

bors added a commit that referenced this pull request Jan 25, 2017
…es, r=alexcrichton

Added information about examples as libraries to the documentation

[Allow examples to be library](#3556) has been committed.
The PR adds information to the documentation.
@ehuss
Copy link
Contributor

ehuss commented Feb 6, 2017

I have a minor problem with this change. cargo metadata now outputs "kind": ["staticlib"] instead of "kind": ["example"] when you have an example library. I'm working on a tool that uses cargo metadata to determine the command-line arguments to use to build a specific target. Since there is no longer an indication that the target is an example, it is impossible to know that you need to use --example to build. Should I maybe open a new issue?

@alexcrichton
Copy link
Member

@ehuss yeah want to open a new issue?

@KalitaAlexey
Copy link
Contributor Author

Hi @ehuss,
I am sorry about that. When I was working on it I didn't know what it was for.

@alexcrichton
Copy link
Member

@KalitaAlexey since we're seeing some breakage want to send a PR to revert back to the original behavior? We can probably add a new field to the metadata which indicates whether an example is a library or not.

@KalitaAlexey
Copy link
Contributor Author

@alexcrichton,
I will send a PR today (in 3 hours).

@ehuss ehuss added this to the 1.16.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow specifying crate-type on examples
7 participants