summaryrefslogtreecommitdiffstats
path: root/pkgs/development/interpreters/bqn/cbqn/default.nix
blob: 9b6dd5f040d298dc3544a968c643540a15b795ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
{
  lib,
  callPackage,
  fixDarwinDylibNames,
  libffi,
  mbqn-source,
  pkg-config,
  stdenv,
  # Boolean flags
  enableReplxx ? false,
  enableLibcbqn ? ((stdenv.hostPlatform.isLinux || stdenv.hostPlatform.isDarwin) && !enableReplxx),
  generateBytecode ? false,
  # "Configurable" options
  bqn-interpreter,
}:

let
  sources = callPackage ./sources.nix { };
in
stdenv.mkDerivation {
  pname = "cbqn" + lib.optionalString (!generateBytecode) "-standalone";
  inherit (sources.cbqn) version src;

  nativeBuildInputs = [
    pkg-config
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [
    fixDarwinDylibNames
  ];

  buildInputs = [
    libffi
  ];

  makeFlags = [
    "CC=${stdenv.cc.targetPrefix}cc"
  ];

  buildFlags = [
    # interpreter binary
    "o3"
    "notui=1" # display build progress in a plain-text format
    "REPLXX=${if enableReplxx then "1" else "0"}"
    "version=${sources.cbqn.version}"
  ]
  ++ lib.optionals stdenv.hostPlatform.avx2Support [
    "has=avx2"
  ]
  ++ lib.optionals enableLibcbqn [
    # embeddable interpreter as a shared lib
    "shared-o3"
  ];

  outputs = [
    "out"
  ]
  ++ lib.optionals enableLibcbqn [
    "lib"
    "dev"
  ];

  dontConfigure = true;

  doInstallCheck = true;

  strictDeps = true;

  postPatch = ''
    sed -i '/SHELL =.*/ d' makefile
    patchShebangs build/build
  '';

  preBuild = ''
    mkdir -p build/singeliLocal/
    cp -r ${sources.singeli.src}/* build/singeliLocal/
  ''
  + (
    if generateBytecode then
      ''
        mkdir -p build/bytecodeLocal/gen
        ${bqn-interpreter} ./build/genRuntime ${mbqn-source} build/bytecodeLocal/
      ''
    else
      ''
        mkdir -p build/bytecodeLocal/gen
        cp -r ${sources.cbqn-bytecode.src}/* build/bytecodeLocal/
      ''
  )
  + lib.optionalString enableReplxx ''
    mkdir -p build/replxxLocal/
    cp -r ${sources.replxx.src}/* build/replxxLocal/
  '';

  installPhase = ''
    runHook preInstall

    mkdir -p $out/bin/
    cp BQN -t $out/bin/
    # note guard condition for case-insensitive filesystems
    [ -e $out/bin/bqn ] || ln -s $out/bin/BQN $out/bin/bqn
    [ -e $out/bin/cbqn ] || ln -s $out/bin/BQN $out/bin/cbqn
  ''
  + lib.optionalString enableLibcbqn ''
    install -Dm644 include/bqnffi.h -t "$dev/include"
    install -Dm755 libcbqn${stdenv.hostPlatform.extensions.sharedLibrary} -t "$lib/lib"
  ''
  + ''
    runHook postInstall
  '';

  installCheckPhase = ''
    runHook preInstallCheck

    # main test suite from mlochbaum/BQN
    $out/bin/BQN ${mbqn-source}/test/this.bqn

    # run tests in test/cases/
    $out/bin/BQN test/run.bqn lint

    runHook postInstallCheck
  '';

  meta = {
    homepage = "https://github.com/dzaima/CBQN/";
    description = "BQN implementation in C";
    license = with lib.licenses; [
      # https://github.com/dzaima/CBQN?tab=readme-ov-file#licensing
      asl20
      boost
      gpl3Only
      lgpl3Only
      mit
      mpl20
    ];
    mainProgram = "cbqn";
    maintainers = with lib.maintainers; [
      detegr
      shnarazk
      sternenseemann
    ];
    platforms = lib.platforms.all;
  };
}