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