Project

General

Profile

Download (5.08 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
(* 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 arrow_suffix fmt deps =
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
    :: sprintf "${INC}/arrow%s.c" arrow_suffix
67
    :: (* IO functions when a main function is computed *)
68
       List.map
69
         (fun dep ->
70
           (if dep.local then dep.name
71
           else Version.include_path ^ "/" ^ dep.name)
72
           ^ ".c")
73
         compiled_deps)
74

    
75
module type MODIFIERS_MKF = sig
76
  (* dep was (bool * ident * top_decl list) *)
77
  val other_targets : formatter -> string -> string -> dep_t list -> unit
78
  val pp_print_dependencies: formatter -> dep_t list -> unit
79
  val pp_arrow_o: formatter -> unit -> unit
80
end
81

    
82
module EmptyMod : MODIFIERS_MKF = struct
83
  let other_targets _ _ _ _ = ()
84
  let pp_print_dependencies = fprintf_dependencies ""
85
  let pp_arrow_o fmt () = pp_print_string fmt "arrow.o"
86
end
87

    
88
module Main =
89
functor
90
  (Mod : MODIFIERS_MKF)
91
  ->
92
  struct
93
    (* TODO: BEWARE OF THE BUG !! in case of very long nodename or maybe even
94
       basename, the string basename_nodename exceed a limit length for files on
95
       linux and prevent gcc to generate the binary of that name.
96

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

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

    
125
      (* Main binary *)
126
      fprintf
127
        fmt
128
        "%s_%s: %s.c %s_main.c@."
129
        basename
130
        "run"
131
        (*nodename*) basename
132
        basename;
133
      fprintf fmt "\t${GCC} -I${INC} -I. -c %s.c@." basename;
134
      fprintf fmt "\t${GCC} -I${INC} -I. -c %s_main.c@." basename;
135
      Mod.pp_print_dependencies fmt dependencies;
136
      fprintf
137
        fmt
138
        "\t${GCC} -o ${BINNAME} io_frontend.o %a %a %s.o %s_main.o %a@."
139
        Mod.pp_arrow_o ()
140
        (pp_print_list (fun fmt dep -> fprintf fmt "%s.o" dep.name))
141
        (compiled_dependencies dependencies)
142
        basename
143
        (* library .o *) basename
144
        (* main function . o *)
145
        (pp_print_list (fun fmt lib -> fprintf fmt "-l%s" lib))
146
        (lib_dependencies dependencies);
147
      fprintf fmt "@.";
148
      fprintf fmt "clean:@.";
149
      fprintf fmt "\t\\rm -f *.o ${BINNAME}@.";
150
      fprintf fmt "@.";
151
      Mod.other_targets fmt basename nodename dependencies;
152
      fprintf fmt "@."
153
  end
154

    
155
(* Local Variables: *)
156
(* compile-command:"make -C ../../.." *)
157
(* End: *)
(11-11/18)