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

Reduce the need for users to write build scripts #14948

Open
6 tasks
epage opened this issue Dec 17, 2024 · 13 comments
Open
6 tasks

Reduce the need for users to write build scripts #14948

epage opened this issue Dec 17, 2024 · 13 comments
Labels
A-build-scripts Area: build.rs scripts C-tracking-issue Category: A tracking issue for something unstable.

Comments

@epage
Copy link
Contributor

epage commented Dec 17, 2024

Like RUSTFLAGS, build scripts are an important escape hatch. Like RUSTFLAGS (#12739), we should find replacements for common uses of build scripts so people don't have to reach to this escape hatch so often

Reducing build scripts would

  • Improve build times
  • Reduce risk of bugs
  • Reduce the dependency review audit scope

Uses of build scripts

@epage epage added A-build-scripts Area: build.rs scripts C-tracking-issue Category: A tracking issue for something unstable. labels Dec 17, 2024
@ChrisDenton
Copy link
Member

ChrisDenton commented Dec 26, 2024

I would draw a distinction between main binary build scripts (e.g. the executable or cdylib) and libraries. I think a build scripts for an executable is far more tolerable than for libraries. They're in direct control of the crate author and don't have any other users. Also a single top-level build script is going to have negligible impact on build times for all but the smallest projects.

Which is not to say reducing the need for them is not important too, just that reducing the need for library build scripts will likely have the far bigger impact.

@epage
Copy link
Contributor Author

epage commented Dec 26, 2024

This is not about trying to remove all build scripts but finding patterns in the community and providing a solution to avoid writing one yourself. In some cases, this will be using an artifact dependency as your build script. This is a general solution that helps libs and bins. And the benefits extend to bins. For smaller projects it democratizes the benefits of build scripts by making it easier to discover what you can do, easier to implement, and with fewer bugs. For larger projects with multiple bins, it makes reuse easier, reduces build times, and makes it easier to audit your own code base.

Yes, libs will get more benefits as we prioritize solutions to implement but understanding and tracking the needs of bins takes little away from that but instead gives a better understanding for designing soluations.

@kamulos
Copy link

kamulos commented Jan 3, 2025

I don't know if this is important enough, but I also have a small use-case: I integrate a version string from an environment variable into my program during compile time. A natural way to do this would be the env!() macro, but this will not rebuild if the env variable changes.

So I am forced to use a build script with the cargo::rerun-if-env-changed declaration.

@bjorn3
Copy link
Member

bjorn3 commented Jan 3, 2025

env!() should rebuild when the env var changes. We changed rustc to record which env vars are read using env!() into the dep info file years ago and at the same time changed cargo to rebuild crates when env vars mentioned in the dep info file changes.

@kamulos
Copy link

kamulos commented Jan 3, 2025

Oh that works! 😊 I don't know what went wrong the last time I tested it, sorry for the confusion.

@epage
Copy link
Contributor Author

epage commented Jan 3, 2025

Cargo might not fully track its own env variables which would be a bug.

iirc try_env! does not report env variables for tracking, at least if they aren't there.

@weihanglo
Copy link
Member

Cargo might not fully track its own env variables which would be a bug.

iirc try_env! does not report env variables for tracking, at least if they aren't there.

Did you mean option_env!? They're also tracked by # env-dep: comment in rustc dep-info files.

@nicoburns
Copy link
Contributor

nicoburns commented Jan 9, 2025

Can we add "cfg aliases" to this list (i.e. what is implemented using a build script by the https://docs.rs/cfg_aliases/latest/cfg_aliases/ crate)?

Winit is a prominent crate that depends on this cfg_aliases, and from a Dioxus/Blitz perspective I understand why they've done this. We support 6 platforms (windows/macos/linux/ios/android/web) and try to provide fine(ish)-grained feature flagging for each supported feature to allow users to optimise build times and binary sizes. This leads to cfg expressions that spread to 6 lines or more.

Use cases where this is commonly comes up are where there is functionality that works on N of M platforms (e.g. 3 of 6), that you also want to be controllable with a feature flag (users may or may not want the function), and that you want to be disabled on platforms where it doesn't work even when the feature is enabled (as otherwise it becomes burdensome for users to set platform-specific features (where this is even possible)).

To give an entirely concrete example. In blitz-shell we use muda for implementing system menus on windows/macos/linux(desktop), but this functionality isn't available and doesn't make sense on android/ios/web. We also wish to make it optional on platforms that do support it. So we need a feature flag AND platform-specific cfgs, which ends up being a lot of boilerplate.

It would be nice if we could define aliases (either in lib.rs or Cargo.toml) and then use a simple #[cfg(menu)].

@epage
Copy link
Contributor Author

epage commented Jan 10, 2025

@nicoburns I've added cfg_aliases. An intermediate solution would be to stabilize metadeps and then you could declare cfg_aliases as one of your build scripts, passing parameters to it. This would mean only one build script bin related to cfg_alises would be built across your entire dependency tree or need to be audited.

@epage
Copy link
Contributor Author

epage commented Jan 10, 2025

I feel like the use cases given might have some overlap with global, mutually exclusive features and I wonder if there is a way to fit a higher level construct into that.

@heaths
Copy link

heaths commented Jan 21, 2025

Another use case we have is to record the rustc version used to compile with our library that we telemeter, if enabled. Currently I don't see anything obvious recorded in ELF or PE/COFF executables that record the rustc version used - perhaps it's in the debug info, but that may not have been deployed - but perhaps making some basic info available in header or section data - perhaps the debug section - could be exposed at runtime. I admit I don't know much about ELF - Windows had been my primary dev env for decades - but looking at a release-build from rustc, it does look like GCC is recording quite a bit of build/version information so maybe it's warranted to add some rustc info? MSVC (and apparently clang on Windows now) also stores some linker info in the PE/COFF as well.

I mean, there's other ways as well like linking in function to get info, but stamping this in the executable might be useful to external tools as well. Just some thoughts.

@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2025

Currently I don't see anything obvious recorded in ELF or PE/COFF executables that record the rustc version used

The .comment section for ELF executables contains the version of rustc as well as many other tools that were involved in producing the executable. For example:

$ readelf --string-dump=.comment ~/.cargo/bin/rustup

String dump of section '.comment':
  [     0]  GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)
  [    2d]  GCC: (GNU) 9.5.0
  [    3e]  rustc version 1.77.2 (25ef9e3d8 2024-04-09)
  [    6a]  clang version 18.1.0rc

As for PE/COFF executables, it looks like the PDB debuginfo file will contain the rustc string. At least https://rust.godbolt.org/z/3zxrKehsK shows the clang LLVM (rustc version 1.84.0 (9fc6b4312 2025-01-07)) string ending up in the .debug$S section of the object file.

@heaths
Copy link

heaths commented Jan 21, 2025

Interesting. I ran readelf -a <path> and it didn't dump the .comment section, but doing what you did explicitly worked. Maybe std could expose that if there's enough demand.

As for PE/COFF, I expected this was in the .pdb. somewhere but it's rare symbols are deployed. What might be ideal is to actually store it in a debug directory like MSVC does, something to the effect of:

$ dumpbin /headers /section:.rdata <exe>

# ...
  Debug Directories

        Time Type        Size      RVA  Pointer
    -------- ------- -------- -------- --------
    678FF0B6 cv            4E 00003960     2960    Format: RSDS, {0466A304-9921-46F8-8636-239DB83AA58C}, 1, <path>.pdb
    678FF0B6 feat          14 000039B0     29B0    Counts: Pre-VC++ 11.00=0, C/C++=35, /GS=35, /sdl=1, guardN=34
    678FF0B6 coffgrp      30C 000039C4     29C4    4C544347 (LTCG)
    678FF0B6 iltcg          0 00000000        0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts C-tracking-issue Category: A tracking issue for something unstable.
Projects
Status: No status
Development

No branches or pull requests

7 participants