summary refs log tree commit diff
path: root/pkgs/applications/emulators/retroarch/cores.nix
blob: b553640d144e8afc26942a6dd5c97db803bfecad (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
{ lib
, stdenv
, alsa-lib
, boost
, bzip2
, cmake
, curl
, fetchFromGitHub
, ffmpeg
, fluidsynth
, gcc10Stdenv
, gettext
, hexdump
, hidapi
, icu
, libaio
, libevdev
, libGL
, libGLU
, libjpeg
, libpcap
, libpng
, libvorbis
, libxml2
, libzip
, makeWrapper
, nasm
, openssl
, pcre
, pkg-config
, portaudio
, python3
, retroarch
, SDL
, sfml
, snappy
, udev
, which
, xorg
, xxd
, xz
, zlib
}:

let
  hashesFile = builtins.fromJSON (builtins.readFile ./hashes.json);

  getCoreSrc = core:
    fetchFromGitHub (builtins.getAttr core hashesFile);

  mkLibRetroCore =
    { core
    , description
      # Check https://github.com/libretro/libretro-core-info for license information
    , license
    , stdenvOverride ? stdenv
    , src ? (getCoreSrc core)
    , broken ? false
    , version ? "unstable-2022-04-21"
    , platforms ? retroarch.meta.platforms
      # The resulting core file is based on core name
      # Setting `normalizeCore` to `true` will convert `-` to `_` on the core filename
    , normalizeCore ? true
    , ...
    }@args:
    stdenvOverride.mkDerivation (
      let
        inherit (stdenvOverride) hostPlatform;
        d2u = if normalizeCore then (lib.replaceChars [ "-" ] [ "_" ]) else (x: x);
      in
      (rec {
        pname = "libretro-${core}";
        inherit version src;

        buildInputs = [ zlib ] ++ args.extraBuildInputs or [ ];
        nativeBuildInputs = [ makeWrapper ] ++ args.extraNativeBuildInputs or [ ];

        makefile = "Makefile.libretro";
        makeFlags = [
          "platform=${{
            linux = "unix";
            darwin = "osx";
            windows = "win";
          }.${hostPlatform.parsed.kernel.name} or hostPlatform.parsed.kernel.name}"
          "ARCH=${{
            armv7l = "arm";
            armv6l = "arm";
            i686 = "x86";
          }.${hostPlatform.parsed.cpu.name} or hostPlatform.parsed.cpu.name}"
        ] ++ (args.makeFlags or [ ]);

        coreDir = "${placeholder "out"}/lib/retroarch/cores";

        installPhase = ''
          runHook preInstall

          mkdir -p $out/bin
          mkdir -p $coreDir
          mv ${d2u args.core}_libretro${hostPlatform.extensions.sharedLibrary} $coreDir
          makeWrapper ${retroarch}/bin/retroarch $out/bin/retroarch-${core} \
            --add-flags "-L $coreDir/${d2u core}_libretro${hostPlatform.extensions.sharedLibrary} $@"

          runHook postInstall
        '';

        enableParallelBuilding = true;

        passthru = {
          inherit core;
          libretroCore = "/lib/retroarch/cores";
        };

        meta = with lib; {
          inherit broken description license platforms;
          homepage = "https://www.libretro.com/";
          maintainers = with maintainers; [ edwtjo hrdinka MP2E thiagokokada ];
        };
      }) // builtins.removeAttrs args [ "core" "src" "description" "license" "makeFlags" ]
    );
