Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An OCaml cross-toolchain for Windows and several useful libraries

NotificationsYou must be signed in to change notification settings

ocaml-cross/opam-cross-windows

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository contains an up-to-date Windows toolchain featuring OCaml4.07.0,4.08.0,4.12.0,4.14.1,5.1.1 and5.3.0, as well as some commonly used packages.

The supported build systems are 32-bit and 64-bit x86 Linux andarm64 macos (Apple Silicon). The supported target systems are 32-bit and 64-bit x86 Windows.

Only 64-bit build and target are supported now on (from OCaml5.1.1.

If you need support for other platforms or versions, pleaseopen an issue.

Prerequisites

A C cross-compiler targeting the appropriate Windows platform must be installed. On Debian derivatives they are provided in thegcc-mingw-w64-x86-64 packages (gcc-mingw-w64-i686 for 32-bit x86 Windows targets). Alternatively, theMXE environment can be used.

Installation

Add this repository to OPAM:

opam repository add windows https://github.com/ocaml-cross/opam-cross-windows.git

The version of the regular compiler installed in your currentopam switch must match the version of the cross-compiler:

opam switch 5.3.0eval `opam config env`

Before 5.3.0, if desired, request the compiler to be built withflambda optimizers:

opam install conf-flambda-windows

From 5.3.0,ocaml-option-* packages are taken into account by the cross-compiler (so simply installocaml-option-flambda to getflambda).

Legagy: If you are compiling for 32bit windows on a 64bitx86_64architecture, you will also need to installocaml-option-32bit tomake sure that your ocaml compiler is configured to output 32bitbinary objects. This does not seem to be required onarm64 macos(Apple Silicon).

Install the compiler:

opam install ocaml-windows

The compiler version is selected automatically based on the current OPAM switch.

Alternatively, specify the path to the C toolchain explicitly:

TOOLPREF64=~/mxe/usr/bin/x86_64-w64-mingw32.static- opam install ocaml-windows

The options have the following meaning:

  • TOOLPREF64 specify the compiler path prefix. The tools named${TOOLPREF*}gcc,${TOOLPREF*}as,${TOOLPREF*}ar,${TOOLPREF*}ranlib and${TOOLPREF*}ld must be possible to locate viaPATH.

    The values above are suitable for use with theMXE environment located in~/mxe after runningmake gcc.

TheTOOLPREF* options are recorded inside theconf-gcc-windows* packages, so make sure to reinstall those if you wish to switch to a different toolchain. Otherwise, it is not necessary to supply them while upgrading theocaml-windows* packages.

Build some code:

echo 'let () = print_endline "Hello, world!"' >helloworld.mlocamlfind -toolchain windows ocamlc helloworld.ml -o helloworld.byteocamlfind -toolchain windows ocamlopt helloworld.ml -o helloworld.native

Run it:

wine cmd /c "set PATH=Z:/$(ocamlfind -toolchain windows printconf path)/../bin;%PATH%             && ./helloworld.byte"wine ./helloworld.native

Install some packages:

opam install re-windows

Write some code using them:

let () =  let regexp = Re_pcre.regexp {|\b([a-z]+)\b|} in  let result = Re.exec regexp "Hello, world!" in  Format.printf "match: %s\n" (Re.get result 1)

Build it:

ocamlfind -toolchain windows ocamlopt -package re.pcre -linkpkg test_pcre.ml -o test_pcre

Make an object file out of it and link it with your native project (you'll need to callcaml_startup(argv) to run OCaml code; seethis article):

ocamlfind -toolchain windows ocamlopt -package re.pcre -linkpkg -output-complete-obj test_pcre.ml -o test_pcre.o

Make a DLL out of it:

ocamlfind -toolchain windows ocamlopt -package re.pcre -linkpkg -output-obj -cclib -shared test_pcre.ml -o test_pcre.dll

With opam-windows-cross, cross-compilation is easy!

External dependencies

opam-windows-cross is designed to use native dependencies from theMXE environment. It is possible to automatically install all required dependencies for an OPAM package, e.g.camlbz2-windows, using one short command within an MXE checkout:

make `opam list --short --recursive --external --vars os-distribution=mxe --required-by=camlbz2-windows`

Porting packages

OCaml packages often have components that execute at compile-time (camlp4 or ppx syntax extensions, cstubs, OASIS, ...). Thus, it is not possible to just blanketly cross-compile every package in the OPAM repository; sometimes you would even need a cross-compiled and a non-cross-compiled package at once. The package definitions also often need package-specific modification in order to work.

As a result, if you want a package to be cross-compiled, you have to copy the definition fromopam-repository, rename the package to add-windows suffix while updating any dependencies it could have, and update the build script. Don't forget to addocaml-windows as a dependency!

Findlib 1.5.4 adds a feature that makes porting packages much simpler; namely, anOCAMLFIND_TOOLCHAIN environment variable that is equivalent to the-toolchain command-line flag. Now it is not necessary to patch the build systems of the packages to select the Windows toolchain; it is often enough to add["env" "OCAMLFIND_TOOLCHAIN=windows" make ...] to the build command in theopam file.

For projects using OASIS, the following steps will work:

build: [  ["env" "OCAMLFIND_TOOLCHAIN=windows"   "ocaml" "setup.ml" "-configure" "--prefix" "%{prefix}%/windows-sysroot"                                   "--override" "ext_dll" ".dll"]  ["env" "OCAMLFIND_TOOLCHAIN=windows"   "ocaml" "setup.ml" "-build"]]install: [  ["env" "OCAMLFIND_TOOLCHAIN=windows"   "ocaml" "setup.ml" "-install"]]remove: [["ocamlfind" "-toolchain" "windows" "remove" "pkg"]]depends: ["ocaml-windows" ...]

The output of theconfigure script will be entirely wrong, referring to the host configuration rather than target configuration. Thankfully, it is not actually used in the build process itself, so it doesn't matter.

For projects installing the files via OPAM's.install files (e.g.topkg), the following steps will work:

build: [["ocaml" "pkg/pkg.ml" "build" "--toolchain" "windows" ]]install: [["opam-installer" "--prefix=%{prefix}%/windows-sysroot" "pkg.install"]]remove: [["ocamlfind" "-toolchain" "windows" "remove" "pkg"]]depends: ["ocaml-windows" ...]

License

All files contained in this repository are licensed under theCC0 1.0 Universal license.

References

See alsoopam-cross-android andopam-cross-ios.

About

An OCaml cross-toolchain for Windows and several useful libraries

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors15

Languages


[8]ページ先頭

©2009-2025 Movatter.jp