summaryrefslogtreecommitdiff
path: root/.nix/adaptive-cpp.nix
blob: 2e631e1d7796f7765abe05e23ecf258deb5f6ee8 (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
{ lib
, stdenv
, fetchFromGitHub
, cmake
, makeWrapper
, boost
, llvmPackages
, lld
, libffi
, libxml2
, python3
, config ? {
    rocm = false;
  }
, rocmPackages
, spirv-tools
, spirv-headers
, spirv-llvm-translator
}:

let
	version = "25.02.0";
in stdenv.mkDerivation ({
	pname = "adaptive-cpp-kel-custom";
	inherit version;

	src = fetchFromGitHub {
		owner = "AdaptiveCpp";
		repo = "AdaptiveCpp";
		tag = "v${version}";
		sha256 = "sha256-vXfw8+xn3/DYxUKp3QGdQ8sEbDwyk+8jDCyuvQOXigc=";
	};

	nativeBuildInputs = [
    cmake
    makeWrapper
	];

	buildInputs = [
		boost
		llvmPackages.openmp
		llvmPackages.libclang
		llvmPackages.llvm
		python3
	] ++ lib.optionals config.rocm [
		rocmPackages.clr
		rocmPackages.rocm-runtime
		libffi
		libxml2
		spirv-tools
		spirv-headers
		spirv-llvm-translator
	];

  cmakeFlags = [
    "-DCLANG_INCLUDE_PATH=${llvmPackages.libclang.dev}/include"
  ] ++ lib.optionals config.rocm [
    "-DWITH_ROCM_BACKEND=ON"

    # Keep OpenCL and Level Zero off. src/CMakeLists.txt turns WITH_LLVM_TO_SPIRV
    # on for either of them, and that target FetchContent-clones
    # SPIRV-LLVM-Translator at configure time, which a nix build cannot do --
    # it has neither git nor network. ROCm is the backend we want here and it
    # does not go through SPIR-V.
    #
    # This only bites when rocm = true: rocmPackages.clr brings OpenCL in, so
    # the backend autodetects on, where in the base build it stays off.
    "-DWITH_OPENCL_BACKEND=OFF"
    "-DWITH_LEVEL_ZERO_BACKEND=OFF"
  ];

  # nixpkgs 26.05 turns the zerocallusedregs hardening flag on by default, and
  # clang rejects -fzero-call-used-regs for the amdgcn-amd-amdhsa target, so
  # every device bitcode object under src/libkernel/sscp/amdgpu fails to build.
  # Not an AdaptiveCpp problem -- the same source built fine on 25.11.

  postInstall = ''
    if [ -d "$out/include/AdaptiveCpp" ]; then
      mv $out/include/AdaptiveCpp .hidden-fake
      mv .hidden-fake/* $out/include/
      rmdir .hidden-fake
    fi
  '';

  postFixup = ''
    wrapProgram $out/bin/syclcc-clang \
      --prefix PATH : ${lib.makeBinPath [ lld python3 ]} \
      --add-flags "-L${llvmPackages.openmp}/lib" \
      --add-flags "-I${llvmPackages.openmp.dev}/include" \
  '' + lib.optionalString config.rocm ''
    --add-flags "--rocm-device-lib-path=${rocmPackages.rocm-device-libs}/amdgcn/bitcode"
  '';

  postPatch = ''
    patchShebangs .
  '';
} // lib.optionalAttrs config.rocm {
  # nixpkgs 26.05 turns the zerocallusedregs hardening flag on by default, and
  # clang rejects -fzero-call-used-regs for the amdgcn-amd-amdhsa target, so
  # every device bitcode object under src/libkernel/sscp/amdgpu fails to build.
  # Not an AdaptiveCpp problem -- the same source built fine on 25.11.
  #
  # Set via optionalAttrs rather than lib.optionals so the attribute is absent
  # (not just empty) without rocm, which keeps the base derivation hash stable.
  hardeningDisable = [ "zerocallusedregs" ];
})