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