1{
2 lib,
3 stdenv,
4 fetchurl,
5 gawk,
6 fetchpatch,
7 undmg,
8 cpio,
9 xar,
10 libiconv,
11}:
12
13let
14 startFPC = import ./binary.nix {
15 inherit
16 stdenv
17 fetchurl
18 undmg
19 cpio
20 xar
21 lib
22 ;
23 };
24in
25
26stdenv.mkDerivation rec {
27 version = "3.2.2";
28 pname = "fpc";
29
30 src = fetchurl {
31 url = "mirror://sourceforge/freepascal/fpcbuild-${version}.tar.gz";
32 sha256 = "85ef993043bb83f999e2212f1bca766eb71f6f973d362e2290475dbaaf50161f";
33 };
34
35 buildInputs = [
36 startFPC
37 gawk
38 ];
39
40 glibc = stdenv.cc.libc.out;
41
42 # Patch paths for linux systems. Other platforms will need their own patches.
43 patches = [
44 ./mark-paths.patch # mark paths for later substitution in postPatch
45 ]
46 ++ lib.optional stdenv.hostPlatform.isAarch64 (fetchpatch {
47 # backport upstream patch for aarch64 glibc 2.34
48 url = "https://gitlab.com/freepascal.org/fpc/source/-/commit/a20a7e3497bccf3415bf47ccc55f133eb9d6d6a0.patch";
49 hash = "sha256-xKTBwuOxOwX9KCazQbBNLhMXCqkuJgIFvlXewHY63GM=";
50 stripLen = 1;
51 extraPrefix = "fpcsrc/";
52 });
53
54 postPatch = ''
55 # substitute the markers set by the mark-paths patch
56 substituteInPlace fpcsrc/compiler/systems/t_linux.pas --subst-var-by dynlinker-prefix "${glibc}"
57 substituteInPlace fpcsrc/compiler/systems/t_linux.pas --subst-var-by syslibpath "${glibc}/lib"
58
59 substituteInPlace fpcsrc/compiler/systems/t_darwin.pas \
60 --replace-fail "LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib',true)" "LibrarySearchPath.AddLibraryPath(sysrootpath,'$SDKROOT/usr/lib',true)"
61
62 # Replace the `codesign --remove-signature` command with a custom script, since `codesign` is not available
63 # in nixpkgs
64 # Remove the -no_uuid strip flag which does not work on llvm-strip, only
65 # Apple strip.
66 substituteInPlace fpcsrc/compiler/Makefile \
67 --replace \
68 "\$(CODESIGN) --remove-signature" \
69 "${./remove-signature.sh}" \
70 --replace "ifneq (\$(CODESIGN),)" "ifeq (\$(OS_TARGET), darwin)" \
71 --replace "-no_uuid" ""
72 '';
73
74 preConfigure = lib.optionalString stdenv.hostPlatform.isDarwin ''
75 NIX_LDFLAGS="-syslibroot $SDKROOT -L${lib.getLib libiconv}/lib"
76 '';
77
78 makeFlags = [
79 "NOGDB=1"
80 "FPC=${startFPC}/bin/fpc"
81 ];
82
83 # disabled by default in fpcsrc/compiler/llvm/agllvm.pas
84 hardeningDisable = [ "pie" ];
85
86 installFlags = [ "INSTALL_PREFIX=\${out}" ];
87
88 postInstall = ''
89 for i in $out/lib/fpc/*/ppc*; do
90 ln -fs $i $out/bin/$(basename $i)
91 done
92 mkdir -p $out/lib/fpc/etc/
93 $out/lib/fpc/*/samplecfg $out/lib/fpc/${version} $out/lib/fpc/etc/
94
95 # Generate config files in /etc since on darwin, ppc* does not follow symlinks
96 # to resolve the location of /etc
97 mkdir -p $out/etc
98 $out/lib/fpc/*/samplecfg $out/lib/fpc/${version} $out/etc
99 '';
100
101 passthru = {
102 bootstrap = startFPC;
103 };
104
105 meta = with lib; {
106 description = "Free Pascal Compiler from a source distribution";
107 homepage = "https://www.freepascal.org";
108 maintainers = [ maintainers.raskin ];
109 license = with licenses; [
110 gpl2
111 lgpl2
112 ];
113 platforms = platforms.unix;
114 # See:
115 # * <https://gitlab.com/freepascal.org/fpc/source/-/issues/41045>
116 # * <https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/887>
117 broken = stdenv.cc.isClang && stdenv.hostPlatform.isx86_64;
118 };
119}