nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 minor_version,
3 major_version,
4 patch_version,
5 patches ? [ ],
6 doCheck ? true,
7 ...
8}@args:
9let
10 versionNoPatch = "${toString major_version}.${toString minor_version}";
11 version = "${versionNoPatch}.${toString patch_version}";
12 safeX11 =
13 stdenv:
14 !(stdenv.hostPlatform.isAarch32 || stdenv.hostPlatform.isMips || stdenv.hostPlatform.isStatic);
15in
16
17{
18 lib,
19 stdenv,
20 fetchurl,
21 ncurses,
22 binutils,
23 buildEnv,
24 libunwind,
25 fetchpatch,
26 libX11,
27 xorgproto,
28 useX11 ? safeX11 stdenv && lib.versionOlder version "4.09",
29 aflSupport ? false,
30 flambdaSupport ? false,
31 spaceTimeSupport ? false,
32 unsafeStringSupport ? false,
33 framePointerSupport ? false,
34 noNakedPointers ? false,
35}:
36
37assert useX11 -> safeX11 stdenv;
38assert aflSupport -> lib.versionAtLeast version "4.05";
39assert flambdaSupport -> lib.versionAtLeast version "4.03";
40assert spaceTimeSupport -> lib.versionAtLeast version "4.04" && lib.versionOlder version "4.12";
41assert unsafeStringSupport -> lib.versionAtLeast version "4.06" && lib.versionOlder version "5.0";
42assert framePointerSupport -> lib.versionAtLeast version "4.01";
43assert noNakedPointers -> lib.versionAtLeast version "4.02" && lib.versionOlder version "5.0";
44
45let
46 src =
47 args.src or (fetchurl {
48 url =
49 args.url or "http://caml.inria.fr/pub/distrib/ocaml-${versionNoPatch}/ocaml-${version}.tar.xz";
50 inherit (args) sha256;
51 });
52in
53
54let
55 useNativeCompilers = !stdenv.hostPlatform.isMips && !stdenv.hostPlatform.isLoongArch64;
56 inherit (lib)
57 optional
58 optionals
59 optionalString
60 strings
61 concatStrings
62 ;
63 pname = concatStrings [
64 "ocaml"
65 (optionalString aflSupport "+afl")
66 (optionalString spaceTimeSupport "+spacetime")
67 (optionalString flambdaSupport "+flambda")
68 (optionalString framePointerSupport "+fp")
69 ];
70in
71
72let
73 x11env = buildEnv {
74 name = "x11env";
75 paths = [
76 libX11
77 xorgproto
78 ];
79 };
80 x11lib = x11env + "/lib";
81 x11inc = x11env + "/include";
82
83 fetchpatch' = x: if builtins.isAttrs x then fetchpatch x else x;
84in
85
86stdenv.mkDerivation (
87 finalArgs:
88 args
89 // {
90
91 inherit pname version src;
92
93 patches = map fetchpatch' patches;
94
95 strictDeps = true;
96
97 prefixKey = "-prefix ";
98 configureFlags =
99 let
100 flags = new: old: if lib.versionAtLeast version "4.08" then new else old;
101 in
102 optionals useX11 (
103 flags
104 [ "--x-libraries=${x11lib}" "--x-includes=${x11inc}" ]
105 [ "-x11lib" x11lib "-x11include" x11inc ]
106 )
107 ++ optional aflSupport (flags "--with-afl" "-afl-instrument")
108 ++ optional flambdaSupport (flags "--enable-flambda" "-flambda")
109 ++ optional spaceTimeSupport (flags "--enable-spacetime" "-spacetime")
110 ++ optional framePointerSupport (flags "--enable-frame-pointers" "-with-frame-pointers")
111 ++ optionals unsafeStringSupport [
112 "--disable-force-safe-string"
113 "DEFAULT_STRING=unsafe"
114 ]
115 ++ optional (stdenv.hostPlatform.isStatic && (lib.versionOlder version "4.08")) "-no-shared-libs"
116 ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform && lib.versionOlder version "4.08") [
117 "-host ${stdenv.hostPlatform.config}"
118 "-target ${stdenv.targetPlatform.config}"
119 ]
120 ++ optional noNakedPointers (flags "--disable-naked-pointers" "-no-naked-pointers")
121 ++ optional finalArgs.doCheck "--enable-ocamltest";
122
123 dontAddStaticConfigureFlags = lib.versionOlder version "4.08";
124
125 env =
126 lib.optionalAttrs (lib.versionOlder version "4.14" || lib.versions.majorMinor version == "5.0")
127 {
128 NIX_CFLAGS_COMPILE = "-std=gnu11";
129 };
130
131 # on aarch64-darwin using --host and --target causes the build to invoke
132 # `aarch64-apple-darwin-clang` while using assembler. However, such binary
133 # does not exist. So, disable these configure flags on `aarch64-darwin`.
134 # See #144785 for details.
135 configurePlatforms =
136 lib.optionals
137 (
138 lib.versionAtLeast version "4.08"
139 && !(stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64)
140 )
141 [
142 "host"
143 "target"
144 ];
145 # x86_64-unknown-linux-musl-ld: -r and -pie may not be used together
146 hardeningDisable =
147 lib.optional (lib.versionAtLeast version "5.0" && stdenv.cc.isClang) "strictoverflow"
148 ++ lib.optionals (args ? hardeningDisable) args.hardeningDisable;
149
150 # Older versions have some race:
151 # cp: cannot stat 'boot/ocamlrun': No such file or directory
152 # make[2]: *** [Makefile:199: backup] Error 1
153 enableParallelBuilding = lib.versionAtLeast version "4.08";
154
155 # Workaround missing dependencies for install parallelism:
156 # install: target '...-ocaml-4.14.0/lib/ocaml/threads': No such file or directory
157 # make[1]: *** [Makefile:140: installopt] Error 1
158 enableParallelInstalling = false;
159
160 # Workaround lack of parallelism support among top-level targets:
161 # we place nixpkgs-specific targets to a separate file and set
162 # sequential order among them as a single rule.
163 makefile = ./Makefile.nixpkgs;
164 buildFlags =
165 if useNativeCompilers then
166 [
167 (if lib.versionOlder version "5.2" then "nixpkgs_world_bootstrap_world_opt" else "defaultentry")
168 ]
169 else
170 [ "nixpkgs_world" ];
171 buildInputs =
172 optional (lib.versionOlder version "4.07") ncurses
173 ++ optionals useX11 [
174 libX11
175 xorgproto
176 ];
177 depsBuildBuild = lib.optionals (!stdenv.hostPlatform.isDarwin) [ binutils ];
178 propagatedBuildInputs = optional spaceTimeSupport libunwind;
179 installTargets = [ "install" ] ++ optional useNativeCompilers "installopt";
180 preConfigure =
181 optionalString (lib.versionOlder version "4.04") ''
182 CAT=$(type -tp cat)
183 sed -e "s@/bin/cat@$CAT@" -i config/auto-aux/sharpbang
184 ''
185 + optionalString (stdenv.hostPlatform.isDarwin) ''
186 # Do what upstream does by default now: https://github.com/ocaml/ocaml/pull/10176
187 # This is required for aarch64-darwin, everything else works as is.
188 AS="${stdenv.cc}/bin/cc -c" ASPP="${stdenv.cc}/bin/cc -c"
189 ''
190 + optionalString (lib.versionOlder version "4.08" && stdenv.hostPlatform.isStatic) ''
191 configureFlagsArray+=("-cc" "$CC" "-as" "$AS" "-partialld" "$LD -r")
192 '';
193 postBuild = ''
194 mkdir -p $out/include
195 ln -sv $out/lib/ocaml/caml $out/include/caml
196 '';
197
198 passthru = {
199 nativeCompilers = useNativeCompilers;
200 };
201
202 checkTarget = "tests";
203 inherit doCheck;
204
205 meta = {
206 homepage = "https://ocaml.org/";
207 branch = versionNoPatch;
208 license = with lib.licenses; [
209 qpl # compiler
210 lgpl2 # library
211 ];
212 description = "OCaml is an industrial-strength programming language supporting functional, imperative and object-oriented styles";
213
214 maintainers = [ lib.maintainers.georgyo ];
215
216 longDescription = ''
217 OCaml is a general purpose programming language with an emphasis on expressiveness and safety. Developed for more than 20 years at Inria by a group of leading researchers, it has an advanced type system that helps catch your mistakes without getting in your way. It's used in environments where a single mistake can cost millions and speed matters, is supported by an active community, and has a rich set of libraries and development tools. It's widely used in teaching for its power and simplicity.
218
219 Strengths:
220 * A powerful type system, equipped with parametric polymorphism and type inference. For instance, the type of a collection can be parameterized by the type of its elements. This allows defining some operations over a collection independently of the type of its elements: sorting an array is one example. Furthermore, type inference allows defining such operations without having to explicitly provide the type of their parameters and result.
221 * User-definable algebraic data types and pattern-matching. New algebraic data types can be defined as combinations of records and sums. Functions that operate over such data structures can then be defined by pattern matching, a generalized form of the well-known switch statement, which offers a clean and elegant way of simultaneously examining and naming data.
222 * Automatic memory management, thanks to a fast, unobtrusive, incremental garbage collector.
223 * Separate compilation of standalone applications. Portable bytecode compilers allow creating stand-alone applications out of Caml Light or OCaml programs. A foreign function interface allows OCaml code to interoperate with C code when necessary. Interactive use of OCaml is also supported via a “read-evaluate-print” loop.
224
225 In addition, OCaml features:
226 * A sophisticated module system, which allows organizing modules hierarchically and parameterizing a module over a number of other modules.
227 * An expressive object-oriented layer, featuring multiple inheritance, parametric and virtual classes.
228 * Efficient native code compilers. In addition to its bytecode compiler, OCaml offers a compiler that produces efficient machine code for many architectures.
229
230 Learn more at: https://ocaml.org/learn/description.html
231 '';
232
233 platforms = with lib.platforms; linux ++ darwin;
234 broken =
235 stdenv.hostPlatform.isAarch64
236 && lib.versionOlder version (if stdenv.hostPlatform.isDarwin then "4.10" else "4.02");
237 };
238
239 }
240)