123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- open Printf
- let failwithf fmt = kprintf failwith fmt;;
- let splitwith chr = Str.split (Str.regexp_string (Char.escaped chr))
- let (|>) a f = f a
- let rec mk_seq from_num to_num =
- if from_num > to_num then []
- else from_num :: mk_seq (from_num + 1) to_num
- let dedup_list lst =
- let rec do_dedup acc rst =
- match rst with
- [] -> acc
- | h::t -> if List.mem h acc then do_dedup acc t
- else do_dedup (h :: acc) t
- in do_dedup [] lst
- let usage (progname: string) =
- eprintf "usage: %s [options] <file> ...\n" progname;
- eprintf "\n[options]\n\
- --search-path <path> Specify the search path of EDL files\n\
- --use-prefix Prefix untrusted proxy with Enclave name\n\
- --header-only Only generate header files\n\
- --untrusted Generate untrusted proxy and bridge\n\
- --trusted Generate trusted proxy and bridge\n\
- --untrusted-dir <dir> Specify the directory for saving untrusted code\n\
- --trusted-dir <dir> Specify the directory for saving trusted code\n\
- --help Print this help message\n";
- eprintf "\n\
- If neither `--untrusted' nor `--trusted' is specified, generate both.\n";
- exit 1
- type edger8r_params = {
- input_files : string list;
- use_prefix : bool;
- header_only : bool;
- gen_untrusted : bool;
- gen_trusted : bool;
- untrusted_dir : string;
- trusted_dir : string;
- }
- let search_paths = ref [|"."|]
- let path_separator : char =
- match Sys.os_type with
- "Win32" -> ';'
- | _ -> ':'
- let rec parse_cmdline (progname: string) (cmdargs: string list) =
- let use_pref = ref false in
- let hd_only = ref false in
- let untrusted= ref false in
- let trusted = ref false in
- let u_dir = ref "." in
- let t_dir = ref "." in
- let files = ref [] in
- let rec local_parser (args: string list) =
- match args with
- [] -> ()
- | op :: ops ->
- match String.lowercase op with
- "--use-prefix" -> use_pref := true; local_parser ops
- | "--header-only"-> hd_only := true; local_parser ops
- | "--untrusted" -> untrusted := true; local_parser ops
- | "--trusted" -> trusted := true; local_parser ops
- | "--untrusted-dir" ->
- (match ops with
- [] -> usage progname
- | x::xs -> u_dir := x; local_parser xs)
- | "--trusted-dir" ->
- (match ops with
- [] -> usage progname
- | x::xs -> t_dir := x; local_parser xs)
- | "--help" -> usage progname
- | "--search-path" ->
- if ops = [] then usage progname
- else
- let search_path_str = List.hd ops in
- let extra_paths = splitwith path_separator search_path_str in
- let extra_path_arry = Array.of_list extra_paths in
- search_paths := Array.append extra_path_arry !search_paths;
- local_parser (List.tl ops)
- | _ -> files := op :: !files; local_parser ops
- in
- local_parser cmdargs;
- let opt =
- { input_files = List.rev !files; use_prefix = !use_pref;
- header_only = !hd_only; gen_untrusted = true; gen_trusted = true;
- untrusted_dir = !u_dir; trusted_dir = !t_dir;
- }
- in
- if !untrusted || !trusted
- then { opt with gen_trusted = !trusted; gen_untrusted = !untrusted }
- else opt
- let separator_str : string = Filename.dir_sep
- let get_file_path (fname: string) =
- let get_full_name path =
- if Filename.is_relative fname then path ^ separator_str ^ fname
- else fname
- in
- let targets = Array.map get_full_name !search_paths in
- let fn_list = Array.to_list targets in
- try
- List.find Sys.file_exists fn_list
- with
- Not_found -> failwithf "File not found within search paths: %s\n" fname
- let get_short_name (fname: string) =
- let bn = Filename.basename fname in
- try Filename.chop_extension bn
- with Invalid_argument _ -> bn
- let isdigit = function '0' | '1' .. '9' -> true | _ -> false
- let isalpha = function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false
- let isalnum c = isdigit c || isalpha c
- let str_map f s =
- let len = String.length s in
- let res = String.create len in
- for i = 0 to len - 1 do
- String.set res i (f (String.get s i))
- done;
- res
- let str_to_list s =
- let rec iter i lst =
- if i < 0 then lst else iter (i - 1) (s.[i] :: lst)
- in
- iter (String.length s - 1) []
- let str_forall p s = List.for_all p (str_to_list s)
- let to_c_identifier (s: string) =
- let convert_char ch =
- if isalnum ch then ch else '_'
- in
- let first_ch =
- let ch = s.[0] in
- if isalnum ch then ch else '_'
- in
- let rest_str =
- String.sub s 1 (String.length s - 1)
- in
- Char.escaped first_ch ^ str_map convert_char rest_str
- let is_c_identifier(s: string) =
- let first_ch = s.[0] in
- let rest_str = String.sub s 1 (String.length s - 1) in
- if isalpha first_ch || first_ch = '_'
- then str_forall (fun ch -> isalnum ch || ch = '_') rest_str
- else false
- let trusted_headers : string list ref = ref []
- let untrusted_headers: string list ref = ref []
- let create_dir (d: string) =
- let curr_dir = Unix.getcwd () in
-
- let get_root_dir (dirs: string list) =
- match Sys.os_type with
- "Win32" -> List.hd dirs
- | _ -> Filename.dir_sep
- in
-
- let normalize (ds: string list) =
- if Sys.os_type <> "Win32" then ds
- else
- let d = List.hd ds in
- if String.length d = 2 && d.[1] = ':'
- then (d ^ Filename.dir_sep) :: List.tl ds
- else ds
- in
- let dir_exist_p dir =
- if Sys.file_exists dir then
- let stats = Unix.stat dir in
- match stats.Unix.st_kind with
- | Unix.S_DIR -> true
-
- | _ -> false
- else false
- in
- let __do_create_and_goto_dir dir =
- (if dir_exist_p dir then () else Unix.mkdir dir 0o755);
- Unix.chdir dir
- in
- let do_create_dir () =
- let rec do_create_dir_recursively dirs =
- match dirs with
- [] -> ()
- | x::xs ->
- __do_create_and_goto_dir x; do_create_dir_recursively xs
- in
-
- let dirs = normalize (Str.split (Str.regexp separator_str) d) in
- let start_dir = if Filename.is_relative d then curr_dir else get_root_dir dirs in
- Unix.chdir start_dir;
-
- do_create_dir_recursively (List.filter (fun s -> s <> "") dirs);
- Unix.chdir curr_dir;
- in
- try do_create_dir ()
- with exn -> (eprintf "error: failed to create directory: `%s'\n" d; exit 1)
|