summaryrefslogtreecommitdiff
path: root/Makefile.DLLs
blob: 011f49d3bcf033a9cf8886fdf21e8f43084ea90b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#-----------------------------------------------------------------------------#

# Makefile.DLLs, version 0.4.

# Contributed by Fergus Henderson.

# This Makefile contains rules for creating DLLs on Windows using gnu-win32.

#-----------------------------------------------------------------------------#

# This rule creates a `.def' file, which lists the symbols that are exported
# from the DLL.  We use `nm' to get a list of all the exported text (`T')
# symbols and data symbols -- including uninitialized data (`B'),
# initialized data (`D'), read-only data (`R'), and common blocks (`C').
%.def: %.a
	echo EXPORTS > $@
	nm $< | grep '^........ [BCDRT] _' | sed 's/[^_]*_//' >> $@

# We need to use macros to access global data:
# the user of the DLL must refer to `foo' as `(*__imp_foo)'.
# This rule creates a `_globals.h' file, which contains macros
# for doing this.

SYM_PREFIX = $(firstword $(SYM_PREFIX-$*) $*)
DLL_MACRO = $(SYM_PREFIX)_USE_DLL
IMP_MACRO = $(SYM_PREFIX)_IMP
GLOBAL_MACRO = $(SYM_PREFIX)_GLOBAL

%_globals.h: %.a
	echo "/* automatically generated by Makefile.DLLs */"	> $@
	echo "#if defined(__GNUC__) && defined(_WIN32) \\"	>> $@
	echo "	&& defined($(DLL_MACRO))"			>> $@
	echo "#  define $(IMP_MACRO)(name)	__imp_##name" 	>> $@
	echo "#  define $(GLOBAL_MACRO)(name)	(*$(IMP_MACRO)(name))" >> $@
	echo "#else"						>> $@
	echo "#  define $(GLOBAL_MACRO)(name)	name"		>> $@
	echo "#endif"						>> $@
	echo ""							>> $@
	for sym in `nm $< | grep '^........ [BCDR] _' | sed 's/[^_]*_//'`; do \
		echo "#define $$sym	$(GLOBAL_MACRO)($$sym)"	>> $@; \
	done

# This rule creates the export object file (`foo.exp') which contains the
# jump table array; this export object file becomes part of the DLL. 
# This rule also creates the import library (`foo_dll.a') which contains small
# stubs for all the functions exported by the DLL which jump to them via the
# jump table.  Executables that will use the DLL must be linked against this
# stub library.
%.exp %_dll.a : %.def
	dlltool $(DLLTOOLFLAGS) $(DLLTOOLFLAGS-$*)		\
		--def $<					\
		--dllname $*.dll				\
		--output-exp $*.exp				\
		--output-lib $*_dll.a

# The `sed' commands below are to convert DOS-style `C:\foo\bar'
# pathnames into Unix-style `//c/foo/bar' pathnames.
CYGWIN32_LIBS = $(shell echo					\
	-L`dirname \`gcc -print-file-name=libgcc.a |		\
	sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
	-L`dirname \`gcc -print-file-name=libcygwin.a |	\
	sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
	-L`dirname \`gcc -print-file-name=libkernel32.a | \
	sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
	-lgcc -lcygwin -lkernel32 -lgcc)

RELOCATABLE=yes

ifeq "$(strip $(RELOCATABLE))" "yes"

# to create relocatable DLLs, we need to do two passes
%.dll: %.exp %.a dll_fixup.o dll_init.o
	$(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll -o $*.base			\
		-e _dll_entry@12 dll_init.o				\
		dll_fixup.o $*.exp $*.a					\
		$(LDLIBS) $(LDLIBS-$*)					\
		$(CYGWIN32_LIBS)
	$(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll --base-file $*.base -o $@	\
		-e _dll_entry@12 dll_init.o				\
		dll_fixup.o $*.exp $*.a					\
		$(LDLIBS) $(LDLIBS-$*)					\
		$(CYGWIN32_LIBS)
	rm -f $*.base
else

%.dll: %.exp %.a dll_fixup.o dll_init.o
	$(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll -o $@			\
		-e _dll_entry@12 dll_init.o				\
		dll_fixup.o $*.exp $*.a					\
		$(LDLIBS) $(LDLIBS-$*)					\
		$(CYGWIN32_LIBS)

endif

# This black magic piece of assembler needs to be linked in in order to
# properly terminate the list of imported DLLs.
dll_fixup.s:
	echo '.section .idata$$3' 	> dll_fixup.s
	echo '.long 0,0,0,0, 0,0,0,0'	>> dll_fixup.s

# This bit is necessary to provide an initialization function for the DLL.
dll_init.c:
	echo '__attribute__((stdcall))' > dll_init.c
	echo 'int dll_entry(int handle, int reason, void *ptr)' >> dll_init.c
	echo '{return 1; }' >> dll_init.c

dont_throw_away: dll_fixup.o dll_init.o