www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - compiling lua

reply Hoenir <mrmocool gmx.de> writes:
is anybody able to compile lua 5.1.4 with dmc? I want to include it 
statically in a D program.
Sep 15 2008
next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Mon, Sep 15, 2008 at 1:03 PM, Hoenir <mrmocool gmx.de> wrote:
 is anybody able to compile lua 5.1.4 with dmc? I want to include it
 statically in a D program.
What's the issue you're having? I think I compiled 5.1.3 with DMC and didn't need any modifications. It's written strictly in ANSI C for a reason ;)
Sep 15 2008
parent reply Hoenir <mrmocool gmx.de> writes:
Jarrett Billingsley schrieb:
 On Mon, Sep 15, 2008 at 1:03 PM, Hoenir <mrmocool gmx.de> wrote:
 is anybody able to compile lua 5.1.4 with dmc? I want to include it
 statically in a D program.
What's the issue you're having? I think I compiled 5.1.3 with DMC and didn't need any modifications. It's written strictly in ANSI C for a reason ;)
Well, at least there's a #define controlling that ANSI thing. I had to add a cast in liolib.c to make it compile without errors. But the dmd linker complains about undefined symbols __pclose and __popen
Sep 15 2008
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Mon, Sep 15, 2008 at 6:48 PM, Hoenir <mrmocool gmx.de> wrote:
 Jarrett Billingsley schrieb:
 On Mon, Sep 15, 2008 at 1:03 PM, Hoenir <mrmocool gmx.de> wrote:
 is anybody able to compile lua 5.1.4 with dmc? I want to include it
 statically in a D program.
What's the issue you're having? I think I compiled 5.1.3 with DMC and didn't need any modifications. It's written strictly in ANSI C for a reason ;)
Well, at least there's a #define controlling that ANSI thing. I had to add a cast in liolib.c to make it compile without errors. But the dmd linker complains about undefined symbols __pclose and __popen
Ah yes, I remember that now. popen is not supported on Windows, at least not by many compilers other than MS's. I think I ended up just commenting out those functions, sadly.
Sep 15 2008
parent reply Hoenir <mrmocool gmx.de> writes:
Jarrett Billingsley schrieb:
 On Mon, Sep 15, 2008 at 6:48 PM, Hoenir <mrmocool gmx.de> wrote:
 Jarrett Billingsley schrieb:
 On Mon, Sep 15, 2008 at 1:03 PM, Hoenir <mrmocool gmx.de> wrote:
 is anybody able to compile lua 5.1.4 with dmc? I want to include it
 statically in a D program.
