From 17e20716c0a6fef0b6db2f3cd0835eb7c8b77665 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 6 Jul 2004 17:25:10 +0000 Subject: [PATCH] * A function to build libraries. * The linker can link against libraries. * C flags can be passed to the C compiler. --- lib/compile-c.sh | 2 +- lib/default.nix | 24 +++++++++++++++++------- lib/find-includes.sh | 2 +- lib/link.sh | 11 +++++++++-- lib/make-library.sh | 15 +++++++++++++++ 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 lib/make-library.sh diff --git a/lib/compile-c.sh b/lib/compile-c.sh index babd038b9..ee5a6d880 100644 --- a/lib/compile-c.sh +++ b/lib/compile-c.sh @@ -70,4 +70,4 @@ fi mkdir $out test "$prefix" && cd $prefix -gcc -Wall -c $mainName -o $out/$mainName.o +gcc -Wall $cFlags -c $mainName -o $out/$mainName.o diff --git a/lib/default.nix b/lib/default.nix index f295eea78..83f7109aa 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -2,15 +2,19 @@ rec { inherit (import /home/eelco/nixpkgs/pkgs/system/i686-linux.nix) stdenv; - compileC = {main, localIncludes ? []}: stdenv.mkDerivation { + compileC = {main, localIncludes ? [], cFlags ? ""}: stdenv.mkDerivation { name = "compile-c"; builder = ./compile-c.sh; localIncludes = if localIncludes == "auto" then - import (findIncludes {main = toString main; hack = curTime;}) + import (findIncludes { + main = toString main; + hack = curTime; + inherit cFlags; + }) else localIncludes; - inherit main; + inherit main cFlags; }; /* @@ -21,16 +25,22 @@ rec { }; */ - findIncludes = {main, hack}: stdenv.mkDerivation { + findIncludes = {main, hack, cFlags ? ""}: stdenv.mkDerivation { name = "find-includes"; builder = ./find-includes.sh; - inherit main hack; + inherit main hack cFlags; }; - link = {objects, programName ? "program"}: stdenv.mkDerivation { + link = {objects, programName ? "program", libraries ? []}: stdenv.mkDerivation { name = "link"; builder = ./link.sh; - inherit objects programName; + inherit objects programName libraries; + }; + + makeLibrary = {objects, libraryName ? []}: stdenv.mkDerivation { + name = "library"; + builder = ./make-library.sh; + inherit objects libraryName; }; } diff --git a/lib/find-includes.sh b/lib/find-includes.sh index de580239f..4824207c2 100644 --- a/lib/find-includes.sh +++ b/lib/find-includes.sh @@ -5,7 +5,7 @@ echo "finding includes of \`$(basename $main)'..." makefile=$NIX_BUILD_TOP/makefile mainDir=$(dirname $main) -(cd $mainDir && gcc -MM $(basename $main) -MF $makefile) || false +(cd $mainDir && gcc $cFlags -MM $(basename $main) -MF $makefile) || false echo "[" >$out diff --git a/lib/link.sh b/lib/link.sh index 048652f9b..d623e2af1 100644 --- a/lib/link.sh +++ b/lib/link.sh @@ -1,12 +1,19 @@ . $stdenv/setup objs= -for i in "$objects"; do +for i in $objects; do obj=$(echo $i/*.o) objs="$objs $obj" done +libs= +for i in $libraries; do + lib=$(echo $i/*.a) + name=$(echo $(basename $lib) | sed -e 's/^lib//' -e 's/.a$//') + libs="$libs -L$(dirname $lib) -l$name" +done + echo "linking object files into \`$programName'..." mkdir $out -gcc -o $out/$programName $objs +gcc -o $out/$programName $objs $libs diff --git a/lib/make-library.sh b/lib/make-library.sh new file mode 100644 index 000000000..5c635997a --- /dev/null +++ b/lib/make-library.sh @@ -0,0 +1,15 @@ +. $stdenv/setup + +objs= +for i in $objects; do + obj=$(echo $i/*.o) + objs="$objs $obj" +done + +echo "archiving object files into library \`$libraryName'..." + +outPath=$out/lib${libraryName}.a + +mkdir $out +ar crs $outPath $objs +ranlib $outPath