Makefile.DLLs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #-----------------------------------------------------------------------------#
  2. # Makefile.DLLs, version 0.4.
  3. # Contributed by Fergus Henderson.
  4. # This Makefile contains rules for creating DLLs on Windows using gnu-win32.
  5. #-----------------------------------------------------------------------------#
  6. # This rule creates a `.def' file, which lists the symbols that are exported
  7. # from the DLL. We use `nm' to get a list of all the exported text (`T')
  8. # symbols and data symbols -- including uninitialized data (`B'),
  9. # initialized data (`D'), read-only data (`R'), and common blocks (`C').
  10. %.def: %.a
  11. echo EXPORTS > $@
  12. nm $< | grep '^........ [BCDRT] _' | sed 's/[^_]*_//' >> $@
  13. # We need to use macros to access global data:
  14. # the user of the DLL must refer to `foo' as `(*__imp_foo)'.
  15. # This rule creates a `_globals.h' file, which contains macros
  16. # for doing this.
  17. SYM_PREFIX = $(firstword $(SYM_PREFIX-$*) $*)
  18. DLL_MACRO = $(SYM_PREFIX)_USE_DLL
  19. IMP_MACRO = $(SYM_PREFIX)_IMP
  20. GLOBAL_MACRO = $(SYM_PREFIX)_GLOBAL
  21. %_globals.h: %.a
  22. echo "/* automatically generated by Makefile.DLLs */" > $@
  23. echo "#if defined(__GNUC__) && defined(_WIN32) \\" >> $@
  24. echo " && defined($(DLL_MACRO))" >> $@
  25. echo "# define $(IMP_MACRO)(name) __imp_##name" >> $@
  26. echo "# define $(GLOBAL_MACRO)(name) (*$(IMP_MACRO)(name))" >> $@
  27. echo "#else" >> $@
  28. echo "# define $(GLOBAL_MACRO)(name) name" >> $@
  29. echo "#endif" >> $@
  30. echo "" >> $@
  31. for sym in `nm $< | grep '^........ [BCDR] _' | sed 's/[^_]*_//'`; do \
  32. echo "#define $$sym $(GLOBAL_MACRO)($$sym)" >> $@; \
  33. done
  34. # This rule creates the export object file (`foo.exp') which contains the
  35. # jump table array; this export object file becomes part of the DLL.
  36. # This rule also creates the import library (`foo_dll.a') which contains small
  37. # stubs for all the functions exported by the DLL which jump to them via the
  38. # jump table. Executables that will use the DLL must be linked against this
  39. # stub library.
  40. %.exp %_dll.a : %.def
  41. dlltool $(DLLTOOLFLAGS) $(DLLTOOLFLAGS-$*) \
  42. --def $< \
  43. --dllname $*.dll \
  44. --output-exp $*.exp \
  45. --output-lib $*_dll.a
  46. # The `sed' commands below are to convert DOS-style `C:\foo\bar'
  47. # pathnames into Unix-style `//c/foo/bar' pathnames.
  48. CYGWIN32_LIBS = $(shell echo \
  49. -L`dirname \`gcc -print-file-name=libgcc.a | \
  50. sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
  51. -L`dirname \`gcc -print-file-name=libcygwin.a | \
  52. sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
  53. -L`dirname \`gcc -print-file-name=libkernel32.a | \
  54. sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
  55. -lgcc -lcygwin -lkernel32 -lgcc)
  56. RELOCATABLE=yes
  57. ifeq "$(strip $(RELOCATABLE))" "yes"
  58. # to create relocatable DLLs, we need to do two passes
  59. %.dll: %.exp %.a dll_fixup.o dll_init.o
  60. $(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll -o $*.base \
  61. -e _dll_entry@12 dll_init.o \
  62. dll_fixup.o $*.exp $*.a \
  63. $(LDLIBS) $(LDLIBS-$*) \
  64. $(CYGWIN32_LIBS)
  65. $(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll --base-file $*.base -o $@ \
  66. -e _dll_entry@12 dll_init.o \
  67. dll_fixup.o $*.exp $*.a \
  68. $(LDLIBS) $(LDLIBS-$*) \
  69. $(CYGWIN32_LIBS)
  70. rm -f $*.base
  71. else
  72. %.dll: %.exp %.a dll_fixup.o dll_init.o
  73. $(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll -o $@ \
  74. -e _dll_entry@12 dll_init.o \
  75. dll_fixup.o $*.exp $*.a \
  76. $(LDLIBS) $(LDLIBS-$*) \
  77. $(CYGWIN32_LIBS)
  78. endif
  79. # This black magic piece of assembler needs to be linked in in order to
  80. # properly terminate the list of imported DLLs.
  81. dll_fixup.s:
  82. echo '.section .idata$$3' > dll_fixup.s
  83. echo '.long 0,0,0,0, 0,0,0,0' >> dll_fixup.s
  84. # This bit is necessary to provide an initialization function for the DLL.
  85. dll_init.c:
  86. echo '__attribute__((stdcall))' > dll_init.c
  87. echo 'int dll_entry(int handle, int reason, void *ptr)' >> dll_init.c
  88. echo '{return 1; }' >> dll_init.c
  89. dont_throw_away: dll_fixup.o dll_init.o