From 08de98f8b6a66237b38ba483e0dc1a7983280f3b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 6 Jul 2004 12:27:19 +0000 Subject: [PATCH] * Header files that are used by a compilation must be declared explicitly. If you forget a dependency, it's simply not visible to the compiler, and so the compilation fails. This is a big plus over conventional Make. --- examples/default.nix | 3 +++ examples/simple-header/default.nix | 11 +++++++++++ examples/simple-header/hello.c | 9 +++++++++ examples/simple-header/hello.h | 1 + examples/trivial/default.nix | 2 +- lib/compile-c.sh | 17 ++++++++++++++++- lib/default.nix | 4 ++-- 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 examples/default.nix create mode 100644 examples/simple-header/default.nix create mode 100644 examples/simple-header/hello.c create mode 100644 examples/simple-header/hello.h diff --git a/examples/default.nix b/examples/default.nix new file mode 100644 index 000000000..17144a255 --- /dev/null +++ b/examples/default.nix @@ -0,0 +1,3 @@ +[ (import ./trivial) + (import ./simple-header) +] \ No newline at end of file diff --git a/examples/simple-header/default.nix b/examples/simple-header/default.nix new file mode 100644 index 000000000..e943471aa --- /dev/null +++ b/examples/simple-header/default.nix @@ -0,0 +1,11 @@ +let { + + inherit (import ../../lib) compileC link; + + hello = link {objects = compileC { + main = ./hello.c; + localIncludes = [ [./hello.h "hello.h"] ]; + };}; + + body = [hello]; +} diff --git a/examples/simple-header/hello.c b/examples/simple-header/hello.c new file mode 100644 index 000000000..15f1ac714 --- /dev/null +++ b/examples/simple-header/hello.c @@ -0,0 +1,9 @@ +#include + +#include "hello.h" + +int main(int argc, char * * argv) +{ + printf("Hello " WHAT "\n"); + return 0; +} diff --git a/examples/simple-header/hello.h b/examples/simple-header/hello.h new file mode 100644 index 000000000..4595fad98 --- /dev/null +++ b/examples/simple-header/hello.h @@ -0,0 +1 @@ +#define WHAT "World" diff --git a/examples/trivial/default.nix b/examples/trivial/default.nix index 3ee30ecb6..132245e58 100644 --- a/examples/trivial/default.nix +++ b/examples/trivial/default.nix @@ -5,4 +5,4 @@ let { hello = link {objects = compileC {main = ./hello.c;};}; body = [hello]; -} \ No newline at end of file +} diff --git a/lib/compile-c.sh b/lib/compile-c.sh index 0b7ffaad5..701c16048 100644 --- a/lib/compile-c.sh +++ b/lib/compile-c.sh @@ -1,3 +1,18 @@ . $stdenv/setup + +mainName=$(basename $main | cut -c34-) +ln -s $main $mainName + +echo "compiling $mainName..." + +localIncludes=($localIncludes) +n=0 +while test $n -lt ${#localIncludes[*]}; do + source=${localIncludes[n]} + target=${localIncludes[$((n+1))]} + ln -s $source $target + n=$((n + 2)) +done + mkdir $out -gcc -Wall -c $main -o $out/$(basename $main).o +gcc -Wall -c $mainName -o $out/$mainName.o diff --git a/lib/default.nix b/lib/default.nix index 0cbe0367e..ab4e517a4 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -2,10 +2,10 @@ rec { inherit (import /home/eelco/nixpkgs/pkgs/system/i686-linux.nix) stdenv; - compileC = {main}: stdenv.mkDerivation { + compileC = {main, localIncludes ? []}: stdenv.mkDerivation { name = "compile-c"; builder = ./compile-c.sh; - inherit main; + inherit main localIncludes; }; link = {objects}: stdenv.mkDerivation {