[project @ 2003-02-13 15:20:27 by sof]
[ghc-hetmet.git] / ghc / docs / users_guide / win32-dlls.sgml
1 <chapter id="win32">
2 <title>Running GHC on Win32 systems</title>
3
4 <sect1>
5 <title>
6 Starting GHC on Win32 platforms</title>
7
8 <para>
9 The installer that installs GHC on Win32 also sets up the file-suffix associations
10 for ".hs" and ".lhs" files so that double-clicking them starts <command>ghci</command>.
11 </para>
12 <para>
13 Be aware of that <command>ghc</command> and <command>ghci</command> do
14 require filenames containing spaces to be escaped using quotes:
15 <programlisting>
16   c:\ghc\bin\ghci "c:\\Program Files\\Haskell\\Project.hs"
17 </programlisting>
18
19 <para>
20 If the quotes are left off in the above command, <command>ghci</command> will
21 interpret the filename as two, "c:\\Program" and "Files\\Haskell\\Project.hs".
22 </para>
23
24 <!-- not clear whether there are current editions of Win32 OSes that
25      doesn't do this by default.
26
27 <para> Solution: don't use "Open With...", avoid spaces in file names, 
28 or fiddle with the appropriate registry setting:
29 <programlisting>
30   HKEY_CLASSES_ROOT\Unknown\shell\openas\command
31 </programlisting>
32 Notice how the "%1" argument is quoted (or not).
33 </para>
34 <para> This problem doesn't occur when double-clicking.
35 </para>
36 -->
37
38 </sect1>
39
40 <sect1>
41 <title>
42 Using GHC (and other GHC-compiled executables) with cygwin</title>
43
44 <sect2>
45 <title>Background</title> <para>The cygwin tools aim to provide a
46 unix-style API on top of the windows libraries, to facilitate ports of
47 unix software to windows. To this end, they introduce a unix-style
48 directory hierarchy under some root directory (typically
49 <filename>/</filename> is <filename>C:\cygwin\</filename>). Moreover,
50 everything built against the cygwin API (including the cygwin tools
51 and programs compiled with cygwin's ghc) will see / as the root of
52 their file system, happily pretending to work in a typical unix
53 environment, and finding things like <filename>/bin</filename> and <filename>/usr/include</filename> without
54 ever explicitly bothering with their actual location on the windows
55 system (probably <filename>C:\cygwin\bin</filename> and <filename>C:\cygwin\usr\include</filename>).
56 </para>
57 </sect2>
58
59 <sect2><title>The problem</title>
60 <para>GHC, by default, no longer depends on cygwin, but is a native
61 windows program. It is built using mingw, and it uses mingw's ghc
62 while compiling your Haskell sources (even if you call it from
63 cygwin's bash), but what matters here is that - just like any other
64 normal windows program - neither GHC nor the executables it produces
65 are aware of cygwin's pretended unix hierarchy. GHC will happily
66 accept either '/' or '\' as path separators, but it won't know where
67 to find <filename>/home/joe/Main.hs</filename> or <filename>/bin/bash</filename> 
68 or the like. This causes all
69 kinds of fun when GHC is used from within cygwin's bash, or in
70 make-sessions running under cygwin.
71 </para>
72 </sect2>
73
74 <sect2><title>Things to do</title>
75 <itemizedlist>
76 <listitem>
77 <para> Don't use absolute paths in make, configure & co if there is any chance 
78   that those might be passed to GHC (or to GHC-compiled programs). Relative
79   paths are fine because cygwin tools are happy with them and GHC accepts 
80   '/' as path-separator. And relative paths don't depend on where cygwin's
81   root directory is located, or on which partition or network drive your source
82   tree happens to reside, as long as you 'cd' there first.
83 </para></listitem>
84
85 <listitem>
86 <para> If you have to use absolute paths (beware of the innocent-looking
87   <literal>ROOT=`pwd`</literal> in makefile hierarchies or configure scripts), cygwin provides
88   a tool called <command>cygpath</command> that can convert cygwin's unix-style paths to their
89   actual windows-style counterparts. Many cygwin tools actually accept
90   absolute windows-style paths (remember, though, that you either need 
91   to escape '\' or convert '\' to '/'), so you should be fine just using those 
92   everywhere. If you need to use tools that do some kind of path-mangling 
93   that depends on unix-style paths (one fun example is trying to interpret ':' 
94   as a separator in path lists..), you can still try to convert paths using 
95   <command>cygpath</command> just before they are passed to GHC and friends.
96 </para></listitem>
97   
98 <listitem>
99 <para> If you don't have <command>cygpath</command>, you probably don't have cygwin and hence
100   no problems with it... unless you want to write one build process for several
101   platforms. Again, relative paths are your friend, but if you have to use
102   absolute paths, and don't want to use different tools on different platforms,
103   you can simply write a short Haskell program to print the current directory
104    (thanks to George Russell for this idea): compiled with GHC, this will give 
105   you the view of the file system that GHC depends on (which will differ 
106   depending on whether GHC is compiled with cygwin's gcc or mingw's
107   gcc or on a real unix system..) - that little program can also deal with 
108   escaping '\' in paths. Apart from the banner and the startup time, 
109   something like this would also do:
110 <programlisting>
111   $ echo "Directory.getCurrentDirectory >>= putStrLn . init . tail . show " | ghci
112 </programlisting>
113 </para></listitem>
114 </itemizedlist>
115 </sect2>
116 </sect1>
117
118
119 <sect1 id="win32-dlls">
120 <Title>Building and using Win32 DLLs
121 </Title>
122
123 <Para>
124 <IndexTerm><Primary>Dynamic link libraries, Win32</Primary></IndexTerm>
125 <IndexTerm><Primary>DLLs, Win32</Primary></IndexTerm>
126 On Win32 platforms, the compiler is capable of both producing and using
127 dynamic link libraries (DLLs) containing ghc-compiled code. This
128 section shows you how to make use of this facility.
129 </Para>
130
131 <Para>
132 Until recently, <Command>strip</Command> didn't work reliably on DLLs, so you
133 should test your version with care, or make sure you have the latest
134 binutils. Unfortunately, we don't know exactly which version of binutils
135 cured the problem (it was supposedly fixed some years ago).
136 </Para>
137
138
139 <Sect2 id="win32-dlls-link">
140 <Title>Linking with DLLs
141 </Title>
142
143 <Para>
144 The default on Win32 platforms is to link applications in such a way
145 that the executables will use the Prelude and system libraries DLLs,
146 rather than contain (large chunks of) them. This is transparent at the
147 command-line, so 
148 </Para>
149
150 <Para>
151 <Screen>
152 sh$ cat main.hs
153 module Main where
154 main = putStrLn "hello, world!"
155 sh$ ghc -o main main.hs
156 ghc: module version changed to 1; reason: no old .hi file
157 sh$ strip main.exe
158 sh$ ls -l main.exe
159 -rwxr-xr-x   1 544      everyone     4608 May  3 17:11 main.exe*
160 sh$ ./main
161 hello, world!
162 sh$ 
163 </Screen>
164 </Para>
165
166 <Para>
167 will give you a binary as before, but the <Filename>main.exe</Filename>
168 generated will use the Prelude and RTS DLLs instead of linking them in
169 statically.
170 </Para>
171
172 <Para>
173 4K for a <Literal>"hello, world"</Literal> application&mdash;not bad, huh? :-)
174 </Para>
175
176 </Sect2>
177
178 <Sect2 id="win32-dlls-linking-static">
179 <Title>Not linking with DLLs
180 <IndexTerm><Primary>-static option (Win32)</Primary></IndexTerm></Title>
181
182 <Para>
183 If you want to build an executable that doesn't depend on any
184 ghc-compiled DLLs, use the <Option>-static</Option> option to link in
185 the code statically.
186 </Para>
187
188 <Para>
189 Notice that you cannot mix code that has been compiled with
190 <Option>-static</Option> and not, so you have to use the <Option>-static</Option>
191 option on all the Haskell modules that make up your application.
192 </Para>
193
194 </Sect2>
195
196 <Sect2 id="win32-dlls-create">
197 <Title>Creating a DLL
198 </Title>
199
200 <Para>
201 <emphasis>Making libraries into DLLs doesn't work on Windows at the
202 moment (and is no longer supported); however, all the machinery is
203 still there. If you're interested, contact the GHC team. Note that
204 building an entire Haskell application as a DLL is still supported
205 (it's just inter-DLL Haskell calls that don't work).</emphasis>
206 <IndexTerm><Primary>Creating a Win32 DLL</Primary></IndexTerm>
207 <IndexTerm><Primary>&ndash;&ndash;mk-dll</Primary></IndexTerm>
208 Sealing up your Haskell library inside a DLL is straightforward;
209 compile up the object files that make up the library, and then build
210 the DLL by issuing a command of the form:
211 </Para>
212
213 <Para>
214 <Screen>
215 ghc &ndash;&ndash;mk-dll -o foo.dll bar.o baz.o wibble.a -lfooble
216 </Screen>
217 </Para>
218
219 <Para>
220 By feeding the ghc compiler driver the option <Option>&ndash;&ndash;mk-dll</Option>, it
221 will build a DLL rather than produce an executable. The DLL will
222 consist of all the object files and archives given on the command
223 line.
224 </Para>
225
226 <Para>
227 To create a `static' DLL, i.e. one that does not depend on the GHC DLLs,
228 use the <Option>-static</Option> when compiling up your Haskell code and
229 building the DLL.
230 </Para>
231
232 <Para>
233 A couple of things to notice:
234 </Para>
235
236 <Para>
237
238 <ItemizedList>
239 <ListItem>
240 <Para>
241 Since DLLs correspond to packages (see <XRef LinkEnd="packages">) you need
242 to use <Option>-package-name dll-name</Option> when compiling modules that
243 belong to a DLL if you're going to call them from Haskell. Otherwise, Haskell
244 code that calls entry points in that DLL will do so incorrectly, and crash.
245 For similar reasons, you can only compile a single module tree into a DLL,
246 as <Function>startupHaskell</Function> needs to be able to call its
247 initialisation function, and only takes one such argument (see <XRef
248 LinkEnd="win32-dlls-foreign">). Hence the modules
249 you compile into a DLL must have a common root.
250 </Para>
251 </ListItem>
252
253 <ListItem>
254 <Para>
255 By default, the entry points of all the object files will be exported from
256 the DLL when using <Option>&ndash;&ndash;mk-dll</Option>. Should you want to constrain
257 this, you can specify the <Emphasis>module definition file</Emphasis> to use
258 on the command line as follows:
259
260 <Screen>
261 ghc &ndash;&ndash;mk-dll -o .... -optdll--def -optdllMyDef.def
262 </Screen>
263
264 See Microsoft documentation for details, but a module definition file
265 simply lists what entry points you want to export. Here's one that's
266 suitable when building a Haskell COM server DLL:
267
268 <ProgramListing>
269 EXPORTS
270  DllCanUnloadNow     = DllCanUnloadNow@0
271  DllGetClassObject   = DllGetClassObject@12
272  DllRegisterServer   = DllRegisterServer@0
273  DllUnregisterServer = DllUnregisterServer@0
274 </ProgramListing>
275 </Para>
276 </ListItem>
277
278 <ListItem>
279 <Para>
280 In addition to creating a DLL, the <Option>&ndash;&ndash;mk-dll</Option> option also
281 creates an import library. The import library name is derived from the
282 name of the DLL, as follows:
283
284 <ProgramListing>
285 DLL: HScool.dll  ==&#62; import lib: libHScool_imp.a
286 </ProgramListing>
287
288 The naming scheme may look a bit weird, but it has the purpose of allowing
289 the co-existence of import libraries with ordinary static libraries (e.g.,
290 <Filename>libHSfoo.a</Filename> and
291 <Filename>libHSfoo&lowbar;imp.a</Filename>.
292
293 Additionally, when the compiler driver is linking in non-static mode, it
294 will rewrite occurrence of <Option>-lHSfoo</Option> on the command line to
295 <Option>-lHSfoo&lowbar;imp</Option>. By doing this for you, switching from
296 non-static to static linking is simply a question of adding
297 <Option>-static</Option> to your command line.
298
299 </Para>
300 </ListItem>
301 </ItemizedList>
302 </Para>
303
304 </Sect2>
305
306
307 <Sect2 id="win32-dlls-foreign">
308 <Title>Making DLLs to be called from other languages</Title>
309
310 <Para>
311
312 If you want to package up Haskell code to be called from other languages,
313 such as Visual Basic or C++, there are some extra things it is useful to
314 know. The dirty details are in the <Emphasis>Foreign Function
315 Interface</Emphasis> definition, but it can be tricky to work out how to
316 combine this with DLL building, so here's an example:
317
318 </Para>
319
320 <ItemizedList>
321
322 <ListItem>
323 <Para>
324 Use <Literal>foreign export</Literal> declarations to export the Haskell
325 functions you want to call from the outside. For example,
326
327 <ProgramListing>
328 module Adder where
329
330 adder :: Int -> Int -> IO Int  -- gratuitous use of IO
331 adder x y = return (x+y)
332
333 foreign export stdcall adder :: Int -> Int -> IO Int
334 </ProgramListing>
335 </Para>
336 </ListItem>
337
338 <ListItem>
339 <Para>
340 Compile it up:
341
342 <Screen>
343 ghc -c adder.hs -fglasgow-exts
344 </Screen>
345   
346 This will produce two files, adder.o and adder_stub.o
347 </Para>
348 </ListItem>
349
350 <ListItem>
351 <Para>
352 compile up a <Function>DllMain()</Function> that starts up the Haskell
353 RTS-&ndash;&ndash;a possible implementation is:
354
355 <ProgramListing>
356 #include &lt;windows.h&gt;
357 #include &lt;Rts.h&gt;
358
359 EXTFUN(__stginit_Adder);
360
361 static char* args[] = { "ghcDll", NULL };
362                        /* N.B. argv arrays must end with NULL */
363 BOOL
364 STDCALL
365 DllMain
366    ( HANDLE hModule
367    , DWORD reason
368    , void* reserved
369    )
370 {
371   if (reason == DLL_PROCESS_ATTACH) {
372       /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
373       startupHaskell(1, args, __stginit_Adder);
374       return TRUE;
375   }
376   return TRUE;
377 }
378 </ProgramListing>
379
380 Here, <Literal>Adder</Literal> is the name of the root module in the module
381 tree (as mentioned above, there must be a single root module, and hence a
382 single module tree in the DLL).
383
384 Compile this up:
385
386 <Screen>
387 ghc -c dllMain.c
388 </Screen>
389 </Para>
390 </ListItem>
391
392 <ListItem>
393 <Para>
394 Construct the DLL:
395
396 <Screen>
397 ghc &ndash;&ndash;mk-dll -o adder.dll adder.o adder_stub.o dllMain.o
398 </Screen>
399
400 </Para>
401 </ListItem>
402
403 <ListItem>
404 <Para>
405 Start using <Function>adder</Function> from VBA-&ndash;&ndash;here's how I would
406 <Constant>Declare</Constant> it:
407
408 <ProgramListing>
409 Private Declare Function adder Lib "adder.dll" Alias "adder@8"
410       (ByVal x As Long, ByVal y As Long) As Long
411 </ProgramListing>
412
413 Since this Haskell DLL depends on a couple of the DLLs that come with GHC,
414 make sure that they are in scope/visible.
415 </Para>
416
417 <Para>
418 Building statically linked DLLs is the same as in the previous section: it
419 suffices to add <Option>-static</Option> to the commands used to compile up
420 the Haskell source and build the DLL.
421 </Para>
422
423 </ListItem>
424
425 </ItemizedList>
426
427 </Sect2>
428
429 </sect1>
430 </Chapter>
431
432 <!-- Emacs stuff:
433      ;;; Local Variables: ***
434      ;;; mode: sgml ***
435      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
436      ;;; End: ***
437  -->