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