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
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
Next Next commit
almost to jsoo
  • Loading branch information
jaredly committed Aug 21, 2018
commit 8f195cf23e16ea264de41350b8f5904ed4eb4500
187 changes: 116 additions & 71 deletions bspacks/reason_bspack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ set -e

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

echo "**This script is switching you to ocaml 4.02.3 for the subsequent bspacking. Please switch back to your own version afterward. Thanks!**\n"
opam switch 4.02.3
eval $(opam config env)
command -v dune || { echo "Dune not found. If you're using esy, do 'esy ./reason_bspack.sh'"; exit 1; }

# echo "**This script is switching you to ocaml 4.02.3 for the subsequent bspacking. Please switch back to your own version afterward. Thanks!**\n"
# opam switch 4.02.3
# eval $(opam config env)
# Because OCaml 4.02 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/src"
# ocamlMigrateParseTreeTargetDir="$THIS_SCRIPT_DIR/ocaml-migrate-parsetree/_build/default/src"
ocamlMigrateParseTreeTargetDir=`cat _get_root | bash`/_build/default/src
rm _get_root
cd -

reasonTargetDir="$THIS_SCRIPT_DIR/.."
buildDir="$THIS_SCRIPT_DIR/build"

Expand All @@ -40,70 +45,110 @@ 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
# =============
../node_modules/bs-platform/bin/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/reason-parser-tests/" \
-I "$reasonTargetDir/_build/default/src/refmt/" \
-I "$reasonTargetDir/_build/default/src/refmttype/" \
-I "$ocamlMigrateParseTreeTargetDir" \
-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)
../node_modules/bs-platform/bin/bspack.exe \
-bs-main Reason_toolchain \
-prelude-str "$resultStub" \
-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/reason-parser-tests/" \
-I "$reasonTargetDir/_build/default/src/refmt/" \
-I "$reasonTargetDir/_build/default/src/refmttype/" \
-I "$ocamlMigrateParseTreeTargetDir" \
-bs-MD \
-o "$REFMT_API.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"

# compile refmtJsApi as the final entry file
ocamlfind ocamlc -bin-annot -g -w -30 -w -40 -package js_of_ocaml,ocaml-migrate-parsetree -o "$REFMT_API_ENTRY" -I $buildDir -c -impl ./refmtJsApi.ml
# link them together into the final bytecode
ocamlfind ocamlc -linkpkg -package js_of_ocaml,ocaml-migrate-parsetree -g -o "$REFMT_API_FINAL.byte" "$REFMT_API.cmo" "$REFMT_API_ENTRY.cmo"
# # use js_of_ocaml to take the compiled bytecode and turn it into a js file
js_of_ocaml --source-map --debug-info --pretty --linkall +weak.js +toplevel.js --opt 3 --disable strict -o "$REFMT_PRE_CLOSURE.js" "$REFMT_API_FINAL.byte"
# # use closure compiler to minify the final file!
java -jar ./closure-compiler/closure-compiler-v20170910.jar --create_source_map "$REFMT_CLOSURE.map" --language_in ECMASCRIPT6 --compilation_level SIMPLE "$REFMT_PRE_CLOSURE.js" > "$REFMT_CLOSURE.js"

# small integration test to check that the process went well
# for the native binary
echo "let f = (a) => 1" | "$REFMT_BINARY.byte" --parse re --print ml
# for the js bundle
node ./testRefmtJs.js
# Get OMP source from esy
mkdir $THIS_SCRIPT_DIR/build/omp
cp $ocamlMigrateParseTreeTargetDir/*.ml $THIS_SCRIPT_DIR/build/omp
for i in $(ls build/omp/*.pp.ml); do
newname=$(basename $i | sed 's/\.pp\././')
target=${THIS_SCRIPT_DIR}/build/omp/${newname}
mv $i ${target}
done;
ocamlMigrateParseTreeTargetDir=$THIS_SCRIPT_DIR/build/omp

# Build ourselves a bspack.exe
if [ ! -f $THIS_SCRIPT_DIR/bspack.exe ]; then
echo "👉 building bspack.exe"
cd $THIS_SCRIPT_DIR/bin
ocamlopt -g -w -40-30-3 ./ext_basic_hash_stubs.c unix.cmxa ./bspack.ml -o ../bspack.exe
cd -
fi

build_js_api () {
echo "👉 bspacking the js api"
set -x
# =============
# 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" \
-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/reason-parser-tests/" \
-I "$reasonTargetDir/_build/default/src/refmt/" \
-I "$reasonTargetDir/_build/default/src/refmttype/" \
-I "$ocamlMigrateParseTreeTargetDir" \
-bs-MD \
-o "$REFMT_API.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-3 -I +compiler-libs ocamlcommon.cma "$REFMT_API.ml" -o "$REFMT_API.byte"

cd jsoo
# compile refmtJsApi as the final entry file
esy ocamlfind ocamlc -bin-annot -g -w -30-3-40 -package js_of_ocaml,ocaml-migrate-parsetree -o "$REFMT_API_ENTRY" -I $buildDir -c -impl ../refmtJsApi.ml
# link them together into the final bytecode
esy ocamlfind ocamlc -linkpkg -package js_of_ocaml,ocaml-migrate-parsetree -g -o "$REFMT_API_FINAL.byte" "$REFMT_API.cmo" "$REFMT_API_ENTRY.cmo"
# # use js_of_ocaml to take the compiled bytecode and turn it into a js file
esy js_of_ocaml --source-map --debug-info --pretty --linkall +weak.js +toplevel.js --opt 3 --disable strict -o "$REFMT_PRE_CLOSURE.js" "$REFMT_API_FINAL.byte"
cd ..

# # use closure compiler to minify the final file!
java -jar ./closure-compiler/closure-compiler-v20170910.jar --create_source_map "$REFMT_CLOSURE.map" --language_in ECMASCRIPT6 --compilation_level SIMPLE "$REFMT_PRE_CLOSURE.js" > "$REFMT_CLOSURE.js"

# for the js bundle
node ./testRefmtJs.js
echo
echo "✅ finished building refmt js api"
}

build_refmt () {
echo "👉 bspacking refmt"

# =============
# 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/reason-parser-tests/" \
-I "$reasonTargetDir/_build/default/src/refmt/" \
-I "$reasonTargetDir/_build/default/src/refmttype/" \
-I "$ocamlMigrateParseTreeTargetDir" \
-bs-MD \
-o "$REFMT_BINARY.ml"

echo "👉 compiling refmt"
# build REFMT_BINARY into an actual binary too. For testing purposes at the end
ocamlc -g -no-alias-deps -w -40-3 -I +compiler-libs ocamlcommon.cma "$REFMT_BINARY.ml" -o "$REFMT_BINARY.byte"
echo "👉 opt compiling refmt"
ocamlopt -g -no-alias-deps -w -40-3 -I +compiler-libs ocamlcommon.cmxa "$REFMT_BINARY.ml" -o "$REFMT_BINARY.exe"

# small integration test to check that the process went well
# for the native binary
echo "let f = (a) => 1" | "$REFMT_BINARY.byte" --parse re --print ml
echo "✅ finished building refmt binary"
}

# build_refmt
build_js_api