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