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