From aac50fb4a0eea25a057bb2d7ebe80961c542a2a5 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 17 Aug 2023 21:21:07 -0400 Subject: Nixify C build This is working with libsodium as an example. Its unfortunate that we need the extra ':arg -lsodium' but how else can I get the name of the library for linking? Is that something in the nix attr metadata? Anyway, an optimization for another day. --- Biz/Bild.hs | 35 +++++++---------------------------- Biz/Bild.nix | 4 ++-- Biz/Bild/Builder.nix | 20 +++++++++++++++++--- Biz/Bild/Example.c | 15 ++++++++++++--- Biz/Namespace.hs | 1 + 5 files changed, 39 insertions(+), 36 deletions(-) (limited to 'Biz') diff --git a/Biz/Bild.hs b/Biz/Bild.hs index 81947ed..2db5ccc 100644 --- a/Biz/Bild.hs +++ b/Biz/Bild.hs @@ -423,7 +423,6 @@ analyze hmap ns = case Map.lookup ns hmap of user <- Env.getEnv "USER" /> Text.pack host <- HostName.getHostName /> Text.pack Log.info ["bild", "analyze", str path] - let runw cmd args = Process.readProcess cmd args "" /> Text.pack /> Text.words contentLines <- withFile absPath ReadMode <| \h -> IO.hSetEncoding h IO.utf8_bom @@ -466,20 +465,6 @@ analyze hmap ns = case Map.lookup ns hmap of Namespace.Sh -> pure Nothing Namespace.C -> Meta.detectAll "//" contentLines |> \Meta.Parsed {..} -> do - nixCFlags <- - Env.getEnv "NIX_CFLAGS_COMPILE" - /> Text.pack - /> Text.words - langdepFlags <- - null pdep - ?. ( runw "pkg-config" ("--cflags" : Set.toList pdep), - pure [] - ) - sysdepFlags <- - null psys - ?. ( runw "pkg-config" ("--libs" : Set.toList psys), - pure [] - ) Target { langdeps = pdep, sysdeps = psys, @@ -487,18 +472,11 @@ analyze hmap ns = case Map.lookup ns hmap of compiler = Gcc, builder = Local user host, out = pout, - packageSet = "", -- none, or maybe I should make cPackages? - compilerFlags = - concat - [ [o, dir, str absPath] - ++ langdepFlags - ++ sysdepFlags - ++ nixCFlags - ++ (map Text.pack <| Set.toList parg) - | let outable = pout /= Meta.None, - o <- outable ?: (["-o"], []), - dir <- outable ?: ([Text.pack <| root outToPath pout], []) - ], + packageSet = "cPackages", + compilerFlags = case pout of + Meta.Bin o -> + ["-o", o, path] <> Set.toList parg |> map Text.pack + _ -> panic "can only bild C exes, not libs", outPath = outToPath pout, srcs = Set.singleton absPath, .. @@ -756,7 +734,7 @@ build andTest loud analysis = >> nixBuild loud target Gcc -> Log.info ["bild", label, "gcc", nschunk namespace] - >> proc loud namespace (toNixFlag compiler) compilerFlags + >> nixBuild loud target where label = case out of Meta.Bin _ -> "bin" @@ -977,4 +955,5 @@ selectBuilder = \case Namespace _ Namespace.Hs -> "haskell" Namespace _ Namespace.Py -> "python" Namespace _ Namespace.Rs -> "base" + Namespace _ Namespace.C -> "c" Namespace _ ext -> panic <| "no builder for " <> show ext diff --git a/Biz/Bild.nix b/Biz/Bild.nix index 94305bc..ee0b1fe 100644 --- a/Biz/Bild.nix +++ b/Biz/Bild.nix @@ -30,6 +30,8 @@ rec { pythonPackages = nixpkgs.python3Packages; pythonWith = nixpkgs.python3.withPackages; + cPackages = nixpkgs.pkgs; + ghcPackageSetFull = private.ghcWith private.haskellDeps; ghcPackageSetBild = private.ghcWith (hpkgs: with hpkgs; [ aeson async base bytestring conduit conduit-extra containers directory @@ -53,8 +55,6 @@ rec { (private.ghcWith (hpkgs: with hpkgs; [])) /* disable until nixified builds are complete */ - # c deps - gcc gdb valgrind argp-standalone SDL # lisp deps guile (private.sbclWith (p: with p; [asdf alexandria])) # just enough to build Example.lisp diff --git a/Biz/Bild/Builder.nix b/Biz/Bild/Builder.nix index 7ce29a2..2b62b89 100644 --- a/Biz/Bild/Builder.nix +++ b/Biz/Bild/Builder.nix @@ -19,6 +19,8 @@ with builtins; let srcs_ = (lib.strings.splitString " " srcs) ++ [main]; + isEmpty = x: x == null || x == []; + skip = ["_" ".direnv"]; filter = file: type: if elem (baseNameOf file) skip then false @@ -35,12 +37,12 @@ let src = lib.sources.cleanSourceWith {inherit filter; src = lib.sources.cleanSource root;}; langdeps_ = - if langdeps == null || langdeps == [] then + if isEmpty langdeps then [] else private.selectAttrs (lib.strings.splitString " " langdeps) private.${packageSet}; sysdeps_ = - if sysdeps == null || sysdeps == [] then + if isEmpty sysdeps then [] else private.selectAttrs (lib.strings.splitString " " sysdeps) private.nixpkgs.pkgs; @@ -53,7 +55,6 @@ in { buildPhase = compileLine; }; - haskell = stdenv.mkDerivation rec { inherit name src BIZ_ROOT postUnpack; buildInputs = sysdeps_ ++ [ @@ -65,6 +66,19 @@ in { buildPhase = compileLine; }; + c = stdenv.mkDerivation rec { + inherit name src BIZ_ROOT postUnpack; + buildInputs = langdeps_ ++ sysdeps_; + installPhase = "install -D ${name} $out/bin/${name}"; + buildPhase = lib.strings.concatStringsSep " " [ + compileLine + (if isEmpty langdeps then "" else + "$(pkg-config --cflags ${langdeps})") + (if isEmpty sysdeps then "" else + "$(pkg-config --libs ${sysdeps})") + ]; + }; + python = buildPythonApplication rec { inherit name src BIZ_ROOT postUnpack; propagatedBuildInputs = [ (private.pythonWith (_: langdeps_)) ] ++ sysdeps_; diff --git a/Biz/Bild/Example.c b/Biz/Bild/Example.c index 9966dba..52ea9b9 100644 --- a/Biz/Bild/Example.c +++ b/Biz/Bild/Example.c @@ -1,6 +1,15 @@ -// : out helloworld.exe -void -main () +// : out examplesodium.exe +// : dep libsodium +// : arg -lsodium +#include + +int +main (void) { + if (sodium_init () < 0) + { + /* panic! the library couldn't be initialized; it is not safe to use */ + } printf ("Biz/Bild/Example.c: Hello world!\n"); + return 0; } diff --git a/Biz/Namespace.hs b/Biz/Namespace.hs index c811554..6d099fd 100644 --- a/Biz/Namespace.hs +++ b/Biz/Namespace.hs @@ -91,6 +91,7 @@ toModule (Namespace parts Hs) = joinWith "." parts toModule (Namespace parts Py) = joinWith "." parts toModule (Namespace parts Scm) = "(" ++ joinWith " " parts ++ ")" toModule (Namespace parts Rs) = joinWith "::" parts +toModule (Namespace parts C) = joinWith "/" parts <> ".c" toModule (Namespace {..}) = panic <| "toModule not implemented for " <> show ext toHaskellModule :: Namespace -> String -- cgit v1.2.3