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

two step builds fail if CMake is used in a build.rs #285

Closed
wucke13 opened this issue Apr 22, 2023 · 5 comments · Fixed by #288
Closed

two step builds fail if CMake is used in a build.rs #285

wucke13 opened this issue Apr 22, 2023 · 5 comments · Fixed by #288

Comments

@wucke13
Copy link

wucke13 commented Apr 22, 2023

Some crates with C bindings use CMake to actually build the C library in question. This breaks with two step builds, the error could look like this:

 CMake Error: The current CMakeCache.txt directory /build/source/target/x86_64-unknown-linux-gnu/release/build/oqs-sys-c5d43ceffffee31f/out/build/CMakeCache.txt is different than the directory /build/dummy-src/target/x86_64-unknown-linux-gnu/release/build/oqs-sys-c5d43ceffffee31f/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt

Basically, CMake detects that the target folder was moved around, causing the build to fail. An easy hotfix would be:

{
 #...
 overrideMain = x: x // {
   preBuild = x.preBuild + ''sed s_/dummy-src/_/source/_g --in-place "$(find -name CMakeCache.txt)"}'';
 };
}

Edit: Obviously its more elegant to use this as hotfix: find -name CMakeCache.txt -exec sed s_/dummy-src/_/source/_g --in-place {} \;

Hiding the evidence that trips of CMake.

@Patryk27
Copy link
Contributor

Patryk27 commented Apr 24, 2023

Hi, I've tried to replicate it (even with the same dependency, oqs), and everything seems to be working fine 👀

Could you prepare a failing example?
(preferably a repository I could git clone and nix-build / nix build locally to see)

@wucke13
Copy link
Author

wucke13 commented May 3, 2023

{
  inputs = {
    naersk.url = "github:nix-community/naersk";
  };

  outputs = { nixpkgs, ... } @ inputs: let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    naersk = pkgs.callPackage inputs.naersk { };
  in {
    packages."x86_64-linux".default = naersk.buildPackage {
      nativeBuildInputs = with pkgs; [ rustPlatform.bindgenHook cmake pkg-config ];
      buildInputs = with pkgs; [ openssl ];
      src = ./.;
    };
  };
}
[package]
name = "minimal"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
oqs-sys = "0.7.2"

minimal.tar.gz

log of failing build
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/9pc2q8zqhigry5cinm48f8rclda3r2mw-source
source root is source
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
[naersk] cargo_version (read): 1.64.0
[naersk] cargo_message_format (set): json-diagnostic-rendered-ansi
[naersk] cargo_release: --release
[naersk] cargo_options:
[naersk] cargo_build_options: $cargo_release -j "$NIX_BUILD_CORES" --message-format=$cargo_message_format
[naersk] cargo_test_options: $cargo_release -j "$NIX_BUILD_CORES"
[naersk] RUST_TEST_THREADS: 16
[naersk] cargo_bins_jq_filter: select(.reason == "compiler-artifact" and .executable != null and .profile.test == false)
[naersk] cargo_build_output_json (created): /build/tmp.uFCeg0Ifbx
[naersk] crate_sources: /nix/store/fz61i7w76ygjgyhs0jbmixs5p5k86j3b-dependencies
[naersk] RUSTFLAGS:
[naersk] CARGO_BUILD_RUSTFLAGS:
[naersk] CARGO_BUILD_RUSTFLAGS (updated): --remap-path-prefix /nix/store/fz61i7w76ygjgyhs0jbmixs5p5k86j3b-dependencies=/sources
[naersk] pre-installing dep /nix/store/ajcg6xx4yv4wb3908rr7wkwlylmmcznf-minimal-deps-0.1.0
@nix { "action": "setPhase", "phase": "buildPhase" }
building
cargo build $cargo_release -j "$NIX_BUILD_CORES" --message-format=$cargo_message_format
   Compiling bindgen v0.60.1
   Compiling oqs-sys v0.7.2
