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

[WIP] protobuf: build with cmake instead of autoconf #84824

Closed

Conversation

eamsden
Copy link
Contributor

@eamsden eamsden commented Apr 9, 2020

Motivation for this change

Protobuf has both cmake and autoconf/Make based build systems. The CMake build system will install both pkgconfig and cmake packages for other builds using protobuf to import, while the autoconf build system only installs the pkgconfig files.

In particular, this is a prerequisite for packaging the onnx library

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • 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 nixpkgs-review --run "nixpkgs-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)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@FRidh
Copy link
Member

FRidh commented Apr 10, 2020

cc @veprbl who has a protobuf PR open as well

@nh2
Copy link
Contributor

nh2 commented Apr 10, 2020

cc @veprbl who has a protobuf PR open as well

That other PR is #84867.

@veprbl
Copy link
Member

veprbl commented Apr 10, 2020

Thanks for the ping. I don't mind either solution.

@veprbl veprbl linked an issue Apr 10, 2020 that may be closed by this pull request
@veprbl
Copy link
Member

veprbl commented Apr 15, 2020

@eamsden please rebase on top of master.

@eamsden eamsden force-pushed the eamsden/protobuf-build-with-cmake branch from 7bb0771 to 0351abd Compare April 15, 2020 18:04
@eamsden
Copy link
Contributor Author

eamsden commented Apr 15, 2020

@veprbl done


-install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}/
+install(DIRECTORY ${CMAKE_INSTALL_CMAKEDIR}/
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
Copy link
Member

Choose a reason for hiding this comment

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

I suspect you are trying to deal with an absolute path we set in our default setup-hook:

cmakeFlags="-DCMAKE_INSTALL_LIBDIR=${!outputLib}/lib $cmakeFlags"

But how can this work that you install from a directory in "$out" to itself.
I think, we typically workaround this by doing:
"-DCMAKE_INSTALL_LIBDIR=lib"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it preferred to do it through flags instead of patching the (very odd and unfortunate) CMake call?

Copy link
Member

Choose a reason for hiding this comment

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

The good thing about "-DCMAKE_INSTALL_LIBDIR=lib" is that we can search for it through nixpkgs and see packages affected.

Also, I am still not sure how your patch works.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at the source code, it intends to put intermediary file generated by configure_file under lib/cmake/protobuf in the build directory but (wrongly) assumes the CMAKE_INSTALL_LIBDIR is relative.

The line you patch would install the directory from the build directory to the target location but since we have absolute CMAKE_INSTALL_LIBDIR, it is already in the target location and not in the build directory (${CMAKE_CURRENT_BINARY_DIR}).

The correct patch would be changing the output of configure_file and the input of install(DIRECTORY to something like ${CMAKE_CURRENT_BINARY_DIR}/this_is_an_intermediary_dir_for_cmake_files. I do not think there is any need for the intermediate directory to be the same as the target destination.

# CMake doesn't generate the version.rc file from version.rc.in.
++ lib.optional
(lib.versionAtLeast version "3.6" && lib.versionOlder version "3.7")
./protobuf-3.6-cmake-versionrc.patch;
Copy link
Member

Choose a reason for hiding this comment

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

Upstream only generates that one for windows:
https://github.com/protocolbuffers/protobuf/blob/7b8a241b575b756c4dd7a5aec7f7a60854d5f711/cmake/CMakeLists.txt#L219-L220
Why do we need it? If we need it, we should let upstream know by submitting the patch or filing an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It causes the build to fail, but just for version 3.6, if we don't generate it, thus the version bounding check.

Comment on lines +26 to +38
(if lib.versionAtLeast version "3.6.0"
then ''
rm -rf third_party/googletest
cp -a ${gmock.src} third_party/googletest
chmod -R a+w third_party/googletest
''
else ''
cp -a ${gmock.src}/googlemock gmock
cp -a ${gmock.src}/googletest googletest
chmod -R a+w gmock
chmod -R a+w googletest
ln -s ../googletest gmock/gtest
'')
Copy link
Member

Choose a reason for hiding this comment

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

Can set fetchSubmodules = true in fetchFromGithub (don't forget to update hashes).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is an approach I could take. I borrowed this approach from the existing Autoconf build.

Copy link
Member

Choose a reason for hiding this comment

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

Let's try that. Also remove the darwin hack below, we'll figure something out if it's still needed.

Copy link
Member

Choose a reason for hiding this comment

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

(Don't forget to drop gmock argument)

buildInputs = [ zlib ];
configureFlags = if buildProtobuf == null then [] else [ "--with-protoc=${buildProtobuf}/bin/protoc" ];
Copy link
Member

Choose a reason for hiding this comment

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

Did you check that cross builds still function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe so. protobuf builds protoc but doesn't actually use it as part of the build, it appears someone misread the instructions for cross compiling projects that depend on protobuf as directions for cross-compiling protobuf. But I'll run the build one more time to be sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nix-build -A pkgsCross.armv7l-hf-multiplatform.protobuf is now failing with a not found rather than a exec format error for protoc (the latter would have been what I expected. I'll investigate in a few hours.


enableParallelBuilding = true;

doCheck = true;

dontDisableStatic = true;
Copy link
Member

Choose a reason for hiding this comment

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

This option is only applicable to the default configurePhase:

# By default, disable static builds.
if [ -z "${dontDisableStatic:-}" ]; then
if [ -f "$configureScript" ] && grep -q enable-static "$configureScript"; then
configureFlags="--disable-static $configureFlags"
fi
fi

I would check if static libraries are still present and remove this line. In fact we should compare what files are present in the old build result vs the new.

buildInputs = [ zlib ];
configureFlags = if buildProtobuf == null then [] else [ "--with-protoc=${buildProtobuf}/bin/protoc" ];

enableParallelBuilding = true;
Copy link
Member

Choose a reason for hiding this comment

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

This line is not needed for cmake builds, as it is the default there.

@veprbl
Copy link
Member

veprbl commented Apr 19, 2020

Also, please rebase to staging. already done

@FRidh FRidh added this to WIP in Staging via automation Apr 27, 2020
@FRidh FRidh changed the base branch from master to staging April 27, 2020 18:13
@FRidh
Copy link
Member

FRidh commented May 8, 2020

Could you please resolve the conversations and explicitly approve. That way I know when a PR is ready for staging. Thanks!

@veprbl veprbl changed the title protobuf: build with cmake instead of autoconf [WIP] protobuf: build with cmake instead of autoconf May 8, 2020
--replace 'tmpnam(b)' '"'$TMPDIR'/foo"'
'';
patches =
[ ./protobuf-cmake.patch ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment and link to upstream issue/PR?

let
mkProtobufDerivation = buildProtobuf: stdenv: stdenv.mkDerivation {
stdenv.mkDerivation rec {
# make sure you test also -A pythonPackages.protobuf
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add

passthru = {
  tests = {
    python = python3.pkgs.protobuf;
  };
};

Ofborg will then check the python module automatically.

@ryantm ryantm marked this pull request as draft October 23, 2020 03:01
@stale
Copy link

stale bot commented Jun 4, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 4, 2021
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 9, 2022
@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 10, 2022
@tobim
Copy link
Contributor

tobim commented Feb 6, 2024

This has been implemented in #169497.

@tobim tobim closed this Feb 6, 2024
Staging automation moved this from WIP to Done Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Staging
  
Done
Development

Successfully merging this pull request may close these issues.

closure size: protobuf depends on C++ compiler
6 participants