Project

General

Profile

Download (4.51 KB) Statistics
| Branch: | Tag: | Revision:
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

    
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
20

    
21
let header_has_code header =
22
  List.exists 
23
    (fun top -> 
24
      match top.top_decl_desc with
25
      | Const _ -> true 
26
      | ImportedNode nd -> nd.nodei_in_lib = []
27
      | _ -> false
28
    )
29
    header
30

    
31
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 = 
40
  List.filter (fun dep -> header_has_code dep.content) deps
41

    
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
  (* Format.eprintf "Deps: %a@." pp_deps dep; *)
48
  let compiled_deps = compiled_dependencies deps in
49
  (* 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
62
end
63

    
64
module EmptyMod =
65
(struct
66
  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
end
124

    
125
(* Local Variables: *)
126
(* compile-command:"make -C ../../.." *)
127
(* End: *)
(7-7/10)