Skip to content

Commit

Permalink
nix/flake: modular
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Sep 19, 2023
1 parent f9319c1 commit 74b476a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 78 deletions.
78 changes: 46 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,40 +233,68 @@ function(wtr_add_hdr_target NAME INCLPATH HDR_NAME)
COMPONENT "include")
endfunction()

function(wtr_add_rel_bin_target NAME SRC_SET CC_EXTOPT_SET LL_EXTOPT_SET)
function(wtr_add_rel_bin_target NAME SRC_SET)
wtr_add_bin_target(
"${NAME}"
"bin"
"OFF" # is test
"${SRC_SET}"
"${COMPILE_OPTIONS};${CC_EXTOPT_SET}"
"${LINK_OPTIONS};${LL_EXTOPT_SET}"
"${COMPILE_OPTIONS}"
"${LINK_OPTIONS}"
"${INCLUDE_PATH_SINGLE_HEADER}"
"${LINK_LIBRARIES}")
endfunction()

function(wtr_add_test_bin_target NAME SRC_SET CC_EXTOPT_SET LL_EXTOPT_SET)
function(wtr_add_test_bin_target NAME SRC_SET)
wtr_add_bin_target(
"${NAME}"
"test-bin"
"ON" # is test
"${SRC_SET}"
"${COMPILE_OPTIONS};${CC_EXTOPT_SET}"
"${LINK_OPTIONS};${LL_EXTOPT_SET}"
"${COMPILE_OPTIONS}"
"${LINK_OPTIONS}"
"${INCLUDE_PATH_DEVEL}"
"${LINK_LIBRARIES};snitch::snitch")
endfunction()

function(wtr_add_san_test_bin_target NAME SRC_SET SAN)
wtr_add_bin_target(
"${NAME}.${SAN}"
"test-bin"
"ON" # is test
"${SRC_SET}"
"${COMPILE_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
"${LINK_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
"${INCLUDE_PATH_DEVEL}"
"${LINK_LIBRARIES};snitch::snitch")
endfunction()

function(wtr_add_bench_bin_target NAME SRC_SET CC_EXTOPT_SET LL_EXTOPT_SET)
wtr_add_test_bin_target(
function(wtr_add_bench_bin_target NAME SRC_SET)
wtr_add_bin_target(
"${NAME}"
"bench-bin"
"ON" # is test
"${SRC_SET}"
"${COMPILE_OPTIONS}"
"${LINK_OPTIONS}"
"${INCLUDE_PATH_DEVEL}"
"${LINK_LIBRARIES};snitch::snitch")
endfunction()

function(wtr_add_san_bench_bin_target NAME SRC_SET SAN)
wtr_add_bin_target(
"${NAME}.${SAN}"
"bench-bin"
"ON" # is test
"${SRC_SET}"
"${CC_EXTOPT_SET}"
"${LL_EXTOPT_SET}")
"${COMPILE_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
"${LINK_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
"${INCLUDE_PATH_DEVEL}"
"${LINK_LIBRARIES};snitch::snitch")
endfunction()

function(wtr_add_autosan_bin_target NAME SRC_SET)
wtr_add_rel_bin_target("${NAME}" "${SRC_SET}" "" "")
wtr_add_rel_bin_target("${NAME}" "${SRC_SET}")
foreach(SAN ${SAN_NAMES})
if(WTR_WATCHER_ALLOWED_${SAN})
wtr_add_bin_target(
Expand All @@ -283,29 +311,21 @@ function(wtr_add_autosan_bin_target NAME SRC_SET)
endfunction()

function(wtr_add_autosan_test_bin_target NAME SRC_SET)
wtr_add_test_bin_target("${NAME}" "${SRC_SET}" "" "")
wtr_add_test_bin_target("${NAME}" "${SRC_SET}")
foreach(SAN ${SAN_NAMES})
if(WTR_WATCHER_ALLOWED_${SAN})
wtr_add_bin_target(
"${NAME}.${SAN}"
"test-bin"
"ON" # is test
"${SRC_SET}"
"${COMPILE_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
"${LINK_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
"${INCLUDE_PATH_DEVEL}"
"${LINK_LIBRARIES};snitch::snitch")
wtr_add_san_test_bin_target("${NAME}" "${SRC_SET}" "${SAN}")
endif()
endforeach()
endfunction()

function(wtr_add_autosan_bench_bin_target NAME SRC_SET)
wtr_add_bench_bin_target("${NAME}" "${SRC_SET}" "" "")
wtr_add_bench_bin_target("${NAME}" "${SRC_SET}")
foreach(SAN ${SAN_NAMES})
if(WTR_WATCHER_ALLOWED_${SAN})
wtr_add_bin_target(
"${NAME}.${SAN}"
"test-bin"
"bench-bin"
"ON" # is test
"${SRC_SET}"
"${COMPILE_OPTIONS};${CCLL_EXTOPT_SET_${SAN}}"
Expand All @@ -327,19 +347,13 @@ wtr_add_hdr_target(

wtr_add_autosan_bin_target(
"wtr.watcher"
"${WTR_WATCHER_SOURCE_SET}"
"${COMPILE_OPTIONS}"
"${LINK_OPTIONS}")
"${WTR_WATCHER_SOURCE_SET}")

wtr_add_autosan_test_bin_target(
"wtr.test_watcher"
"${WTR_TEST_WATCHER_SOURCE_SET}"
"${COMPILE_OPTIONS}"
"${LINK_OPTIONS}")
"${WTR_TEST_WATCHER_SOURCE_SET}")

wtr_add_bench_bin_target(
"wtr.bench_watcher"
"${WTR_BENCH_WATCHER_SOURCE_SET}"
"${COMPILE_OPTIONS}"
"${LINK_OPTIONS}")
"${WTR_BENCH_WATCHER_SOURCE_SET}")