What's the issue you're having? I think I compiled 5.1.3 with DMC and didn't need any modifications. It's written strictly in ANSI C for a reason ;)
Well, at least there's a #define controlling that ANSI thing. I had to add a cast in liolib.c to make it compile without errors. But the dmd linker complains about undefined symbols __pclose and __popen
Ah yes, I remember that now. popen is not supported on Windows, at least not by many compilers other than MS's. I think I ended up just commenting out those functions, sadly.
well, indeed there are functions popen and pclose defined in dmc's stdio.h (remember MS's versions have an underscore _pclose) so I removed the underscore in the luaconf header, but it didn't work. I also tried defining _WIN32, but this only brought me a lot of new errors. Then I tried using a precompiled library from the luabinaries project. First I downloaded the VC9 version, converted it using objconv to OMF, but there were a lot of symbol undefined and "Previous Definition Different" errors. After that I tried the Borland lib (since it uses OMF, doesn't it), but it resulted in the same error messages :(
Sep 15 2008
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Mon, Sep 15, 2008 at 7:24 PM, Hoenir <mrmocool gmx.de> wrote:
 Jarrett Billingsley schrieb:
 On Mon, Sep 15, 2008 at 6:48 PM, Hoenir <mrmocool gmx.de> wrote:
 Jarrett Billingsley schrieb:
 On Mon, Sep 15, 2008 at 1:03 PM, Hoenir <mrmocool gmx.de> wrote:
 is anybody able to compile lua 5.1.4 with dmc? I want to include it
 statically in a D program.
What's the issue you're having? I think I compiled 5.1.3 with DMC and didn't need any modifications. It's written strictly in ANSI C for a reason ;)
Well, at least there's a #define controlling that ANSI thing. I had to add a cast in liolib.c to make it compile without errors. But the dmd linker complains about undefined symbols __pclose and __popen
Ah yes, I remember that now. popen is not supported on Windows, at least not by many compilers other than MS's. I think I ended up just commenting out those functions, sadly.
well, indeed there are functions popen and pclose defined in dmc's stdio.h (remember MS's versions have an underscore _pclose) so I removed the underscore in the luaconf header, but it didn't work. I also tried defining _WIN32, but this only brought me a lot of new errors. Then I tried using a precompiled library from the luabinaries project. First I downloaded the VC9 version, converted it using objconv to OMF, but there were a lot of symbol undefined and "Previous Definition Different" errors. After that I tried the Borland lib (since it uses OMF, doesn't it), but it resulted in the same error messages :(
I'm sorry, but I've reached the end of my utility in this matter :\
Sep 15 2008
parent Hoenir <mrmocool gmx.de> writes:
Jarrett Billingsley schrieb:
  > I'm sorry, but I've reached the end of my utility in this matter :\

Me too. I'll use dll version for the moment.
Can anybody tell me why the VC6 dll and coffimplib'ed import lib work 
and the VC9 ones don't?
Sep 16 2008
prev sibling parent reply "Carlos" <carlos-smith sympatico.ca> writes:
: is anybody able to compile lua 5.1.4 with dmc?

Using: Digital Mars C/C++ Compiler Version 8.51.0n
    and lua 5.1.4 source code.

The following command:
    dmc -DLUA_WIN -DWITH_POPEN -olua.exe lua.c  c.lst popen.c

does produce lua.exe:

E:\lua-5.1.4\src>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

When tested with the following lua program:

  f = io.popen("cmd.exe /c dir *.*", "r")
  for line in f:lines() do   -- line 5
    print(line)
  end
  io.close(f)

produce a directory listing, but terminates with an error:

---[ data snipped ] ---

               2 Rép(s)  69,243,314,176 octets libres
E:\lua-5.1.4\src\lua.exe: pp.lua:5: Bad file descriptor
stack traceback:
        [C]: in function '(for generator)'
        pp.lua:5: in main chunk
        [C]: ?

And, i think it's a problem within the DMC runtime.
I used Visual C++ V6, to compile Lua, making sure i am using
the custom _Popen and _Pclose, and lua.exe works fine.
ie, the pp.lua program does not terminate in error.

There has been problems in the past with fdopen().

You will find the popen.c source code at the end of this post.

i had to modify luaconf.h (to force usa of custom popen):

#elif defined(LUA_WIN)
  #ifdef WITH_POPEN
    #define lua_popen(L,c,m) ((void)L, _Popen(c,m))
    #define lua_pclose(L,file) ((void)L, (_Pclose(file) != -1))
  #else
    #define lua_popen(L,c,m) ((void)L, _popen(c,m))
    #define lua_pclose(L,file) ((void)L, (_pclose(file) != -1))
  #endif

and liolib.c (to declare _Popen and _Pclose):

#ifdef WITH_POPEN
extern FILE *_Popen(const char*,const char*);
extern int _Pclose(FILE*);
#endif

--- popen.c ---

/*
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS system libraries
 * FILE:        lib/crt/??????
 * PURPOSE:     Unknown
 * PROGRAMER:   Unknown
 * UPDATE HISTORY:
 *    25/11/05: Created
 */

#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include <io.h>
#include <fcntl.h>

FILE *_Popen(const char*,const char*);
int _Pclose(FILE*);
FILE *_Popencmd(const char*,const char*);

FILE *_Popen( const char *command, const char *mode )
{
  char *szCmdLine=NULL;
  char *szComSpec=NULL;
  char *s;
  FILE *pf;
  HANDLE hReadPipe, hWritePipe;
  int fd;
  BOOL result;
  STARTUPINFO StartupInfo;
  PROCESS_INFORMATION ProcessInformation;
  SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};

  if( command == NULL )
  {
    printf("command==null\n");
    return( NULL );
  }

  if( !CreatePipe(&hReadPipe,&hWritePipe,&sa,1024))
  {
    fprintf(stderr, "CreatePipe error\n");
    return NULL;
  }

  memset(&StartupInfo, 0, sizeof(STARTUPINFO));
  StartupInfo.cb = sizeof(STARTUPINFO);

  if (*mode == 'r' )
  {
   StartupInfo.hStdOutput = hWritePipe;
   StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
  }
  else if ( *mode == 'w' )
  {
   StartupInfo.hStdInput = hReadPipe;
   StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
  }

  result = CreateProcess(
    NULL,
   (char*) command,
  NULL,
  NULL,
  TRUE,
  0,
  NULL,
  NULL,
  &StartupInfo,
  &ProcessInformation);

  if (result == FALSE)
  {
    CloseHandle(hReadPipe);
    CloseHandle(hWritePipe);
    fprintf(stderr,"CreateProcess error\n");
    return NULL;
  }

  CloseHandle(ProcessInformation.hThread);
  if( *mode == 'r' )
  {
    fd = _open_osfhandle((int)hReadPipe, _O_RDONLY|_O_TEXT);
    CloseHandle(hWritePipe);
  }
  else
  {
    fd = _open_osfhandle((int)hWritePipe, _O_WRONLY|_O_TEXT);
    CloseHandle(hReadPipe);
  }

  pf = fdopen(fd,mode);
  return( pf );
}

