Skip to content

Commit f5a2dfe

Browse files
cristianocryyppy
andauthored
Add dune build for editor integration. (rescript-lang#5371)
* Add dune build for editor integration. This is one way to get standard ocaml editor integration work out of the box. At dev time one would do `dune build` to set up the necessary files for the editor. And repeat on file changes. Notice this does not replace the ordinary build, which does much more than build bsc.exe. * Update CONTRIBUTING.md with newest dune instructions * Add node sanity check for ninja script Co-authored-by: Patrick Ecker <[email protected]>
1 parent d509e7e commit f5a2dfe

File tree

6 files changed

+99
-19
lines changed

6 files changed

+99
-19
lines changed

CONTRIBUTING.md

+54-19
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,75 @@
11
# Contributing
22

3-
Thanks for your help! Due to ReScript's nature, the contribution setup isn't all straightforward. If something isn't working, please file an issue!
3+
Welcome to the ReScript compiler project!
4+
5+
This documet will give you guidance on how to get up and running to work on the ReScript compiler and toolchain.
6+
7+
We tried to keep the installation process as simple as possible. In case you are having issues or get stuck in the process, please let us know in the issue tracker.
8+
9+
Happy hacking!
410

511
## Prerequisites
612

7-
- [NodeJS](https://nodejs.org/)
8-
- C compiler toolchain (you probably already have it installed)
9-
- OS: Mac/Linux (ReScript works on Windows, but developing the repo using Windows isn't tested. Contribution welcome!)
13+
> We are mainly working on Apple machines, so all our instructions are currently MacOS / Linux centric. Contributions for Windows development welcome!
1014
11-
## Install native OCaml compiler
15+
- [NodeJS v16](https://nodejs.org/)
16+
- C compiler toolchain (usually installed with `xcode` on Mac)
17+
- `opam` (OCaml Package Manager)
18+
- VSCode (+ [OCaml Platform Extension](https://marketplace.visualstudio.com/items?itemName=ocamllabs.ocaml-platform))
1219

13-
If you are familiar with OCaml toolchain, you can use
20+
## Install native OCaml compiler, dune, testing utilities
1421

15-
```
16-
opam switch 4.06.1+rescript
17-
eval $(opam env)
18-
```
22+
The ReScript compiler compiles with any recent OCaml compiler. We are using `dune` as a build system for easy workflows and proper IDE support.
1923

20-
If you don't want to bother with opam, we provided a vendored compiler, you can use it directly
2124
```
22-
node ./scripts/buildocaml.js
23-
# make sure the built compiler is in your PATH, checkout native directory
25+
brew install opam
26+
opam init
27+
28+
# Install build system
29+
opam install dune
30+
31+
# Install language server for IDE support
32+
opam install ocaml-lsp-server
33+
34+
# Any recent OCaml version works as a development compiler
35+
opam switch create 4.12.1
36+
37+
# We use NodeJS to run our test suites and other utilities.
38+
npm install
2439
```
2540

41+
## Configure and Build the Compiler (`ninja` workflow)
2642

27-
## Build
43+
> Note: These instructions allow you to do full builds of the project. In case you only want to build the project for development purposes, you can use the `dune` workflow.
44+
45+
The ReScript project is built with a vendored version of `ninja`. It requires build files to correctly detect, compile and link all the OCaml files within our project. The build files are generated and managed by a NodeJS script (`./scripts/ninja.js`).
2846

2947
```sh
30-
npm install # install some JS tools for testing purposes
31-
./scripts/ninja.js config # the repo is build with Ninja. Generate the ninja build files
32-
./scripts/ninja.js build # runs `ninja` under the hood against the generated ninja build files
48+
# Generate all the necessary ninja build files
49+
./scripts/ninja.js config
50+
51+
# Run ninja to read and execute the generated build files
52+
./scripts/ninja.js build
53+
54+
# Clean (remove) all ninja build files
55+
./scripts/ninja.js clean
3356
```
3457

35-
Whenever you edit a file, run `./scripts/ninja.js build` to rebuild. Optional watcher to auto-rebuild on file changes: `node scripts/tasks.js`.
58+
Whenever you edit a file, run `./scripts/ninja.js build` to rebuild the ReScript compiler. There's also an optional watcher to auto-rebuild on file changes: `node scripts/tasks.js`.
59+
60+
## Building the Project During Development (`dune` workflow)
61+
62+
When working on a project you may want to use `dune` to compile all the files you've been working on. This is especially important for full IDE support, including auto-completion and showing compilation errors.
63+
64+
```
65+
# One off build
66+
dune build
67+
68+
# Watch mode
69+
dune build -w
70+
```
3671

37-
In the rare case there you're making changes to the vendored OCaml fork, rebuild the fork with `node scripts/buildocaml.js` then run `./scripts/ninja.js cleanbuild`. `cleanbuild` (aka `clean` + `build`) is necessary since the binary artifacts between versions of compiler may be incompatible.
72+
> Please note that `dune` will not build the final `rescript` binaries. Use the aforementioned `ninja` workflow if you want to build, test and distribute the final product.
3873
3974
### Troubleshoot Broken Build
4075

dummy.opam

Whitespace-only changes.

dune

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(dirs jscomp)

dune-project

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(lang dune 2.0)
2+
(name rescript)
3+

jscomp/dune

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(copy_files# ext/*.{ml,mli})
2+
(copy_files# ml/*.{ml,mli})
3+
(copy_files# napkin/*.{ml,mli})
4+
(copy_files# stubs/*.{ml,mli,c})
5+
(copy_files# js_parser/*.{ml,mli})
6+
(copy_files# depends/*.{ml,mli})
7+
(copy_files# common/*.{ml,mli})
8+
(copy_files# core/*.{ml,mli})
9+
(copy_files# frontend/*.{ml,mli})
10+
(copy_files# outcome_printer/*.{ml,mli})
11+
(copy_files# super_errors/*.{ml,mli})
12+
(copy_files# main/*.{ml,mli})
13+
(
14+
executable
15+
(public_name bsc.exe)
16+
17+
; The main module that will become the binary.
18+
(name Rescript_compiler_main)
19+
20+
(flags "-w" "+26+27+32+33+39")
21+
22+
; Depends on:
23+
(libraries unix str)
24+
25+
(modules_without_implementation Jscmj_main Lam_pass_unused_params Lam_runtime)
26+
(foreign_stubs
27+
(language c)
28+
(names ext_basic_hash_stubs)
29+
(flags )
30+
)
31+
)

scripts/ninja.js

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var fs = require("fs");
55
var path = require("path");
66
var cp = require("child_process");
7+
var semver = require("semver");
78

89
var jscompDir = path.join(__dirname, "..", "jscomp");
910
var runtimeDir = path.join(jscompDir, "runtime");
@@ -34,6 +35,15 @@ var my_target =
3435

3536
var vendorNinjaPath = path.join(__dirname, "..", my_target, "ninja.exe");
3637

38+
// Let's enforce a Node version >= 16 to make sure M1 users don't trip up on
39+
// cryptic issues caused by mismatching assembly architectures Node 16 ships
40+
// with a native arm64 binary, and will set process.arch to "arm64" (instead of
41+
// Rosetta emulated "x86")
42+
if(semver.lt(process.version, "16.0.0")) {
43+
console.error("Requires node version 16 or above... Abort.")
44+
process.exit(1);
45+
}
46+
3747
exports.vendorNinjaPath = vendorNinjaPath;
3848
/**
3949
* By default we use vendored,

0 commit comments

Comments
 (0)