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