1{ lib
2, stdenv
3, fetchurl
4, fetchpatch
5, cmake
6, nasm
7
8 # NUMA support enabled by default on NUMA platforms:
9, numaSupport ? (stdenv.hostPlatform.isLinux && (stdenv.hostPlatform.isx86 || stdenv.hostPlatform.isAarch64))
10, numactl
11
12 # Multi bit-depth support (8bit+10bit+12bit):
13, multibitdepthSupport ? (stdenv.is64bit && !(stdenv.isAarch64 && stdenv.isLinux))
14
15 # Other options:
16, cliSupport ? true # Build standalone CLI application
17, custatsSupport ? false # Internal profiling of encoder work
18, debugSupport ? false # Run-time sanity checks (debugging)
19, ppaSupport ? false # PPA profiling instrumentation
20, unittestsSupport ? stdenv.isx86_64 # Unit tests - only testing x64 assembly
21, vtuneSupport ? false # Vtune profiling instrumentation
22, werrorSupport ? false # Warnings as errors
23}:
24
25let
26 mkFlag = optSet: flag: if optSet then "-D${flag}=ON" else "-D${flag}=OFF";
27
28 isCross = stdenv.buildPlatform != stdenv.hostPlatform;
29in
30
31stdenv.mkDerivation rec {
32 pname = "x265";
33 version = "3.5";
34
35 outputs = [ "out" "dev" ];
36
37 # Check that x265Version.txt contains the expected version number
38 # whether we fetch a source tarball or a tag from the git repo
39 src = fetchurl {
40 url = "https://bitbucket.org/multicoreware/x265_git/downloads/x265_${version}.tar.gz";
41 hash = "sha256-5wozNcrKy7oLOiDsb+zWeDkyKI68gWOtdLzJYGR3yug=";
42 };
43
44 sourceRoot = "x265_${version}/source";
45
46 patches = [
47 # More aliases for ARM platforms + do not force CLFAGS for ARM :
48 (fetchpatch {
49 url = "https://gitweb.gentoo.org/repo/gentoo.git/plain/media-libs/x265/files/arm-r1.patch?id=1d1de341e1404a46b15ae3e84bc400d474cf1a2c";
50 sha256 = "1hgzq5vxkwh0nyikxjfz8gz3jvx2nq3yy12mz3fn13qvzdlb5ilp";
51 })
52 # use proper check to avoid undefined symbols when enabling assembly on ARM :
53 (fetchpatch {
54 url = "https://gitweb.gentoo.org/repo/gentoo.git/plain/media-libs/x265/files/neon.patch?id=1d1de341e1404a46b15ae3e84bc400d474cf1a2c";
55 sha256 = "1mmshpbyldrfqxfmdajqal4l647zvlrwdai8pxw99qg4v8gajfii";
56 })
57 # More complete PPC64 matches :
58 (fetchpatch {
59 url = "https://gitweb.gentoo.org/repo/gentoo.git/plain/media-libs/x265/files/x265-3.3-ppc64.patch?id=1d1de341e1404a46b15ae3e84bc400d474cf1a2c";
60 sha256 = "1mvw678xfm0vr59n5jilq56qzcgk1gmcip2afyafkqiv21nbms8c";
61 })
62 # Namespace functions for multi-bitdepth builds so that libraries are self-contained (and tests succeeds) :
63 (fetchpatch {
64 url = "https://gitweb.gentoo.org/repo/gentoo.git/plain/media-libs/x265/files/test-ns.patch?id=1d1de341e1404a46b15ae3e84bc400d474cf1a2c";
65 sha256 = "0zg3g53l07yh7ar5c241x50y5zp7g8nh8rh63ad4bdpchpc2f52d";
66 })
67 # Fix detection of NEON (and armv6 build) :
68 ./fix-neon-detection.patch
69 ];
70
71 postPatch = ''
72 substituteInPlace cmake/Version.cmake \
73 --replace "unknown" "${version}" \
74 --replace "0.0" "${version}"
75 ''
76 # There is broken and complicated logic when setting X265_LATEST_TAG for
77 # mingwW64 builds. This bypasses the logic by setting it at the end of the
78 # file
79 + lib.optionalString stdenv.hostPlatform.isMinGW ''
80 echo 'set(X265_LATEST_TAG "${version}")' >> ./cmake/Version.cmake
81 '';
82
83 nativeBuildInputs = [ cmake nasm ] ++ lib.optionals (numaSupport) [ numactl ];
84
85 cmakeFlags = [
86 "-Wno-dev"
87 (mkFlag custatsSupport "DETAILED_CU_STATS")
88 (mkFlag debugSupport "CHECKED_BUILD")
89 (mkFlag ppaSupport "ENABLE_PPA")
90 (mkFlag vtuneSupport "ENABLE_VTUNE")
91 (mkFlag werrorSupport "WARNINGS_AS_ERRORS")
92 ];
93
94 cmakeStaticLibFlags = [
95 "-DHIGH_BIT_DEPTH=ON"
96 "-DENABLE_CLI=OFF"
97 "-DENABLE_SHARED=OFF"
98 "-DEXPORT_C_API=OFF"
99 ] ++ lib.optionals stdenv.hostPlatform.isPower [
100 "-DENABLE_ALTIVEC=OFF" # https://bitbucket.org/multicoreware/x265_git/issues/320/fail-to-build-on-power8-le
101 ];
102
103 preConfigure = lib.optionalString multibitdepthSupport ''
104 cmake -B build-10bits $cmakeFlags "''${cmakeFlagsArray[@]}" $cmakeStaticLibFlags
105 cmake -B build-12bits $cmakeFlags "''${cmakeFlagsArray[@]}" $cmakeStaticLibFlags -DMAIN12=ON
106 cmakeFlagsArray+=(
107 -DEXTRA_LIB="x265-10.a;x265-12.a"
108 -DEXTRA_LINK_FLAGS=-L.
109 -DLINKED_10BIT=ON
110 -DLINKED_12BIT=ON
111 )
112 '' + ''
113 cmakeFlagsArray+=(
114 -DGIT_ARCHETYPE=1 # https://bugs.gentoo.org/814116
115 ${mkFlag (!stdenv.hostPlatform.isStatic) "ENABLE_SHARED"}
116 -DHIGH_BIT_DEPTH=OFF
117 -DENABLE_HDR10_PLUS=ON
118 ${mkFlag (isCross && stdenv.hostPlatform.isAarch) "CROSS_COMPILE_ARM"}
119 ${mkFlag cliSupport "ENABLE_CLI"}
120 ${mkFlag unittestsSupport "ENABLE_TESTS"}
121 )
122 '';
123
124 # Builds 10bits and 12bits static libs on the side if multi bit-depth is wanted
125 # (we are in x265_<version>/source/build)
126 preBuild = lib.optionalString multibitdepthSupport ''
127 make -C ../build-10bits -j $NIX_BUILD_CORES
128 make -C ../build-12bits -j $NIX_BUILD_CORES
129 ln -s ../build-10bits/libx265.a ./libx265-10.a
130 ln -s ../build-12bits/libx265.a ./libx265-12.a
131 '';
132
133 doCheck = unittestsSupport;
134 checkPhase = ''
135 runHook preCheck
136 ./test/TestBench
137 runHook postCheck
138 '';
139
140 postInstall = ''
141 rm -f ${placeholder "out"}/lib/*.a
142 ''
143 # For mingw, libs are located in $out/bin not $out/lib
144 + lib.optionalString stdenv.hostPlatform.isMinGW ''
145 ln -s $out/bin/*.dll $out/lib
146 '';
147
148 meta = with lib; {
149 description = "Library for encoding H.265/HEVC video streams";
150 mainProgram = "x265";
151 homepage = "https://www.x265.org/";
152 changelog = "https://x265.readthedocs.io/en/master/releasenotes.html#version-${lib.strings.replaceStrings ["."] ["-"] version}";
153 license = licenses.gpl2Plus;
154 maintainers = with maintainers; [ codyopel ];
155 platforms = platforms.all;
156 };
157}