in
{
  inherit mkLibRetroCore;

  atari800 = mkLibRetroCore {
    core = "atari800";
    description = "Port of Atari800 to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
    makeFlags = [ "GIT_VERSION=" ];
  };

  beetle-gba = mkLibRetroCore {
    core = "mednafen-gba";
    src = getCoreSrc "beetle-gba";
    description = "Port of Mednafen's GameBoy Advance core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-lynx = mkLibRetroCore {
    core = "mednafen-lynx";
    src = getCoreSrc "beetle-lynx";
    description = "Port of Mednafen's Lynx core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-ngp = mkLibRetroCore {
    core = "mednafen-ngp";
    src = getCoreSrc "beetle-ngp";
    description = "Port of Mednafen's NeoGeo Pocket core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-pce-fast = mkLibRetroCore {
    core = "mednafen-pce-fast";
    src = getCoreSrc "beetle-pce-fast";
    description = "Port of Mednafen's PC Engine core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-pcfx = mkLibRetroCore {
    core = "mednafen-pcfx";
    src = getCoreSrc "beetle-pcfx";
    description = "Port of Mednafen's PCFX core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-psx = mkLibRetroCore {
    core = "mednafen-psx";
    src = getCoreSrc "beetle-psx";
    description = "Port of Mednafen's PSX Engine core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
    makeFlags = [ "HAVE_HW=0" "HAVE_LIGHTREC=1" ];
  };

  beetle-psx-hw = mkLibRetroCore {
    core = "mednafen-psx-hw";
    src = getCoreSrc "beetle-psx";
    description = "Port of Mednafen's PSX Engine (with HW accel) core to libretro";
    license = lib.licenses.gpl2Only;
    extraBuildInputs = [ libGL libGLU ];
    makefile = "Makefile";
    makeFlags = [ "HAVE_VULKAN=1" "HAVE_OPENGL=1" "HAVE_HW=1" "HAVE_LIGHTREC=1" ];
  };

  beetle-saturn = mkLibRetroCore {
    core = "mednafen-saturn";
    src = getCoreSrc "beetle-saturn";
    description = "Port of Mednafen's Saturn core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
    platforms = [ "x86_64-linux" "aarch64-linux" ];
  };

  beetle-snes = mkLibRetroCore {
    core = "mednafen-snes";
    src = getCoreSrc "beetle-snes";
    description = "Port of Mednafen's SNES core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-supergrafx = mkLibRetroCore {
    core = "mednafen-supergrafx";
    src = getCoreSrc "beetle-supergrafx";
    description = "Port of Mednafen's SuperGrafx core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-vb = mkLibRetroCore {
    core = "mednafen-vb";
    src = getCoreSrc "beetle-vb";
    description = "Port of Mednafen's VirtualBoy core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  beetle-wswan = mkLibRetroCore {
    core = "mednafen-wswan";
    src = getCoreSrc "beetle-wswan";
    description = "Port of Mednafen's WonderSwan core to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  blastem = mkLibRetroCore {
    core = "blastem";
    description = "Port of BlastEm to libretro";
    license = lib.licenses.gpl3Only;
  };

  bluemsx = mkLibRetroCore {
    core = "bluemsx";
    description = "Port of BlueMSX to libretro";
    license = lib.licenses.gpl2Only;
  };

  bsnes = mkLibRetroCore {
    core = "bsnes";
    description = "Port of bsnes to libretro";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
  };

  bsnes-hd =
    let
      # linux = bsd
      # https://github.com/DerKoun/bsnes-hd/blob/f0b6cf34e9780d53516977ed2de64137a8bcc3c5/bsnes/GNUmakefile#L37
      platform = if stdenv.isDarwin then "macos" else "linux";
    in
    mkLibRetroCore {
      core = "bsnes-hd-beta";
      src = getCoreSrc "bsnes-hd";
      description = "Port of bsnes-hd to libretro";
      license = lib.licenses.gpl3Only;
      makefile = "GNUmakefile";
      makeFlags = [
        "-C"
        "bsnes"
        "target=libretro"
        "platform=${platform}"
      ];
      extraBuildInputs = [ xorg.libX11 xorg.libXext ];
      postBuild = "cd bsnes/out";
    };

  bsnes-mercury = mkLibRetroCore {
    core = "bsnes-mercury-accuracy";
    src = getCoreSrc "bsnes-mercury";
    description = "Fork of bsnes with HLE DSP emulation restored";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
    makeFlags = [ "PROFILE=accuracy" ];
  };

  bsnes-mercury-balanced = mkLibRetroCore {
    core = "bsnes-mercury-balanced";
    src = getCoreSrc "bsnes-mercury";
    description = "Fork of bsnes with HLE DSP emulation restored";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
    makeFlags = [ "PROFILE=balanced" ];
  };

  bsnes-mercury-performance = mkLibRetroCore {
    core = "bsnes-mercury-performance";
    src = getCoreSrc "bsnes-mercury";
    description = "Fork of bsnes with HLE DSP emulation restored";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
    makeFlags = [ "PROFILE=performance" ];
  };

  citra = mkLibRetroCore {
    core = "citra";
    description = "Port of Citra to libretro";
    stdenvOverride = gcc10Stdenv;
    license = lib.licenses.gpl2Plus;
    extraBuildInputs = [ libGLU libGL boost ffmpeg nasm ];
    makefile = "Makefile";
    makeFlags = [ "HAVE_FFMPEG_STATIC=0" ];
  };

  desmume = mkLibRetroCore {
    core = "desmume";
    description = "libretro wrapper for desmume NDS emulator";
    license = lib.licenses.gpl2Plus;
    extraBuildInputs = [ libpcap libGLU libGL xorg.libX11 ];
    preBuild = "cd desmume/src/frontend/libretro";
    makeFlags = lib.optional stdenv.hostPlatform.isAarch32 "platform=armv-unix"
      ++ lib.optional (!stdenv.hostPlatform.isx86) "DESMUME_JIT=0";
  };

  desmume2015 = mkLibRetroCore {
    core = "desmume2015";
    description = "libretro wrapper for desmume NDS emulator from 2015";
    license = lib.licenses.gpl2Plus;
    extraBuildInputs = [ libpcap libGLU libGL xorg.libX11 ];
    makeFlags = lib.optional stdenv.hostPlatform.isAarch32 "platform=armv-unix"
      ++ lib.optional (!stdenv.hostPlatform.isx86) "DESMUME_JIT=0";
    preBuild = "cd desmume";
  };

  dolphin = mkLibRetroCore {
    core = "dolphin";
    description = "Port of Dolphin to libretro";
    license = lib.licenses.gpl2Plus;

    extraNativeBuildInputs = [ cmake curl pkg-config ];
    extraBuildInputs = [
      libGLU
      libGL
      pcre
      sfml
      gettext
      hidapi
      libevdev
      udev
    ] ++ (with xorg; [ libSM libX11 libXi libpthreadstubs libxcb xcbutil libXext libXrandr libXinerama libXxf86vm ]);
    makefile = "Makefile";
    cmakeFlags = [
      "-DLIBRETRO=ON"
      "-DLIBRETRO_STATIC=1"
      "-DENABLE_QT=OFF"
      "-DENABLE_LTO=OFF"
      "-DUSE_UPNP=OFF"
      "-DUSE_DISCORD_PRESENCE=OFF"
    ];
    dontUseCmakeBuildDir = true;
  };

  dosbox = mkLibRetroCore {
    core = "dosbox";
    description = "Port of DOSBox to libretro";
    license = lib.licenses.gpl2Only;
    stdenvOverride = gcc10Stdenv;
  };

  eightyone = mkLibRetroCore {
    core = "81";
    src = getCoreSrc "eightyone";
    description = "Port of EightyOne to libretro";
    license = lib.licenses.gpl3Only;
  };

  fbalpha2012 = mkLibRetroCore {
    core = "fbalpha2012";
    description = "Port of Final Burn Alpha ~2012 to libretro";
    license = "Non-commercial";
    makefile = "makefile.libretro";
    preBuild = "cd svn-current/trunk";
  };

  fbneo = mkLibRetroCore {
    core = "fbneo";
    description = "Port of FBNeo to libretro";
    license = "Non-commercial";
    makefile = "Makefile";
    preBuild = "cd src/burner/libretro";
  };

  fceumm = mkLibRetroCore {
    core = "fceumm";
    description = "FCEUmm libretro port";
    license = lib.licenses.gpl2Only;
  };

  flycast = mkLibRetroCore {
    core = "flycast";
    description = "Flycast libretro port";
    license = lib.licenses.gpl2Only;
    extraBuildInputs = [ libGL libGLU ];
    makefile = "Makefile";
    makeFlags = lib.optional stdenv.hostPlatform.isAarch64 [ "platform=arm64" ];
    platforms = [ "aarch64-linux" "x86_64-linux" ];
  };

  fmsx = mkLibRetroCore {
    core = "fmsx";
    description = "FMSX libretro port";
    license = "Non-commercial";
    makefile = "Makefile";
  };

  freeintv = mkLibRetroCore {
    core = "freeintv";
    description = "FreeIntv libretro port";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
  };

  gambatte = mkLibRetroCore {
    core = "gambatte";
    description = "Gambatte libretro port";
    license = lib.licenses.gpl2Only;
  };

  genesis-plus-gx = mkLibRetroCore {
    core = "genesis-plus-gx";
    description = "Enhanced Genesis Plus libretro port";
    license = "Non-commercial";
  };

  gpsp = mkLibRetroCore {
    core = "gpsp";
    description = "Port of gpSP to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  gw = mkLibRetroCore {
    core = "gw";
    description = "Port of Game and Watch to libretro";
    license = lib.licenses.zlib;
    makefile = "Makefile";
  };

  handy = mkLibRetroCore {
    core = "handy";
    description = "Port of Handy to libretro";
    license = lib.licenses.zlib;
    makefile = "Makefile";
  };

  hatari = mkLibRetroCore {
    core = "hatari";
    description = "Port of Hatari to libretro";
    license = lib.licenses.gpl2Only;
    extraBuildInputs = [ SDL zlib ];
    extraNativeBuildInputs = [ which ];
    dontConfigure = true;
    makeFlags = [ "EXTERNAL_ZLIB=1" ];
  };

  mame = mkLibRetroCore {
    core = "mame";
    description = "Port of MAME to libretro";
    license = with lib.licenses; [ bsd3 gpl2Plus ];
    extraBuildInputs = [ alsa-lib libGLU libGL portaudio python3 xorg.libX11 ];
  };

  mame2000 = mkLibRetroCore {
    core = "mame2000";
    description = "Port of MAME ~2000 to libretro";
    license = "MAME";
    makefile = "Makefile";
    makeFlags = lib.optional (!stdenv.hostPlatform.isx86) "IS_X86=0";
    enableParallelBuilding = false;
  };

  mame2003 = mkLibRetroCore {
    core = "mame2003";
    description = "Port of MAME ~2003 to libretro";
    license = "MAME";
    makefile = "Makefile";
    enableParallelBuilding = false;
  };

  mame2003-plus = mkLibRetroCore {
    core = "mame2003-plus";
    description = "Port of MAME ~2003+ to libretro";
    license = "MAME";
    makefile = "Makefile";
    enableParallelBuilding = false;
  };

  mame2010 = mkLibRetroCore {
    core = "mame2010";
    description = "Port of MAME ~2010 to libretro";
    license = "MAME";
    makefile = "Makefile";
    makeFlags = lib.optionals stdenv.hostPlatform.isAarch64 [ "PTR64=1" "ARM_ENABLED=1" "X86_SH2DRC=0" "FORCE_DRC_C_BACKEND=1" ];
    enableParallelBuilding = false;
  };

  mame2015 = mkLibRetroCore {
    core = "mame2015";
    description = "Port of MAME ~2015 to libretro";
    license = "MAME";
    makeFlags = [ "PYTHON=python3" ];
    extraNativeBuildInputs = [ python3 ];
    extraBuildInputs = [ alsa-lib ];
    makefile = "Makefile";
    enableParallelBuilding = false;
  };

  mame2016 = mkLibRetroCore {
    core = "mame2016";
    description = "Port of MAME ~2016 to libretro";
    license = with lib.licenses; [ bsd3 gpl2Plus ];
    extraNativeBuildInputs = [ python3 ];
    extraBuildInputs = [ alsa-lib ];
    makeFlags = [ "PYTHON_EXECUTABLE=python3" ];
    postPatch = ''
      # Prevent the failure during the parallel building of:
      # make -C 3rdparty/genie/build/gmake.linux -f genie.make obj/Release/src/host/lua-5.3.0/src/lgc.o
      mkdir -p 3rdparty/genie/build/gmake.linux/obj/Release/src/host/lua-5.3.0/src
    '';
    enableParallelBuilding = false;
  };

  melonds = mkLibRetroCore {
    core = "melonds";
    description = "Port of MelonDS to libretro";
    license = lib.licenses.gpl3Only;
    extraBuildInputs = [ libGL libGLU ];
    makefile = "Makefile";
  };

  mesen = mkLibRetroCore {
    core = "mesen";
    description = "Port of Mesen to libretro";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
    preBuild = "cd Libretro";
  };

  mesen-s = mkLibRetroCore {
    core = "mesen-s";
    description = "Port of Mesen-S to libretro";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
    preBuild = "cd Libretro";
    normalizeCore = false;
  };

  meteor = mkLibRetroCore {
    core = "meteor";
    description = "Port of Meteor to libretro";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
    preBuild = "cd libretro";
  };

  mgba = mkLibRetroCore {
    core = "mgba";
    description = "Port of mGBA to libretro";
    license = lib.licenses.mpl20;
  };

  mupen64plus = mkLibRetroCore {
    core = "mupen64plus-next";
    src = getCoreSrc "mupen64plus";
    description = "Libretro port of Mupen64 Plus, GL only";
    license = lib.licenses.gpl3Only;
    extraBuildInputs = [ libGLU libGL libpng nasm xorg.libX11 ];
    makefile = "Makefile";
  };

  neocd = mkLibRetroCore {
    core = "neocd";
    description = "NeoCD libretro port";
    license = lib.licenses.lgpl3Only;
    makefile = "Makefile";
  };

  nestopia = mkLibRetroCore {
    core = "nestopia";
    description = "Nestopia libretro port";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
    preBuild = "cd libretro";
  };

  np2kai = mkLibRetroCore rec {
    core = "np2kai";
    src = getCoreSrc core;
    description = "Neko Project II kai libretro port";
    license = lib.licenses.mit;
    makeFlags = [
      # See https://github.com/AZO234/NP2kai/tags
      "NP2KAI_VERSION=rev.22"
      "NP2KAI_HASH=${src.rev}"
    ];
    preBuild = "cd sdl";
  };

  o2em = mkLibRetroCore {
    core = "o2em";
    description = "Port of O2EM to libretro";
    license = lib.licenses.artistic1;
    makefile = "Makefile";
  };

  opera = mkLibRetroCore {
    core = "opera";
    description = "Opera is a port of 4DO/libfreedo to libretro";
    license = "Non-commercial";
    makefile = "Makefile";
    makeFlags = [ "CC_PREFIX=${stdenv.cc.targetPrefix}" ];
  };

  parallel-n64 = mkLibRetroCore {
    core = "parallel-n64";
    description = "Parallel Mupen64plus rewrite for libretro.";
    license = lib.licenses.gpl3Only;
    extraBuildInputs = [ libGLU libGL libpng ];
    makefile = "Makefile";
    postPatch = lib.optionalString stdenv.hostPlatform.isAarch64 ''
      sed -i -e '1 i\CPUFLAGS += -DARM_FIX -DNO_ASM -DARM_ASM -DDONT_WANT_ARM_OPTIMIZATIONS -DARM64' Makefile \
      && sed -i -e 's,CPUFLAGS  :=,,g' Makefile
    '';
  };

  pcsx2 = mkLibRetroCore {
    core = "pcsx2";
    description = "Port of PCSX2 to libretro";
    license = lib.licenses.gpl3Plus;
    extraNativeBuildInputs = [
      cmake
      gettext
      pkg-config
    ];
    extraBuildInputs = [
      libaio
      libGL
      libGLU
      libpcap
      libpng
      libxml2
      xz
      xxd
    ];
    makefile = "Makefile";
    cmakeFlags = [
      "-DLIBRETRO=ON"
    ];
    postPatch = ''
      # remove ccache
      substituteInPlace CMakeLists.txt --replace "ccache" ""
    '';
    postBuild = "cd /build/source/build/pcsx2";
    platforms = lib.platforms.x86;
  };

  pcsx_rearmed = mkLibRetroCore {
    core = "pcsx_rearmed";
    description = "Port of PCSX ReARMed with GNU lightning to libretro";
    license = lib.licenses.gpl2Only;
    dontConfigure = true;
  };

  picodrive = mkLibRetroCore {
    core = "picodrive";
    description = "Fast MegaDrive/MegaCD/32X emulator";
    license = "MAME";

    extraBuildInputs = [ libpng SDL ];
    SDL_CONFIG = "${SDL.dev}/bin/sdl-config";
    dontAddPrefix = true;
    configurePlatforms = [ ];
    makeFlags = lib.optional stdenv.hostPlatform.isAarch64 [ "platform=aarch64" ];
  };

  play = mkLibRetroCore {
    core = "play";
    description = "Port of Play! to libretro";
    license = lib.licenses.bsd2;
    extraBuildInputs = [ boost bzip2 curl openssl icu libGL libGLU xorg.libX11 ];
    extraNativeBuildInputs = [ cmake ];
    makefile = "Makefile";
    cmakeFlags = [ "-DBUILD_PLAY=OFF" "-DBUILD_LIBRETRO_CORE=ON" ];
    postBuild = "cd Source/ui_libretro";
  };

  ppsspp = mkLibRetroCore {
    core = "ppsspp";
    description = "ppsspp libretro port";
    license = lib.licenses.gpl2Plus;
    extraNativeBuildInputs = [ cmake pkg-config python3 ];
    extraBuildInputs = [ libGLU libGL libzip ffmpeg snappy xorg.libX11 ];
    makefile = "Makefile";
    cmakeFlags = [
      "-DLIBRETRO=ON"
      "-DUSE_SYSTEM_FFMPEG=ON"
      "-DUSE_SYSTEM_SNAPPY=ON"
      "-DUSE_SYSTEM_LIBZIP=ON"
      "-DOpenGL_GL_PREFERENCE=GLVND"
    ];
    postBuild = "cd lib";
  };

  prboom = mkLibRetroCore {
    core = "prboom";
    description = "Prboom libretro port";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  prosystem = mkLibRetroCore {
    core = "prosystem";
    description = "Port of ProSystem to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  quicknes = mkLibRetroCore {
    core = "quicknes";
    description = "QuickNES libretro port";
    license = lib.licenses.lgpl21Plus;
    makefile = "Makefile";
  };

  sameboy = mkLibRetroCore {
    core = "sameboy";
    description = "SameBoy libretro port";
    license = lib.licenses.mit;
    extraNativeBuildInputs = [ which hexdump ];
    preBuild = "cd libretro";
    makefile = "Makefile";
  };

  scummvm = mkLibRetroCore {
    core = "scummvm";
    description = "Libretro port of ScummVM";
    license = lib.licenses.gpl2Only;
    extraBuildInputs = [ fluidsynth libjpeg libvorbis libGLU libGL SDL ];
    makefile = "Makefile";
    preConfigure = "cd backends/platform/libretro/build";
  };

  smsplus-gx = mkLibRetroCore {
    core = "smsplus";
    src = getCoreSrc "smsplus-gx";
    description = "SMS Plus GX libretro port";
    license = lib.licenses.gpl2Plus;
  };

  snes9x = mkLibRetroCore {
    core = "snes9x";
    description = "Port of SNES9x git to libretro";
    license = "Non-commercial";
    makefile = "Makefile";
    preBuild = "cd libretro";
  };

  snes9x2002 = mkLibRetroCore {
    core = "snes9x2002";
    description = "Optimized port/rewrite of SNES9x 1.39 to Libretro";
    license = "Non-commercial";
    makefile = "Makefile";
  };

  snes9x2005 = mkLibRetroCore {
    core = "snes9x2005";
    description = "Optimized port/rewrite of SNES9x 1.43 to Libretro";
    license = "Non-commercial";
    makefile = "Makefile";
  };

  snes9x2005-plus = mkLibRetroCore {
    core = "snes9x2005-plus";
    src = getCoreSrc "snes9x2005";
    description = "Optimized port/rewrite of SNES9x 1.43 to Libretro, with Blargg's APU";
    license = "Non-commercial";
    makefile = "Makefile";
    makeFlags = [ "USE_BLARGG_APU=1" ];
  };

  snes9x2010 = mkLibRetroCore {
    core = "snes9x2010";
    description = "Optimized port/rewrite of SNES9x 1.52+ to Libretro";
    license = "Non-commercial";
  };

  stella = mkLibRetroCore {
    core = "stella";
    description = "Port of Stella to libretro";
    license = lib.licenses.gpl2Only;
    extraBuildInputs = [ libpng pkg-config SDL ];
    makefile = "Makefile";
    preBuild = "cd src/libretro";
    dontConfigure = true;
  };

  stella2014 = mkLibRetroCore {
    core = "stella2014";
    description = "Port of Stella to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  swanstation = mkLibRetroCore {
    core = "swanstation";
    description = "Port of SwanStation (a fork of DuckStation) to libretro";
    license = lib.licenses.gpl3Only;
    extraNativeBuildInputs = [ cmake ];
    makefile = "Makefile";
    cmakeFlags = [
      "-DBUILD_LIBRETRO_CORE=ON"
    ];
  };

  tgbdual = mkLibRetroCore {
    core = "tgbdual";
    description = "Port of TGBDual to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
  };

  thepowdertoy = mkLibRetroCore {
    core = "thepowdertoy";
    description = "Port of The Powder Toy to libretro";
    license = lib.licenses.gpl3Only;
    extraNativeBuildInputs = [ cmake ];
    makefile = "Makefile";
    postBuild = "cd src";
  };

  tic80 = mkLibRetroCore {
    core = "tic80";
    description = "Port of TIC-80 to libretro";
    license = lib.licenses.mit;
    extraNativeBuildInputs = [ cmake pkg-config libGL libGLU ];
    makefile = "Makefile";
    cmakeFlags = [
      "-DBUILD_LIBRETRO=ON"
      "-DBUILD_DEMO_CARTS=OFF"
      "-DBUILD_PRO=OFF"
      "-DBUILD_PLAYER=OFF"
      "-DBUILD_SDL=OFF"
      "-DBUILD_SOKOL=OFF"
    ];
    preConfigure = "cd core";
    postBuild = "cd lib";
  };

  vba-m = mkLibRetroCore {
    core = "vbam";
    src = getCoreSrc "vba-m";
    description = "vanilla VBA-M libretro port";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
    preBuild = "cd src/libretro";
  };

  vba-next = mkLibRetroCore {
    core = "vba-next";
    description = "VBA-M libretro port with modifications for speed";
    license = lib.licenses.gpl2Only;
  };

  vecx = mkLibRetroCore {
    core = "vecx";
    description = "Port of Vecx to libretro";
    license = lib.licenses.gpl3Only;
    extraBuildInputs = [ libGL libGLU ];
  };

  virtualjaguar = mkLibRetroCore {
    core = "virtualjaguar";
    description = "Port of VirtualJaguar to libretro";
    license = lib.licenses.gpl3Only;
    makefile = "Makefile";
  };

  yabause = mkLibRetroCore {
    core = "yabause";
    description = "Port of Yabause to libretro";
    license = lib.licenses.gpl2Only;
    makefile = "Makefile";
    # Disable SSE for non-x86. DYNAREC doesn't build on aarch64.
    makeFlags = lib.optional (!stdenv.hostPlatform.isx86) "HAVE_SSE=0";
    preBuild = "cd yabause/src/libretro";
  };
}