Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1882: dylib.path

File dylib.path, 13.9 kB (added by doob, 2 years ago)
Line 
1 Index: build/script/bob.rb
2 ===================================================================
3 --- build/script/bob.rb (revision 5403)
4 +++ build/script/bob.rb (working copy)
5 @@ -104,7 +104,7 @@
6  end
7  
8  class FileFilter
9 -   VERSION = 1.1
10 +   VERSION = 1.2
11     @@builders = {}
12    
13     def initialize (args)
14 @@ -131,6 +131,8 @@
15         exclude("tango/core/rt/compiler/ldc");
16         exclude("tango/core/vendor")
17        
18 +       exclude("tango/core/rt/util/darwin")
19 +       
20         include("tango/core/rt/compiler/" + args.target)
21         include("tango/core/vendor/" + args.target)
22     end
23 @@ -197,12 +199,20 @@
24        
25         file = File.join(@args.objs, obj)
26        
27 -       @libs << file << eol if File.exists?(file)
28 +       @libs << file << eol if File.exists?(file) && file !~ /cmain/
29     end
30    
31     def makeLib
32 -       exec("ar -r " + @args.lib + " " + @libs.string) if (@libs.length > 0)
33 -   end
34 +       if @args.dynamic
35 +           if (@libs.length > 0)
36 +               options = "-dynamiclib -install_name @rpath/#{File.basename(@args.lib)} -Xlinker -headerpad_max_install_names" if DARWIN
37 +               
38 +               exec("gcc #{options} -o #{@args.lib} #{@libs.string} -lz -lbz2")
39 +           end
40 +       else
41 +           exec("ar -r #{@args.lib} #{@libs.string}") if (@libs.length > 0)
42 +       end
43 +   end
44    
45     def exec (cmd)
46         exec2(cmd, nil, nil)
47 @@ -275,6 +285,7 @@
48     def initialize (args, os, dmd, ldc, gdc)
49         super(args)
50         include("tango/sys/" + os)
51 +       include("tango/core/rt/util/darwin") if os == "darwin"
52         FileFilter.register(os, "dmd", :dmd, self)
53         FileFilter.register(os, "ldc", :ldc, self)
54         FileFilter.register(os, "gdc", :gdc, self)
55 @@ -370,7 +381,8 @@
56  end
57  
58  Args = Struct.new(:verbose, :inhibit, :include, :target, :compiler,
59 -                 :flags, :lib, :os, :core, :root, :filter, :quick, :objs) do
60 +                 :flags, :lib, :os, :core, :root, :filter, :quick,
61 +                 :objs, :dynamic) do
62                    
63     def initialize
64         self.verbose = false
65 @@ -388,6 +400,7 @@
66         self.filter = false
67         self.quick = false
68         self.objs = File.expand_path("objs")
69 +       self.dynamic = false
70        
71         self.os = ""
72         self.os = "darwin" if DARWIN
73 @@ -434,12 +447,20 @@
74             options.compiler = opt
75         end
76        
77 +       opts.on("-d", "--dynamic", "Build Tango as a dynamic/shared library") do |opt|
78 +           if DARWIN
79 +               options.dynamic = true
80 +           else
81 +               die "Building Tango as a dynamic/shared library is currently not supported on this platform."
82 +           end
83 +       end
84 +       
85         opts.on("-o", "--options OPTIONS", "Specify D compiler options") do |opt|
86             options.flags = opt
87         end
88        
89         opts.on("-l", "--library NAME", "Specify library name (sans .ext)") do |opt|
90 -           options.lib = opt + libext
91 +           options.lib = opt
92         end
93        
94         opts.on(nil, "--objs PATH", "Specify the path where to place temporary object files (defaults to ./objs)") do |opt|
95 @@ -476,8 +497,18 @@
96                 die "No package filter given" unless options.filter
97             end
98            
99 -           options.lib += ".lib" if WINDOWS
100 -           options.lib += ".a" unless WINDOWS
101 +           if options.dynamic
102 +               if DARWIN
103 +                   options.lib += ".dylib"
104 +               elsif WINDOWS
105 +                   options.lib += ".dll"
106 +               else
107 +                   options.lib += ".so"
108 +               end
109 +           else
110 +               options.lib += ".lib" if WINDOWS
111 +               options.lib += ".a" unless WINDOWS
112 +           end
113            
114             options.root = File.expand_path(args[0])
115         end
116 Index: tango/text/locale/Win32.d
117 ===================================================================
118 --- tango/text/locale/Win32.d   (revision 5403)
119 +++ tango/text/locale/Win32.d   (working copy)
120 @@ -12,6 +12,8 @@
121  
122  module tango.text.locale.Win32;
123  
124 +version (Windows):
125 +
126  alias tango.text.locale.Win32 nativeMethods;
127  
128  extern (Windows)
129 Index: tango/core/rt/compiler/dmd/object_.d
130 ===================================================================
131 --- tango/core/rt/compiler/dmd/object_.d    (revision 5403)
132 +++ tango/core/rt/compiler/dmd/object_.d    (working copy)
133 @@ -48,6 +48,9 @@
134      import rt.compiler.dmd.rt.aaA;
135      debug(PRINTF) import tango.stdc.stdio: printf;
136      extern (C) Object _d_newclass(ClassInfo ci);
137 +   
138 +    version (darwin)
139 +        import rt.compiler.util.darwin.Image;
140  }
141  
142  // NOTE: For some reason, this declaration method doesn't work
143 @@ -1220,15 +1223,6 @@
144      extern (C) ModuleReference *_Dmodule_ref;   // start of linked list
145  }
146  
147 -version( OSX )
148 -{
149 -    extern (C)
150 -    {
151 -    extern void* _minfo_beg;
152 -    extern void* _minfo_end;
153 -    }
154 -}
155 -
156  ModuleInfo[] _moduleinfo_dtors;
157  uint         _moduleinfo_dtors_i;
158  
159 @@ -1280,9 +1274,14 @@
160       * and __minfo_end. The variables _minfo_beg and _minfo_end
161       * are of zero size and are in the two bracketing segments,
162       * respectively.
163 +     *
164 +     * The __minfo_beg and __minfo_end sections are not put into
165 +     * dynamic libraries therefore we cannot use them or the
166 +     * _minfo_beg and _minfo_end variables. Instead we loop through
167 +     * all the loaded images (executables and dynamic libraries) and
168 +     * collect all the ModuleInfo references.
169       */
170 -        size_t length = cast(ModuleInfo*)&_minfo_end - cast(ModuleInfo*)&_minfo_beg;
171 -        _moduleinfo_array = (cast(ModuleInfo*)&_minfo_beg)[0 .. length];
172 +        _moduleinfo_array = getSectionData!(ModuleInfo, "__DATA", "__minfodata");       
173          debug(PRINTF) printf("moduleinfo: ptr = %p, length = %d\n", _moduleinfo_array.ptr, _moduleinfo_array.length);
174  
175          debug(PRINTF) foreach (m; _moduleinfo_array)
176 Index: tango/core/rt/compiler/dmd/dynamic_lib_fixes.c
177 ===================================================================
178 --- tango/core/rt/compiler/dmd/dynamic_lib_fixes.c  (revision 0)
179 +++ tango/core/rt/compiler/dmd/dynamic_lib_fixes.c  (revision 0)
180 @@ -0,0 +1 @@
181 +void* _Dmain __attribute__ ((weak));
182 \ No newline at end of file
183 Index: tango/core/rt/compiler/dmd/posix/deh.d
184 ===================================================================
185 --- tango/core/rt/compiler/dmd/posix/deh.d  (revision 5403)
186 +++ tango/core/rt/compiler/dmd/posix/deh.d  (working copy)
187 @@ -29,10 +29,18 @@
188  import tango.stdc.stdio : printf;
189  import tango.stdc.stdlib : exit;
190  
191 +version (darwin)
192 +    import rt.compiler.util.darwin.Image;
193 +
194  extern (C)
195  {
196 -    extern void* _deh_beg;
197 -    extern void* _deh_end;
198 +    version (darwin) {}
199 +   
200 +    else
201 +    {
202 +        extern void* _deh_beg;
203 +        extern void* _deh_end;
204 +    }
205  
206      int _d_isbaseof(ClassInfo oc, ClassInfo c);
207  }
208 @@ -99,26 +107,49 @@
209  
210  DHandlerTable *__eh_finddata(void *address)
211  {
212 -    FuncTable *ft;
213 -
214 -//    debug printf("__eh_finddata(address = x%x)\n", address);
215 -//    debug printf("_deh_beg = x%x, _deh_end = x%x\n", &_deh_beg, &_deh_end);
216 -    for (ft = cast(FuncTable *)&_deh_beg;
217 -         ft < cast(FuncTable *)&_deh_end;
218 -         ft++)
219 +    version (darwin)
220      {
221 -//      debug printf("\tfptr = x%x, fsize = x%03x, handlertable = x%x\n",
222 -//              ft.fptr, ft.fsize, ft.handlertable);
223 +        static FuncTable[] functionTables;
224 +        static bool hasFunctionTables;
225 +       
226 +        if (!hasFunctionTables)
227 +        {
228 +            functionTables = getSectionData!(FuncTable, "__DATA", "__deh_eh");
229 +            hasFunctionTables = true;
230 +        }
231 +       
232 +        foreach (ft ; functionTables)
233 +        {
234 +            if (ft.fptr <= address && address < cast(void *)(cast(char *)ft.fptr + ft.fsize))
235 +                return ft.handlertable;
236 +        }
237 +               
238 +        return null;
239 +    }
240 +   
241 +    else
242 +    {
243 +        FuncTable *ft;
244  
245 -        if (ft.fptr <= address &&
246 -            address < cast(void *)(cast(char *)ft.fptr + ft.fsize))
247 +    //      debug printf("__eh_finddata(address = x%x)\n", address);
248 +    //    debug printf("_deh_beg = x%x, _deh_end = x%x\n", &_deh_beg, &_deh_end);
249 +        for (ft = cast(FuncTable *)&_deh_beg;
250 +             ft < cast(FuncTable *)&_deh_end;
251 +             ft++)
252          {
253 -//          debug printf("\tfound handler table\n");
254 -            return ft.handlertable;
255 +    //      debug printf("\tfptr = x%x, fsize = x%03x, handlertable = x%x\n",
256 +    //              ft.fptr, ft.fsize, ft.handlertable);
257 +
258 +            if (ft.fptr <= address &&
259 +                address < cast(void *)(cast(char *)ft.fptr + ft.fsize))
260 +            {
261 +    //          debug printf("\tfound handler table\n");
262 +                return ft.handlertable;
263 +            }
264          }
265 +    //    debug printf("\tnot found\n");
266 +        return null;
267      }
268 -//    debug printf("\tnot found\n");
269 -    return null;
270  }
271  
272  
273 Index: tango/core/rt/compiler/util/darwin/getsect.d
274 ===================================================================
275 --- tango/core/rt/compiler/util/darwin/getsect.d    (revision 0)
276 +++ tango/core/rt/compiler/util/darwin/getsect.d    (revision 0)
277 @@ -0,0 +1,16 @@
278 +/**
279 + * Copyright: Copyright (c) 2010 Jacob Carlborg. All rights reserved.
280 + * Authors: Jacob Carlborg
281 + * Version: Initial created: Mar 16, 2010
282 + * License: BSD style: $(LICENSE)
283 + */
284 +module rt.compiler.util.darwin.getsect;
285 +
286 +version (darwin):
287 +
288 +import rt.compiler.util.darwin.loader;
289 +
290 +extern (C):
291 +
292 +section* getsectbynamefromheader (in mach_header* mhp, in char* segname, in char* sectname);
293 +section_64* getsectbynamefromheader_64 (mach_header_64* mhp, in char* segname, in char* sectname);
294 \ No newline at end of file
295 Index: tango/core/rt/compiler/util/darwin/Image.d
296 ===================================================================
297 --- tango/core/rt/compiler/util/darwin/Image.d  (revision 0)
298 +++ tango/core/rt/compiler/util/darwin/Image.d  (revision 0)
299 @@ -0,0 +1,144 @@
300 +/**
301 + * Copyright: Copyright (c) 2010 Jacob Carlborg. All rights reserved.
302 + * Authors: Jacob Carlborg
303 + * Version: Initial created: Feb 23, 2010
304 + * License: BSD style: $(LICENSE)
305 + */
306 +module rt.compiler.util.darwin.Image;
307 +
308 +version (darwin):
309 +
310 +import rt.compiler.util.darwin.dyld;
311 +import rt.compiler.util.darwin.loader;
312 +import rt.compiler.util.darwin.getsect;
313 +
314 +struct Image
315 +{
316 +   private mach_header* header_;
317 +   private uint index_;
318 +   
319 +   static Image opCall (uint index)
320 +   {
321 +       Image image;
322 +       image.header_ = _dyld_get_image_header(index);
323 +       image.index_ = index;
324 +       
325 +       return image;
326 +   }
327 +   
328 +   static uint numberOfImages ()
329 +   {
330 +       return _dyld_image_count;
331 +   }
332 +   
333 +   static int opApply (int delegate(ref Image) dg)
334 +   {
335 +       int result;
336 +       
337 +       for (size_t i = 0; i < numberOfImages; i++)
338 +       {
339 +           result = dg(Image(i));
340 +           
341 +           if (result)
342 +               break;
343 +       }
344 +       
345 +       return result;
346 +   }
347 +   
348 +   static int opApplyReverse (int delegate(ref Image) dg)
349 +   {
350 +       int result;
351 +       
352 +       for (int i = numberOfImages - 1; i >= 0; i--)
353 +       {
354 +           result = dg(Image(i));
355 +           
356 +           if (result)
357 +               break;
358 +       }
359 +       
360 +       return result;
361 +   }
362 +   
363 +   mach_header* header ()
364 +   {
365 +       return header_;
366 +   }
367 +   
368 +   mach_header_64* header64 ()
369 +   {
370 +       return cast(mach_header_64*) header_;
371 +   }
372 +   
373 +   CPU cpu ()
374 +   {
375 +       return CPU(header_);
376 +   }
377 +}
378 +
379 +struct CPU
380 +{
381 +   private mach_header* header;
382 +   
383 +   static CPU opCall (mach_header* header)
384 +   {
385 +       CPU cpu;
386 +       cpu.header = header;
387 +       
388 +       return cpu;
389 +   }
390 +   
391 +   bool is32bit ()
392 +   {       
393 +       return (header.magic & MH_MAGIC) != 0;
394 +   }
395 +
396 +   bool is64bit ()
397 +   {
398 +       return (header.magic & MH_MAGIC_64) != 0;
399 +   }
400 +}
401 +
402 +T[] getSectionData (T, char[] segmentName, char[] sectionName) ()
403 +{
404 +    T[] array;
405 +   
406 +    const c_segmentName = segmentName.ptr;
407 +    const c_sectionName = sectionName.ptr;   
408 +   
409 +    void* start;
410 +    void* end;
411 +   
412 +    foreach_reverse (image ; Image)
413 +    {           
414 +        if (image.cpu.is32bit)
415 +        {
416 +            auto header = image.header;
417 +            section* sect = getsectbynamefromheader(header, c_segmentName, c_sectionName);
418 +           
419 +            if (sect is null || sect.size == 0)
420 +                continue;
421 +
422 +            start = cast(void*) (cast(byte*) header + sect.offset);
423 +           end = cast(void*) (cast(byte*) start + sect.size);
424 +        }
425 +       
426 +        else
427 +        {
428 +            auto header = image.header64;
429 +            section_64* sect = getsectbynamefromheader_64(header, c_segmentName, c_sectionName);
430 +           
431 +            if (sect is null || sect.size == 0)
432 +                continue;
433 +
434 +            start = cast(void*) (cast(byte*) header + sect.offset);
435 +           end = cast(void*) (cast(byte*) start + sect.size);
436 +        }
437 +       
438 +       size_t len = cast(T*)end - cast(T*)start;
439 +       array ~= (cast(T*)start)[0 .. len];
440 +    }
441 +   
442 +    return array;
443 +}
444 \ No newline at end of file
445 Index: tango/core/rt/compiler/util/darwin/loader.d
446 ===================================================================
447 --- tango/core/rt/compiler/util/darwin/loader.d (revision 0)
448 +++ tango/core/rt/compiler/util/darwin/loader.d (revision 0)
449 @@ -0,0 +1,71 @@
450 +/**
451 + * Copyright: Copyright (c) 2010 Jacob Carlborg. All rights reserved.
452 + * Authors: Jacob Carlborg
453 + * Version: Initial created: Feb 20, 2010
454 + * License: BSD style: $(LICENSE)
455 + */
456 +module rt.compiler.util.darwin.loader;
457 +
458 +version (darwin):
459 +
460 +struct mach_header
461 +{
462 +   uint magic;
463 +   int cputype;
464 +   int cpusubtype;
465 +   uint filetype;
466 +   uint ncmds;
467 +   uint sizeofcmds;
468 +   uint flags;
469 +}
470 +
471 +struct mach_header_64
472 +{
473 +   uint magic;
474 +   int cputype;
475 +   int cpusubtype;
476 +   uint filetype;
477 +   uint ncmds;
478 +   uint sizeofcmds;
479 +   uint flags;
480 +   uint reserved;
481 +}
482 +
483 +enum : uint
484 +{
485 +   MH_MAGIC = 0xfeedface,
486 +   MH_CIGAM = 0xcefaedfe,
487 +   MH_MAGIC_64 = 0xfeedfacf,
488 +   MH_CIGAM_64 = 0xcffaedfe,
489 +}
490 +
491 +struct section
492 +{
493 +   char[16] sectname;
494 +   char[16] segname;
495 +   uint addr;
496 +   uint size;
497 +   uint offset;
498 +   uint align_;
499 +   uint reloff;
500 +   uint nreloc;
501 +   uint flags;
502 +   uint reserved1;
503 +   uint reserved2;
504 +}
505 +
506 +struct section_64
507 +{
508 +   char[16] sectname;
509 +   char[16] segname;
510 +   long addr;
511 +   long size;
512 +   uint offset;
513 +   uint align_;
514 +   uint reloff;
515 +   uint nreloc;
516 +   uint flags;
517 +   uint reserved1;
518 +   uint reserved2;
519 +   uint reserved3;
520 +}
521 \ No newline at end of file
522 Index: tango/core/rt/compiler/util/darwin/dyld.d
523 ===================================================================
524 --- tango/core/rt/compiler/util/darwin/dyld.d   (revision 0)
525 +++ tango/core/rt/compiler/util/darwin/dyld.d   (revision 0)
526 @@ -0,0 +1,16 @@
527 +/**
528 + * Copyright: Copyright (c) 2010 Jacob Carlborg.
529 + * Authors: Jacob Carlborg
530 + * Version: Initial created: Feb 20, 2010
531 + * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
532 + */
533 +module rt.compiler.util.darwin.dyld;
534 +
535 +version (darwin):
536 +
537 +import rt.compiler.util.darwin.loader;
538 +
539 +extern (C):
540 +
541 +uint _dyld_image_count ();
542 +mach_header* _dyld_get_image_header (uint image_index);
543 \ No newline at end of file