Makefile.DLLs   [plain text]


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

# 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