Revision ca7ff3f7
Added by Lélio Brun over 1 year ago
src/backends/C/c_backend_makefile.ml | ||
---|---|---|
13 | 13 |
open Lustre_types |
14 | 14 |
|
15 | 15 |
let pp_dep fmt dep = |
16 |
Format.fprintf fmt "%b, %s, {%a}, %b" |
|
17 |
dep.local dep.name Printers.pp_prog dep.content dep.is_stateful |
|
18 |
|
|
19 |
let pp_deps fmt deps = Format.fprintf fmt "@[<v 0>%a@ @]" (Utils.fprintf_list ~sep:"@ ," pp_dep) deps |
|
16 |
Format.fprintf fmt "%b, %s, {%a}, %b" dep.local dep.name Printers.pp_prog |
|
17 |
dep.content dep.is_stateful |
|
18 |
|
|
19 |
let pp_deps fmt deps = |
|
20 |
Format.fprintf fmt "@[<v 0>%a@ @]" (Utils.fprintf_list ~sep:"@ ," pp_dep) deps |
|
20 | 21 |
|
21 | 22 |
let header_has_code header = |
22 |
List.exists
|
|
23 |
(fun top ->
|
|
23 |
List.exists |
|
24 |
(fun top -> |
|
24 | 25 |
match top.top_decl_desc with |
25 |
| Const _ -> true |
|
26 |
| ImportedNode nd -> nd.nodei_in_lib = [] |
|
27 |
| _ -> false |
|
28 |
) |
|
26 |
| Const _ -> |
|
27 |
true |
|
28 |
| ImportedNode nd -> |
|
29 |
nd.nodei_in_lib = [] |
|
30 |
| _ -> |
|
31 |
false) |
|
29 | 32 |
header |
30 | 33 |
|
31 | 34 |
let header_libs header = |
32 |
List.fold_left (fun accu top -> |
|
33 |
match top.top_decl_desc with |
|
34 |
| ImportedNode nd -> Utils.list_union nd.nodei_in_lib accu |
|
35 |
| _ -> accu |
|
36 |
) [] header |
|
37 |
|
|
38 |
|
|
39 |
let compiled_dependencies deps = |
|
35 |
List.fold_left |
|
36 |
(fun accu top -> |
|
37 |
match top.top_decl_desc with |
|
38 |
| ImportedNode nd -> |
|
39 |
Utils.list_union nd.nodei_in_lib accu |
|
40 |
| _ -> |
|
41 |
accu) |
|
42 |
[] header |
|
43 |
|
|
44 |
let compiled_dependencies deps = |
|
40 | 45 |
List.filter (fun dep -> header_has_code dep.content) deps |
41 | 46 |
|
42 |
let lib_dependencies deps = |
|
43 |
List.fold_left |
|
44 |
(fun accu dep -> Utils.list_union (header_libs dep.content) accu) [] deps |
|
45 |
|
|
46 |
let fprintf_dependencies fmt (deps: dep_t list) = |
|
47 |
let lib_dependencies deps = |
|
48 |
List.fold_left |
|
49 |
(fun accu dep -> Utils.list_union (header_libs dep.content) accu) |
|
50 |
[] deps |
|
51 |
|
|
52 |
let fprintf_dependencies fmt (deps : dep_t list) = |
|
47 | 53 |
(* Format.eprintf "Deps: %a@." pp_deps dep; *) |
48 | 54 |
let compiled_deps = compiled_dependencies deps in |
55 |
|
|
49 | 56 |
(* Format.eprintf "Compiled Deps: %a@." pp_deps compiled_dep; *) |
50 |
|
|
51 |
List.iter (fun s -> Log.report ~level:1 (fun fmt -> fprintf fmt "Adding dependency: %s@." s); |
|
52 |
fprintf fmt "\t${GCC} -I${INC} -c %s@." s) |
|
53 |
(("${INC}/io_frontend.c"):: (* IO functions when a main function is computed *) |
|
54 |
(List.map |
|
55 |
(fun dep -> |
|
56 |
(if dep.local then dep.name else Version.include_path ^ "/" ^ dep.name) ^ ".c") |
|
57 |
compiled_deps)) |
|
58 |
|
|
59 |
module type MODIFIERS_MKF = |
|
60 |
sig (* dep was (bool * ident * top_decl list) *) |
|
61 |
val other_targets: Format.formatter -> string -> string -> dep_t list -> unit |
|
57 |
List.iter |
|
58 |
(fun s -> |
|
59 |
Log.report ~level:1 (fun fmt -> fprintf fmt "Adding dependency: %s@." s); |
|
60 |
fprintf fmt "\t${GCC} -I${INC} -c %s@." s) |
|
61 |
("${INC}/io_frontend.c" |
|
62 |
:: |
|
63 |
(* IO functions when a main function is computed *) |
|
64 |
List.map |
|
65 |
(fun dep -> |
|
66 |
(if dep.local then dep.name else Version.include_path ^ "/" ^ dep.name) |
|
67 |
^ ".c") |
|
68 |
compiled_deps) |
|
69 |
|
|
70 |
module type MODIFIERS_MKF = sig |
|
71 |
(* dep was (bool * ident * top_decl list) *) |
|
72 |
val other_targets : Format.formatter -> string -> string -> dep_t list -> unit |
|
62 | 73 |
end |
63 | 74 |
|
64 |
module EmptyMod = |
|
65 |
(struct |
|
75 |
module EmptyMod : MODIFIERS_MKF = struct |
|
66 | 76 |
let other_targets _ _ _ _ = () |
67 |
end: MODIFIERS_MKF) |
|
68 |
|
|
69 |
module Main = functor (Mod: MODIFIERS_MKF) -> |
|
70 |
struct |
|
71 |
|
|
72 |
(* TODO: BEWARE OF THE BUG !! in case of very long nodename or maybe even |
|
73 |
basename, the string basename_nodename exceed a limit length for files on linux |
|
74 |
and prevent gcc to generate the binary of that name. |
|
75 |
|
|
76 |
To be solved (later) with |
|
77 |
- either the default name if it short |
|
78 |
- a shorter version if it is long (md5?) |
|
79 |
- a provided name given when calling the makefile so the user can control the |
|
80 |
expected name of the binary |
|
81 |
|
|
82 |
*) |
|
83 |
|
|
84 |
let print_makefile basename nodename (dependencies: dep_t list) fmt = |
|
85 |
let binname = |
|
86 |
let s = basename ^ "_" ^ nodename in |
|
87 |
if String.length s > 100 (* seems that GCC fails from 144 characters and |
|
88 |
on: File name too long collect2 ld error. *) |
|
89 |
then |
|
90 |
if String.length nodename > 100 then |
|
91 |
basename ^ "_run" (* shorter version *) |
|
92 |
else |
|
93 |
nodename ^ "run" |
|
94 |
else |
|
95 |
s |
|
96 |
in |
|
97 |
fprintf fmt "BINNAME?=%s@." binname; |
|
98 |
fprintf fmt "GCC=gcc -O0@."; |
|
99 |
fprintf fmt "LUSTREC=%s@." Sys.executable_name; |
|
100 |
fprintf fmt "LUSTREC_BASE=%s@." (Filename.dirname (Filename.dirname Sys.executable_name)); |
|
101 |
fprintf fmt "INC=%s@." Version.include_path (*"${LUSTREC_BASE}/include/lustrec"*); |
|
102 |
fprintf fmt "@."; |
|
103 |
|
|
104 |
(* Main binary *) |
|
105 |
fprintf fmt "%s_%s: %s.c %s_main.c@." basename "run"(*nodename*) basename basename; |
|
106 |
fprintf fmt "\t${GCC} -I${INC} -I. -c %s.c@." basename; |
|
107 |
fprintf fmt "\t${GCC} -I${INC} -I. -c %s_main.c@." basename; |
|
108 |
fprintf_dependencies fmt dependencies; |
|
109 |
fprintf fmt "\t${GCC} -o ${BINNAME} io_frontend.o %a %s.o %s_main.o %a@." |
|
110 |
(Utils.fprintf_list ~sep:" " (fun fmt dep -> Format.fprintf fmt "%s.o" dep.name)) |
|
111 |
(compiled_dependencies dependencies) |
|
112 |
basename (* library .o *) |
|
113 |
basename (* main function . o *) |
|
114 |
(Utils.fprintf_list ~sep:" " (fun fmt lib -> fprintf fmt "-l%s" lib)) (lib_dependencies dependencies) |
|
115 |
; |
|
116 |
fprintf fmt "@."; |
|
117 |
fprintf fmt "clean:@."; |
|
118 |
fprintf fmt "\t\\rm -f *.o ${BINNAME}@."; |
|
119 |
fprintf fmt "@."; |
|
120 |
Mod.other_targets fmt basename nodename dependencies; |
|
121 |
fprintf fmt "@."; |
|
122 |
|
|
123 | 77 |
end |
124 | 78 |
|
79 |
module Main = |
|
80 |
functor |
|
81 |
(Mod : MODIFIERS_MKF) |
|
82 |
-> |
|
83 |
struct |
|
84 |
(* TODO: BEWARE OF THE BUG !! in case of very long nodename or maybe even |
|
85 |
basename, the string basename_nodename exceed a limit length for files on |
|
86 |
linux and prevent gcc to generate the binary of that name. |
|
87 |
|
|
88 |
To be solved (later) with - either the default name if it short - a |
|
89 |
shorter version if it is long (md5?) - a provided name given when calling |
|
90 |
the makefile so the user can control the expected name of the binary *) |
|
91 |
|
|
92 |
let print_makefile basename nodename (dependencies : dep_t list) fmt = |
|
93 |
let binname = |
|
94 |
let s = basename ^ "_" ^ nodename in |
|
95 |
if |
|
96 |
String.length s > 100 |
|
97 |
(* seems that GCC fails from 144 characters and on: File name too long |
|
98 |
collect2 ld error. *) |
|
99 |
then |
|
100 |
if String.length nodename > 100 then basename ^ "_run" |
|
101 |
(* shorter version *) |
|
102 |
else nodename ^ "run" |
|
103 |
else s |
|
104 |
in |
|
105 |
fprintf fmt "BINNAME?=%s@." binname; |
|
106 |
fprintf fmt "GCC=gcc -O0@."; |
|
107 |
fprintf fmt "LUSTREC=%s@." Sys.executable_name; |
|
108 |
fprintf fmt "LUSTREC_BASE=%s@." |
|
109 |
(Filename.dirname (Filename.dirname Sys.executable_name)); |
|
110 |
fprintf fmt "INC=%s@." Version.include_path |
|
111 |
(*"${LUSTREC_BASE}/include/lustrec"*); |
|
112 |
fprintf fmt "@."; |
|
113 |
|
|
114 |
(* Main binary *) |
|
115 |
fprintf fmt "%s_%s: %s.c %s_main.c@." basename "run" (*nodename*) basename |
|
116 |
basename; |
|
117 |
fprintf fmt "\t${GCC} -I${INC} -I. -c %s.c@." basename; |
|
118 |
fprintf fmt "\t${GCC} -I${INC} -I. -c %s_main.c@." basename; |
|
119 |
fprintf_dependencies fmt dependencies; |
|
120 |
fprintf fmt "\t${GCC} -o ${BINNAME} io_frontend.o %a %s.o %s_main.o %a@." |
|
121 |
(Utils.fprintf_list ~sep:" " (fun fmt dep -> |
|
122 |
Format.fprintf fmt "%s.o" dep.name)) |
|
123 |
(compiled_dependencies dependencies) |
|
124 |
basename (* library .o *) basename |
|
125 |
(* main function . o *) |
|
126 |
(Utils.fprintf_list ~sep:" " (fun fmt lib -> fprintf fmt "-l%s" lib)) |
|
127 |
(lib_dependencies dependencies); |
|
128 |
fprintf fmt "@."; |
|
129 |
fprintf fmt "clean:@."; |
|
130 |
fprintf fmt "\t\\rm -f *.o ${BINNAME}@."; |
|
131 |
fprintf fmt "@."; |
|
132 |
Mod.other_targets fmt basename nodename dependencies; |
|
133 |
fprintf fmt "@." |
|
134 |
end |
|
135 |
|
|
125 | 136 |
(* Local Variables: *) |
126 | 137 |
(* compile-command:"make -C ../../.." *) |
127 | 138 |
(* End: *) |
Also available in: Unified diff
reformatting