Skip to content

Commit f19d17a

Browse files
committed
Fix nix build for c2rust.
This commit adds a number of improvements to the nix build for c2rust the main highlights are: - A c2rust package is provided, allowing nix users to install and run c2rust itself, instead of just getting a development environment from nix. - Update LLVM / clang to version 18. - Test cases pass in the c2rust dev environment now. Some fixes include... - Removing oxalica and just using fenix. Mixing rust overlays in nix was causing issues. - Use the nix tinycbor package to build c2rust. The c2rust cmake files add an external dependency on tinycbor, which causes sandboxed nix builds to fail because they cannot fetch the package. This involves patching the CMakeLists.txt file, and using pkg-config to find the system tinycbor library. Note: in the dev shell cmake will still fetch the external tinycbor. - Use the C2RUST_TINYCBOR_PATH environment variable, if it exists, to find tinycbor in c2rust-ast-exporter's build.rs - Use bindgenHook to make sure Rust's bindgen can find header files. - Update the test_translator.py script to include nix flags in compiler_commands.json Messing with bindgenHook, disable checks so package builds. - test_translator.py will use nix dependencies when the environment variable C2RUST_USE_NIX=1 + Passing the --use-nix flag to test_translator.py will also explicitly force the script to use nix dependencies.
1 parent e75d601 commit f19d17a

File tree

5 files changed

+215
-140
lines changed

5 files changed

+215
-140
lines changed

c2rust-ast-exporter/build.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,18 @@ fn build_native(llvm_info: &LLVMInfo) {
149149
}
150150
};
151151

152-
// Statically link against 'clangAstExporter' which requires 'tinycbor'
152+
// Use a custom tinybor directory via an environment variable
153+
if let Ok(tinycbor_dir) = env::var("TINYCBOR_DIR") {
154+
let include_dir = Path::new(&tinycbor_dir).join("include");
155+
let lib_dir = Path::new(&tinycbor_dir).join("lib");
156+
157+
println!("cargo:rustc-link-search=native={}", lib_dir.display());
158+
println!("cargo:rerun-if-changed={}", include_dir.display());
159+
}
160+
153161
println!("cargo:rustc-link-lib=static=tinycbor");
162+
163+
// Statically link against 'clangAstExporter' which requires 'tinycbor'
154164
println!("cargo:rustc-link-lib=static=clangAstExporter");
155165

156166
println!("cargo:rustc-link-search=native={}", llvm_lib_dir);

flake.lock

Lines changed: 18 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,69 @@
22
description = "Flake for c2rust";
33

44
inputs = {
5-
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
66
utils.url = "github:numtide/flake-utils";
77
fenix = {
88
url = "github:nix-community/fenix";
9-
};
10-
oxalica = {
11-
url = "github:oxalica/rust-overlay";
9+
inputs.nixpkgs.follows = "nixpkgs";
1210
};
1311
};
1412

