ocaml / ocamlbuild

The legacy OCamlbuild build manager
Other
121 stars 81 forks source link

MPR#5963: Include support for rpath and static linking flags in ocamlbuild #199

Open damiendoligez opened 7 years ago

damiendoligez commented 7 years ago

PR transferred from https://caml.inria.fr/mantis/view.php?id=5963 [original reporter: @hcarty]

These are rather straightforward to support with command line flags or a myocamlbuild.ml plugin. It would be nice to have more robust support in the ocamlbuild tool itself though. For example, the sample plugin I've included only supports a single link.rpath file. It would be nice to have the name of the file come from the matching executable target (foo.rpath -> foo.native).


An example myocamlbuild.ml which:

  1. Creates an rpath tag and reads link path information from link.rpath
  2. Creates a static tag for fully statically linked executables

    open Ocamlbuild_plugin
    
    let rpaths = string_list_of_file "link.rpath"
    let static_flags = S [A "-cclib"; A "-static"]
    
    let () = dispatch begin function
      | After_rules ->
        List.iter (
          fun path ->
            let rpath_flags = S [A "-cclib"; A ("-Wl,-rpath," ^ path)] in
            flag ["ocaml"; "link"; "program"; "rpath"] rpath_flags;
            flag ["ocaml"; "link"; "library"; "rpath"] rpath_flags;
        ) rpaths;
        flag ["ocaml"; "link"; "program"; "static"] static_flags;
      | _ -> ()
    end
whitequark commented 7 years ago

I think this would make more sense as a parameterized flag e.g. rpath($ORIGIN/../lib) than a file.

hcarty commented 7 years ago

I wrote an overly simple plugin here: https://github.com/hcarty/ocamlbuild-plugins/tree/master/tests/rpath with a parameterized flag.

whitequark commented 7 years ago

@hcarty A pull request that adds these tags and a test would be warmly welcome.

hcarty commented 7 years ago

I'll hopefully have a chance to look at this next week.