nixctor: init a buildsystem written in nixlang
This commit is contained in:
parent
a9e40e51dc
commit
afa80b0937
5 changed files with 107 additions and 0 deletions
30
nixctor/README.md
Normal file
30
nixctor/README.md
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
## ctor.nix - C/C++/D buildsystem written in Nixlang
|
||||||
|
|
||||||
|
Here is a buildsystem I made that incrementally compiles a program and
|
||||||
|
then links it together at the end. The file default.nix shows an
|
||||||
|
example on how to use this code in a real derivation, it's as simple
|
||||||
|
as just adding your source files into the `sourceFiles` array and
|
||||||
|
ctor.nix will generate a build phase and an install phase for you. It
|
||||||
|
just calls `stdenv.mkDerivation` so you'll have to supply the required
|
||||||
|
arguments to that too.
|
||||||
|
|
||||||
|
To test:
|
||||||
|
|
||||||
|
nix-build
|
||||||
|
|
||||||
|
To use a different compiler or flags, you can use the `compiler` and
|
||||||
|
`cflags` arguments:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
# Example for a D program
|
||||||
|
constructProgram {
|
||||||
|
pname = "hello-world";
|
||||||
|
version = "1.0.0";
|
||||||
|
|
||||||
|
src = fetchFromWhatever {};
|
||||||
|
|
||||||
|
compiler = "ldc2";
|
||||||
|
cflags = "-O -wi";
|
||||||
|
sourceFiles = [ "main.d" "lib.d" ];
|
||||||
|
}
|
||||||
|
```
|
54
nixctor/ctor.nix
Normal file
54
nixctor/ctor.nix
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
# ctor.nix: simple buildsystem written in Nix
|
||||||
|
# It can be adapted for any language, just substitute the default arguments
|
||||||
|
args @
|
||||||
|
{ pkgs ? import <nixpkgs> { }
|
||||||
|
, lib ? pkgs.lib
|
||||||
|
, stdenv ? pkgs.stdenv
|
||||||
|
|
||||||
|
# Name of the compiler to use when compiling source code
|
||||||
|
, compiler ? "cc"
|
||||||
|
# Flags passed to the compiler during compilation stages.
|
||||||
|
, cflags ? "-O2 -c"
|
||||||
|
# Flags passed to the compiler at link time
|
||||||
|
, ldflags ? ""
|
||||||
|
# Flag passed to the compiler to specify the output file
|
||||||
|
, outFlag ? "-o"
|
||||||
|
# Source code files to compile
|
||||||
|
, sourceFiles ? [ "main.c" ]
|
||||||
|
# Suffix of source code files
|
||||||
|
, suffix ? ".c"
|
||||||
|
# Name of the binary file to produce
|
||||||
|
, programName ? "main"
|
||||||
|
|
||||||
|
, ...
|
||||||
|
}:
|
||||||
|
|
||||||
|
with builtins;
|
||||||
|
let
|
||||||
|
newline = "
|
||||||
|
";
|
||||||
|
run = command: ''
|
||||||
|
printf '%s\n' "${command}"
|
||||||
|
${command}
|
||||||
|
'';
|
||||||
|
objify = sourceFile: replaceStrings [ suffix ] [ ".o" ] sourceFile;
|
||||||
|
compile = sourceFile:
|
||||||
|
"${compiler} ${cflags} ${outFlag} ${objify sourceFile} ${sourceFile}";
|
||||||
|
link = objs:
|
||||||
|
"${compiler} ${outFlag} ${programName} ${concatStringsSep " " objs} ${ldflags}";
|
||||||
|
in
|
||||||
|
stdenv.mkDerivation ({
|
||||||
|
buildPhase = ''
|
||||||
|
runHook preBuild
|
||||||
|
${concatStringsSep newline (map
|
||||||
|
(source: run (compile source)) sourceFiles)}
|
||||||
|
${run "${link (map objify sourceFiles)}"}
|
||||||
|
runHook postBuild
|
||||||
|
'';
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
runHook preInstall
|
||||||
|
install -Dm755 ${programName} $out/bin/${programName}
|
||||||
|
runHook postInstall
|
||||||
|
'';
|
||||||
|
} // args)
|
9
nixctor/default.nix
Normal file
9
nixctor/default.nix
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{ constructProgram ? import ./ctor.nix }:
|
||||||
|
constructProgram {
|
||||||
|
pname = "hello-world";
|
||||||
|
version = "1.0.0";
|
||||||
|
|
||||||
|
src = ./.; # use the current directory
|
||||||
|
|
||||||
|
sourceFiles = [ "main.c" "lib.c" ];
|
||||||
|
}
|
7
nixctor/lib.c
Normal file
7
nixctor/lib.c
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
libfunc(void)
|
||||||
|
{
|
||||||
|
puts("hello, world!");
|
||||||
|
}
|
7
nixctor/main.c
Normal file
7
nixctor/main.c
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
void libfunc(void);
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
libfunc();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue