diff --git a/examples/not-so-simple-header-auto/bar/hello.h b/examples/not-so-simple-header-auto/bar/hello.h new file mode 100644 index 000000000..4595fad98 --- /dev/null +++ b/examples/not-so-simple-header-auto/bar/hello.h @@ -0,0 +1 @@ +#define WHAT "World" diff --git a/examples/not-so-simple-header-auto/default.nix b/examples/not-so-simple-header-auto/default.nix new file mode 100644 index 000000000..5843a985f --- /dev/null +++ b/examples/not-so-simple-header-auto/default.nix @@ -0,0 +1,11 @@ +let { + + inherit (import ../../lib) compileC findIncludes link; + + hello = link {programName = "hello"; objects = compileC { + main = ./foo/hello.c; + localIncludes = import (findIncludes {main = toString ./foo/hello.c;}); + };}; + + body = [hello]; +} diff --git a/examples/not-so-simple-header-auto/foo/fnord/indirect.h b/examples/not-so-simple-header-auto/foo/fnord/indirect.h new file mode 100644 index 000000000..2fde1e26c --- /dev/null +++ b/examples/not-so-simple-header-auto/foo/fnord/indirect.h @@ -0,0 +1,3 @@ +#define HELLO "Hello" + +#include "../../bar/hello.h" diff --git a/examples/not-so-simple-header-auto/foo/hello.c b/examples/not-so-simple-header-auto/foo/hello.c new file mode 100644 index 000000000..7d5b402ce --- /dev/null +++ b/examples/not-so-simple-header-auto/foo/hello.c @@ -0,0 +1,9 @@ +#include + +#include "fnord/indirect.h" + +int main(int argc, char * * argv) +{ + printf(HELLO " " WHAT "\n"); + return 0; +} diff --git a/lib/compile-c.sh b/lib/compile-c.sh index e07d6afc0..babd038b9 100644 --- a/lib/compile-c.sh +++ b/lib/compile-c.sh @@ -64,7 +64,9 @@ for ((n = 0; n < ${#localIncludes[*]}; n += 2)); do done # Create a symlink to the main file. -ln -s $main $prefix$mainName +if ! test "$(readlink $prefix$mainName)" = $main; then + ln -s $main $prefix$mainName +fi mkdir $out test "$prefix" && cd $prefix diff --git a/lib/default.nix b/lib/default.nix index d2cafc8c2..fe9a98a7d 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -8,10 +8,24 @@ rec { inherit main localIncludes; }; + /* + runCommand = {command}: { + name = "run-command"; + builder = ./run-command.sh; + inherit command; + }; + */ + + findIncludes = {main}: stdenv.mkDerivation { + name = "find-includes"; + builder = ./find-includes.sh; + inherit main; + }; + link = {objects, programName ? "program"}: stdenv.mkDerivation { name = "link"; builder = ./link.sh; inherit objects programName; }; - + } diff --git a/lib/find-includes.sh b/lib/find-includes.sh new file mode 100644 index 000000000..de580239f --- /dev/null +++ b/lib/find-includes.sh @@ -0,0 +1,20 @@ +. $stdenv/setup + +echo "finding includes of \`$(basename $main)'..." + +makefile=$NIX_BUILD_TOP/makefile + +mainDir=$(dirname $main) +(cd $mainDir && gcc -MM $(basename $main) -MF $makefile) || false + +echo "[" >$out + +while read line; do + line=$(echo "$line" | sed 's/.*://') + for i in $line; do + fullPath=$(readlink -f $mainDir/$i) + echo " [ $fullPath \"$i\" ]" >>$out + done +done < $makefile + +echo "]" >>$out