107 changes: 61 additions & 46 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@
let
pkgs = import nixpkgs { inherit system; };

watcher_targets = {
cli = "wtr.watcher";
hdr = "wtr.hdr_watcher";
test = "wtr.test_watcher";
bench = "wtr.bench_watcher";
};
watcher_components = {
bin = "bin";
include = "include";
};
watcher_buildcfgs = {
rel = "Release";
deb = "Debug";
rwd = "RelWithDebInfo";
msr = "MinSizeRel";
};

build_deps = [ pkgs.clang pkgs.cmake ];
# If on Darwin, we need this to use FSEvents, dispatch, etc.
maybe_sys_deps = pkgs.lib.optionals pkgs.stdenv.isDarwin [ pkgs.darwin.apple_sdk.frameworks.CoreServices ];
Expand All @@ -52,52 +35,83 @@

mkWatcherDerivation =
{ src
, pname
, buildcfg
, targets
, components
# TODO Just make this a regular cmake target
, installBashScriptName ? ""
, installBashScript ? ""
}: pkgs.stdenv.mkDerivation {
inherit src buildcfg targets components;
pname = "wtr.watcher";
inherit src pname buildcfg targets components;
version = "0.9.2"; # hook: tool/release
nativeBuildInputs = build_deps ++ maybe_sys_deps ++ [ snitch ];
env.WTR_WATCHER_USE_SYSTEM_SNITCH = 1;
buildPhase = ''for target in $targets; do cmake --build . --target "$target" --config "$buildcfg"; done'';
installPhase = ''for component in $components; do cmake --install . --prefix "$out" --config "$buildcfg" --component "$component"; done'';
buildPhase = ''
for target in $targets
do cmake --build . --target "$target" --config "$buildcfg" &
done
wait
'';
installPhase = ''
for component in $components
do cmake --install . --prefix "$out" --config "$buildcfg" --component "$component"
done
if [ -n "${installBashScript}" ]
then
echo "${installBashScript}" | tee -a "$out/bin/${installBashScriptName}"
chmod +x "$out/bin/${installBashScriptName}"
fi
'';
};

watcher = mkWatcherDerivation {
src = self;
buildcfg = watcher_buildcfgs.rel;
targets = [ watcher_targets.cli watcher_targets.hdr ];
components = [ watcher_components.bin watcher_components.include ];
src = self;
pname = "wtr.watcher";
buildcfg = "Release";
targets = [ "wtr.watcher" "wtr.hdr_watcher" ];
components = [ "bin" "include" ];
};

watcher-cli = mkWatcherDerivation {
src = self;
buildcfg = watcher_buildcfgs.rel;
targets = [ watcher_targets.cli ];
components = [ watcher_components.bin ];
src = self;
pname = "wtr.watcher";
buildcfg = "Release";
targets = [ "wtr.watcher" ];
components = [ "bin" ];
};

watcher-hdr = mkWatcherDerivation {
src = self;
buildcfg = watcher_buildcfgs.rel;
targets = [ watcher_targets.hdr ];
components = [ watcher_components.include ];
src = self;
pname = "exit";
buildcfg = "Release";
targets = [ "wtr.hdr_watcher" ];
components = [ "include" ];
};

watcher-test = mkWatcherDerivation {
src = self;
buildcfg = watcher_buildcfgs.deb;
targets = [ watcher_targets.test ];
components = [ watcher_components.bin ];
src = self;
pname = "wtr.test_watcher.allsan";
buildcfg = "Debug";
targets = [ "wtr.test_watcher" "wtr.test_watcher.asan" "wtr.test_watcher.msan" "wtr.test_watcher.tsan" "wtr.test_watcher.ubsan" ];
components = [ "test-bin" ];
installBashScriptName = "wtr.test_watcher.allsan";
installBashScript = ''
#!/usr/bin/env bash
PATH="$PATH:$out/bin"
mkdir -p /tmp/watcher-test
cd /tmp/watcher-test
if command -v wtr.test_watcher ; then wtr.test_watcher ; fi
if command -v wtr.test_watcher.asan ; then wtr.test_watcher.asan ; fi
if command -v wtr.test_watcher.msan ; then wtr.test_watcher.msan ; fi
if command -v wtr.test_watcher.tsan ; then wtr.test_watcher.tsan ; fi
if command -v wtr.test_watcher.ubsan; then wtr.test_watcher.ubsan; fi
rm -rf /tmp/watcher-test
'';
};

watcher-bench = mkWatcherDerivation {
src = self;
buildcfg = watcher_buildcfgs.rel;
targets = [ watcher_targets.bench ];
components = [ watcher_components.bin ];
src = self;
pname = "wtr.bench_watcher";
buildcfg = "Release";
targets = [ "wtr.bench_watcher" ];
components = [ "bin" ];
};

# Bring watcher's tree and all of its dependencies in via inputsFrom,
Expand All @@ -108,7 +122,8 @@
};

in {
defaultApp = flake-utils.lib.mkApp { drv = watcher; };
packages = { inherit watcher watcher-cli watcher-hdr watcher-test watcher-bench; };
defaultApp = flake-utils.lib.mkApp { drv = watcher-cli; };
defaultPackage = watcher;
devShell = watcher-devshell;
}
Expand Down

0 comments on commit 74b476a

Please sign in to comment.