1{ config, lib, stdenv, fetchurl, fetchpatch, fetchFromGitHub, pkgs, buildPackages
2, callPackage
3, enableThreading ? true, coreutils, makeWrapper
4}:
5
6# Note: this package is used for bootstrapping fetchurl, and thus
7# cannot use fetchpatch! All mutable patches (generated by GitHub or
8# cgit) that are needed here should be included directly in Nixpkgs as
9# files.
10
11with lib;
12
13let
14
15 libc = if stdenv.cc.libc or null != null then stdenv.cc.libc else "/usr";
16 libcInc = lib.getDev libc;
17 libcLib = lib.getLib libc;
18 crossCompiling = stdenv.buildPlatform != stdenv.hostPlatform;
19
20 common = { perl, buildPerl, version, sha256 }: stdenv.mkDerivation (rec {
21 inherit version;
22
23 name = "perl-${version}";
24
25 src = fetchurl {
26 url = "mirror://cpan/src/5.0/${name}.tar.gz";
27 inherit sha256;
28 };
29
30 # TODO: Add a "dev" output containing the header files.
31 outputs = [ "out" "man" "devdoc" ] ++
32 optional crossCompiling "mini";
33 setOutputFlags = false;
34
35 disallowedReferences = [ stdenv.cc ];
36
37 patches =
38 [
39 # Do not look in /usr etc. for dependencies.
40 ./no-sys-dirs-5.31.patch
41 ]
42 ++ optional stdenv.isSunOS ./ld-shared.patch
43 ++ optionals stdenv.isDarwin [ ./cpp-precomp.patch ./sw_vers.patch ]
44 ++ optionals crossCompiling [
45 ./MakeMaker-cross.patch
46 # https://github.com/arsv/perl-cross/pull/120
47 (fetchpatch {
48 url = "https://github.com/arsv/perl-cross/commit/3c318ae6572f8b36cb077c8b49c851e2f5fe181e.patch";
49 sha256 = "0cmcy8bams3c68f6xadl52z2w378wcpdjzi3qi4pcyvcfs011l6g";
50 })
51 ];
52
53 # This is not done for native builds because pwd may need to come from
54 # bootstrap tools when building bootstrap perl.
55 postPatch = (if crossCompiling then ''
56 substituteInPlace dist/PathTools/Cwd.pm \
57 --replace "/bin/pwd" '${coreutils}/bin/pwd'
58 substituteInPlace cnf/configure_tool.sh --replace "cc -E -P" "cc -E"
59 '' else ''
60 substituteInPlace dist/PathTools/Cwd.pm \
61 --replace "/bin/pwd" "$(type -P pwd)"
62 '') +
63 # Perl's build system uses the src variable, and its value may end up in
64 # the output in some cases (when cross-compiling)
65 ''
66 unset src
67 '';
68
69 # Build a thread-safe Perl with a dynamic libperl.so. We need the
70 # "installstyle" option to ensure that modules are put under
71 # $out/lib/perl5 - this is the general default, but because $out
72 # contains the string "perl", Configure would select $out/lib.
73 # Miniperl needs -lm. perl needs -lrt.
74 configureFlags =
75 (if crossCompiling
76 then [ "-Dlibpth=\"\"" "-Dglibpth=\"\"" "-Ddefault_inc_excludes_dot" ]
77 else [ "-de" "-Dcc=cc" ])
78 ++ [
79 "-Uinstallusrbinperl"
80 "-Dinstallstyle=lib/perl5"
81 ] ++ lib.optional (!crossCompiling) "-Duseshrplib" ++ [
82 "-Dlocincpth=${libcInc}/include"
83 "-Dloclibpth=${libcLib}/lib"
84 ]
85 ++ optionals ((builtins.match ''5\.[0-9]*[13579]\..+'' version) != null) [ "-Dusedevel" "-Uversiononly" ]
86 ++ optional stdenv.isSunOS "-Dcc=gcc"
87 ++ optional enableThreading "-Dusethreads"
88 ++ optional stdenv.hostPlatform.isStatic "--all-static"
89 ++ optionals (!crossCompiling) [
90 "-Dprefix=${placeholder "out"}"
91 "-Dman1dir=${placeholder "out"}/share/man/man1"
92 "-Dman3dir=${placeholder "out"}/share/man/man3"
93 ];
94
95 configureScript = optionalString (!crossCompiling) "${stdenv.shell} ./Configure";
96
97 dontAddStaticConfigureFlags = true;
98
99 dontAddPrefix = !crossCompiling;
100
101 enableParallelBuilding = !crossCompiling;
102
103 # perl includes the build date, the uname of the build system and the
104 # username of the build user in some files.
105 # We override these to make it build deterministically.
106 # other distro solutions
107 # https://github.com/bmwiedemann/openSUSE/blob/master/packages/p/perl/perl-reproducible.patch
108 # https://github.com/archlinux/svntogit-packages/blob/packages/perl/trunk/config.over
109 # https://salsa.debian.org/perl-team/interpreter/perl/blob/debian-5.26/debian/config.over
110 # A ticket has been opened upstream to possibly clean some of this up: https://rt.perl.org/Public/Bug/Display.html?id=133452
111 preConfigure = ''
112 cat > config.over <<EOF
113 ${lib.optionalString (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isGnu) ''osvers="gnulinux"''}
114 myuname="nixpkgs"
115 myhostname="nixpkgs"
116 cf_by="nixpkgs"
117 cf_time="$(date -d "@$SOURCE_DATE_EPOCH")"
118 EOF
119 '' + optionalString stdenv.isDarwin ''
120 substituteInPlace hints/darwin.sh --replace "env MACOSX_DEPLOYMENT_TARGET=10.3" ""
121 '' + optionalString (!enableThreading) ''
122 # We need to do this because the bootstrap doesn't have a static libpthread
123 sed -i 's,\(libswanted.*\)pthread,\1,g' Configure
124 '';
125
126 # Default perl does not support --host= & co.
127 configurePlatforms = [];
128
129 setupHook = ./setup-hook.sh;
130
131 passthru = rec {
132 interpreter = "${perl}/bin/perl";
133 libPrefix = "lib/perl5/site_perl";
134 pkgs = callPackage ../../../top-level/perl-packages.nix {
135 inherit perl buildPerl;
136 overrides = config.perlPackageOverrides or (p: {}); # TODO: (self: super: {}) like in python
137 };
138 buildEnv = callPackage ./wrapper.nix {
139 inherit perl;
140 inherit (pkgs) requiredPerlModules;
141 };
142 withPackages = f: buildEnv.override { extraLibs = f pkgs; };
143 };
144
145 doCheck = false; # some tests fail, expensive
146
147 # TODO: it seems like absolute paths to some coreutils is required.
148 postInstall =
149 ''
150 # Remove dependency between "out" and "man" outputs.
151 rm "$out"/lib/perl5/*/*/.packlist
152
153 # Remove dependencies on glibc and gcc
154 sed "/ *libpth =>/c libpth => ' '," \
155 -i "$out"/lib/perl5/*/*/Config.pm
156 # TODO: removing those paths would be cleaner than overwriting with nonsense.
157 substituteInPlace "$out"/lib/perl5/*/*/Config_heavy.pl \
158 --replace "${libcInc}" /no-such-path \
159 --replace "${
160 if stdenv.hasCC then stdenv.cc.cc else "/no-such-path"
161 }" /no-such-path \
162 --replace "${stdenv.cc}" /no-such-path \
163 --replace "$man" /no-such-path
164 '' + optionalString crossCompiling
165 ''
166 mkdir -p $mini/lib/perl5/cross_perl/${version}
167 for dir in cnf/{stub,cpan}; do
168 cp -r $dir/* $mini/lib/perl5/cross_perl/${version}
169 done
170
171 mkdir -p $mini/bin
172 install -m755 miniperl $mini/bin/perl
173
174 export runtimeArch="$(ls $out/lib/perl5/site_perl/${version})"
175 # wrapProgram should use a runtime-native SHELL by default, but
176 # it actually uses a buildtime-native one. If we ever fix that,
177 # we'll need to fix this to use a buildtime-native one.
178 #
179 # Adding the arch-specific directory is morally incorrect, as
180 # miniperl can't load the native modules there. However, it can
181 # (and sometimes needs to) load and run some of the pure perl
182 # code there, so we add it anyway. When needed, stubs can be put
183 # into $mini/lib/perl5/cross_perl/${version}.
184 wrapProgram $mini/bin/perl --prefix PERL5LIB : \
185 "$mini/lib/perl5/cross_perl/${version}:$out/lib/perl5/${version}:$out/lib/perl5/${version}/$runtimeArch"
186 ''; # */
187
188 meta = {
189 homepage = "https://www.perl.org/";
190 description = "The standard implementation of the Perl 5 programmming language";
191 license = licenses.artistic1;
192 maintainers = [ maintainers.eelco ];
193 platforms = platforms.all;
194 priority = 6; # in `buildEnv' (including the one inside `perl.withPackages') the library files will have priority over files in `perl`
195 };
196 } // optionalAttrs (stdenv.buildPlatform != stdenv.hostPlatform) rec {
197 crossVersion = "393821c7cf53774233aaf130ff2c8ccec701b0a9"; # Sep 22, 2021
198
199 perl-cross-src = fetchFromGitHub {
200 name = "perl-cross-${crossVersion}";
201 owner = "arsv";
202 repo = "perl-cross";
203 rev = crossVersion;
204 sha256 = "1fn35b1773aibi2z54m0mar7114737mvfyp81wkdwhakrmzr5nv1";
205 };
206
207 depsBuildBuild = [ buildPackages.stdenv.cc makeWrapper ];
208
209 postUnpack = ''
210 unpackFile ${perl-cross-src}
211 chmod -R u+w ${perl-cross-src.name}
212 cp -R ${perl-cross-src.name}/* perl-${version}/
213 '';
214
215 configurePlatforms = [ "build" "host" "target" ];
216
217 # TODO merge setup hooks
218 setupHook = ./setup-hook-cross.sh;
219 });
220in {
221 # Maint version
222 perl532 = common {
223 perl = pkgs.perl532;
224 buildPerl = buildPackages.perl532;
225 version = "5.32.1";
226 sha256 = "0b7brakq9xs4vavhg391as50nbhzryc7fy5i65r81bnq3j897dh3";
227 };
228
229 # Maint version
230 perl534 = common {
231 perl = pkgs.perl534;
232 buildPerl = buildPackages.perl534;
233 version = "5.34.0";
234 sha256 = "16mywn5afpv1mczv9dlc1w84rbgjgrr0pyr4c0hhb2wnif0zq7jm";
235 };
236
237 # the latest Devel version
238 perldevel = common {
239 perl = pkgs.perldevel;
240 buildPerl = buildPackages.perldevel;
241 version = "5.35.4";
242 sha256 = "1ss2r0qq5li6d2qghfv1iah5nl6nraymd7b7ib1iy1395rwyhl4q";
243 };
244}