Skip to content

Commit

Permalink
start to factorize function for generators
Browse files Browse the repository at this point in the history
  • Loading branch information
gautierhattenberger committed Dec 22, 2010
1 parent 7746f13 commit 9d2421a
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 135 deletions.
31 changes: 0 additions & 31 deletions sw/tools/gen_aircraft.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,6 @@ let targets_of_field = fun field ->
with
_ -> []

(** singletonize a sorted list *)
let rec singletonize = fun l ->
match l with
[] | [_] -> l
| x :: ((y :: t) as yt) -> if x = y then singletonize yt else x :: singletonize yt

(** union of two lists *)
let union = fun l1 l2 ->
let l = l1 @ l2 in
let sl = List.sort compare l in
singletonize sl

let union_of_lists = fun l ->
let sl = List.sort compare (List.flatten l) in
singletonize sl

(** [get_modules dir xml]
* [dir] is the conf directory for modules, [xml] is the parsed airframe.xml *)
let get_modules = fun dir xml ->
Expand All @@ -103,21 +87,6 @@ let get_modules = fun dir xml ->
(* return a list of name and a list of pairs (xml, xml list) *)
List.split extract

(** [get_targets_of_module xml] Returns the list of targets of a module *)
let get_targets_of_module = fun m ->
let targets = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"makefile" -> targets_of_field x
| _ -> []
) (Xml.children m) in
(* return a singletonized list *)
singletonize (List.sort compare (List.flatten targets))

(** [get_modules_dir xml] Returns the list of modules directories *)
let get_modules_dir = fun modules ->
let dir = List.map (fun (m, _) -> try Xml.attrib m "dir" with _ -> ExtXml.attrib m "name") modules in
singletonize (List.sort compare dir)

