about summary refs log tree commit diff
path: root/nixpkgs/pkgs/games/katago/default.nix
blob: fd4303eb2b1dbc372b15a84456b67ed8f52aef36 (plain) (blame)
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
{ stdenv
, gcc8Stdenv
, lib
, libzip
, boost
, cmake
, makeWrapper
, fetchFromGitHub
, fetchpatch
, cudnn ? null
, cudatoolkit ? null
, libGL_driver ? null
, opencl-headers ? null
, ocl-icd ? null
, gperftools ? null
, cudaSupport ? false
, useTcmalloc ? true}:

assert cudaSupport -> (
  libGL_driver != null && 
  cudatoolkit != null &&
  cudnn != null);

assert !cudaSupport -> (
  opencl-headers != null &&
  ocl-icd != null);

assert useTcmalloc -> (
  gperftools != null);

let
  env = if cudaSupport 
    then gcc8Stdenv
    else stdenv;

in env.mkDerivation rec {
  pname = "katago";
  version = "1.4.4";

  src = fetchFromGitHub {
    owner = "lightvector";
    repo = "katago";
    rev = "v${version}";
    sha256 = "14xs2bm8sky9cdsjdahjqs82q6blzcw05f5d9r1h171dm1hcx566";
  };

  # To workaround CMake 3.17.0's new buggy behavior wrt CUDA Compiler testing
  # See the following tracking issues:
  # KataGo:
  #  - Issue #225: https://github.com/lightvector/KataGo/issues/225
  #  - PR #227: https://github.com/lightvector/KataGo/pull/227
  # CMake:
  #  - Issue #20708: https://gitlab.kitware.com/cmake/cmake/-/issues/20708
  patches = [
    (fetchpatch {
      name = "227.patch";
      url = "https://patch-diff.githubusercontent.com/raw/lightvector/KataGo/pull/227.patch";
      sha256 = "03f1vmdjhb79mpj95sijcwla8acy32clrjgrn4xqw5h90zdgj511";
    })
  ];

  nativeBuildInputs = [
    cmake
    makeWrapper
  ];

  buildInputs = [
    libzip
    boost
  ] ++ lib.optionals cudaSupport [
    cudnn
    libGL_driver
  ] ++ lib.optionals (!cudaSupport) [
    opencl-headers
    ocl-icd
  ] ++ lib.optionals useTcmalloc [
    gperftools
  ];

  cmakeFlags = [
    "-DNO_GIT_REVISION=ON"
  ] ++ lib.optionals cudaSupport [
    "-DUSE_BACKEND=CUDA"
  ] ++ lib.optionals (!cudaSupport) [
    "-DUSE_BACKEND=OPENCL"
  ] ++ lib.optionals useTcmalloc [
    "-DUSE_TCMALLOC=ON"
  ];

  preConfigure = ''
    cd cpp/
  '' + lib.optionalString cudaSupport ''
    export CUDA_PATH="${cudatoolkit}"
    export EXTRA_LDFLAGS="-L/run/opengl-driver/lib"
  '';

  installPhase = ''
    mkdir -p $out/bin; cp katago $out/bin;
  '' + lib.optionalString cudaSupport ''
    wrapProgram $out/bin/katago \
      --prefix LD_LIBRARY_PATH : "/run/opengl-driver/lib"
  '';

  enableParallelBuilding = true;

  meta = with stdenv.lib; {
    description = "Go engine modeled after AlphaGo Zero";
    homepage    = "https://github.com/lightvector/katago";
    license     = licenses.mit;
    maintainers = [ maintainers.omnipotententity ];
    platforms   = [ "x86_64-linux" ];
  };
}