15-
outputs = inputs@{ self, nixpkgs, utils, fenix, oxalica}:
13+
outputs = inputs@{ self, nixpkgs, utils, fenix}:
1614
utils.lib.eachDefaultSystem (system:
17-
let
18-
fenixStable = fenix.packages.${system}.fromToolchainFile {
19-
file = ./rust-toolchain.toml;
20-
sha256 = "sha256-r/8YBFuFa4hpwgE3FnME7nQA2Uc1uqj0eCE1NWmI1u0";
21-
};
15+
let
16+
fenixToolchain =
17+
let
18+
toml = with builtins; (fromTOML (readFile ./rust-toolchain.toml)).toolchain;
19+
in
20+
(fenix.packages.${system}.fromToolchainName {
21+
name = toml.channel;
22+
sha256 = "sha256-r/8YBFuFa4hpwgE3FnME7nQA2Uc1uqj0eCE1NWmI1u0";
23+
})."completeToolchain";
24+
2225
pkgs = import nixpkgs {
2326
inherit system;
24-
overlays = [
25-
(import oxalica)
26-
];
27+
overlays = [ ];
2728
config = {
2829
allowUnfree = true;
2930
};
3031
};
31-
in {
32-
defaultPackage = self.devShell.${system};
33-
devShell = pkgs.mkShell.override { } {
3432

35-
LIBCLANG_PATH = "${pkgs.llvmPackages_14.libclang.lib}/lib";
36-
CMAKE_LLVM_DIR = "${pkgs.llvmPackages_14.libllvm.dev}/lib/cmake/llvm";
37-
CMAKE_CLANG_DIR = "${pkgs.llvmPackages_14.libclang.dev}/lib/cmake/clang";
38-
shellHook = ''
39-
export CARGO_TARGET_DIR="$(git rev-parse --show-toplevel)/target_dirs/nix_rustc";
40-
'';
41-
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
42-
buildInputs =
33+
myLLVM = pkgs.llvmPackages_18;
34+
myStdenv = pkgs.clang18Stdenv;
35+
36+
rustPlatform = pkgs.makeRustPlatform {
37+
cargo = fenixToolchain;
38+
rustc = fenixToolchain;
39+
};
40+
env = with pkgs; {
41+
LIBCLANG_PATH = "${myLLVM.libclang.lib}/lib";
42+
CMAKE_LLVM_DIR = "${myLLVM.libllvm.dev}/lib/cmake/llvm";
43+
CMAKE_CLANG_DIR = "${myLLVM.libclang.dev}/lib/cmake/clang";
44+
LLVM_CONFIG_PATH = "${myLLVM.libllvm.dev}/bin/llvm-config";
45+
CLANG_PATH = "${myLLVM.clang}/bin/clang";
46+
TINYCBOR_DIR = "${pkgs.tinycbor}";
47+
NIX_ENFORCE_NO_NATIVE = 0; # Enable SSE instructions.
48+
# Enable nix in the c2rust test suite
49+
# This flag is used to tell the test scripts to look for
50+
# libraries under nix paths.
51+
C2RUST_USE_NIX = 1;
52+
RUST_SRC_PATH = "${fenixToolchain}/lib/rustlib/src/rust/library";
53+
};
54+
in rec {
55+
packages = {
56+
default = rustPlatform.buildRustPackage (with pkgs; env // {
57+
pname = "c2rust";
58+
version = "0.20.0";
59+
src = ./.;
60+
doCheck = false; # Can use checkFlags to disable specific tests
61+
62+
patches = [ ./nix-tinycbor-cmake.patch ];
63+
64+
nativeBuildInputs =
4365
with pkgs; [
44-
clangStdenv.cc
45-
llvmPackages_14.libclang
4666
pkg-config
47-
fenix.packages.${system}.rust-analyzer
48-
llvmPackages_14.clang
4967
cmake
50-
llvmPackages_14.llvm
51-
llvmPackages_14.libllvm
52-
openssl
5368
(python3.withPackages
5469
(python-pkgs:
5570
with python-pkgs;
@@ -68,9 +83,43 @@
6883
]
6984
)
7085
)
86+
myStdenv.cc
87+
myLLVM.libclang
88+
myLLVM.clang
89+
myLLVM.llvm
90+
myLLVM.libllvm
91+
];
92+
93+
buildInputs =
94+
with pkgs; [
95+
rustPlatform.bindgenHook
96+
myStdenv.cc
97+
myLLVM.libclang
98+
myLLVM.clang
99+
myLLVM.llvm
100+
myLLVM.libllvm
101+
tinycbor
102+
openssl
71103
zlib
72-
(rust-bin.fromRustupToolchainFile ./rust-toolchain.toml)
104+
fenixToolchain
73105
];
74-
};
75-
});
106+
107+
cargoLock = {
108+
lockFile = ./Cargo.lock;
109+
};
110+
});
111+
};
112+
defaultPackage = packages.default;
113+
114+
devShells = {
115+
# Include a fixed version of clang in the development environment for testing.
116+
default = pkgs.mkShell (with pkgs; env // {
117+
strictDeps = true;
118+
inputsFrom = [ packages.default ];
119+
buildInputs = [ ];
120+
});
121+
};
122+
123+
devShell = devShells.default;
124+
});
76125
}

0 commit comments

Comments
 (0)