Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[internal] get the reason bspack build script working with esy #2153

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8f195cf
almost to jsoo
jaredly Aug 21, 2018
e660347
gettin it workin
jaredly Aug 21, 2018
1675c55
working with esy
jaredly Aug 21, 2018
79fa2cc
ignore
jaredly Aug 21, 2018
a9925c5
cleanup
jaredly Aug 21, 2018
d118601
more cleanup
jaredly Aug 21, 2018
b9f17a8
more cleanup
jaredly Aug 21, 2018
d4461ad
version default
jaredly Aug 21, 2018
9677c52
locks
jaredly Aug 21, 2018
5eb1090
vendor bspack
jaredly Aug 21, 2018
e876003
missed that too
jaredly Aug 22, 2018
a912b5e
cleanup
jaredly Aug 22, 2018
4c709b3
gnu sed
jaredly Aug 22, 2018
1aca9a2
fix version number
jaredly Sep 12, 2018
22a520c
Merge branch 'master' into joo. Conflicts: bspacks/reason_bspack.sh
jchavarri Nov 24, 2019
23d08c1
Add ppx_derivers to bspacks deps
jchavarri Nov 25, 2019
28e4c96
Fix paths to esy sandbox
jchavarri Nov 25, 2019
1d05e43
Native build passing
jchavarri Nov 25, 2019
7b335f1
Fix refmt_js api
jchavarri Nov 26, 2019
d667cb8
Merge pull request #2 from jchavarri/joo
jaredly Nov 27, 2019
e92ca99
quiet esy.lock
jaredly Nov 28, 2019
9c0248d
get bspack.ml from github
jchavarri Nov 28, 2019
74e623a
actually remove bspack and fix paths
jchavarri Nov 28, 2019
20a8f01
Merge pull request #3 from jchavarri/joo
jaredly Nov 30, 2019
6a8a441
Merge branch 'master' into joo
jaredly Dec 2, 2019
a56fc71
igonre output directory
jaredly Dec 2, 2019
5b79c17
use sandboxes! yay
jaredly Dec 2, 2019
b5d3aef
rm old
jaredly Dec 2, 2019
458baa1
Merge branch 'master' into joo
jaredly Dec 12, 2019
fd11d9d
simplify
jaredly Dec 12, 2019
654f582
generated
jaredly Dec 12, 2019
dbd1034
undo that
jaredly Dec 12, 2019
e3eec8a
do the prerelease thing
jaredly Dec 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'master' into joo
  • Loading branch information
jaredly committed Dec 2, 2019
commit 6a8a441aa0639f8e0740d9f7acac0ae109ad4837
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
common_cache_key: &common_cache_key
key: dependency-cache-{{ checksum "../ocaml-version" }}-{{ checksum ".circleci/config.yml" }}-{{ checksum "reason.opam" }}-{{ checksum "rtop.opam" }}
key: dependency-cache-{{ .Branch }}-{{ checksum "../ocaml-version" }}-{{ checksum ".circleci/config.yml" }}-{{ checksum "reason.opam" }}-{{ checksum "rtop.opam" }}

