Path checks for windows.
authorthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Wed, 24 Oct 2007 16:47:17 +0000 (16:47 +0000)
committerthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Wed, 24 Oct 2007 16:47:17 +0000 (16:47 +0000)
git-svn-id: http://svn.complang.org/ragel/trunk@323 052ea7fc-9027-0410-9066-f65837a77df0

ragel/Makefile.in
ragel/main.cpp

index 911ff9c..78b396f 100644 (file)
@@ -31,7 +31,9 @@ CC_SRCS = \
 
 GEN_SRC = rlscan.cpp rlparse.h rlparse.cpp
 
-LIBS = ../common/common.a
+RAGEL_LIBS = ../common/common.a
+LIBS = $(RAGEL_LIBS)
+MINGW_LIBS = -lpsapi
 
 #*************************************
 
@@ -40,6 +42,10 @@ BUILD_PARSERS = @BUILD_PARSERS@
 EXEEXT = @EXEEXT@
 CXX = @CXX@
 
+ifeq ($(EXEEXT), .exe)
+LIBS += $(MINGW_LIBS)
+endif
+
 # Get objects and dependencies from sources.
 OBJS = $(CC_SRCS:%.cpp=%.o)
 DEPS = $(CC_SRCS:%.cpp=.%.d)
@@ -47,7 +53,7 @@ DEPS = $(CC_SRCS:%.cpp=.%.d)
 # Rules.
 all: ragel$(EXEEXT)
 
-ragel$(EXEEXT): $(GEN_SRC) $(OBJS) $(LIBS)
+ragel$(EXEEXT): $(GEN_SRC) $(OBJS) $(RAGEL_LIBS)
        $(CXX) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
 
 ifeq ($(BUILD_PARSERS),true)
index 746bee8..1edcfe9 100644 (file)
@@ -437,12 +437,21 @@ char *openIntermed( char *inputFileName, char *outputFileName )
        return result;
 }
 
+
+void cleanExit( char *intermed, int status )
+{
+       unlink( intermed );
+       exit( status );
+}
+
+#ifndef WIN32
+
 /* If any forward slash is found in argv0 then it is assumed that the path is
  * explicit and the path to the backend executable should be derived from
  * that. If no forward slash is found it is assumed the file is being run from
  * the installed location. The PREFIX supplied during configuration is used.
  * */
-char **makePathChecks( const char *argv0, const char *progName )
+char **makePathChecksUnix( const char *argv0, const char *progName )
 {
        char **result = new char*[3];
        const char *lastSlash = strrchr( argv0, '/' );
@@ -478,13 +487,6 @@ char **makePathChecks( const char *argv0, const char *progName )
        return result;
 }
 
-void cleanExit( char *intermed, int status )
-{
-       unlink( intermed );
-       exit( status );
-}
-
-#ifndef WIN32
 
 int forkAndExec( char *progName, char **pathChecks, 
                ArgsVector &args, char *intermed )
@@ -521,6 +523,45 @@ int forkAndExec( char *progName, char **pathChecks,
        return status;
 }
 
+#else
+
+/* If any forward slash is found in argv0 then it is assumed that the path is
+ * explicit and the path to the backend executable should be derived from
+ * that. If no forward slash is found it is assumed the file is being run from
+ * the installed location. The PREFIX supplied during configuration is used.
+ * */
+char **makePathChecksWin( const char *progName )
+{
+       int len = 1024;
+       char *imageFileName = new char[len];
+       HANDLE h = GetCurrentProcess();
+       len = GetModuleFileNameEx( h, NULL, imageFileName, len );
+       imageFileName[len] = 0;
+
+       char **result = new char*[3];
+       const char *lastSlash = strrchr( imageFileName, '\\' );
+       int numChecks = 0;
+
+       assert( lastSlash != 0 );
+       char *path = strdup( imageFileName );
+       int givenPathLen = (lastSlash - imageFileName) + 1;
+       path[givenPathLen] = 0;
+
+       int progNameLen = strlen(progName);
+       int length = givenPathLen + progNameLen + 1;
+       char *check = new char[length];
+       sprintf( check, "%s%s", path, progName );
+       result[numChecks++] = check;
+
+       length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
+       check = new char[length];
+       sprintf( check, "%s..\\%s\\%s", path, progName, progName );
+       result[numChecks++] = check;
+
+       result[numChecks] = 0;
+       return result;
+}
+
 #endif
 
 void execFrontend( const char *argv0, char *inputFileName, char *intermed )
@@ -535,10 +576,13 @@ void execFrontend( const char *argv0, char *inputFileName, char *intermed )
        frontendArgs.append( inputFileName );
        frontendArgs.append( 0 );
 
-       char **pathChecks = makePathChecks( argv0, progName );
-
 #ifndef WIN32
+       char **pathChecks = makePathChecksUnix( argv0, progName );
        forkAndExec( progName, pathChecks, frontendArgs, intermed );
+#else
+       char **pathChecks = makePathChecksWin( progName );
+       while ( *pathChecks != 0 )
+               cerr << *pathChecks++ << endl;
 #endif
 }
 
@@ -567,10 +611,13 @@ void execBackend( const char *argv0, char *intermed, char *outputFileName )
        backendArgs.append( intermed );
        backendArgs.append( 0 );
 
-       char **pathChecks = makePathChecks( argv0, progName );
-
 #ifndef WIN32
+       char **pathChecks = makePathChecksUnix( argv0, progName );
        forkAndExec( progName, pathChecks, backendArgs, intermed );
+#else
+       char **pathChecks = makePathChecksWin( progName );
+       while ( *pathChecks != 0 )
+               cerr << *pathChecks++ << endl;
 #endif
 }