README (7803B)
1 Porting PicoLisp to Cygwin 2 3 A few months back, I was looking at Lisp programming language 4 offerings for the MS Windows environment. I want an interpreter 5 that is fast and powerful, yet small. I want it to work well in 6 the Cygwin/Win32 environment. 7 8 Enter PicoLisp. http://software-lab.de/down.html 9 10 According to the PicoLisp FAQ, "PicoLisp is for programmers 11 who want to control their programming environment, at all 12 levels, from the application domain down to the bare metal." 13 Yes! That's part of what I want a Lisp for. Especially a Lisp I 14 might embed in other applications. I want control. PicoLisp 15 looked promising. 16 17 PicoLisp is designed with a philosophy of "succinctness", 18 according to the literature on the site. Although there are 19 even smaller Lisp interpreters available, PicoLisp seemed to 20 strike a balance between terseness and functionality. 21 22 PicoLisp is written using standard C, and the author 23 (Alexander Burger) distributes it as C source code under the 24 GNU General Public License. That means if you want to use 25 PicoLisp, you'll need to compile it yourself, or otherwise obtain 26 the executables. PicoLisp comes in two flavours: picolisp, and 27 an even smaller version: mini picolisp. (More about mini 28 picolisp in a bit.) 29 30 When you do build PicoLisp for yourself, you'll get a 31 powerhouse of a Lisp including APIs for building web servers, 32 gui web application servers (for browser clients running java 33 and/or javascript) integrated relational databases, prolog db 34 access, and much more. PicoLisp even comes with two example 35 versions of a flight simulator: one which runs under X-Windows, 36 the other which uses a client's browser/java for the display. 37 There's a chess game written in PicoLisp and Prolog. 38 39 Lest one think that PicoLisp is a mere toy, consider this. In 40 2006, PicoLisp won second prize in the German-language C't 41 Magazine database contest, beating entries written using DB2 42 and Oracle. Industrial-strength databases with tightly 43 integrated web applications have been crafted with PicoLisp. 44 http://tinyurl.com/y9wu39 45 46 PicoLisp has some drawbacks and limitations. As the FAQ warns, 47 PicoLisp "does not pretend to be easy to learn." It is not a 48 Common Lisp flavor. It is not "some standard, 'safe' black-box, 49 which may be easier to learn." Also, for my purposes, I want 50 software that runs not only on Linux, but also on PCs with the 51 MS-Windows operating systems. And there was the rub: PicoLisp 52 isn't distributed with binaries or Windows exe files. 53 54 Even worse (for Windows users), PicoLisp wasn't ported to 55 Cygwin. I have a growing list of portable apps that will run on 56 a flash drive, many of them I compiled from source from using 57 Cygwin tools like make, gcc, etc. 58 59 Cygwin provides a POSIX emulation layer in the form of 60 cygwin1.dll and other libraries. This lets a PC running Windows 61 look like much like a Linux or UNIX box to programs which have 62 been compiled for Cygwin. I'd compiled hundreds of programs 63 for Cygwin and here was PicoLisp which I wanted to have 64 running on all my PCs, Linux ones as well as the MS-Windows 65 PCs, too. 66 67 So this was beginning to look like a challenge. I'd just take a 68 little peek at porting PicoLisp to Cygwin, and see how it 69 would go. I'd ported everything from sox to busybox to fluxbox 70 to Cygwin, so I felt ready for porting PicoLisp. 71 72 PicoLisp comes in two flavors. Mini picolisp and full 73 picolisp. 74 75 Mini PicoLisp is a kind of a "pure" PicoLisp without 76 system-dependent functions like databases, UTF-8, bignums, IPC, 77 and networking. This seemed like a good place to start my 78 PicoLisp porting adventures. I first tried a straight Cygwin/gcc 79 build, and that worked fine, no hitches. 80 81 Then I remembered the -mno-cygwin compiler flag for Cygwin's 82 gcc. When you compile with -mno-cygwin, gcc causes the 83 resulting executable to be built without Cygwin dll library 84 dependances. For C code that relies heavily upon the POSIX 85 emulation aspects of Cygwin, this might not work. But why not 86 try building mini picolisp with the -mno-cygwin option? 87 88 The C code for mini picolisp is free from Linux/POSIX system 89 calls, and it compiled with no problems using -mno-cygwin. It 90 produced a mini picolisp exe file of about 73K, which is not 91 dependant upon any Cygwin DLLs. 92 93 Porting the full PicoLisp interpreter proved to be more of a 94 challenge. Whereas the mini picolisp was careful to avoid Linux 95 system calls, PicoLisp takes the opposite approach and uses 96 Linux (UNIX/POSIX) system functions where needed. 97 98 Additionally, PicoLisp has the ability to dynamically load 99 shared libraries for various extensions. 100 101 Since we need to use shared libraries anyway, I wanted for all 102 of picolisp to go into a single DLL. Then the picolisp exe 103 would be a just small stub that uses that the shared library, 104 picolisp.dll. PicoLisp applications often use fork, so this 105 should also be more efficient when forking. 106 107 Splitting up PicoLisp this way (a DLL and an exe stub) would 108 allow the picolisp.dll to be used as a Lisp library. As a 109 shared library, it would then be possible for other 110 applications to treat PicoLisp as an embedded interpreter, 111 somewhat like librep, but much smaller and more portable. 112 113 Wanting to see how much I could squeeze down the size of the 114 executables and libraries under Cygwin, I used gcc's -Os 115 option, which requests that gcc optimize by making the smallest 116 possible code. Doing this resulted in a picolisp dll of just 117 150K, and a picolisp exe stub of only 2K. 118 119 Of course, if you want this full PicoLisp to run on a Windows 120 PC which does not already have Cygwin installed, you'll need to 121 obtain a few Cygwin DLLs which provide the POSIX emulation 122 layer for PicoLisp. 123 124 For the most part, the port to Win32/Cygwin went smoothly. 125 There were just a few differences between Linux and Cygwin that 126 were handled with macro ifdef statements in the C code that 127 allow something to be done differently for the Cygwin 128 compilation. 129 130 In the end it turned out that the biggest problem was the fcntl 131 system function that does file and record locking. This was 132 especially frustrating, as time and time again, I thought I'd 133 found a solution or a work-around to the differences in 134 semantics of the fcntl call between Linux and Cygwin, only to 135 have the my "solution" fail with more rigorous testing. 136 137 The locking problem was finally just circumvented for Windows 138 by simply not using fcntl locking. So, there is no file or 139 record locking for PicoLisp running under Windows. (See the 140 locking notes in http://www.sqlite.org/lockingv3.html for 141 another perspective on locking system functions in Windows.) 142 However, all the example applications run fine, running in a 143 special (Solo) mode in PicoLisp, in the few places it even 144 matters. This avoids depending on buggy or non-existent record 145 locking functionality with the various Windows versions and 146 file system types. 147 148 So, what do we have at this point? PicoLisp is running on the 149 PC. A working, industrial-strength Lisp interpreter is 150 PicoLisp, ready for writing applications that are succinct yet 151 powerful. PicoLisp comes with a Prolog interpreter and 152 relational databases and flight simulators and chess games and 153 web servers and chat servers and sendmail and much more. 154 155 And PicoLisp itself is written in highly portable C, running 156 on Linux and Windows. PicoLisp is readily embedable, and will 157 be useful to add scripting languages (Lisp, Prolog) to other 158 applications, either statically linked, or as a shared library 159 (DLL). 160 161 PicoLisp is a little dynamo. It even has the ability to use 162 in-line C code which is compiled on-the-fly into a shared 163 library. This in-line C ability uses gcc. (And it works with 164 tcc, the Tiny C Compiler, too.) 165 166 With the tremendous number of PCs out there now able to run 167 PicoLisp, watch out! PicoLisp may be small, but sometimes 168 very powerful things come in small packages. 169 170 Doug Snead, Jan. 2007