error: failed to run custom build command for `oqs-sys v0.7.2`

Caused by:
  process didn't exit successfully: `/build/source/target/release/build/oqs-sys-78a20b4b21cddd62/build-script-build` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=feature="bike"
  cargo:rustc-link-lib=crypto
  cargo:rerun-if-env-changed=OPENSSL_ROOT_DIR
  CMAKE_TOOLCHAIN_FILE_x86_64-unknown-linux-gnu = None
  CMAKE_TOOLCHAIN_FILE_x86_64_unknown_linux_gnu = None
  HOST_CMAKE_TOOLCHAIN_FILE = None
  CMAKE_TOOLCHAIN_FILE = None
  CMAKE_GENERATOR_x86_64-unknown-linux-gnu = None
  CMAKE_GENERATOR_x86_64_unknown_linux_gnu = None
  HOST_CMAKE_GENERATOR = None
  CMAKE_GENERATOR = None
  CMAKE_PREFIX_PATH_x86_64-unknown-linux-gnu = None
  CMAKE_PREFIX_PATH_x86_64_unknown_linux_gnu = None
  HOST_CMAKE_PREFIX_PATH = None
  CMAKE_PREFIX_PATH = Some("/nix/store/qbx0gskpalv09scmqk9p8iwfglxblq1h-cargo-1.64.0:/nix/store/gbb6f4jm25w0c52fhs4jgk6splng8irx-jq-1.6-dev:/nix/store/bd91f7hxf5q7mfhvhk6fa22mrnyxz7ii-jq-1.6-bin:/nix/store/wa7ygydcjfh4zlycyh9zix19q7pr4ap0-jq-1.6-lib:/nix/store/3jz5p4psa1aw15p2lslday4pdi2vj28m-rsync-3.2.6:/nix/store/6xlgvlx3bixi2plk5g7j3r3f08sgqlpy-rust-bindgen-hook:/nix/store/kmmq2lp8s34wgvz4rxyxh3vps6nzhqbh-cmake-3.24.3:/nix/store/m0p2xy7km6sj75bn30j1dvrz2v83bzi9-pkg-config-wrapper-0.29.2:/nix/store/qbbh33w8jwr1pb6vxp2aplcsdlj6hvvk-patchelf-0.15.0:/nix/store/9nj6qjqp0d25giirzj9f0k35g2s58g0h-gcc-wrapper-11.3.0:/nix/store/lxgn5spk779wflksgibx4jv4n10w6x71-binutils-wrapper-2.39:/nix/store/gz31hsi6hdf0a2xzsznwfmv7vdw4ahcn-openssl-3.0.8-dev:/nix/store/hpcwwg6k6hfrp1fyf3vc1llkkl5gf3vv-openssl-3.0.8-bin:/nix/store/np58sm45gngld2nnqjq6p532j3v2kbfl-openssl-3.0.8")
  CMAKE_x86_64-unknown-linux-gnu = None
  CMAKE_x86_64_unknown_linux_gnu = None
  HOST_CMAKE = None
  CMAKE = None
  running: "cmake" "/nix/store/fz61i7w76ygjgyhs0jbmixs5p5k86j3b-dependencies/oqs-sys-0.7.2/liboqs" "-DOQS_BUILD_ONLY_LIB=Yes" "-DOQS_DIST_BUILD=Yes" "-DOQS_ENABLE_KEM_BIKE=Yes" "-DOQS_ENABLE_KEM_CLASSIC_MCELIECE=Yes" "-DOQS_ENABLE_KEM_FRODOKEM=Yes" "-DOQS_ENABLE_KEM_HQC=Yes" "-DOQS_ENABLE_KEM_KYBER=Yes" "-DOQS_ENABLE_KEM_NTRU=Yes" "-DOQS_ENABLE_KEM_NTRUPRIME=Yes" "-DOQS_ENABLE_KEM_SABER=Yes" "-DOQS_ENABLE_KEM_SIDH=No" "-DOQS_ENABLE_KEM_SIKE=No" "-DOQS_ENABLE_SIG_DILITHIUM=Yes" "-DOQS_ENABLE_SIG_FALCON=Yes" "-DOQS_ENABLE_SIG_PICNIC=Yes" "-DOQS_ENABLE_SIG_RAINBOW=Yes" "-DOQS_ENABLE_SIG_SPHINCS=Yes" "-DOQS_USE_OPENSSL=Yes" "-DCMAKE_INSTALL_PREFIX=/build/source/target/release/build/oqs-sys-7cbadcde4b967837/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_C_COMPILER=/nix/store/9nj6qjqp0d25giirzj9f0k35g2s58g0h-gcc-wrapper-11.3.0/bin/gcc" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_CXX_COMPILER=/nix/store/9nj6qjqp0d25giirzj9f0k35g2s58g0h-gcc-wrapper-11.3.0/bin/g++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_ASM_COMPILER=/nix/store/9nj6qjqp0d25giirzj9f0k35g2s58g0h-gcc-wrapper-11.3.0/bin/gcc" "-DCMAKE_BUILD_TYPE=Release"
  -- Configuring incomplete, errors occurred!
  See also "/build/source/target/release/build/oqs-sys-7cbadcde4b967837/out/build/CMakeFiles/CMakeOutput.log".

  --- stderr
  CMake Error: The current CMakeCache.txt directory /build/source/target/release/build/oqs-sys-7cbadcde4b967837/out/build/CMakeCache.txt is different than the directory /build/dummy-src/target/release/build/oqs-sys-7cbadcde4b967837/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 1

  build script failed, must exit now', /sources/cmake-0.1.50/src/lib.rs:1098:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[naersk] cargo returned with exit code 101, exiting

