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

buildRustCrate: add heuristic to picking the right binary source files #46210

Merged
merged 5 commits into from Sep 15, 2018

Conversation

andir
Copy link
Member

@andir andir commented Sep 6, 2018

Motivation for this change

Cargo has a few odd (old) ways of picking source files if the bin.path
attribute isn't given in the Cargo.toml. This commit adds support for
some of those. The previous behaviour always defaulted to src/main.rs
which was not always the right choice.

Since there is look-ahead into the unpacked sources before running the
actual builder the path selection logic has to be embedded within the
build script.

buildRustCrate currently supports two ways of running building
binaries when processing a crate:

  • Explicit definition of all the binaries (& optionally the paths to
    their respective main.rs) and,
  • if not binary was explictly configured all files matching the patterns
    src/main.rs, src/bin/*.rs.

When the explicit list is given without path information paths are now
being picked from a list of candidates. The first match wins. The order
is the same as within the cargo compatibility code.

If the crate does not provide any libraries the path src/{bin_name}.rs
is also considered.

All underscores within the binary names are translated into dashes (-)
before the lookups are made. This seems to be a common convention.

cc some of the people that touched the code before me: @shlevy @Mic92

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Fits CONTRIBUTING.md.

@andir
Copy link
Member Author

andir commented Sep 7, 2018

@GrahamcOfBorg build way-cooler cargo-update cargo-download cargo-vendor cargo-edit carnix

@GrahamcOfBorg
Copy link

Failure on aarch64-linux (full log)

Attempted: way-cooler, cargo-update, cargo-download, cargo-vendor, cargo-edit, carnix

Partial log (click to expand)


installing
post-installation fixup
shrinking RPATHs of ELF executables and libraries in /nix/store/9xz6yxp42jx0vrxnnv4dazrsbfw4a7p3-rust_cargo-vendor-0.1.13
shrinking /nix/store/9xz6yxp42jx0vrxnnv4dazrsbfw4a7p3-rust_cargo-vendor-0.1.13/bin/cargo-vendor
strip is /nix/store/y4ymnvgxygpq05h03kyzbj572zmh6zla-binutils-2.30/bin/strip
stripping (with command strip and flags -S) in /nix/store/9xz6yxp42jx0vrxnnv4dazrsbfw4a7p3-rust_cargo-vendor-0.1.13/lib  /nix/store/9xz6yxp42jx0vrxnnv4dazrsbfw4a7p3-rust_cargo-vendor-0.1.13/bin
patching script interpreter paths in /nix/store/9xz6yxp42jx0vrxnnv4dazrsbfw4a7p3-rust_cargo-vendor-0.1.13
checking for references to /build in /nix/store/9xz6yxp42jx0vrxnnv4dazrsbfw4a7p3-rust_cargo-vendor-0.1.13...
error: build of '/nix/store/8fnc7yd74x5pjll7zshsarz0hb0g0rs1-way-cooler-with-extensions-0.8.0.drv' failed

@GrahamcOfBorg
Copy link

Failure on x86_64-darwin (full log)

Attempted: cargo-update, cargo-download, cargo-vendor, cargo-edit, carnix

The following builds were skipped because they don't evaluate on x86_64-darwin: way-cooler

Partial log (click to expand)

checking file src/bin/main.rs
checking file src/main.rs
Building cargo_download (src/main.rs)
Running rustc --crate-name cargo_download src/main.rs --crate-type bin -C opt-level=3 -C codegen-units=1 --out-dir target/bin --emit=dep-info,link -L dependency=target/deps -L native=/nix/store/h2hwz7qdyja3s3fq4rg4qhg3fw7xncs8-rust_miniz-sys-0.1.10/lib/miniz-sys.out -l framework=CoreFoundation -l framework=Security -l static=miniz --extern ansi_term=/nix/store/gn5zn64gb39k3yxja3aykg1k0jd1g6vw-rust_ansi_term-0.9.0/lib/libansi_term-3b418bf8e2.rlib --extern clap=/nix/store/8wj79l8flgahw2gi3rzvfcy0hm66cakq-rust_clap-2.32.0/lib/libclap-16cc62ea49.rlib --extern conv=/nix/store/pil0jf0xbg0m6kzgzlqygq9sd13lnyqz-rust_conv-0.3.3/lib/libconv-68a4c7cbd4.rlib --extern derive_error=/nix/store/pk4bz8p7nskl4vq4g0bn5wlz7ba634fc-rust_derive-error-0.0.3/lib/libderive_error-e590d4d043.dylib --extern exitcode=/nix/store/c5kmrv8142jnzj9dv2bgxws1bbw659bz-rust_exitcode-1.1.2/lib/libexitcode-97efdf7751.rlib --extern flate2=/nix/store/fqd0ab8cd0p77968c7pnax3jbjy9r5xx-rust_flate2-0.2.20/lib/libflate2-d93e97d6f5.rlib --extern isatty=/nix/store/67hl84nzikyp89hjanj2ygx72vcxpm7w-rust_isatty-0.1.8/lib/libisatty-246a278aea.rlib --extern itertools=/nix/store/jhvlm445zmh10br08fzg40wb2236w4s5-rust_itertools-0.6.5/lib/libitertools-dc1fc1b2c2.rlib --extern lazy_static=/nix/store/gx6vqvkngvnm026m0p82rndkzwbm6w77-rust_lazy_static-0.2.11/lib/liblazy_static-b161637761.rlib --extern log=/nix/store/rcaac8cyf5cylrlg5k2k3bbxzrcrlifb-rust_log-0.3.9/lib/liblog-4578398e6a.rlib --extern maplit=/nix/store/jv6zrzzndwfws95yl0c33d5lap59771b-rust_maplit-0.1.6/lib/libmaplit-c2c4a9db69.rlib --extern reqwest=/nix/store/jqnw1yig13qspjm7b0x1yfi5v6fvc4jw-rust_reqwest-0.8.6/lib/libreqwest-c0ed5fc5b0.rlib --extern semver=/nix/store/855dbx8vbg43js4qfzpab92shlwyvq9y-rust_semver-0.9.0/lib/libsemver-796001ebc3.rlib --extern serde_json=/nix/store/r62sma39q2kplyddxf7xh6nv4gaqas3i-rust_serde_json-1.0.24/lib/libserde_json-e04edd2352.rlib --extern slog=/nix/store/p09an9sf2hilki3nlrm60vhym6236rb1-rust_slog-1.7.1/lib/libslog-ae2c60979f.rlib --extern slog_envlogger=/nix/store/429gn6nw6vxyn8kklj8ycdfvspc0nhdc-rust_slog-envlogger-0.5.0/lib/libslog_envlogger-0764403d63.rlib --extern slog_stdlog=/nix/store/f5kb50ghr35paj525nk1n97lfz8zccmr-rust_slog-stdlog-1.1.0/lib/libslog_stdlog-38c94023a3.rlib --extern slog_stream=/nix/store/fhxa9f2rb932ygfp7hf0xdqs6y5paxsq-rust_slog-stream-1.2.1/lib/libslog_stream-1306cf4508.rlib --extern tar=/nix/store/mkc2dg03villvalbymxjf9jikvf085vx-rust_tar-0.4.16/lib/libtar-98b766acc5.rlib --extern time=/nix/store/mj819yn47567g0l5jiaia3gg861dck9d-rust_time-0.1.40/lib/libtime-fed4edec4f.rlib --cap-lints allow -L native=/nix/store/h2hwz7qdyja3s3fq4rg4qhg3fw7xncs8-rust_miniz-sys-0.1.10/lib/miniz-sys.out -l framework=CoreFoundation -l framework=Security -l static=miniz --color always
installing
post-installation fixup
strip is /nix/store/df6k4mgdjxciy0f637lryp7c9ln7n1m3-cctools-binutils-darwin/bin/strip
stripping (with command strip and flags -S) in /nix/store/w0xsw6dmhwgwqsk0c3cf1rj85pnjj6sb-rust_cargo-download-0.1.1/lib  /nix/store/w0xsw6dmhwgwqsk0c3cf1rj85pnjj6sb-rust_cargo-download-0.1.1/bin
patching script interpreter paths in /nix/store/w0xsw6dmhwgwqsk0c3cf1rj85pnjj6sb-rust_cargo-download-0.1.1
error: build of '/nix/store/yi284bnj6kb49v9bfdhp9y0kh02hlccx-cargo-update-1.5.2.drv' failed

@GrahamcOfBorg
Copy link

Success on x86_64-linux (full log)

Attempted: way-cooler, cargo-update, cargo-download, cargo-vendor, cargo-edit, carnix

Partial log (click to expand)

/nix/store/4z34dd0pig96n0zchff8y1vhmxfnqk8d-rust_wc-grab-0.3.0/lib:
link: /nix/store/7aqixrjffwyj1231n2wwczmq1xy8zmbp-rust_way-cooler-0.8.0/lib/link
/nix/store/r6h3rxng3y2pyqc5qbxzqpm8ay3k5fdx-rust_wc-lock-0.2.1/lib:
link: /nix/store/7aqixrjffwyj1231n2wwczmq1xy8zmbp-rust_way-cooler-0.8.0/lib/link
/nix/store/9vdgyw0k20kpdw2digdylsiv0xs6cvba-way-cooler-with-extensions-0.8.0
/nix/store/8qm0xj2nji1xkqvfj43ijr3pgksnyhgx-cargo-update-1.5.2
/nix/store/qscp928jczwnxy1kczj1a7vd98sbnnpx-rust_cargo-download-0.1.1
/nix/store/cl5zama1y02xz3glh38rmqfad4nnr0v9-rust_cargo-vendor-0.1.13
/nix/store/2kc7svz62qrq41sybkvfv1hzvnfmrz4l-cargo-edit-0.2.0
/nix/store/z8msyy12g5pbf6g97xbldk7vzy6wpzc1-rust_carnix-0.7.2


# the first two cases are the "new" default IIRC
BIN_NAME_=$(echo $BIN_NAME | sed -e 's/_/-/g')
FILES="src/bin/$BIN_NAME_.rs src/bin/$BIN_NAME_/main.rs src/bin/main.rs src/main.rs"
Copy link
Member

Choose a reason for hiding this comment

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

Should this be an array instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably should be an array instead. I'll dig how to do that with bash.

@andir
Copy link
Member Author

andir commented Sep 9, 2018

I started restructuring the buildRustCrate after some feedback from @Mic92. There are also a bunch of rudimentary tests now for all the edge cases of cargo that I encountered so far.

@andir
Copy link
Member Author

andir commented Sep 9, 2018

@GrahamcOfBorg build buildRustCrateTests

@GrahamcOfBorg
Copy link

Success on x86_64-darwin (full log)

Attempted: buildRustCrateTests

Partial log (click to expand)

building '/nix/store/f9l11q5wnh6qc2ll4cy98fl4k2wzzyw8-run-buildRustCrate-libPath-test.drv'...
/nix/store/ha77fgmzaj73iwrqy71rsdn17fsv07h1-run-buildRustCrate-crateBinNoPath1-test
/nix/store/qxc8rnvblrzkcd4vsxxk2isanh7y82cn-run-buildRustCrate-crateBinNoPath2-test
/nix/store/l1ad4nd4mi3pgdskaz4d00vxhz0myrni-run-buildRustCrate-crateBinNoPath3-test
/nix/store/hwyfcf7pw7sliyfgz7yblhg4ma2pgaxv-run-buildRustCrate-crateBinNoPath4-test
/nix/store/n1j97y6jrhpx21h98hlms1gmkkz5mns8-run-buildRustCrate-crateBinWithPath-test
/nix/store/gi5lnamxg9qbqk7vsvqvlkipm2hdk2if-run-buildRustCrate-customLibName-test
/nix/store/yaqx2zlw0khzrj7lhphz0pld8gahids1-run-buildRustCrate-customLibNameAndLibPath-test
/nix/store/3cn34p41kpxf9h1fxl3hh8bkg19k0msk-run-buildRustCrate-libPath-test
/nix/store/w11w1b6r80kcw99nsh7c3qkpns6vakgz-run-buildRustCrate-srcLib-test

@GrahamcOfBorg
Copy link

Success on aarch64-linux (full log)

Attempted: buildRustCrateTests

Partial log (click to expand)

executed my-binary4
/nix/store/ny90nfks58fgkh5y43h0jz53hnnrrcgn-run-buildRustCrate-crateBinNoPath1-test
/nix/store/m214lrghhmq1avp9y71cxa9iycx3zzcm-run-buildRustCrate-crateBinNoPath2-test
/nix/store/hd5nykwk68xfp1rc51k8fmvjpcam593c-run-buildRustCrate-crateBinNoPath3-test
/nix/store/mlijrmng87ip55v2w7wq3zcclj52yvm6-run-buildRustCrate-crateBinNoPath4-test
/nix/store/lj36r0nn45bs2ny3hcjz6rb9vq0nlj58-run-buildRustCrate-crateBinWithPath-test
/nix/store/n61zpjxmfgfcm0ij790b2ig28faxvv6v-run-buildRustCrate-customLibName-test
/nix/store/i5r60g0n2a7vrp2v29py9k5hbp8skv10-run-buildRustCrate-customLibNameAndLibPath-test
/nix/store/yzvsqcskkljini5bzfyxgg25dxslddjz-run-buildRustCrate-libPath-test
/nix/store/qvqbjm7hw7hkxsl2jjn02lqkck5yh4kq-run-buildRustCrate-srcLib-test

@GrahamcOfBorg
Copy link

Success on x86_64-linux (full log)

Attempted: buildRustCrateTests

Partial log (click to expand)

executed my-binary4
/nix/store/dv6xnclmaa8xjynxb486qhrpf9yzw6qr-run-buildRustCrate-crateBinNoPath1-test
/nix/store/siypv08i2ylmak21zp0s1vavssqpgkxf-run-buildRustCrate-crateBinNoPath2-test
/nix/store/sdzklp20pv9s75bl3w0k5r4vzifzicx0-run-buildRustCrate-crateBinNoPath3-test
/nix/store/2ccd4srzkrjmnfxbmdq573m38sps5b3x-run-buildRustCrate-crateBinNoPath4-test
/nix/store/lk3sfc8bzp17lrapvpgl0aczx3n5cc8y-run-buildRustCrate-crateBinWithPath-test
/nix/store/j8shwvfsbiiyrp76lsv6q28cv0by0h89-run-buildRustCrate-customLibName-test
/nix/store/01d3wb4w8j49qlkvj5zcj4rrliwcv52h-run-buildRustCrate-customLibNameAndLibPath-test
/nix/store/ilx79fdlz1z8xirxjih5m9iy6x88gx92-run-buildRustCrate-libPath-test
/nix/store/d9as748r11k9x7gc2vrxrd383yhpy86p-run-buildRustCrate-srcLib-test

@andir andir changed the title buildRustCrate: add heuristic to picking the right binary source files WIP: buildRustCrate: add heuristic to picking the right binary source files Sep 10, 2018
@andir
Copy link
Member Author

andir commented Sep 10, 2018

I started playing with this a bit more and it seems that there is still a lot of headroom to improve in regards to what carnix produces and what we are able to properly build.. The brotli crate is a great example...

Copy link
Member

@Mic92 Mic92 left a comment

Choose a reason for hiding this comment

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

Definitely more readable and build with all our current expression.

Cargo has a few odd (old) ways of picking source files if the `bin.path`
attribute isn't given in the Cargo.toml. This commit adds support for
some of those. The previous behaviour always defaulted to `src/main.rs`
which was not always the right choice.

Since there is  look-ahead into the unpacked sources before running the
actual builder the path selection logic has to be embedded within the
build script.

`buildRustCrate` currently supports two ways of running building
binaries when processing a crate:

- Explicit definition of all the binaries (& optionally the paths to
their respective `main.rs`) and,
- if not binary was explictly configured all files matching the patterns
  `src/main.rs`, `src/bin/*.rs`.

When the explicit list is given without path information paths are now
being picked from a list of candidates. The first match wins. The order
is the same as within the cargo compatibility code.

If the crate does not provide any libraries the path `src/{bin_name}.rs`
is also considered.

All underscores within the binary names are translated into dashes (`-`)
before the lookups are made. This seems to be a common convention.
The build expression got quiet large over time and to make it a bit
easier to grasp the different scripts involved in the build are now
separated from the nix file.
This commit adds test based on real-world crates (brotli).
There were a few more edge cases that were missing beforehand. Also it
turned out that we can get rid of the `finalBins` list since that will
now be handled during runtime.
@andir andir changed the title WIP: buildRustCrate: add heuristic to picking the right binary source files buildRustCrate: add heuristic to picking the right binary source files Sep 13, 2018
@Mic92 Mic92 merged commit f83a262 into NixOS:master Sep 15, 2018
@andir andir deleted the build-rust-crate-changes branch September 21, 2018 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants