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

protobuf3: support cross compilation #48450

Merged
merged 4 commits into from Oct 15, 2018
Merged

protobuf3: support cross compilation #48450

merged 4 commits into from Oct 15, 2018

Conversation

ryan4729
Copy link
Contributor

@ryan4729 ryan4729 commented Oct 15, 2018

Provide a native c compiler, which, and protoc

Motivation for this change

Support cross compilation for protobuf3. For this to work we need to:

  • ensure a native c compiler is on the PATH (for jsembed)
  • ensure a native which is on the PATH (for the configure script to find the native c compiler)
  • provide a native protoc binary as a configure flag (used to generate code at build-time)
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 (gentoo with nix-2.0.4)
  • 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.

Provide a native c compiler, which, and protoc
Copy link
Member

@lheckemann lheckemann left a comment

Choose a reason for hiding this comment

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

In principle, LGTM. Not sure I like this externalProtoc though — it took reading almost the whole diff for me to realise it was a boolean and not a reference to some external protoc. While this could be fixed by just changing the name to e.g. useExternalProtoc (which indicates the booleanness better) I think a better approach might be to just use (stdenv.hostPlatform != stdenv.buildPlatform) in generic-v3.nix (maybe with a let binding if you need to use it in multiple locations) directly. This results in much less cruft in all-packages.nix (in fact allowing it to remain completely as is) and avoids the need to thread the variables through in protobuf/3.*.nix.


