Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006642OCaml~DO NOT USE (was: OCaml general)public2014-11-03 08:312016-12-07 11:47
Reporterwhitequark 
Assigned Towhitequark 
PrioritynormalSeverityfeatureReproducibilityhave not tried
StatusclosedResolutionfixed 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version4.02.2+dev / +rc1 
Summary0006642: Add rpath-like $ORIGIN support to compiler flags embedded in cma/cmxa
DescriptionLet me begin with a motivating example. A typical native application consists of a bunch of executables and a bunch of shared libraries that can depend on each other. If you invoke an executable, the dynamic linker would normally only look into the default system paths like /lib, /usr/lib, etc. However, what if you want to make an application that doesn't care where it is installed?

Enter -rpath. -rpath instructs ld to embed a special string in the ELF executable or shared object that provides additional paths to the dynamic linker, a lá LD_LIBRARY_PATH (but it's not global or as fragile). What's more, it also replaces $ORIGIN in these paths with the path to the ELF file itself, allowing the application to be fully relocatable.

OCaml currently has something that's very similar to -rpath--it allows to embed compiler flags (and dllpaths) in the cma/cmxa. However, those paths must be absolute. I propose to make the frontend expand $CAMLORIGIN with the name of cma/cmxa file that is being linked together, so that a cma file in .../lib/ocaml could pass the -L.../lib flag to the linker.

This would greatly help the LLVM OCaml bindings, which currently have to rely on odd hacks, and I imagine there would be other uses as well.
Tagspatch
Attached Filespatch file icon camlorigin.patch [^] (5,048 bytes) 2015-01-07 23:04 [Show Content]

- Relationships

-  Notes
(0012898)
whitequark (developer)
2014-12-20 14:06

Reminder sent to: gasche

@gasche, I've attached a patch, can you please take a look?
(0012951)
doligez (administrator)
2014-12-22 22:42
edited on: 2014-12-22 22:44

I see 3 problems with this patch:
1. duplicate definition of replace_origin
2. replacement fails as soon as there is any $ sign anywhere else in the string
3. documentation doesn't mention the $(CAMLORIGIN) and ${CAMLORIGIN} syntaxes

I'm not convinced Buffer.add_substitute is the right tool for this job: not only it forces to add a layer of quoting for just one variable, but its quoting conventions are not surjective.

It looks like the $CAMLORIGIN notation only ever makes sense at the beginning of the string anyway, so why not search and replace it with simple substring manipulations?

(0012996)
whitequark (developer)
2014-12-29 00:43

I've updated the patch as per @doligez's suggestion. I decided against moving the three-line replacement function somewhere else; should I?
(0013001)
gasche (administrator)
2015-01-02 09:20

I think you should use Misc.search_substring to implement replace_origin (that would both make the code simpler and make it work for usages that are not at the beginning), and this suggests adding a Misc.replace_substring function to factorize usage -- note that it ought to replace all occurences of the substring, not only the first.

I find it problematic that the Changes entry mentions "$(CAMLORIGIN)" while only "$CAMLORIGIN" works according to the implementation.
(0013011)
whitequark (developer)
2015-01-06 02:00

Addressed.
(0013023)
doligez (administrator)
2015-01-07 17:59

OK to apply this patch, but it seems slightly broken as is (name of the replace_[sub]string function, argument labels or not).
(0013026)
whitequark (developer)
2015-01-07 23:04

Sorry for uploading an unverified patch--it builds now.
(0013066)
whitequark (developer)
2015-01-13 10:10

Could you also apply this to 4.02.2? I'd like $CAMLORIGIN to be used in the LLVM 3.6 release, which will branch tomorrow.
(0013179)
gasche (administrator)
2015-01-25 17:29
edited on: 2015-01-25 18:35

The replace function of the current patch is rather terrible for any usage other than $CAMLORIGIN (in particular it loops when replacing "a" with "a"). I think I'll push the patch in both 4.02 and trunk, but with a more sensible implementation, such as:

 let replace_substring ~pat ~sub str =
  let rec search acc curr =
    try
      let next = search_substring pat str curr in
      let prefix = String.sub str curr (next - curr) in
      search (prefix :: acc) (next + String.length pat)
    with Not_found ->
      let suffix = String.sub str curr (String.length str - curr) in
      List.rev (suffix :: acc)
  in String.concat sub (search [] 0)

(0013249)
whitequark (developer)
2015-02-07 00:35

Do I need to do something to have this accepted?
(0013250)
gasche (administrator)
2015-02-07 12:25

No: it's on my merge list, but I'm not sure I'll get to it this week-end.
(0013253)
gasche (administrator)
2015-02-08 12:18

Merged in trunk and 4.02. I decided to merge in 4.02 because the risk of breaking seems low (non-invasive patch that mostly adds a new feature), and whitequark considers using it for the newcoming ocaml-llvm release.

- Issue History
Date Modified Username Field Change
2014-11-03 08:31 whitequark New Issue
2014-12-20 14:06 whitequark File Added: camlorigin.patch
2014-12-20 14:06 whitequark Note Added: 0012898
2014-12-22 22:19 doligez Tag Attached: patch
2014-12-22 22:42 doligez Note Added: 0012951
2014-12-22 22:44 doligez Note Edited: 0012951 View Revisions
2014-12-22 22:45 doligez Status new => feedback
2014-12-29 00:43 whitequark File Deleted: camlorigin.patch
2014-12-29 00:43 whitequark File Added: camlorigin.patch
2014-12-29 00:43 whitequark Note Added: 0012996
2014-12-29 00:43 whitequark Status feedback => new
2015-01-02 09:20 gasche Note Added: 0013001
2015-01-06 01:58 whitequark File Deleted: camlorigin.patch
2015-01-06 02:00 whitequark Note Added: 0013011
2015-01-06 02:00 whitequark File Added: camlorigin.patch
2015-01-07 17:59 doligez Note Added: 0013023
2015-01-07 17:59 doligez Status new => confirmed
2015-01-07 23:04 whitequark File Deleted: camlorigin.patch
2015-01-07 23:04 whitequark File Added: camlorigin.patch
2015-01-07 23:04 whitequark Note Added: 0013026
2015-01-13 10:10 whitequark Note Added: 0013066
2015-01-25 17:29 gasche Note Added: 0013179
2015-01-25 18:35 gasche Note Edited: 0013179 View Revisions
2015-02-07 00:35 whitequark Note Added: 0013249
2015-02-07 12:25 gasche Note Added: 0013250
2015-02-08 12:18 gasche Note Added: 0013253
2015-02-08 12:18 gasche Status confirmed => resolved
2015-02-08 12:18 gasche Fixed in Version => 4.02.2+dev / +rc1
2015-02-08 12:18 gasche Resolution open => fixed
2015-02-08 12:18 gasche Assigned To => whitequark
2016-12-07 11:47 xleroy Status resolved => closed
2017-02-23 16:36 doligez Category OCaml general => -OCaml general
2017-03-03 17:55 doligez Category -OCaml general => -(deprecated) general
2017-03-03 18:01 doligez Category -(deprecated) general => ~deprecated (was: OCaml general)
2017-03-06 17:04 doligez Category ~deprecated (was: OCaml general) => ~DO NOT USE (was: OCaml general)


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker