Tuesday, July 27, 2010

Drizzle on Windows - Libdrizzle

lately  I started thinking from a users point of view.
Web developers want to try drizzle. but many of them got windows platform for devleopment.
So they are not able to connect to drizzle server

so started seaching for options. found that i have 3 options.
1. Native compile the source using MinGW.
2. Take the help of cygwin to get posix platform
3. Coss-compile the source from Linux to windows.

Toru, Stewart and Monty responded to my views on drizzle-discuss.
Monty was already trying on libdrizzle for sometime.
and Toru also voted for libdrizzle first.
Monty's approach was to use option 3 with option 1.
(later i realized his approach is better and smart). like:
./configure --build=x86_64-unknown-linux-gnu --host=i586-mingw32msvc

everybody voted for libdrizzle to be available for windows first.
That voting makes sence because developer in windows should be able to connect to server first.

My step was to get libdrizzle compiled using cygwin.
cygwin gives full Linux system calls in a dll. that is a very big offer for me.
i could not deny that.

as expected everything went smooth in cygwin.
libdrizzle got compiled without any effort. and i am able to verify the connectivity from windows xp box.

Next step is very challenging.
(The step to compile libdrizzle on windows natively without help of cygwin1.dll)
for a non-programmer like me it is a herculean task.
Monty's working branch (lp:~mordred/libdrizzle/mingwport)
gave a kick start because he already done all background work.

after adding few more lines to Monty's work, (drizzle.h and conn.c )
I am able to compile files from mingw

it was bit challenging to figure out that following defines are must have (i added them to common.h)
#define WINVER WindowsXP
#define _WIN32_WINNT 0x0501
(otherwise haunting, "undefined reference to `freeaddrinfo'"and undefined reference to `getaddrinfo'"" like messages will come when you try to link)

My effort is to create static library which can be linked to make executable.
since configure and make are giving me trouble on windows, i tried handweave the compilation.
to see what is happening in the make
logged all commands executed when libdrizzle is getting compiled with cygwin
$>make V=1

more over go a good guidence from:
http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

Step 1. compiling the library files:
all below executed in windows command prompt.
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\drizzle.c  -o libdrizzle\libdrizzle_la-drizzle.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\conn.c  -o libdrizzle\libdrizzle_la-conn.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\conn_uds.c  -o libdrizzle\libdrizzle_la-conn_uds.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\handshake.c   -o libdrizzle\libdrizzle_la-handshake.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\command.c   -o libdrizzle\libdrizzle_la-command.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\query.c   -o libdrizzle\libdrizzle_la-query.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\result.c   -o libdrizzle\libdrizzle_la-result.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\column.c   -o libdrizzle\libdrizzle_la-column.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\row.c   -o libdrizzle\libdrizzle_la-row.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\field.c   -o libdrizzle\libdrizzle_la-field.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\pack.c   -o libdrizzle\libdrizzle_la-pack.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\state.c   -o libdrizzle\libdrizzle_la-state.o
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c libdrizzle\sha1.c   -o libdrizzle\libdrizzle_la-sha1.o

Step 2. get the poll implementation on windows
cd libdrizzle\win32
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c poll.c   -o poll.o
cd ..\..

Step 3. Create a single static library (libdrizzle.a)
"C:\MinGW\bin\ar.exe" cru libdrizzle/libdrizzle.a  libdrizzle/libdrizzle_la-drizzle.o  libdrizzle/libdrizzle_la-conn.o libdrizzle/libdrizzle_la-conn_uds.o libdrizzle/libdrizzle_la-handshake.o libdrizzle/libdrizzle_la-command.o  libdrizzle/libdrizzle_la-query.o  libdrizzle/libdrizzle_la-result.o  libdrizzle/libdrizzle_la-column.o  libdrizzle/libdrizzle_la-row.o libdrizzle/libdrizzle_la-field.o libdrizzle/libdrizzle_la-pack.o libdrizzle/libdrizzle_la-state.o libdrizzle/libdrizzle_la-sha1.o libdrizzle/win32/poll.o

Step 4. Compile user program and link to the above created library.
"C:\MinGW\bin\gcc.exe" -I. -I"C:\MinGW\include" -c examples/simple.c   -o examples/simple.o
"C:\MinGW\bin\gcc.exe" -op -o examples/simple.exe examples/simple.o  ./libdrizzle/libdrizzle.a  -lwsock32 -lws2_32
cd examples

Step 5. Test the user program.
simple.exe -d test -h 10.97.93.243 -q "select * from t1"

I want to thank you Monty for the huge help and guidance i recived.

3 comments:

  1. Congratulations for getting this together in such a short time. Not only will this bring goodness to Drizzle, MySQL users could benefit a lot from it :)

    Does this survive through the tests?

    ReplyDelete
  2. Thank you Toru.
    Your vote made me concentrating on libdrizzle first.

    Personally, i am feedup with working on closed source database and bureaucracy with it.

    regarding testing, i done minimal level of testing on windows.
    There is absolutely no change in existing code-base. each new lines went inside #ifdef _WIN32. so no regression is expected.

    ReplyDelete
  3. Today found a bug in libdrizzle
    windows error codes were handled properly for recv call but not for send call.
    need to have same error mapping for send also.

    so restoring dump thowed error and stopped.

    another good error mapping function is available at
    https://svn.filezilla-project.org/svn/FileZilla3/trunk/src/engine/socket.cpp
    look for :
    static int ConvertMSWErrorCode(int error)

    ReplyDelete