Project

General

Profile

Download (4.66 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 Utils
13
open Format
14
open Lustre_types
15

    
16
let pp_dep fmt dep =
17
  fprintf fmt "%b, %s, {%a}, %b" dep.local dep.name Printers.pp_prog
18
    dep.content dep.is_stateful
19

    
20
let pp_deps fmt deps =
21
  fprintf fmt "@[<v 0>%a@ @]" (pp_comma_list pp_dep) deps
22

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

    
35
let header_libs header =
36
  List.fold_left
37
    (fun accu top ->
38
      match top.top_decl_desc with
39
      | ImportedNode nd ->
40
        Utils.list_union nd.nodei_in_lib accu
41
      | _ ->
42
        accu)
43
    [] header
44

    
45
let compiled_dependencies deps =
46
  List.filter (fun dep -> header_has_code dep.content) deps
47

    
48
let lib_dependencies deps =
49
  List.fold_left
50
    (fun accu dep -> Utils.list_union (header_libs dep.content) accu)
51
    [] deps
52

    
53
let fprintf_dependencies fmt (deps : dep_t list) =
54
  (* eprintf "Deps: %a@." pp_deps dep; *)
55
  let compiled_deps = compiled_dependencies deps in
56

    
57
  (* eprintf "Compiled Deps: %a@." pp_deps compiled_dep; *)
58
  List.iter
59
    (fun s ->
60
      Log.report ~level:1 (fun fmt -> fprintf fmt "Adding dependency: %s@." s);
61
      fprintf fmt "\t${GCC} -I${INC} -c %s@." s)
62
    ("${INC}/io_frontend.c"
63
     ::
64
     (* IO functions when a main function is computed *)
65
     List.map
66
       (fun dep ->
67
         (if dep.local then dep.name else Version.include_path ^ "/" ^ dep.name)
68
         ^ ".c")
69
       compiled_deps)
70

    
71
module type MODIFIERS_MKF = sig
72
  (* dep was (bool * ident * top_decl list) *)
73
  val other_targets : formatter -> string -> string -> dep_t list -> unit
74
end
75

    
76
module EmptyMod : MODIFIERS_MKF = struct
77
  let other_targets _ _ _ _ = ()
78
end
79

    
80
module Main =
81
functor
82
  (Mod : MODIFIERS_MKF)
83
  ->
84
  struct
85
    (* TODO: BEWARE OF THE BUG !! in case of very long nodename or maybe even
86
       basename, the string basename_nodename exceed a limit length for files on
87
       linux and prevent gcc to generate the binary of that name.
88

    
89
       To be solved (later) with - either the default name if it short - a
90
       shorter version if it is long (md5?) - a provided name given when calling
91
       the makefile so the user can control the expected name of the binary *)
92

    
93
    let print_makefile basename nodename (dependencies : dep_t list) fmt =
94
      let binname =
95
        let s = basename ^ "_" ^ nodename in
96
        if
97
          String.length s > 100
98
          (* seems that GCC fails from 144 characters and on: File name too long
99
             collect2 ld error. *)
100
        then
101
          if String.length nodename > 100 then basename ^ "_run"
102
            (* shorter version *)
103
          else nodename ^ "run"
104
        else s
105
      in
106
      fprintf fmt "BINNAME?=%s@." binname;
107
      fprintf fmt "GCC=gcc -O0@.";
108
      fprintf fmt "LUSTREC=%s@." Sys.executable_name;
109
      fprintf fmt "LUSTREC_BASE=%s@."
110
        (Filename.dirname (Filename.dirname Sys.executable_name));
111
      fprintf fmt "INC=%s@." Version.include_path
112
      (*"${LUSTREC_BASE}/include/lustrec"*);
113
      fprintf fmt "@.";
114

    
115
      (* Main binary *)
116
      fprintf fmt "%s_%s: %s.c %s_main.c@." basename "run" (*nodename*) basename
117
        basename;
118
      fprintf fmt "\t${GCC} -I${INC} -I. -c %s.c@." basename;
119
      fprintf fmt "\t${GCC} -I${INC} -I. -c %s_main.c@." basename;
120
      fprintf_dependencies fmt dependencies;
121
      fprintf fmt "\t${GCC} -o ${BINNAME} io_frontend.o %a %s.o %s_main.o %a@."
122
        (pp_print_list (fun fmt dep -> fprintf fmt "%s.o" dep.name))
123
        (compiled_dependencies dependencies)
124
        basename (* library .o *) basename
125
        (* main function . o *)
126
        (pp_print_list (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

    
136
(* Local Variables: *)
137
(* compile-command:"make -C ../../.." *)
138
(* End: *)
(11-11/18)