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