(************************************************************ tsort_cmo.ml Created : Wed Nov 6 10:54:35 2002 Last modified: Fri Jan 10 22:21:46 2003 Compile: ocamlfind ocamlc -package unix -linkpkg -g -I /home/tak/lib/ocaml str.cma mylib.cma tsort_cmo.ml -o tsort_cmo # FTP Directory: sources/ocaml # mylib にお手製グラフライブラリを組み込んでね。 ************************************************************) (** @author Takashi Masuyama *) open Graph (*let extension = ".cmo"*) (* make cleanしたあとは .cmo の存在確認だときついのでソース側の存在を * 確認する *) let _ = let is_grouping = ref false in let do_existence_check = ref false in let verbose_mode = ref false in let check_extension = ref ".ml" in let do_add_extension = ref false in let output_extension = ref ".cmo" in let specs = [ ("-v", Arg.Set(verbose_mode), "verbose mode"); ("-g", Arg.Set(is_grouping), "group nodes by drop extensions"); ("-ng", Arg.Clear(is_grouping), "do not group nodes (default)"); ("-e", Arg.Set(do_existence_check), "check file existence and remove non-existential filenames"); ("-ne", Arg.Clear(do_existence_check), "don't check file existence (default)"); ("-addE", Arg.Set(do_add_extension), "add a extension (default is .cmo), the extension is configured by -setE option"); ("-setE", Arg.String(fun x -> do_add_extension := true; output_extension := x), "set extension (this option include -addE option. don't forget \".\" )") ] in let usage_line = "usage: " ^ Sys.argv.(0) ^ " [options] [dependfile] [filename(s) ...]" in let dependency_filename_ref = ref "" in let prerr_verbose_message () = prerr_endline (Printf.sprintf "check file existence %b\ngroup nodes by drop extendsion? %b" !do_existence_check !is_grouping) in let anonymous_option_list_ref = ref [] in let is_first = ref true in (* let _ = Arg.parse specs (fun x -> if !is_first then*) (* begin dependency_filename_ref := x; is_first := false end*) (* else init_node_name_list_ref := x :: !init_node_name_list_ref)*) (* usage_line in*) let _ = begin Arg.parse specs (fun x -> anonymous_option_list_ref := x :: !anonymous_option_list_ref) usage_line; anonymous_option_list_ref := List.rev !anonymous_option_list_ref end in let (dependency_filename, init_node_name_list) = match !anonymous_option_list_ref with hd::tl when tl <> [] -> (hd, tl) | l -> begin prerr_endline "anonymous arguments"; List.iter prerr_endline l; prerr_endline usage_line; exit 1 end in (* maybe debug *) (* let all_graph_name = "all.vcg" in*) (* let sub_graph_name = "sub.vcg" in*) (* end *) let in_channel = try open_in dependency_filename with Sys_error s -> begin prerr_endline ("cannot find depend file\n"^s); exit 1 end in (* ここでまとめても良いかも? *) let node_f = if !is_grouping then id_to_group else (fun x -> x) in let lg = make_graph_from_dependency_file in_channel node_f in (* debug *) (* 決めうちしたファイル名でVCGを出力 *) (* let all_output = open_out all_graph_name in*) (* let _ = begin output_vcg all_output lg; close_out all_output end in*) (* end *) let ((Graph(nodes, node_to_id, edge_table)) as g) = lgraph_to_agraph lg in (* let _ = Array.iter print_endline nodes in*) (* let _ = NodeMap.iter (fun k v -> Printf.printf "%s\t%d\n" k v) node_to_id in*) let init_node_list = List.map (fun x -> NodeMap.find (node_f x) node_to_id) init_node_name_list in let Graph(sub_nodes,_,_) as sub_graph = span_nodes_by_sub_graph_and_gc g init_node_list in (* debug *) (* let sub_lgraph = agraph_to_lgraph sub_graph in*) (* let sub_output = open_out sub_graph_name in*) (* let _ = begin output_vcg sub_output sub_lgraph; close_out sub_output end in*) (* end *) let sorted_list = topological_sort (inverse_graph sub_graph) in let is_file_exist x = try ignore (Unix.stat x); true with Unix.Unix_error(c, _, f) when c = Unix.ENOENT -> false in (* ファイルの存在チェックをするがグルーピングをしない場合は拡張子を付けない *) let iter_function x = let check_name = if !is_grouping then (sub_nodes.(x)^(!check_extension)) else sub_nodes.(x) in let checked_string = if !do_existence_check then (* extension *) if is_file_exist check_name then Some sub_nodes.(x) else None else Some sub_nodes.(x) in let result = match checked_string with Some s -> if !do_add_extension then s^(!output_extension)^" " else s^" " | None -> "" in print_string result in begin if !verbose_mode then prerr_verbose_message (); List.iter iter_function sorted_list; print_newline (); end