Skip to content

Commit 5061426

Browse files
Lef IoannidisCopilot
andcommitted
Parser.Dep: make --dep full output relative paths
Add Filepath.make_relative_to_cwd to convert absolute paths to paths relative to the current working directory. Use it in print_full's norm_path so that .depend files contain portable relative paths instead of machine-specific absolute paths. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 04c562e commit 5061426

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/basic/FStarC.Filepath.fsti

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ val canonicalize : string -> string
3535

3636
(* Like canonicalize, but always returns an absolute path. *)
3737
val normalize_file_path: string -> ML string
38+
39+
(* Make a path relative to the current working directory. *)
40+
val make_relative_to_cwd: string -> ML string

src/ml/FStarC_Filepath.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,20 @@ let file_exists = Sys.file_exists
6363
(* Sys.is_directory raises Sys_error if the path does not exist *)
6464
let is_directory f = Sys.file_exists f && Sys.is_directory f
6565

66+
let make_relative_to_cwd (path:string) : string =
67+
if not (is_path_absolute path) then path
68+
else
69+
let path = normalize_file_path path in
70+
let cwd = normalize_file_path (Sys.getcwd ()) in
71+
let split s = List.filter (fun x -> x <> "") (BatString.nsplit s ~by:"/") in
72+
let path_parts = split path in
73+
let cwd_parts = split cwd in
74+
let rec skip_common pp cp =
75+
match pp, cp with
76+
| ph :: pt, ch :: ct when ph = ch -> skip_common pt ct
77+
| _ -> (pp, cp)
78+
in
79+
let (remaining_path, remaining_cwd) = skip_common path_parts cwd_parts in
80+
let ups = List.map (fun _ -> "..") remaining_cwd in
81+
String.concat "/" (ups @ remaining_path)
82+

src/parser/FStarC.Parser.Dep.fst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,10 @@ let print_full (outc : out_channel) (deps:deps) : ML unit =
20672067
in
20682068
let sb = FStarC.StringBuffer.create 10000 in
20692069
let pr str = ignore <| FStarC.StringBuffer.add str sb in
2070-
let norm_path s = replace_chars (replace_chars s '\\' "/") ' ' "\\ " in
2070+
let norm_path s =
2071+
let s = Filepath.make_relative_to_cwd s in
2072+
replace_chars (replace_chars s '\\' "/") ' ' "\\ "
2073+
in
20712074
let print_entry (target : string) (all_deps : list string) : ML unit =
20722075
(* Print a target with dependencies. *)
20732076
pr target; pr ":";

0 commit comments

Comments
 (0)