callPackage ./generic-v3.nix {
version = "3.1.0";
sha256 = "0qlvpsmqgh9nw0k4zrxlxf75pafi3p0ahz99v6761b903y8qyv4i";
externalProtoc = externalProtoc;
buildProtobuf = buildProtobuf;
Copy link
Member

Choose a reason for hiding this comment

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

You could use inherit externalProtoc buildProtobuf; instead here.

@lheckemann
Copy link
Member

@GrahamcOfBorg build pkgsCross.aarch64-multiplatform.protobuf3_6

@GrahamcOfBorg

This comment has been minimized.

@GrahamcOfBorg

This comment has been minimized.

@GrahamcOfBorg

This comment has been minimized.

@ryan4729
Copy link
Contributor Author

I need to be able to select the specific matching version (protobuf3_5, protobuf3_6, etc) from generic-v3.nix so I can't use a hardcoded attribute path for the native package. I changed it to a function that takes buildProtobuf and stdenv as parameters, and then passes in a native version for the buildProtobuf only for a cross-compile. This de-crufts the rest of the files.

@@ -31,10 +30,10 @@ stdenv.mkDerivation rec {
'';

nativeBuildInputs = [ autoreconfHook buildPackages.which buildPackages.stdenv.cc ]
++ stdenv.lib.optional useExternalProtoc [ buildProtobuf ];
++ (if buildProtobuf == null then [] else [ buildProtobuf ]);
Copy link
Member

Choose a reason for hiding this comment

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

buildInputs entries which are null will just be ignored, so you don't need to put this condition here.

, version, sha256
, ...
}:

stdenv.mkDerivation rec {
let
mkProtobufDerivation = buildProtobuf: stdenv: stdenv.mkDerivation rec {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need a function here? Can't you just do let buildProtobuf = if stdenv.hostPlatform != stdenv.buildPlatform then buildPackages.protobuf else null; in stdenv.mkDerivation rec {…}? I'm not sure about the exact recursion implications of that, but I think it should work.

Copy link
Contributor Author

@ryan4729 ryan4729 Oct 15, 2018

Choose a reason for hiding this comment

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

I can't do this because the host and build protobuf versions must match. buildPackages.protobuf is defined in all-packages.nix as protobuf = protobuf3_6;. So using buildPackages.protobuf, when I try to cross-compile e.g. protobuf3_1 I see that it wants to build protobuf3_6 for the build version:

these derivations will be built:
  /nix/store/cszcilxncv2hhxb5n5j41y1bxbxbwcb6-protobuf-3.6.1.drv
  /nix/store/lp6xfnwlli29r24vb9pmrgmnd84zbd7m-protobuf-3.1.0-aarch64-unknown-linux-gnu.drv

The protobuf docs say they want the versions to match in the cross compile: https://github.com/protocolbuffers/protobuf/blob/master/src/README.md:

Either way, you must make sure that the protoc executable you use has the same version as the protobuf source code you are trying to use it with.

Copy link
Member

Choose a reason for hiding this comment

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

Good call!

@lheckemann
Copy link
Member

@GrahamcOfBorg build pkgsCross.aarch64-multiplatform.protobuf3_6

@GrahamcOfBorg
Copy link

Failure on x86_64-darwin (full log)

Attempted: pkgsCross.aarch64-multiplatform.protobuf3_6

Partial log (click to expand)

builder for '/nix/store/ryphibmpkybq5jiixwsbp7gis1w0qkpp-linux-headers-4.18.3-aarch64-unknown-linux-gnu.drv' failed with exit code 2
cannot build derivation '/nix/store/ga9fig6p18j8k3d5cpc4x0lpk1mgidcs-glibc-2.27-aarch64-unknown-linux-gnu.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/0jzfhiaiajkg68blk38l9xk55snwf690-aarch64-unknown-linux-gnu-binutils-wrapper-2.30.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/2adj4rjbmc124094ybj8w733r64zqwf9-aarch64-unknown-linux-gnu-stage-final-gcc-debug-7.3.0.drv': 2 dependencies couldn't be built
cannot build derivation '/nix/store/qgivv901jhnpaa8xczfpbaq0pwr7mw0p-aarch64-unknown-linux-gnu-stage-final-gcc-debug-wrapper-7.3.0.drv': 3 dependencies couldn't be built
cannot build derivation '/nix/store/bfb5h7di349m96lg1h2vpmnixjf8mdmb-stdenv-darwin.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/wdy5jprnz782pdlj0c400vm514wiybv3-zlib-1.2.11-aarch64-unknown-linux-gnu.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/2mbk25bgpn88v7rip0vhppj7lc7vg0n2-protobuf-3.6.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/41mldzd050s1g2avghn9y1x9liwip1rl-protobuf-3.6.1-aarch64-unknown-linux-gnu.drv': 3 dependencies couldn't be built
error: build of '/nix/store/41mldzd050s1g2avghn9y1x9liwip1rl-protobuf-3.6.1-aarch64-unknown-linux-gnu.drv' failed

@GrahamcOfBorg
Copy link

Success on aarch64-linux (full log)

Attempted: pkgsCross.aarch64-multiplatform.protobuf3_6

Partial log (click to expand)

shrinking RPATHs of ELF executables and libraries in /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1
shrinking /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1/bin/protoc
shrinking /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1/lib/libprotoc.so.17.0.0
shrinking /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1/lib/libprotobuf.so.17.0.0
shrinking /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1/lib/libprotobuf-lite.so.17.0.0
strip is /nix/store/p9akxn2sfy4wkhqdqa3li97pc6jaz3r1-binutils-2.30/bin/strip
stripping (with command strip and flags -S) in /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1/lib  /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1/bin
patching script interpreter paths in /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1
checking for references to /build in /nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1...
/nix/store/8s1rh9c02h3727v0y9y6lnha60pbzwqa-protobuf-3.6.1

@GrahamcOfBorg
Copy link

Success on x86_64-linux (full log)

Attempted: pkgsCross.aarch64-multiplatform.protobuf3_6

Partial log (click to expand)

shrinking RPATHs of ELF executables and libraries in /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu
shrinking /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu/bin/protoc
shrinking /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu/lib/libprotobuf-lite.so.17.0.0
shrinking /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu/lib/libprotobuf.so.17.0.0
shrinking /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu/lib/libprotoc.so.17.0.0
aarch64-unknown-linux-gnu-strip is /nix/store/l7qxjg8gclh4qm2k691abvhsah79pym7-aarch64-unknown-linux-gnu-binutils-2.30/bin/aarch64-unknown-linux-gnu-strip
stripping (with command aarch64-unknown-linux-gnu-strip and flags -S) in /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu/lib  /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu/bin
patching script interpreter paths in /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu
checking for references to /build in /nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu...
/nix/store/faxai1ak2vi8fli5561gsljiy3nn0mgw-protobuf-3.6.1-aarch64-unknown-linux-gnu

@lheckemann
Copy link
Member

Whoops, merged this too hastily. As lots of things depend on protobuf, this is a mass-rebuild and should be based on the staging branch.

lheckemann pushed a commit that referenced this pull request Oct 15, 2018
@lheckemann
Copy link
Member

Pushed to staging as 7414ec1.

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

4 participants