Skip to content

Commit f623362

Browse files
committed
1. bug fix for bin/bsb, and use our own clean strategy instead of relying on ninja 2. now allows user to choose which module system they want
1 parent b0a4948 commit f623362

File tree

5 files changed

+325
-207
lines changed

5 files changed

+325
-207
lines changed

bin/bsb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (fs.existsSync(bsconfig)) {
1414
try {
1515
child_process.execFileSync(bsb_exe, delegate_args, { stdio: 'inherit' })
1616
} catch (e) {
17-
console.error('Error happend when running command', bsb_exe, 'with args', delete_args)
17+
console.error('Error happend when running command', bsb_exe, 'with args', delegate_args)
1818
}
1919
} else {
2020
var path = require('path')
@@ -32,7 +32,7 @@ if (fs.existsSync(bsconfig)) {
3232
try {
3333
child_process.execFileSync(bsb_exe, delegate_args, { stdio: 'inherit', cwd: search_dir })
3434
} catch (e) {
35-
console.error('Error happend when running command', bsb_exe, 'with args', delete_args)
35+
console.error('Error happend when running command', bsb_exe, 'with args', delegate_args)
3636
}
3737
}
3838
}

jscomp/bin/bsb.ml

Lines changed: 161 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7926,6 +7926,10 @@ val run_commands : command list -> unit
79267926
val run_command_execv : bool -> command -> unit
79277927