(**
Search and dump the module section :
xml : the parsed airframe.xml
Expand Down
142 changes: 142 additions & 0 deletions sw/tools/gen_common.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
(*
* $Id$
*
* generic tools for modules
*
* Copyright (C) 2010 Gautier Hattenberger
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*)

open Printf

let paparazzi_conf = Env.paparazzi_home // "conf"
let modules_dir = paparazzi_conf // "modules"

(** remove all duplicated elements of a list *)
let singletonize = fun l ->
let rec loop = fun l ->
match l with
[] | [_] -> l
| x::((x'::_) as xs) ->
if x = x' then loop xs else x::loop xs in
loop (List.sort compare l)

(** union of two lists *)
let union = fun l1 l2 ->
let l = l1 @ l2 in
let sl = List.sort compare l in
singletonize sl

(** union of a list of list *)
let union_of_lists = fun l ->
let sl = List.sort compare (List.flatten l) in
singletonize sl

(* gm *)
let get_modules = fun dir m ->
match Xml.tag m with
"load" -> begin
let name = ExtXml.attrib m "name" in
let xml = Xml.parse_file (dir^name) in
xml
end
| _ -> xml_error "load"

let unload_unused_modules = fun modules ->
let target = try Sys.getenv "TARGET" with _ -> "" in
let is_target_in_module = fun m ->
let target_is_in_module = List.exists (fun x -> String.compare target x = 0) (get_targets_of_module m) in
if not target_is_in_module then
Printf.fprintf stderr "Module %s unloaded, target %s not supported\n" (Xml.attrib m "name") target;
target_is_in_module
in
if String.length target = 0 then
modules
else
List.find_all is_target_in_module modules


(* gp *)
(** [get_targets_of_module xml] Returns the list of targets of a module *)
let get_targets_of_module = fun m ->
let pipe_regexp = Str.regexp "|" in
let targets_of_field = fun field -> try
Str.split pipe_regexp (ExtXml.attrib_or_default field "target" "ap|sim") with _ -> [] in
let targets = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"makefile" -> targets_of_field x
| _ -> []
) (Xml.children m) in
(* return a singletonized list *)
singletonize (List.sort compare (List.flatten targets))

let unload_unused_modules = fun modules ->
let target = try Sys.getenv "TARGET" with _ -> "" in
let is_target_in_module = fun m ->
let target_is_in_module = List.exists (fun x -> String.compare target x = 0) (get_targets_of_module m) in
target_is_in_module
in
if String.length target = 0 then
modules
else
List.find_all is_target_in_module modules

(** [get_modules_name dir xml] Returns a list of modules name *)
let get_modules_name = fun dir xml ->
(* extract all "modules" sections *)
let modules = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"modules" -> Xml.children x
| _ -> []
) (Xml.children xml) in
(* flatten the list (result is a list of "load" xml nodes) *)
let modules = List.flatten modules in
(* parse modules *)
let modules = List.map (fun m -> ExtXml.parse_file (dir // ExtXml.attrib m "name")) modules in
(* filter the list if target is not supported *)
let modules = unload_unused_modules modules in
(* return a list of modules name *)
List.map (fun m -> ExtXml.attrib m "name") modules

(* ga *)
(* extract all "modules" sections *)
let modules = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"modules" -> Xml.children x
| _ -> []
) (Xml.children xml) in
(* flatten the list (result is a list of "load" xml nodes) *)
let modules = List.flatten modules in

(** [get_targets_of_module xml] Returns the list of targets of a module *)
let get_targets_of_module = fun m ->
let targets = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"makefile" -> targets_of_field x
| _ -> []
) (Xml.children m) in
(* return a singletonized list *)
singletonize (List.sort compare (List.flatten targets))

(** [get_modules_dir xml] Returns the list of modules directories *)
let get_modules_dir = fun modules ->
let dir = List.map (fun (m, _) -> try Xml.attrib m "dir" with _ -> ExtXml.attrib m "name") modules in
singletonize (List.sort compare dir)

49 changes: 1 addition & 48 deletions sw/tools/gen_modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ let print_init_functions = fun modules ->
left ();
lprintf out_h "}\n"

let remove_dup = fun l ->
let rec loop = fun l ->
match l with
[] | [_] -> l
| x::((x'::_) as xs) ->
if x = x' then loop xs else x::loop xs in
loop (List.sort compare l)

let print_periodic_functions = fun modules ->
let min_period = 1. /. float !freq
and max_period = 65536. /. float !freq
Expand All @@ -131,7 +123,7 @@ let print_periodic_functions = fun modules ->
((x, module_name), min 65535 (max 1 (int_of_float (float_of_int !freq /. f))))
)
periodic) modules) in
let modulos = remove_dup (List.map snd functions_modulo) in
let modulos = singletonize (List.map snd functions_modulo) in
(** Print modulos *)
List.iter (fun modulo ->
let v = sprintf "i%d" modulo in
Expand Down Expand Up @@ -261,15 +253,6 @@ let parse_modules modules =
nl ();
fprintf out_h "#endif // MODULES_DATALINK_C\n"

let get_modules = fun dir m ->
match Xml.tag m with
"load" -> begin
let name = ExtXml.attrib m "name" in
let xml = Xml.parse_file (dir^name) in
xml
end
| _ -> xml_error "load"

let test_section_modules = fun xml ->
List.fold_right (fun x r -> ExtXml.tag_is x "modules" || r) (Xml.children xml) false

Expand Down Expand Up @@ -324,36 +307,6 @@ let write_settings = fun xml_file out_set modules ->
fprintf out_set " </dl_settings>\n";
fprintf out_set "</settings>\n"

let get_targets_of_module = fun m ->
let pipe_regexp = Str.regexp "|" in
let targets_of_field = fun field -> try
Str.split pipe_regexp (ExtXml.attrib_or_default field "target" "ap|sim") with _ -> [] in
let rec singletonize = fun l ->
match l with
[] | [_] -> l
| x :: ((y :: t) as yt) -> if x = y then singletonize yt else x :: singletonize yt
in
let targets = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"makefile" -> targets_of_field x
| _ -> []
) (Xml.children m) in
(* return a singletonized list *)
singletonize (List.sort compare (List.flatten targets))