@wucke13
Copy link
Author

wucke13 commented May 3, 2023

@Patryk27 Yes, sure, above you find everything needed for a minimal example 😄

@Patryk27
Copy link
Contributor

Patryk27 commented May 6, 2023

Huh, that's interesting - it does fail when I build it with your expression, but this one seems to work:

{ ... }:

naersk.buildPackage {
  src = ./.;

  nativeBuildInputs = with pkgs; [ clang cmake ];
  buildInputs = with pkgs; [ openssl ];

  LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
}

Could you check it on your side as well?

@Patryk27
Copy link
Contributor

Patryk27 commented May 6, 2023

Ah, right, that's probably because CMakeCache.txt dumps some envvars that are built differently when one uses bindgenHook vs writes LIBCLANG_PATH by hand; lemme see if we can safely bypass that on Naersk's side with your hook 🙂

Patryk27 added a commit that referenced this issue May 6, 2023
When CMake builds a project, it generates a file called `CMakeCache.txt`
that contains - among others - a dump of environmental variables.

This file is generated both for the deps-only- and the main- derivation,
and if env-vars happen to differ between both¹, CMake will refuse to
compile the latter derivation, saying:

```
CMake Error: The current CMakeCache.txt directory ... is different than the directory ... where CMakeCache.txt was created.
```

Solving this problem is easy - one just has to remove `CMakeCache.txt`
to let CMake regenerate it.

Note that the original bug report (linked below) suggests to use `sed`,
but that doesn't work on Darwin which uses a different convention for
temporary paths - compare error messages for the failing derivations:

... on Linux:

```
CMake Error: The current CMakeCache.txt directory /build/source/target/release/build/oqs-sys-a19e222d9ef825d3/out/build/CMakeCache.txt is different than the directory /build/dummy-src/target/release/build/oqs-sys-a19e222d9ef825d3/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
```

... and on Darwin:

```
CMake Error: The current CMakeCache.txt directory /tmp/nix-build-app-0.1.0.drv-5/source/target/release/build/oqs-sys-4b5e25b1671aa1e5/out/build/CMakeCache.txt is different than the directory /tmp/nix-build-app-deps-0.1.0.drv-0/source/target/release/build/oqs-sys-4b5e25b1671aa1e5/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
```