79287928
(* val run_command_execvp : command -> unit *)
7929+
7930+
val remove_dirs_recursive : string -> string array -> unit
7931+
7932+
val remove_dir_recursive : string -> unit
79297933
end = struct
79307934
#1 "bsb_unix.ml"
79317935
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -8041,12 +8045,40 @@ let run_command_execv fail_exit cmd =
80418045
prerr_endline ("* Failure : " ^ cmd.cmd ^ "\n* Location: " ^ cmd.cwd);
80428046
if fail_exit then exit eid
80438047
end;
8044-
8048+
80458049
| Unix.WSIGNALED _ | Unix.WSTOPPED _ ->
80468050
begin
80478051
prerr_endline (cmd.cmd ^ " interrupted");
80488052
exit 2
80498053
end
8054+
8055+
(** it assume you have permissions, so always catch it to fail
8056+
gracefully
8057+
*)
8058+
let rec remove_dirs_recursive cwd roots =
8059+
Array.iter
8060+
(fun root ->
8061+
let cur = Filename.concat cwd root in
8062+
if Sys.is_directory cur then
8063+
begin
8064+
remove_dirs_recursive cur (Sys.readdir cur);
8065+
Unix.rmdir cur ;
8066+
end
8067+
else
8068+
Sys.remove cur
8069+
)
8070+
roots
8071+
8072+
let rec remove_dir_recursive dir =
8073+
if Sys.is_directory dir then
8074+
begin
8075+
let files = Sys.readdir dir in
8076+
for i = 0 to Array.length files - 1 do
8077+
remove_dir_recursive (Filename.concat dir (Array.unsafe_get files i))
8078+
done ;
8079+
Unix.rmdir dir
8080+
end
8081+
else Sys.remove dir
80508082
(*
80518083
let () =
80528084
run_commands
@@ -8148,7 +8180,7 @@ let revise_merlin new_content =
81488180
let write_ninja_file bsc_dir cwd =
81498181
let builddir = Bsb_config.lib_bs in
81508182
let () = Bsb_build_util.mkp builddir in
8151-
let bsc, bsdep, bsppx =
8183+
let bsc, bsdep, bsppx =
81528184
bsc_dir // "bsc.exe",
81538185
bsc_dir // "bsb_helper.exe",
81548186
bsc_dir // "bsppx.exe" in
@@ -8173,7 +8205,7 @@ let write_ninja_file bsc_dir cwd =
81738205
S %s\n\
81748206
B %s\n\
81758207
FLG -ppx %s\n\
8176-
" lib_ocaml_dir lib_ocaml_dir bsppx
8208+
" lib_ocaml_dir lib_ocaml_dir bsppx
81778209
) in
81788210
let () =
81798211
match Bsb_default.get_bsc_flags () with
@@ -8185,7 +8217,7 @@ let write_ninja_file bsc_dir cwd =
81858217
Bsb_default.get_bs_dependencies ()
81868218
|> List.iter (fun package ->
81878219
let path = (Bsb_default.resolve_bsb_magic_file ~cwd ~desc:"dependecies"
8188-
(package ^ "/")// "lib"//"ocaml") in
8220+
(package ^ "/")// "lib"//"ocaml") in
81898221
Buffer.add_string buffer "\nS ";
81908222
Buffer.add_string buffer path ;
81918223
Buffer.add_string buffer "\nB ";
@@ -8303,6 +8335,7 @@ let cwd = Sys.getcwd ()
83038335

83048336

83058337
let watch () =
8338+
print_endline "\nStart Watching now ";
83068339
let bsb_watcher =
83078340
Bsb_build_util.get_bsc_dir cwd // "bsb_watcher.js" in
83088341
let bsb_watcher =
@@ -8321,36 +8354,54 @@ let separator = "--"
83218354

83228355
let internal_package_specs = "-internal-package-specs"
83238356
let build_bs_deps package_specs =
8324-
let bsc_dir = Bsb_build_util.get_bsc_dir cwd in
8325-
let bsb_exe = bsc_dir // "bsb.exe" in
8326-
Bsb_default.walk_all_deps true cwd
8357+
let bsc_dir = Bsb_build_util.get_bsc_dir cwd in
8358+
let bsb_exe = bsc_dir // "bsb.exe" in
8359+
Bsb_default.walk_all_deps true cwd
83278360
(fun top cwd ->
8328-
if not top then
8329-
Bsb_unix.run_command_execv true
8330-
{cmd = bsb_exe; cwd = cwd; args =
8331-
[| bsb_exe ; no_dev; internal_package_specs; package_specs; regen; separator |]})
8361+
if not top then
8362+
Bsb_unix.run_command_execv true
8363+
{cmd = bsb_exe; cwd = cwd; args =
8364+
[| bsb_exe ; no_dev; internal_package_specs; package_specs; regen; separator |]})
83328365

8333-
let clean_bs_deps () =
8334-
let bsc_dir = Bsb_build_util.get_bsc_dir cwd in
8335-
let bsb_exe = bsc_dir // "bsb.exe" in
8336-
Bsb_default.walk_all_deps true cwd
8337-
(fun top cwd -> Bsb_unix.run_command_execv (not top)
8338-
{cmd = bsb_exe; cwd = cwd; args = [| bsb_exe ; separator; "-t" ; "clean"|]})
83398366
let annoymous filename =
83408367
String_vec.push filename targets
83418368

83428369
let watch_mode = ref false
83438370
let make_world = ref false
83448371

8345-
8372+
let lib_bs = "lib" // "bs"
8373+
let lib_amdjs = "lib" // "amdjs"
8374+
let lib_goog = "lib" // "goog"
8375+
let lib_js = "lib" // "js"
8376+
8377+
let clean_bs_garbage cwd =
8378+
print_string "Doing cleaning in ";
8379+
print_endline cwd;
8380+
let aux x =
8381+
let x = (cwd // x) in
8382+
if Sys.file_exists x then
8383+
Bsb_unix.remove_dir_recursive x in
8384+
try
8385+
aux lib_bs ;
8386+
aux lib_amdjs ;
8387+
aux lib_goog;
8388+
aux lib_js
8389+
with
8390+
e ->
8391+
prerr_endline ("Failed to clean due to " ^ Printexc.to_string e)
8392+
8393+
let clean_bs_deps () =
8394+
Bsb_default.walk_all_deps true cwd (fun top cwd ->
8395+
clean_bs_garbage cwd
8396+
)
83468397
let bsb_main_flags =
83478398
[
83488399
"-w", Arg.Set watch_mode,
83498400
" Watch mode" ;
83508401
no_dev, Arg.Set Bsb_config.no_dev,
83518402
" (internal)Build dev dependencies in make-world and dev group(in combination with -regen)";
83528403
regen, Arg.Set force_regenerate,
8353-
" Always regenerate build.ninja no matter bsconfig.json is changed or not (for debugging purpose)"
8404+
" Always regenerate build.ninja no matter bsconfig.json is changed or not (for debugging purpose)"
83548405
;
83558406
internal_package_specs, Arg.String Bsb_default.internal_override_package_specs,
83568407
" (internal)Overide package specs (in combination with -regen)";
@@ -8361,7 +8412,7 @@ let bsb_main_flags =
83618412
]
83628413

83638414
(** Regenerate ninja file and return None if we dont need regenerate
8364-
otherwise return some info
8415+
otherwise return some info
83658416
*)
83668417
let regenerate_ninja cwd bsc_dir forced : Bsb_default.package_specs option =
83678418
let output_deps = Bsb_config.lib_bs // bsdeps in
@@ -8388,31 +8439,43 @@ let regenerate_ninja cwd bsc_dir forced : Bsb_default.package_specs option =
83888439
else None
83898440

83908441
let ninja_error_message = "ninja (required for bsb build system) is not installed, \n\
8391-
please visit https://github.com/ninja-build/ninja to have it installed\n"
8442+
please visit https://github.com/ninja-build/ninja to have it installed\n"
83928443
let () =
83938444
Printexc.register_printer (function
8394-
| Unix.Unix_error(Unix.ENOENT, "execvp", "ninja") ->
8395-
Some ninja_error_message
8396-
| _ -> None
8397-
)
8445+
| Unix.Unix_error(Unix.ENOENT, "execvp", "ninja") ->
8446+
Some ninja_error_message
8447+
| _ -> None
8448+
)
8449+
8450+
let print_string_args (args : string array) =
8451+
for i = 0 to Array.length args - 1 do
8452+
print_string (Array.unsafe_get args i) ;
8453+
print_string " ";
8454+
done ;
8455+
print_newline ()
83988456

8399-
84008457
(* Note that [keepdepfile] only makes sense when combined with [deps] for optimizatoin *)
84018458
let ninja_command ninja ninja_args =
84028459
let ninja_args_len = Array.length ninja_args in
84038460
if ninja_args_len = 0 then
8404-
Unix.execvp ninja [|"ninja"; "-C"; Bsb_config.lib_bs |]
8461+
begin
8462+
let args = [|"ninja"; "-C"; Bsb_config.lib_bs |] in
8463+
print_string_args args ;
8464+
Unix.execvp ninja args
8465+
end
84058466
else
84068467
let fixed_args_length = 3 in
8407-
begin Unix.execvp ninja
8408-
(Array.init (fixed_args_length + ninja_args_len)
8409-
(fun i -> match i with
8410-
| 0 -> "ninja"
8411-
| 1 -> "-C"
8412-
| 2 -> Bsb_config.lib_bs
8413-
| _ -> Array.unsafe_get ninja_args (i - fixed_args_length) ))
8414-
end
8415-
8468+
let args = (Array.init (fixed_args_length + ninja_args_len)
8469+
(fun i -> match i with
8470+
| 0 -> "ninja"
8471+
| 1 -> "-C"
8472+
| 2 -> Bsb_config.lib_bs
8473+
| _ -> Array.unsafe_get ninja_args (i - fixed_args_length) )) in
8474+
print_string_args args ;
8475+
Unix.execvp ninja args
8476+
8477+
8478+
84168479
(**
84178480
Cache files generated:
84188481
- .bsdircache in project root dir
@@ -8427,80 +8490,76 @@ let usage = "Usage : bsb.exe <bsb-options> <files> -- <ninja_options>\n\
84278490
It is always recommended to run ninja via bsb.exe \n\
84288491
Bsb options are:"
84298492

8493+
8494+
(*
8495+
let bsb_exe = bsc_dir // "bsb.exe" in
8496+
Bsb_default.walk_all_deps true cwd
8497+
(fun top cwd -> Bsb_unix.run_command_execv (not top)
8498+
{cmd = bsb_exe; cwd = cwd; args = [| bsb_exe ; separator; "-t" ; "clean"|]})
8499+
*)
8500+
let make_world_deps deps =
8501+
print_endline "\nMaking the dependency world!";
8502+
let deps =
8503+
match deps with
8504+
| None ->
8505+
let json = Ext_json.parse_json_from_file Literals.bsconfig_json in
8506+
begin match json with
8507+
| `Obj map ->
8508+
map
8509+
|? (Bsb_build_schemas.package_specs,
8510+
`Arr Bsb_default.set_package_specs_from_array)
8511+
|> ignore ;
8512+
Bsb_default.get_package_specs ()
8513+
| _ -> assert false
8514+
end
8515+
| Some spec -> spec in
8516+
build_bs_deps ( String_set.fold
8517+
(fun k acc -> k ^ "," ^ acc ) deps Ext_string.empty )
84308518
let () =
84318519
let bsc_dir = Bsb_build_util.get_bsc_dir cwd in
84328520
let ninja =
84338521
if Sys.win32 then
84348522
bsc_dir // "ninja.exe"
84358523
else
84368524
"ninja"
8437-
in
8525+
in
84388526
(* try *)
8439-
(* see discussion #929 *)
8440-
if Array.length Sys.argv <= 1 then
8441-
begin
8442-
ignore (regenerate_ninja cwd bsc_dir false);
8443-
ninja_command ninja [||]
8444-
end
8445-
else
8446-
begin
8447-
match Ext_array.find_and_split Sys.argv Ext_string.equal "--" with
8448-
| `No_split
8449-
->
8450-
begin
8451-
Arg.parse bsb_main_flags annoymous usage;
8527+
(* see discussion #929 *)
8528+
if Array.length Sys.argv <= 1 then
8529+
begin
8530+
ignore (regenerate_ninja cwd bsc_dir false);
8531+
ninja_command ninja [||]
8532+
end
8533+
else
8534+
begin
8535+
match Ext_array.find_and_split Sys.argv Ext_string.equal "--" with
8536+
| `No_split
8537+
->
8538+
begin
8539+
Arg.parse bsb_main_flags annoymous usage;
8540+
(* [-make-world] should never be combined with [-package-specs] *)
8541+
if !make_world then
8542+
(* don't regenerate files when we only run [bsb -clean-world] *)
84528543
let deps = regenerate_ninja cwd bsc_dir !force_regenerate in
8453-
(* [-make-world] should never be combined with [-package-specs] *)
8454-
if !make_world then
8455-
let deps =
8456-
match deps with
8457-
| None ->
8458-
let json = Ext_json.parse_json_from_file Literals.bsconfig_json in
8459-
begin match json with
8460-
| `Obj map ->
8461-
map
8462-
|? (Bsb_build_schemas.package_specs,
8463-
`Arr Bsb_default.set_package_specs_from_array)
8464-
|> ignore ;
8465-
Bsb_default.get_package_specs ()
8466-
| _ -> assert false
8467-
end
8468-
| Some spec -> spec in
8469-
build_bs_deps ( String_set.fold
8470-
(fun k acc -> k ^ "," ^ acc ) deps Ext_string.empty ) ;
8471-
if !watch_mode then
8472-
watch ()
8544+
make_world_deps deps ;
8545+
if !watch_mode then
8546+
watch ()
84738547
(* ninja is not triggered in this case *)
8474-
end
8475-
| `Split (bsb_args,ninja_args)
8476-
->
8477-
begin
8478-
Arg.parse_argv bsb_args bsb_main_flags annoymous usage ;
8479-
let deps = (regenerate_ninja cwd bsc_dir !force_regenerate) in
8480-
(* [-make-world] should never be combined with [-package-specs] *)
8481-
if !make_world then
8482-
let deps =
8483-
match deps with
8484-
| None ->
8485-
let json = Ext_json.parse_json_from_file Literals.bsconfig_json in
8486-
begin match json with
8487-
| `Obj map ->
8488-
map
8489-
|? (Bsb_build_schemas.package_specs,
8490-
`Arr Bsb_default.set_package_specs_from_array)
8491-
|> ignore ;
8492-
Bsb_default.get_package_specs ()
8493-
| _ -> assert false
8494-
end
8495-
| Some spec -> spec in
8496-
build_bs_deps ( String_set.fold
8497-
(fun k acc -> k ^ "," ^ acc ) deps Ext_string.empty ) ;
8498-
if !watch_mode then watch ()
8499-
else ninja_command ninja ninja_args
8500-
end
8501-
end
8502-
(*with x ->
8503-
prerr_endline @@ Printexc.to_string x ;
8504-
exit 2*)
8505-
(* with [try, with], there is no stacktrace anymore .. *)
8548+
end
8549+
| `Split (bsb_args,ninja_args)
8550+
->
8551+
begin
8552+
Arg.parse_argv bsb_args bsb_main_flags annoymous usage ;
8553+
let deps = (regenerate_ninja cwd bsc_dir !force_regenerate) in
8554+
(* [-make-world] should never be combined with [-package-specs] *)
8555+
if !make_world then
8556+
make_world_deps deps ;
8557+
if !watch_mode then watch ()
8558+
else ninja_command ninja ninja_args
8559+
end
8560+
end
8561+
(*with x ->
8562+
prerr_endline @@ Printexc.to_string x ;
8563+
exit 2*)
8564+
(* with [try, with], there is no stacktrace anymore .. *)
85068565
end

0 commit comments

Comments
 (0)