From 3822d05e45e6973824479fbb6973fa477d89c4e5 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 13 May 2017 06:05:42 -0400 Subject: Nix packaging. --- Cargo.lock | 2 +- Cargo.toml | 2 +- default.nix | 19 ++---- deps.nix | 128 +++++++++++++++++++++++++++++++++++++ release.nix | 8 +++ rust-utils.nix | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 +- 7 files changed, 344 insertions(+), 18 deletions(-) create mode 100644 deps.nix create mode 100644 release.nix create mode 100644 rust-utils.nix diff --git a/Cargo.lock b/Cargo.lock index 894925e..91497e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,5 @@ [root] -name = "v1" +name = "rafs" version = "0.1.0" dependencies = [ "fuse 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index a126a80..4e938a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "v1" +name = "rafs" version = "0.1.0" authors = ["Kjetil Orbekk "] diff --git a/default.nix b/default.nix index 99e7ecd..59eea20 100644 --- a/default.nix +++ b/default.nix @@ -1,16 +1,9 @@ with import {}; -with rustPlatform; +let mkRustCrate = import ./rust-utils.nix { + lib = pkgs.lib; + inherit buildPlatform stdenv; + }; in +let deps = import ./deps.nix { inherit pkgs mkRustCrate; }; in { - rafs1 = buildRustPackage rec { - name = "rafs-${version}"; - version = "0.1"; - buildInputs = [ fuse pkgconfig glibc ]; - logLevel = "debug"; - src = fetchgit { - url = "https://git.orbekk.com/git/rafs.git" - rev = "35f5d3c514b640d3b99c643890bb4a655025994d"; - sha256 = "1i5nr70jdqhfjmpnmcgnsyinh7rd3gaz2ka0adi9x5z0dx1ijg6g"; - }; - depsSha256 = "1i5nr70jdqhfjmpnmcgnsyinh7rd3gaz2ka0adi9x5z0dx1ijg6g"; - }; + rafs = (deps.rafs_0_1_0 rustUnstable.rustc); } diff --git a/deps.nix b/deps.nix new file mode 100644 index 0000000..f07958a --- /dev/null +++ b/deps.nix @@ -0,0 +1,128 @@ +{ pkgs, mkRustCrate }: +with import {}; +let release = true; +in +rec { + fuse_0_3_0 = mkRustCrate { + crateName = "fuse"; + version = "0.3.0"; + dependencies = [ libc_0_2_22 log_0_3_7 pkg_config_0_3_9 thread_scoped_1_0_1 time_0_1_37 ]; + src = fetchzip { + url = "https://crates.io/api/v1/crates/fuse/0.3.0/download"; + sha256 = "1n49fdna5vxygl5v8s8i2brirg4zn7psc65chga4crcs9fk49w8i"; + name = "fuse-0.3.0.tar.gz"; + }; + buildInputs = with pkgs; [fuse pkgconfig glibc]; + libPath = "src/lib.rs"; + libName = "fuse"; + build = "build.rs"; + inherit release; + }; + kernel32_sys_0_2_2 = mkRustCrate { + crateName = "kernel32-sys"; + version = "0.2.2"; + dependencies = [ winapi_0_2_8 winapi_build_0_1_1 ]; + src = fetchzip { + url = "https://crates.io/api/v1/crates/kernel32-sys/0.2.2/download"; + sha256 = "1lrw1hbinyvr6cp28g60z97w32w8vsk6pahk64pmrv2fmby8srfj"; + name = "kernel32-sys-0.2.2.tar.gz"; + }; + libName = "kernel32"; + build = "build.rs"; + buildDependencies = [ winapi_build_0_0_0 ]; + inherit release; + }; + libc_0_2_22 = mkRustCrate { + crateName = "libc"; + version = "0.2.22"; + features = [ "use_std" ]; + src = fetchzip { + url = "https://crates.io/api/v1/crates/libc/0.2.22/download"; + sha256 = "1yy25nhlyf7papwx2gjpck42p6smh1ilpinp0sij617819cpwj3q"; + name = "libc-0.2.22.tar.gz"; + }; + inherit release; + }; + log_0_3_7 = mkRustCrate { + crateName = "log"; + version = "0.3.7"; + features = [ "use_std" ]; + src = fetchzip { + url = "https://crates.io/api/v1/crates/log/0.3.7/download"; + sha256 = "1qxrwkhpfzhgcmfnw4bl9yy7wwr92wwbin3dp6izcfy58lr369v4"; + name = "log-0.3.7.tar.gz"; + }; + inherit release; + }; + pkg_config_0_3_9 = mkRustCrate { + crateName = "pkg-config"; + version = "0.3.9"; + src = fetchzip { + url = "https://crates.io/api/v1/crates/pkg-config/0.3.9/download"; + sha256 = "06k8fxgrsrxj8mjpjcq1n7mn2p1shpxif4zg9y5h09c7vy20s146"; + name = "pkg-config-0.3.9.tar.gz"; + }; + inherit release; + }; + rafs_0_1_0 = mkRustCrate { + crateName = "rafs"; + version = "0.1.0"; + dependencies = [ fuse_0_3_0 libc_0_2_22 ]; + buildInputs = [ fuse pkgconfig glibc ]; + src = ./.; + inherit release; + }; + redox_syscall_0_1_17 = mkRustCrate { + crateName = "redox_syscall"; + version = "0.1.17"; + src = fetchzip { + url = "https://crates.io/api/v1/crates/redox_syscall/0.1.17/download"; + sha256 = "03y0nsfmrcdi6syl31m94xpdjb2bz9ld9ik23cyb0an3kw79a9f7"; + name = "redox_syscall-0.1.17.tar.gz"; + }; + libName = "syscall"; + inherit release; + }; + thread_scoped_1_0_1 = mkRustCrate { + crateName = "thread-scoped"; + version = "1.0.1"; + src = fetchzip { + url = "https://crates.io/api/v1/crates/thread-scoped/1.0.1/download"; + sha256 = "0fhr1m23qs02iqy27qrc9a62pd150ki8jyn3fb99d4isi5h875pb"; + name = "thread-scoped-1.0.1.tar.gz"; + }; + inherit release; + }; + time_0_1_37 = mkRustCrate { + crateName = "time"; + version = "0.1.37"; + dependencies = [ kernel32_sys_0_2_2 libc_0_2_22 redox_syscall_0_1_17 winapi_0_2_8 ]; + src = fetchzip { + url = "https://crates.io/api/v1/crates/time/0.1.37/download"; + sha256 = "17hsakmyg47gz6b6hcjdyp4wazpqphyzgiwpgfa5rh43shsjvhpb"; + name = "time-0.1.37.tar.gz"; + }; + inherit release; + }; + winapi_0_2_8 = mkRustCrate { + crateName = "winapi"; + version = "0.2.8"; + src = fetchzip { + url = "https://crates.io/api/v1/crates/winapi/0.2.8/download"; + sha256 = "0a45b58ywf12vb7gvj6h3j264nydynmzyqz8d8rqxsj6icqv82as"; + name = "winapi-0.2.8.tar.gz"; + }; + inherit release; + }; + winapi_build_0_1_1 = mkRustCrate { + crateName = "winapi-build"; + version = "0.1.1"; + src = fetchzip { + url = "https://crates.io/api/v1/crates/winapi-build/0.1.1/download"; + sha256 = "1lxlpi87rkhxcwp2ykf1ldw3p108hwm24nywf3jfrvmff4rjhqga"; + name = "winapi-build-0.1.1.tar.gz"; + }; + libName = "build"; + inherit release; + }; +} diff --git a/release.nix b/release.nix new file mode 100644 index 0000000..10949d2 --- /dev/null +++ b/release.nix @@ -0,0 +1,8 @@ +{ supportedSystems ? ["x86_64-linux"] +, pkgs ? +}: + +with import ./default.nix {}; +{ + inherit rafs; +} diff --git a/rust-utils.nix b/rust-utils.nix new file mode 100644 index 0000000..be6ed39 --- /dev/null +++ b/rust-utils.nix @@ -0,0 +1,197 @@ +{ lib, buildPlatform, stdenv }: + +let buildCrate = { crateName, crateVersion, dependencies, complete, crateFeatures, libName, build, release, libPath, crateType, metadata, crateBin, finalBins }: + + let depsDir = builtins.foldl' (deps: dep: deps + " " + dep.out) "" dependencies; + completeDepsDir = builtins.foldl' (deps: dep: deps + " " + dep.out) "" complete; + deps = + builtins.foldl' (deps: dep: + let extern = lib.strings.replaceStrings ["-"] ["_"] dep.libName; in + deps + (if dep.crateType == "lib" then + " --extern ${extern}=${dep.out}/lib${extern}-${dep.metadata}.rlib" + else + " --extern ${extern}=${dep.out}/lib${extern}-${dep.metadata}.so") + ) "" dependencies; + optLevel = if release then 3 else 0; + rustcOpts = (if release then "-C opt-level=3" else "-g"); + rustcMeta = "-C metadata=" + metadata + " -C extra-filename=-" + metadata; + in '' + mkdir -p target/deps + mkdir -p target/build + chmod uga+w target -R + + for i in ${completeDepsDir}; do + ln -s -f $i/*.rlib target/deps #*/ + ln -s -f $i/*.so target/deps #*/ + if [ -e "$i/link" ]; then + cat $i/link >> target/link + cat $i/link >> target/link.final + fi + done + + EXTRA_BUILD="" + BUILD_OUT_DIR="" + export CARGO_PKG_NAME=${crateName} + export CARGO_PKG_VERSION=${crateVersion} + export CARGO_CFG_TARGET_ARCH=$(echo ${buildPlatform.system} | sed -e "s/\([^-]*\)-\([^-]*\)/\1/") + export CARGO_CFG_TARGET_OS=$(echo ${buildPlatform.system} | sed -e "s/\([^-]*\)-\([^-]*\)/\2/") + + export CARGO_CFG_TARGET_ENV="gnu" + export CARGO_MANIFEST_DIR="." + export DEBUG="${toString (!release)}" + export OPT_LEVEL="${toString optLevel}" + export TARGET="${buildPlatform.system}-gnu" + export HOST="${buildPlatform.system}-gnu" + export PROFILE=${if release then "release" else "debug"} + export OUT_DIR=target/build/${crateName}.out + + if [[ ! -z "${build}" ]] ; then + echo "Building ${build}" + mkdir -p target/build/${crateName} + rustc --crate-name build_script_build ${build} --crate-type bin ${rustcOpts} ${crateFeatures} --out-dir target/build/${crateName} --emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow + mkdir -p target/build/${crateName}.out + export RUST_BACKTRACE=1 + BUILD_OUT_DIR="-L $OUT_DIR" + mkdir -p $OUT_DIR + target/build/${crateName}/build_script_build > target/build/${crateName}.opt + set +e + EXTRA_BUILD=$(grep "^cargo:rustc-flags=" target/build/${crateName}.opt | sed -e "s/cargo:rustc-flags=\(.*\)/\1/") + EXTRA_FEATURES=$(grep "^cargo:rustc-cfg=" target/build/${crateName}.opt | sed -e "s/cargo:rustc-cfg=\(.*\)/--cfg \1/") + + EXTRA_LINK=$(grep "^cargo:rustc-link-lib=" target/build/${crateName}.opt | sed -e "s/cargo:rustc-link-lib=\(.*\)/\1/") + EXTRA_LINK_SEARCH=$(grep "^cargo:rustc-link-search=" target/build/${crateName}.opt | sed -e "s/cargo:rustc-link-search=\(.*\)/\1/") + set -e + if [ -n "$(ls target/build/${crateName}.out)" ]; then + + if [ -e "${libPath}" ] ; then + cp -r target/build/${crateName}.out/* $(dirname ${libPath}) #*/ + else + cp -r target/build/${crateName}.out/* src #*/ + fi + fi + fi + # echo "Features: ${crateFeatures}" $EXTRA_FEATURES + + EXTRA_LIB="" + CRATE_NAME=$(echo ${libName} | sed -e "s/-/_/g") + # echo "Libname" ${libName} ${libPath} + # echo "Deps: ${deps}" + if [ -e "${libPath}" ] ; then + + echo "Building ${libPath}" + rustc --crate-name $CRATE_NAME ${libPath} --crate-type ${crateType} ${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/deps --emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES + EXTRA_LIB=" --extern $CRATE_NAME=target/deps/lib$CRATE_NAME-${metadata}.rlib" + + elif [ -e src/lib.rs ] ; then + + echo "Building src/lib.rs" + rustc --crate-name $CRATE_NAME src/lib.rs --crate-type ${crateType} ${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/deps --emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES + EXTRA_LIB=" --extern $CRATE_NAME=target/deps/lib$CRATE_NAME-${metadata}.rlib" + + elif [ -e src/${libName}.rs ] ; then + + echo "Building src/${libName}.rs" + rustc --crate-name $CRATE_NAME src/${libName}.rs --crate-type ${crateType} ${rustcOpts} ${rustcMeta} ${crateFeatures} --out-dir target/deps --emit=dep-info,link -L dependency=target/deps ${deps} --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES + EXTRA_LIB=" --extern $CRATE_NAME=target/deps/lib$CRATE_NAME-${metadata}.rlib" + + fi + + echo "$EXTRA_LINK_SEARCH" | while read i; do + if [ ! -z "$i" ]; then + echo "-L $i" >> target/link + L=$(echo $i | sed -e "s#target/build#$out#") + echo "-L $L" >> target/link.final + fi + done + echo "$EXTRA_LINK" | while read i; do + if [ ! -z "$i" ]; then + echo "-l $i" >> target/link + echo "-l $i" >> target/link.final + fi + done + + if [ -e target/link ]; then + LINK=$(cat target/link) + fi + + mkdir -p target/bin + echo "${crateBin}" | sed -n 1'p' | tr ',' '\n' | while read BIN; do + if [ ! -z "$BIN" ]; then + echo "Building $BIN" + echo rustc --crate-name $BIN --crate-type bin ${rustcOpts} ${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps ${deps}$EXTRA_LIB --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES -C link-args="$LINK" + rustc --crate-name $BIN --crate-type bin ${rustcOpts} ${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps ${deps}$EXTRA_LIB --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES -C link-args="$LINK" + fi + done + if [[ (-z "${crateBin}") && (-e src/main.rs) ]]; then + echo "Building src/main.rs" + echo rustc --crate-name $CRATE_NAME src/main.rs --crate-type bin ${rustcOpts} ${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps ${deps}$EXTRA_LIB --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES -C link-args="$LINK" + rustc --crate-name $CRATE_NAME src/main.rs --crate-type bin ${rustcOpts} ${crateFeatures} --out-dir target/bin --emit=dep-info,link -L dependency=target/deps ${deps}$EXTRA_LIB --cap-lints allow $BUILD_OUT_DIR $EXTRA_BUILD $EXTRA_FEATURES -C link-args="$LINK" + fi + '' + finalBins; + + installCrate = crateName: '' + mkdir -p $out + if [ -e target/link.final ]; then + cp target/link.final $out/link + fi + cp -PR target/deps/* $out # */ + if [ "$(ls -A target/build)" ]; then # */ + cp -PR target/build/* $out # */ + fi + if [ "$(ls -A target/bin)" ]; then # */ + mkdir -p $out/bin + cp -P target/bin/* $out/bin # */ + fi + ''; +in + +crate: rust: stdenv.mkDerivation rec { + + inherit (crate) crateName src; + + release = if crate ? release then crate.release else false; + name = "rust_${crate.crateName}-${crate.version}"; + buildInputs = [ rust ] ++ (lib.attrByPath ["buildInputs"] [] crate); + dependencies = builtins.map (dep: dep rust) (lib.attrByPath ["dependencies"] [] crate); + + complete = builtins.foldl' (comp: dep: if lib.lists.any (x: x == comp) dep.complete then comp ++ dep.complete else comp) dependencies dependencies; + + crateFeatures = if crate ? features then + builtins.foldl' (features: f: features + " --cfg feature=\\\"${f}\\\"") "" crate.features + else ""; + + libName = if crate ? libName then crate.libName else crate.crateName; + libPath = if crate ? libPath then crate.libPath else ""; + + metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion)); + + crateBin = if crate ? crateBin then + builtins.foldl' (bins: bin: + let name = + lib.strings.replaceStrings ["-"] ["_"] + (if bin ? name then bin.name else crateName); + path = if bin ? path then bin.path else "src/main.rs"; + in + bins + (if bin == "" then "" else ",") + "${name} ${path}" + + ) "" crate.crateBin + else ""; + + finalBins = if crate ? crateBin then + builtins.foldl' (bins: bin: + let name = lib.strings.replaceStrings ["-"] ["_"] + (if bin ? name then bin.name else crateName); + new_name = if bin ? name then bin.name else crateName; + in + if name == new_name then bins else + (bins + "mv target/bin/${name} target/bin/${new_name};") + + ) "" crate.crateBin + else ""; + + build = if crate ? build then crate.build else ""; + crateVersion = crate.version; + crateType = if crate ? procMacro then if crate.procMacro then "proc-macro" else "lib" else "lib"; + buildPhase = buildCrate { inherit crateName dependencies complete crateFeatures libName build release libPath crateType crateVersion metadata crateBin finalBins; }; + installPhase = installCrate crateName; +} diff --git a/src/main.rs b/src/main.rs index babac6a..9ad2982 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ extern crate fuse; extern crate libc; -extern crate v1; +extern crate rafs; -use v1::data::get_data1; -use v1::data::SimpleFsData; +use rafs::data::get_data1; +use rafs::data::SimpleFsData; use fuse::{Filesystem, Request, ReplyEntry, ReplyAttr, ReplyDirectory, ReplyData}; use libc::ENOENT; use std::ffi::OsStr; -- cgit v1.2.3