let unload_unused_modules = fun modules ->
let target = try Sys.getenv "TARGET" with _ -> "" in
let is_target_in_module = fun m ->
let target_is_in_module = List.exists (fun x -> String.compare target x = 0) (get_targets_of_module m) in
if not target_is_in_module then
Printf.fprintf stderr "Module %s unloaded, target %s not supported\n" (Xml.attrib m "name") target;
target_is_in_module
in
if String.length target = 0 then
modules
else
List.find_all is_target_in_module modules

let h_name = "MODULES_H"

let () =
Expand Down
57 changes: 1 addition & 56 deletions sw/tools/gen_periodic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ let lprintf = fun c f ->

(**let freq = ref 60 *)

let remove_dup = fun l ->
let rec loop = fun l ->
match l with
[] | [_] -> l
| x::((x'::_) as xs) ->
if x = x' then loop xs else x::loop xs in
loop (List.sort compare l)


let output_modes = fun avr_h process_name channel_name modes freq modules ->
let min_period = 1./.float freq in
let max_period = 65536. /. float freq in
Expand All @@ -74,7 +65,7 @@ let output_modes = fun avr_h process_name channel_name modes freq modules ->
fprintf stderr "Warning: period is bound between %.3fs and %.3fs for message %s\n%!" min_period max_period (ExtXml.attrib x "name");
(x, min 65535 (max 1 (int_of_float (p*.float_of_int freq))))
) filtered_msg in
let modulos = remove_dup (List.map snd messages) in
let modulos = singletonize (List.map snd messages) in
List.iter (fun m ->
let v = sprintf "i%d" m in
let _type = if m >= 256 then "uint16_t" else "uint8_t" in
Expand Down Expand Up @@ -107,52 +98,6 @@ let output_modes = fun avr_h process_name channel_name modes freq modules ->
lprintf avr_h "}\\\n")
modes

(** [get_targets_of_module xml] Returns the list of targets of a module *)
let get_targets_of_module = fun m ->
let pipe_regexp = Str.regexp "|" in
let targets_of_field = fun field -> try
Str.split pipe_regexp (ExtXml.attrib_or_default field "target" "ap|sim") with _ -> [] in
let rec singletonize = fun l ->
match l with
[] | [_] -> l
| x :: ((y :: t) as yt) -> if x = y then singletonize yt else x :: singletonize yt
in
let targets = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"makefile" -> targets_of_field x
| _ -> []
) (Xml.children m) in
(* return a singletonized list *)
singletonize (List.sort compare (List.flatten targets))

let unload_unused_modules = fun modules ->
let target = try Sys.getenv "TARGET" with _ -> "" in
let is_target_in_module = fun m ->
let target_is_in_module = List.exists (fun x -> String.compare target x = 0) (get_targets_of_module m) in
target_is_in_module
in
if String.length target = 0 then
modules
else
List.find_all is_target_in_module modules

(** [get_modules_name dir xml] Returns a list of modules name *)
let get_modules_name = fun dir xml ->
(* extract all "modules" sections *)
let modules = List.map (fun x ->
match String.lowercase (Xml.tag x) with
"modules" -> Xml.children x
| _ -> []
) (Xml.children xml) in
(* flatten the list (result is a list of "load" xml nodes) *)
let modules = List.flatten modules in
(* parse modules *)
let modules = List.map (fun m -> ExtXml.parse_file (dir // ExtXml.attrib m "name")) modules in
(* filter the list if target is not supported *)
let modules = unload_unused_modules modules in
(* return a list of modules name *)
List.map (fun m -> ExtXml.attrib m "name") modules


let _ =
if Array.length Sys.argv <> 5 then begin
Expand Down

0 comments on commit 9d2421a

Please sign in to comment.