Ocamlbuild插件:仅在目标已更改时才构建动态依赖项

时间:2015-09-06 16:38:19

标签: plugins hyperlink ocamlbuild

我正在尝试编写一个ocamlbuild插件。目标是构建Qt应用程序(暂时没有 OCaml代码的)。为了处理最后的链接阶段,我在.qtobjs文件(如.itarget)中编写了objs,mocs,uis和rcs列表,并在.qtpackages文件中编写了Qt库列表。适用于这些文件的规则会解析这些列表,并在链接所有内容之前动态构建对象,moc等。

问题在于我不想动态构建未更改的目标。请参阅以下规则:

rule "Link a c++ executable that uses Qt."
  ~deps:["%.qtobjs"; "%.qtpackages"]
  ~prod:"%.cxxnative"
  (fun env build ->
     let dir = Pathname.dirname (env "%.qtobjs") in
     let output = env "%.cxxnative" in
     let objlist = pwd / (env "%.qtobjs") in
     let liblist = pwd / (env "%.qtpackages") in
     let packages = read_lines liblist in
     let lflags = List.map (fun p -> "-l"^p) packages
                  @ find_all "lflags" in
     let objs = List.map (fun o -> dir / o) (read_lines objlist) in



     (* Here, I would like to forget the targets that have not changed *)
     let objs_to_build = List.filter 
         (fun target -> 
            (* something to tell if "target" has to be rebuilt *))
         objs in



     (* This is where I build the dependencies : *)
     let success_builds = build 
         (List.map (fun o -> [o]) 
         objs_to_build) in


     let objects = filter_map
         (function
           | Outcome.Good x when get_extension x = "opp" ->
             Some (pwd / "_build" / (update_extension "o" x))
           | Outcome.Good _ -> None 
             (* Because I don't want to link the ui.h file generated by uic *)
           | Outcome.Bad exn -> raise exn)
         success_builds in
     let tags_objs = tags_of_pathname (env "%.qtobjs") ++ "link" in
     let tags_packs = tags_of_pathname (env "%.qtpackages") in
     Cmd (S [A (find_one "cxx"); T tags_objs; T tags_packs; Command.atomize lflags;
             A "-o"; P output; Command.atomize objects]));

我不想打电话给#34; build"如果没有必要的话。

NB:功能

newer_than: Pathname.t -> Pathname.t -> bool

可以解决我的问题。

1 个答案:

答案 0 :(得分:0)

问题解决了。

对于那些感兴趣的人来说,问题出在COMPILING规则中:因为" .o"规则已被ocamlbuild使用,我将其注册为正式生产" .opp"文件,但命令实际上写了.o文件。因此,当重新编​​译时,ocamlbuild不知道目标文件已经存在。

三个选项:

  • 替换默认的" .o"规则(我甚至不知道是否可能?),
  • 生产" .opp"文件(但不太标准),
  • 如果.o文件存在,
  • 或更改编译规则以执行Nop:

    if Pathname.exists (env "%.o") then Nop
    else Cmd (S [A ("c++");
                 A "-c";
                 A "-o"; P (env "%.o"); P (env "%.cpp")]));