Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / C / c_backend_makefile.ml @ 5fccce23

History | View | Annotate | Download (4.49 KB)

1 b38ffff3 ploc
(********************************************************************)
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 522938b5 ploc
open Format
13 8446bf03 ploc
open Lustre_types
14 522938b5 ploc
open Corelang
15
16 5fccce23 ploc
let pp_dep fmt dep =
17 0bca9d53 ploc
  Format.fprintf fmt "%b, %s, {%a}, %b"
18 5fccce23 ploc
    dep.local dep.name Printers.pp_prog dep.content dep.is_stateful
19 0bca9d53 ploc
  
20
let pp_deps fmt deps = Format.fprintf fmt "@[<v 0>%a@ @]" (Utils.fprintf_list ~sep:"@ ," pp_dep) deps
21
22 522938b5 ploc
let header_has_code header =
23
  List.exists 
24
    (fun top -> 
25
      match top.top_decl_desc with
26 70e1006b xthirioux
      | Const _ -> true 
27 53206908 xthirioux
      | ImportedNode nd -> nd.nodei_in_lib = []
28 522938b5 ploc
      | _ -> 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 53206908 xthirioux
      | ImportedNode nd -> Utils.list_union nd.nodei_in_lib accu
36 522938b5 ploc
      | _ -> accu 
37
  ) [] header 
38
    
39 e95470b3 ploc
40 5fccce23 ploc
let compiled_dependencies deps = 
41
  List.filter (fun dep -> header_has_code dep.content) deps
42 e95470b3 ploc
43 5fccce23 ploc
let lib_dependencies deps = 
44 e95470b3 ploc
  List.fold_left 
45 5fccce23 ploc
    (fun accu dep -> Utils.list_union (header_libs dep.content) accu) [] deps
46 e95470b3 ploc
    
47 5fccce23 ploc
let fprintf_dependencies fmt (deps: dep_t list) =
48 0bca9d53 ploc
  (* Format.eprintf "Deps: %a@." pp_deps dep; *)
49 5fccce23 ploc
  let compiled_deps = compiled_dependencies deps in
50 0bca9d53 ploc
  (* Format.eprintf "Compiled Deps: %a@." pp_deps compiled_dep; *)
51
 
52 8e6cab20 ploc
  List.iter (fun s -> Log.report ~level:1 (fun fmt -> fprintf fmt "Adding dependency: %s@." s);  
53 e95470b3 ploc
    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 5fccce23 ploc
	   (fun dep -> 
57
	     (if dep.local then dep.name else Version.include_path ^ "/" ^ dep.name) ^ ".c")
58
	   compiled_deps))
59 e95470b3 ploc
60
module type MODIFIERS_MKF =
61 830de634 ploc
sig (* dep was (bool * ident * top_decl list) *)
62
  val other_targets: Format.formatter -> string -> string -> dep_t list -> unit
63 e95470b3 ploc
end
64
65
module EmptyMod =
66 830de634 ploc
(struct
67 e95470b3 ploc
  let other_targets _ _ _ _ = ()
68 830de634 ploc
end: MODIFIERS_MKF)
69 e95470b3 ploc
70
module Main = functor (Mod: MODIFIERS_MKF) -> 
71
struct
72
73 f76eae4f ploc
(* 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 e95470b3 ploc
85 6a078e8a ploc
  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 e95470b3 ploc
105 6a078e8a ploc
    (* 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 5fccce23 ploc
      (Utils.fprintf_list ~sep:" " (fun fmt dep -> Format.fprintf fmt "%s.o" dep.name)) 
112 6a078e8a ploc
      (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 522938b5 ploc
    ;
117 6a078e8a ploc
    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 e95470b3 ploc
124 713176a4 ploc
end
125 522938b5 ploc
126
(* Local Variables: *)
127
(* compile-command:"make -C ../../.." *)
128
(* End: *)