int _Pclose( FILE *pp )
{
  return fclose(pp);
}


FILE *_Popencmd(const char *cmd, const char *mode)
{
  FILE *f;
  char *fullcommand, *comspec;

  comspec = getenv("ComSpec");
  if(comspec==0)
    comspec = "cmd.exe";

  fullcommand = (char*) malloc( 4 + strlen(comspec) + strlen(cmd) );
  if(fullcommand==0)
    return 0;
  sprintf(fullcommand, "%s /c %s", comspec, cmd);
  f = _Popen(fullcommand, mode);
  free(fullcommand);
  return f;
}


#ifdef UNITTEST

int main( int argc, char ** argv )
{
  char buf[1024];
  FILE *f;

  //f = popen( "cmd.exe /c dir *.c /b", "rw" );
  f = popen( "netstat -an", "r" );
  if( f == 0 )
  { printf( "_popen error\n" );
    return 0;
  }

  while( fgets(buf, 1024, f) )
    printf( "%s", buf );
  pclose( f );

  return 0;
}

#endif


-- Carlos
Sep 17 2008
parent reply Hoenir <mrmocool gmx.de> writes:
 The following command:
     dmc -DLUA_WIN -DWITH_POPEN -olua.exe lua.c  c.lst popen.c
 
 does produce lua.exe:
 
Thanks for your answer. Yeah, but I don't want to compile the lua interpreter. I want to compile the library into a lib to link it statically to my D program. Don't know if this makes a difference.
 And, i think it's a problem within the DMC runtime.
 I used Visual C++ V6, to compile Lua, making sure i am using
 the custom _Popen and _Pclose, and lua.exe works fine.
 ie, the pp.lua program does not terminate in error.
 
I think so too.
Sep 18 2008
parent reply "Carlos" <carlos-smith sympatico.ca> writes:
: I think so too.

Yep, DMC does not support pipes on Windows.
After some more test, i found that lua read the pipe with fgets().
When reading the last line of input from the pipe, Windows
returns and error and set errno to 32 (EPIPE).
The DMC runtime does not properly process that error.

Now, if you want to have _popen/_pclose support,
you need to use _Popen/_Pclose and fix liolib.c file.

And the program works fine now.

I have batch file that produce a static lib of lua runtime if you
are interested.

Carlos

--- liolib.c fix ---

static int read_line (lua_State *L, FILE *f) {
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  for (;;)
  { size_t l;
    char *p = luaL_prepbuffer(&b);
    if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) /* eof? */
    { luaL_pushresult(&b);  /* close buffer */
      #ifdef __DMC__
      if( ferror(f) ==  32 ) // 32 == EPIPE (Broken pipe)
        clearerr(f);
      #endif
      return (lua_objlen(L, -1) > 0);  /* check whether read something */
    }

    #ifdef __DMC__
    if( ferror(f) ==  32 ) // 32 == EPIPE (Broken pipe)
      clearerr(f);
    #endif

    l = strlen(p);
    if (l == 0 || p[l-1] != '\n')
      luaL_addsize(&b, l);
    else {
      luaL_addsize(&b, l - 1);  /* do not include `eol' */
      luaL_pushresult(&b);  /* close buffer */
      return 1;  /* read at least an `eol' */
    }
  }
}
Sep 23 2008
parent Hoenir <mrmocool gmx.de> writes:
Carlos schrieb:
 : I think so too.
 
 Yep, DMC does not support pipes on Windows.
 After some more test, i found that lua read the pipe with fgets().
 When reading the last line of input from the pipe, Windows
 returns and error and set errno to 32 (EPIPE).
 The DMC runtime does not properly process that error.
 
 Now, if you want to have _popen/_pclose support,
 you need to use _Popen/_Pclose and fix liolib.c file.
 
 And the program works fine now.
 
Indeed! Thank you very very much my friend. I have a working one as well now. Isn't that popen bug something Walter should fix?
Sep 24 2008