Skip to content

Commit 3f9a8bd

Browse files
committed
Dune rule generation improvements
Signed-off-by: Edwin Török <[email protected]>
1 parent b6e11bb commit 3f9a8bd

File tree

10 files changed

+72
-137
lines changed

10 files changed

+72
-137
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
_build/
22
_opam/
3-
*.install
3+
*.install
4+
compile_commands.json

dune-project

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,9 @@
3232
(lintcstubs-gen (= :version))
3333
(goblint (>= 2.1.0))
3434
goblint-cil
35+
dune-compiledb
3536
fpath
3637
(fmt (>= 0.9.0))
38+
sexplib
3739
)
38-
)
39-
40-
(package
41-
(name lintcstubs-rules)
42-
(synopsis "Generate dune rules for lintcstubs integration")
43-
(description "Generates dune rules for running the static analyzer")
44-
(depends
45-
(ocaml (>= 4.14))
46-
(lintcstubs-arity (>= 0.2.2))
47-
(lintcstubs-gen (= :version))
48-
(lintcstubs (= :version))
49-
)
50-
)
51-
40+
)

example/dune/dune

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,6 @@
1414
foo.h
1515
(run %{bin:lintcstubs_arity_cmt} %{dep:.foo.objs/byte/foo.cmt}))))
1616

17-
(rule
18-
; editor integration: generate include paths for LSPs such as clangd
19-
(target compile_flags.txt)
20-
(mode promote)
21-
(enabled_if
22-
(= %{system} linux))
23-
(action
24-
(with-stdout-to
25-
compile_flags.txt
26-
(pipe-stdout
27-
(progn
28-
(echo "-Wall -Wextra -Wstrict-prototypes -D_FORTIFY_SOURCE=2 ")
29-
(echo
30-
%{ocaml-config:ocamlc_cppflags}
31-
%{ocaml-config:ocamlc_cflags}
32-
-I%{ocaml_where}
33-
-I)
34-
(system pwd))
35-
(system "xargs -n1 echo") ; the format is a single flag per line
36-
))))
37-
3817
(rule
3918
(enabled_if %{bin-available:lintcstubs})
4019
(action

example/dune2/README.md

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
Given a file `foo.ml` that contains a C stub and a 'foostubs.c' that implements.
22

33
Generate some dune rules, for example by doing this:
4-
```
5-
(include dune.analysis.inc)
6-
(rule
7-
(deps
8-
(:mlfiles (glob_files_rec *.ml))
9-
(:cfiles (glob_files_rec *.c))
10-
)
11-
(action (with-stdout-to dune.analysis.inc.gen
12-
(run %{bin:lintcstubs-dune-rules} %{mlfiles} %{cfiles})
13-
))
14-
)
15-
16-
(rule
17-
(alias runtest)
18-
(action
19-
(diff dune.analysis.inc dune.analysis.inc.gen)))
4+
```sh
5+
$ lintcstubs-dune-rules >dune.analysis.inc
206
```
217

22-
And then running:
8+
And then adding this snippet to your dune file:
239
```
24-
rm -f dune.analysis.inc; touch dune.analysis.inc && dune runtest --auto-promote
10+
(include dune.analysis.inc)
2511
```
2612

27-
You will have to create a `compile_commands.json`, e.g. by using `bear`:
28-
```
29-
bear -- dune build @cstubs
13+
The generated file can be kept up-to-date by running:
14+
```sh
15+
dune runtest --auto-promote
3016
```
3117

32-
(where `@cstubs` is an alias generated by the above tool).
18+
You will have to create a `compile_commands.json`, e.g. by using [`dune-compiledb`](https://github.com/edwintorok/dune-compiledb/):
19+
```sh
20+
dune rules | dune-compiledb
21+
```
3322

3423
Now you can run `dune build @analyze` to trigger a static analysis.
3524

36-
TODO: this is very experimental.
25+
TODO: this is very experimental.

example/dune2/dune

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,3 @@
66
(names foostubs)))
77

88
(include dune.analysis.inc)
9-
10-
(rule
11-
(enabled_if %{bin-available:lintcstubs})
12-
(deps
13-
(:mlfiles
14-
(glob_files_rec *.ml))
15-
(:cfiles
16-
(glob_files_rec *.c)))
17-
(action
18-
(with-stdout-to
19-
dune.analysis.inc.gen
20-
(run %{bin:lintcstubs-dune-rules} %{mlfiles} %{cfiles}))))
21-
22-
(rule
23-
(alias runtest)
24-
(package lintcstubs-rules)
25-
(enabled_if %{bin-available:lintcstubs})
26-
(action
27-
(diff dune.analysis.inc dune.analysis.inc.gen)))

example/dune2/dune.analysis.inc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
; AUTO-GENERATED by ../../../install/default/bin/lintcstubs-dune-rules foo2.ml foo2.model.c foostubs.c
2+
; DO NOT EDIT
3+
(rule
4+
(enabled_if
5+
(and %{bin-available:lintcstubs-dune-rules} %{bin-available:lintcstubs}))
6+
(deps (:mlfiles (glob_files_rec *.ml)) (:cfiles (glob_files_rec *.c)))
7+
(action
8+
(with-stdout-to dune.analysis.inc.gen
9+
(run %{bin:lintcstubs-dune-rules} %{mlfiles} %{cfiles}))))
10+
(rule (alias runtest) (package lintcstubs)
11+
(enabled_if %{bin-available:lintcstubs})
12+
(action (diff dune.analysis.inc dune.analysis.inc.gen)))
13+
(rule (target compile_commands.json)
14+
(action (pipe-stdout (run dune rules) (run %{bin:dune-compiledb}))))
115
(rule (enabled_if %{bin-available:lintcstubs_genmain})
216
(targets foo2.model.c ./primitives.h)
317
(deps (:cmt ./.foo2.objs/byte/foo2.cmt) %{bin:lintcstubs_arity_cmt}
@@ -8,8 +22,6 @@
822
(with-stdout-to foo2.model.c
923
(progn (run %{bin:lintcstubs_genwrap} %{cmt})
1024
(run %{bin:lintcstubs_genmain} %{cmt}))))))
11-
(rule (alias cstubs) (deps (:ofiles foo2.model.o foostubs.o))
12-
(action (echo "for BEAR")))
1325
(rule (enabled_if %{bin-available:lintcstubs})
1426
(targets ./lintcstubs.log ./lintcstubs.sarif)
1527
(deps compile_commands.json (:primitives ./primitives.h)

lintcstubs-rules.opam

Lines changed: 0 additions & 34 deletions
This file was deleted.

lintcstubs.opam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ depends: [
1414
"lintcstubs-gen" {= version}
1515
"goblint" {>= "2.1.0"}
1616
"goblint-cil"
17+
"dune-compiledb"
1718
"fpath"
1819
"fmt" {>= "0.9.0"}
20+
"sexplib"
1921
"odoc" {with-doc}
2022
]
2123
build: [

rules/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(library
2-
(package lintcstubs-rules)
2+
(package lintcstubs)
33
(name filter_primitives)
44
(modules filter_primitives)
55
(libraries compiler-libs.common fpath))
66

77
(executable
88
(public_name lintcstubs-dune-rules)
9-
(package lintcstubs-rules)
9+
(package lintcstubs)
1010
(name main)
1111
(modules main)
1212
(libraries filter_primitives sexplib fpath))

rules/main.ml

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ let cmt_rule =
3737
)
3838
|}
3939

40-
let cstubs_alias_rule =
41-
(* for BEAR *)
42-
Sexplib.Sexp.of_string
43-
{| (rule (alias cstubs) (deps (:ofiles)) (action (echo "for BEAR"))) |}
44-
4540
(* can be long running, ensure we see something when running dune with '--no-buffer',
4641
add a 2nd debug target
4742
@@ -72,6 +67,39 @@ let analyze_alias_rule =
7267
)
7368
|}
7469