As for the implementation, since two-step builds are a Naersk-specific
construct, I think solving this transparently on Naersk's side instead
of just adding a tip into README.md is the better approach here, since
it keeps Naersk plug-and-play'able.

Closes #285.

¹ which is not that difficult, e.g. that's how `pkgs.rustPlatform.bindgenHook` behaves
Patryk27 added a commit that referenced this issue May 6, 2023
When CMake builds a project, it generates a file called `CMakeCache.txt`
that contains - among others - a dump of environmental variables.

This file is generated both for the deps-only- and the main- derivation,
and if env-vars happen to differ between both¹, CMake will refuse to
compile the latter derivation, saying:

```
CMake Error: The current CMakeCache.txt directory ... is different than the directory ... where CMakeCache.txt was created.
```

Solving this problem is easy - one just has to remove `CMakeCache.txt`
to let CMake regenerate it.

Note that the original bug report (linked below) suggests to use `sed`,
but that doesn't work on Darwin which uses a different convention for
temporary paths - compare error messages for the failing derivations:

... on Linux:

```
CMake Error: The current CMakeCache.txt directory /build/source/target/release/build/oqs-sys-a19e222d9ef825d3/out/build/CMakeCache.txt is different than the directory /build/dummy-src/target/release/build/oqs-sys-a19e222d9ef825d3/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
```

... and on Darwin:

```
CMake Error: The current CMakeCache.txt directory /tmp/nix-build-app-0.1.0.drv-5/source/target/release/build/oqs-sys-4b5e25b1671aa1e5/out/build/CMakeCache.txt is different than the directory /tmp/nix-build-app-deps-0.1.0.drv-0/source/target/release/build/oqs-sys-4b5e25b1671aa1e5/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
```

As for the implementation, since two-step builds are a Naersk-specific
construct, I think solving this transparently on Naersk's side instead
of just adding a tip into README.md is the better approach here, since
it keeps Naersk plug-and-play'able.

Closes #285.

¹ which is not that difficult, e.g. that's how `pkgs.rustPlatform.bindgenHook` behaves
Patryk27 added a commit that referenced this issue Jun 17, 2023
When CMake builds a project, it generates a file called `CMakeCache.txt`
that contains - among others - a dump of environmental variables.

This file is generated both for the deps-only- and the main- derivation,
and if env-vars happen to differ between both¹, CMake will refuse to
compile the latter derivation, saying:

```
CMake Error: The current CMakeCache.txt directory ... is different than the directory ... where CMakeCache.txt was created.
```

Solving this problem is easy - one just has to remove `CMakeCache.txt`
to let CMake regenerate it.

Note that the original bug report (linked below) suggests to use `sed`,
but that doesn't work on Darwin which uses a different convention for
temporary paths - compare error messages for the failing derivations:

... on Linux:

```
CMake Error: The current CMakeCache.txt directory /build/source/target/release/build/oqs-sys-a19e222d9ef825d3/out/build/CMakeCache.txt is different than the directory /build/dummy-src/target/release/build/oqs-sys-a19e222d9ef825d3/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
```

... and on Darwin:

```
CMake Error: The current CMakeCache.txt directory /tmp/nix-build-app-0.1.0.drv-5/source/target/release/build/oqs-sys-4b5e25b1671aa1e5/out/build/CMakeCache.txt is different than the directory /tmp/nix-build-app-deps-0.1.0.drv-0/source/target/release/build/oqs-sys-4b5e25b1671aa1e5/out/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
```

As for the implementation, since two-step builds are a Naersk-specific
construct, I think solving this transparently on Naersk's side instead
of just adding a tip into README.md is the better approach here, since
it keeps Naersk plug-and-play'able.

Closes #285.

¹ which is not that difficult, e.g. that's how `pkgs.rustPlatform.bindgenHook` behaves
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 a pull request may close this issue.

2 participants