esy_cache_key: &esy_cache_key
key: esy-cache-{{ checksum "esy.json" }}-{{ checksum "esy.lock/index.json" }}-{{ checksum ".circleci/config.yml" }}
Expand Down
52 changes: 48 additions & 4 deletions bspacks/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
This subdirectory is used for packing up the entire Reason `refmt` into a single file through BuckleScript's [bspack](https://github.com/bloomberg/bucklescript/blob/master/jscomp/core/bspack_main.ml), thus discarding all intermediate steps needed to build Reason, except for the final `refmt` binary compilation.
This subdirectory is used for packing up the entire Reason `refmt` into a
single file through BuckleScript's
[bspack](https://github.com/bloomberg/bucklescript/blob/master/jscomp/core/bspack_main.ml),
thus discarding all intermediate steps needed to build Reason, except for the
final `refmt` binary compilation.

This makes our installation much friendlier to e.g. Windows. BuckleScript currently includes the three bundles in its own repo, thus making Reason first-class (Btw, BS also uses a few other pieces of code from Reason, in its vendor/reason folder and jscomp/reason_outcome_printer).
This makes our installation much friendlier to e.g. Windows. BuckleScript
currently includes the three bundles in its own repo, thus making Reason
first-class (Btw, BS also uses a few other pieces of code from Reason, in its
vendor/reason folder and jscomp/reason_outcome_printer).

## Build
## Build (4.06 / BuckleScript v6 and above)

This whole process needs to happen with OCaml 4.02.3, so make sure you switch to that version first:
We use this workflow for building `build/refmt_api.ml` and
`build/refmt_binary.ml` so we can easily vendor Reason for newer BuckleScript
releases. More details on that are in the [BuckleScript
CONTRIBUTING](https://github.com/BuckleScript/CONTRIBUTING.md) file.

For inlining the right version number in the bundle, the script uses
`../reason.json` as the source of truth.

**Note:** Currently you will need to build BuckleScript yourself to get access
to the `bspack.exe` executable. Also we skip the building of the `refmt.js`
artifact entirely here. Will maybe added back later as soon as we need it!

**Instructions:**

```
cd bspacks

opam switch 4.06.1

./downloadSomeDependencies.sh

# Point to your locally built bspack.exe
BSPACK_EXE=~/Projects/bucklescript/jscomp/bin/bspack.exe ./reason_bspack406.sh
```

The bspacked files are also compiled to make sure that the bundle actually
compiles. You should then find all the relevant `.ml` files in the `./build`
directory, ready to be copied over to BuckleScript.

## Legacy Build (4.02 / BuckleScript v5 and below)

> This is an old workflow which also seem to be broken since `reerror` was
> merged. We discourage the use, unless you really need to build bspacked
> bundles for 4.02 based BuckleScript versions

This whole process needs to happen with OCaml 4.02.3, so make sure you switch
to that version first:

```sh
# Then switch to the right ocaml version and install all deps
opam switch 4.02.3
```

Expand Down
6 changes: 6 additions & 0 deletions bspacks/reason_bspack.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Legacy Note:
# This script is used for older 4.02 based bspacking processes.
#
# Use reason_bspack406.sh for bspacking refmt_api and refmt_binary for
# BuckleScript v6 and above (OCaml 4.06 based)!

# exit if anything goes wrong
set -e

Expand Down
141 changes: 141 additions & 0 deletions bspacks/reason_bspack406.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# NOTE:
# --------
# This script is derived from the original `./reason_bspack.sh`
# which is based on the 4.02 based BuckleScript bspack version. The script also
# does way more than this file, such as creating an closure-optimized refmt.js
# file.
#
# What this file is about:
# -----
# For BuckleScript v6 and above (based on 4.06),
# we want to be able to build the `refmt_api` and `refmt_binary` build
# artifacts by leveraging an up to date bspack version.
#
# bspack itself is not vendored in bs-platform anymore, so the user of this
# script has to provide the bspack binary themselves (most likely as a local
# build from the bucklescript repo)
#
# We use the env variable BSPACK_EXE to populate the bspack path.
#
# Example Usage:
# -------
# cd bspacks/
# BSPACK_EXE=~/Projects/bucklescript/jscomp/bin/bspack.exe bash reason_bspack406.sh

# exit if anything goes wrong
set -xeo pipefail

# this script does 2 independent things:
# - pack the whole repo into a single refmt file for vendoring into bucklescript
# - generate the js version of refmt for web usage

THIS_SCRIPT_DIR="$(cd "$( dirname "$0" )" && pwd)"

# Automatically read the version from reason.json, so that dune builds the right Package.ml file (version, git_version, etc.)
export version=$(cat ../reason.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g')

echo "**This script is switching you to ocaml 4.06.1 for the subsequent bspacking. Please switch back to your own version afterward. Thanks!**\n"
opam switch 4.06.1
eval $(opam config env)


if [ -z ${BSPACK_EXE+x} ]; then
echo "Missing env variable 'BSPACK_EXE'"
echo "Example Usage:"
echo "BSPACK_EXE=~/bucklescript/jscomp/bin/bspack.exe bash reason_bspack406.sh"
exit 1
fi

echo "Using bspack located at '${BSPACK_EXE}'..."

# Because OCaml 4.06 doesn't come with the `Result` module, it also needed stubbing out.
resultStub="module Result = struct type ('a, 'b) result = Ok of 'a | Error of 'b end open Result"

menhirSuggestedLib=`menhir --suggest-menhirLib`

# generated from the script ./downloadSomeDependencies.sh
ocamlMigrateParseTreeTargetDir="$THIS_SCRIPT_DIR/ocaml-migrate-parsetree/_build/default"
reasonTargetDir="$THIS_SCRIPT_DIR/.."
buildDir="$THIS_SCRIPT_DIR/build"

REFMT_BINARY="$buildDir/refmt_binary"
REFMT_API="$buildDir/refmt_api"
REFMT_API_ENTRY="$buildDir/refmt_api_entry"
REFMT_API_FINAL="$buildDir/refmt_api_final"
REFMT_PRE_CLOSURE="$buildDir/refmt_pre_closure"

REFMT_CLOSURE="$reasonTargetDir/refmt"

# clean some artifacts
rm -f "$REFMT_CLOSURE.*"
rm -rf $buildDir
mkdir $buildDir

pushd $THIS_SCRIPT_DIR
# rebuild the project in case it was stale
make clean -C ../
make pre_release -C ../
make build -C ../

# =============
# last step for the first task , we're done generating the single file that'll
# be coped over to bucklescript. On BS' side, it'll use a single compile command
# to turn this into a binary, like in
# https://github.com/BuckleScript/bucklescript/blob/2ad2310f18567aa13030cdf32adb007d297ee717/jscomp/bin/Makefile#L29
# =============
$BSPACK_EXE \
-main-export Refmt_impl \
-prelude-str "$resultStub" \
-I "$menhirSuggestedLib" \
-I "$reasonTargetDir" \
-I "$reasonTargetDir/_build/default/src/ppx/" \
-I "$reasonTargetDir/_build/default/src/reason-merlin/" \
-I "$reasonTargetDir/_build/default/src/reason-parser/" \
-I "$reasonTargetDir/_build/default/src/reason-parser/vendor/easy_format/" \
-I "$reasonTargetDir/_build/default/src/reason-parser/vendor/cmdliner/" \
-I "$reasonTargetDir/_build/default/src/refmt/" \
-I "$reasonTargetDir/_build/default/src/refmttype/" \
-I "$ocamlMigrateParseTreeTargetDir/src" \
-I "$ocamlMigrateParseTreeTargetDir/ppx_derivers/src" \
-bs-MD \
-o "$REFMT_BINARY.ml"

# =============
# Now, second task. Packing the repo again but with a new entry file, for JS
# consumption
# =============

# this one is left here as an intermediate file for the subsequent steps. We
# disregard the usual entry point that is refmt_impl above (which takes care of
# terminal flags parsing, etc.) and swap it with a new entry point, refmtJsApi (see below)
$BSPACK_EXE \
-bs-main Reason_toolchain \
-prelude-str "$resultStub" \
-prelude "$reasonTargetDir/_build/default/src/refmt/package.ml" \
-I "$menhirSuggestedLib" \
-I "$reasonTargetDir/_build/default/src/ppx/" \
-I "$reasonTargetDir/_build/default/src/reason-merlin/" \
-I "$reasonTargetDir/_build/default/src/reason-parser/" \
-I "$reasonTargetDir/_build/default/src/reason-parser/vendor/easy_format/" \
-I "$reasonTargetDir/_build/default/src/reason-parser/vendor/cmdliner/" \
-I "$reasonTargetDir/_build/default/src/refmt/" \
-I "$reasonTargetDir/_build/default/src/refmttype/" \
-I "$ocamlMigrateParseTreeTargetDir/src" \
-I "$ocamlMigrateParseTreeTargetDir/ppx_derivers/src" \
-bs-MD \
-o "$REFMT_API.ml"


# This hack is required since the emitted code by bspack somehow adds
sed -i'.bak' -e 's/Migrate_parsetree__Ast_404/Migrate_parsetree.Ast_404/' build/*.ml

# the `-no-alias-deps` flag is important. Not sure why...
# remove warning 40 caused by ocaml-migrate-parsetree
ocamlc -g -no-alias-deps -w -40 -I +compiler-libs ocamlcommon.cma "$REFMT_API.ml" -o "$REFMT_API.byte"

# build REFMT_BINARY into an actual binary too. For testing purposes at the end
ocamlc -g -no-alias-deps -w -40 -I +compiler-libs ocamlcommon.cma "$REFMT_BINARY.ml" -o "$REFMT_BINARY.byte"
5 changes: 5 additions & 0 deletions bspacks/refmtJsApi.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
(*
* Note: This file is currently broken, since Reason removed
* Refmt_api.Reason_syntax_util.Error in favor of Reerror's `Printexc.to_string e`
*)

module RE = Refmt_api.Reason_toolchain.RE
module ML = Refmt_api.Reason_toolchain.ML

Expand Down
8 changes: 8 additions & 0 deletions formatTest/unit_tests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,11 @@ let v =
<A someProp={<> {React.string("Hello")} </>} />;

<A someProp=?{<> {React.string("Hi")} </>} />;

<ActionButton
one={!validated} two={msg##errorText}>
<InactionText
three={msg##prop}
four={msg##errorText}
/>
</ActionButton>;
73 changes: 73 additions & 0 deletions formatTest/unit_tests/expected_output/ocaml_identifiers.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Type names (supported with PR#2342) */
module T = {
type pub_ = unit;
};

/* Value names (already supported) */
module V = {
let method = ();
};

/* Record fields */
module R = {
type r = {method: int};

let foo = {method: 4};
};

/* Class names and instance variables */
module C = {
class pub_ = {
as _;
};

class c = {
as _;
val pub_ = 0;
pub method = () => ();
};

class c' = {
as _;
inherit class method;
val! pub_ = 1;
};
};

/* Class types */
module Ct = {
class type method = {
val method: unit => unit;
};
};

/* Virtual */
module Cv = {
class virtual method = {
as _;
};
};

/* Object methods */
module O = {
let o = {as _; pub method = ()};
};

/* Function parameter labels */
module L = {
let f = (~method) => ignore(method);
};

/* Module types */
module type method = {};

/* Polymorphic variants (probably ok as-is?) */
module P = {
type t = [ | `pub_ | `method];
};

type method = string;

[@some_attr: type_]
[@other_attr: method]
type foo = {method};
Empty file.
8 changes: 8 additions & 0 deletions formatTest/unit_tests/input/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,11 @@ let v =
<A someProp={ <> {React.string("Hello")} </> } />;

<A someProp=?{ <> {React.string("Hi")} </> } />;

<ActionButton
one={!validated}
two={
msg##errorText;
}>
<InactionText three={msg##prop} four={msg##errorText} />
</ActionButton>;
68 changes: 68 additions & 0 deletions formatTest/unit_tests/input/ocaml_identifiers.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(* Type names (supported with PR#2342) *)
module T = struct
type pub = unit
end

(* Value names (already supported) *)
module V = struct
let method_ = ()
end

(* Record fields *)
module R = struct
type r = { method_ : int }

let foo = { method_ = 4 }
end

(* Class names and instance variables *)
module C = struct
class pub = object end

class c = object
val pub = 0
method method_ () = ()
end

class c' = object
inherit method_
val! pub = 1
end
end

(* Class types *)
module Ct = struct
class type method_ = object
val method_: unit -> unit
end
end

(* Virtual *)
module Cv = struct
class virtual method_ = object end
end

(* Object methods *)
module O = struct
let o = object method method_ = () end
end

(* Function parameter labels *)
module L = struct
let f ~method_ = ignore method_
end

(* Module types *)
module type method_ = sig
end

(* Polymorphic variants (probably ok as-is?) *)
module P = struct
type t = [ `pub | `method_ ]
end

type method_ = string

type foo = {method_: method_}
[@@some_attr: type_]
[@@other_attr: method_]
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.