Project

General

Profile

Download (4.74 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" dep.local dep.name Printers.pp_prog
17
    dep.content dep.is_stateful
18

    
19
let pp_deps fmt deps =
20
  Format.fprintf fmt "@[<v 0>%a@ @]" (Utils.fprintf_list ~sep:"@ ," pp_dep) deps
21

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

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

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

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

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

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

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

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

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

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

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

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