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