70+
let incgen_rule =
71+
Sexplib.Sexp.of_string_many
72+
{|
73+
(rule
74+
(enabled_if (and %{bin-available:lintcstubs-dune-rules} %{bin-available:lintcstubs}))
75+
(deps
76+
(:mlfiles
77+
(glob_files_rec *.ml))
78+
(:cfiles
79+
(glob_files_rec *.c)))
80+
(action
81+
(with-stdout-to
82+
dune.analysis.inc.gen
83+
(run %{bin:lintcstubs-dune-rules} %{mlfiles} %{cfiles}))))
84+
85+
(rule
86+
(alias runtest)
87+
(package lintcstubs)
88+
(enabled_if %{bin-available:lintcstubs})
89+
(action
90+
(diff dune.analysis.inc dune.analysis.inc.gen)))
91+
92+
(rule
93+
(target compile_commands.json)
94+
(action
95+
(pipe-stdout
96+
(run dune rules)
97+
(run %{bin:dune-compiledb})
98+
)
99+
)
100+
)
101+
|}
102+
75103
let group_by_dirs paths =
76104
Seq.fold_left
77105
(fun acc path ->
@@ -101,17 +129,15 @@ let () =
101129
f x |> Result.get_ok
102130
in
103131
let files_seq = List.to_seq files in
104-
let o_files =
105-
files_seq
106-
|> Seq.filter Fpath.(has_ext "c")
107-
|> Seq.map (Fpath.set_ext "o")
108-
|> group_by_dirs
109-
in
110132
let ml_files_primitives =
111133
files_seq
112134
|> Seq.filter Fpath.(has_ext "ml")
113135
|> Seq.filter (filter Filter_primitives.has_primitives)
114136
in
137+
Format.printf "@[<h>; AUTO-GENERATED by %a@]@,; DO NOT EDIT@."
138+
(Format.pp_print_list ~pp_sep:Format.pp_print_space Format.pp_print_string)
139+
(Array.to_list Sys.argv) ;
140+
List.iter (Format.printf "%a@." Sexplib.Sexp.pp_hum) incgen_rule ;
115141
group_by_dirs ml_files_primitives
116142
|> Fpath.Map.iter @@ fun dir mls ->
117143
let to_cmtfile mlfile =
@@ -135,14 +161,6 @@ let () =
135161
|> apply_template' "%{model}" model_file
136162
)
137163
in
138-
let o_files_in_dir =
139-
Fpath.Map.find dir o_files |> Option.value ~default:Fpath.Set.empty
140-
in
141-
let cstubs_alias_rules =
142-
cstubs_alias_rule
143-
|> apply_template ":ofiles"
144-
(Fpath.Set.to_seq o_files_in_dir |> List.of_seq)
145-
in
146164
let log_file = Fpath.(dir / "lintcstubs.log") in
147165
let sarif_file = Fpath.set_ext ".sarif" log_file in
148166
let analyze_rule =
@@ -158,7 +176,5 @@ let () =
158176
cmt_rules
159177
|> List.iter @@ fun cmt_rule ->
160178
Format.printf "%a@." Sexplib.Sexp.pp_hum cmt_rule ;
161-
if not (Fpath.Set.is_empty o_files_in_dir) then
162-
Format.printf "%a@." Sexplib.Sexp.pp_hum cstubs_alias_rules ;
163179
Format.printf "%a@." Sexplib.Sexp.pp_hum analyze_rule ;
164180
Format.printf "%a@." Sexplib.Sexp.pp_hum analyze_alias_rule

0 commit comments

Comments
 (0)