Skip to content

Commit 5f220c6

Browse files
author
Michael Norrish
committed
Holmake: can now (recursively) remove directories in EXTRA_CLEANS
1 parent 53a313b commit 5f220c6

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

tools/Holmake/Holmake.sml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ fun clean_deps() =
988988

989989
fun do_clean_target x = let
990990
fun clean_action () =
991-
Holmake_tools.clean_dir {extra_cleans = extra_cleans()}
991+
Holmake_tools.clean_dir outputfns {extra_cleans = extra_cleans()}
992992
in
993993
case x of
994994
"clean" => (info "Cleaning directory of object files\n"; clean_action())

tools/Holmake/Holmake_tools.sig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ sig
7676
{chattiness:int,
7777
debug: {ins:string list,outs:string list} option,
7878
usepfx:bool} -> output_functions
79+
val default_ofns : output_functions
7980
val die_with : string -> 'a
8081

8182

@@ -99,7 +100,7 @@ sig
99100
val exists_readable : string -> bool
100101
val extract_theory : string list -> string option
101102

102-
val clean_dir : {extra_cleans: string list} -> unit
103+
val clean_dir : output_functions -> {extra_cleans: string list} -> unit
103104
val clean_depdir : {depdirname : string} -> bool
104105
val clean_forReloc : {holheap : string option} -> unit
105106
val pushdir : string -> ('a -> 'b) -> ('a -> 'b)

tools/Holmake/Holmake_tools.sml

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ in
224224
{warn = warn, diag = diag, tgtfatal = tgtfatal, info = info, chatty = chatty}
225225
end
226226

227+
val default_ofns =
228+
output_functions {usepfx = true, chattiness = 1, debug = NONE}
229+
227230
fun exists_readable s = OS.FileSys.access(s, [OS.FileSys.A_READ])
228231

229232
fun check_distrib toolname = let
@@ -418,9 +421,55 @@ fun read_files ds P action =
418421
(if P nextfile then action nextfile else ();
419422
read_files ds P action)
420423

424+
fun dir_files dnm A =
425+
let
426+
val ds = OS.FileSys.openDir dnm
427+
fun recurse A =
428+
case OS.FileSys.readDir ds of
429+
NONE => (OS.FileSys.closeDir ds; A)
430+
| SOME nm => recurse (OS.Path.concat (dnm, nm) :: A)
431+
in
432+
recurse A
433+
end
434+
435+
fun recursive_act file_act dir_act name =
436+
let
437+
fun worklist nms rmds =
438+
case nms of
439+
[] => List.app dir_act rmds
440+
| n::ns =>
441+
if OS.FileSys.isDir n then
442+
worklist (dir_files n ns) (n :: rmds)
443+
else (file_act n ; worklist ns rmds)
444+
in
445+
worklist [name] []
446+
end
447+
421448
fun quiet_remove s = OS.FileSys.remove s handle e => ()
449+
fun chatty_remove act (ofns : output_functions) s =
450+
act s handle e =>
451+
(#warn ofns ("Attempt to remove: " ^ s ^
452+
" failed with exception " ^ General.exnMessage e);
453+
raise e)
454+
455+
fun clean1 (ofns : output_functions) s =
456+
let val _ = #diag ofns "tools" (fn () => "clean1 " ^ s)
457+
in
458+
if OS.FileSys.access (s, []) then
459+
if OS.FileSys.isDir s then
460+
if String.isSuffix "/" s then
461+
recursive_act (chatty_remove OS.FileSys.remove ofns)
462+
(chatty_remove OS.FileSys.rmDir ofns)
463+
s
464+
else
465+
(#warn ofns ("Not removing directory " ^ s ^ " from EXTRA_CLEANS.");
466+
#warn ofns (" Use trailing / on name to force this."))
467+
else chatty_remove OS.FileSys.remove ofns s
468+
else (* doesn't exist, do nothing *) ()
469+
end
470+
422471

423-
fun clean_dir {extra_cleans} = let
472+
fun clean_dir ofns {extra_cleans} = let
424473
val cdstream = OS.FileSys.openDir "."
425474
fun to_delete f =
426475
case (toFile f) of
@@ -434,8 +483,8 @@ fun clean_dir {extra_cleans} = let
434483
| ART _ => true
435484
| _ => false
436485
in
437-
read_files cdstream to_delete quiet_remove;
438-
app quiet_remove extra_cleans
486+
read_files cdstream to_delete (chatty_remove OS.FileSys.remove ofns);
487+
app (clean1 ofns) extra_cleans
439488
end
440489

441490
fun clean_forReloc {holheap} =

tools/buildutils.sml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -446,27 +446,30 @@ fun hmakefile_data HOLDIR =
446446

447447
fun clean0 HOLDIR = let
448448
val {includes,extra_cleans,...} = hmakefile_data HOLDIR
449+
open Holmake_tools
449450
in
450-
Holmake_tools.clean_dir {extra_cleans = extra_cleans} ;
451+
clean_dir default_ofns {extra_cleans = extra_cleans} ;
451452
includes
452453
end
453454

454455
fun cleanAll0 HOLDIR = let
455456
val {includes,extra_cleans,...} = hmakefile_data HOLDIR
457+
open Holmake_tools
456458
in
457-
Holmake_tools.clean_dir {extra_cleans = extra_cleans};
458-
Holmake_tools.clean_depdir {depdirname = ".HOLMK"};
459-
Holmake_tools.clean_depdir {depdirname = ".hollogs"};
459+
clean_dir default_ofns {extra_cleans = extra_cleans};
460+
clean_depdir {depdirname = ".HOLMK"};
461+
clean_depdir {depdirname = ".hollogs"};
460462
includes
461463
end
462464

463465
fun cleanForReloc0 HOLDIR =
464466
let
465467
val {includes,holheap,...} = hmakefile_data HOLDIR
468+
open Holmake_tools
466469
in
467-
Holmake_tools.clean_forReloc {holheap = holheap};
468-
Holmake_tools.clean_depdir {depdirname = ".HOLMK"};
469-
Holmake_tools.clean_depdir {depdirname = ".hollogs"};
470+
clean_forReloc {holheap = holheap};
471+
clean_depdir {depdirname = ".HOLMK"};
472+
clean_depdir {depdirname = ".hollogs"};
470473
includes
471474
end
472475

0 commit comments

Comments
 (0)