lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r465265 - in /lucene/lucy/trunk/charmonizer: charmonize.c src/Charmonizer.charm src/Charmonizer/Core.charm src/Charmonizer/Core.harm
Date Wed, 18 Oct 2006 14:48:11 GMT
Author: marvin
Date: Wed Oct 18 07:48:10 2006
New Revision: 465265

URL: http://svn.apache.org/viewvc?view=rev&rev=465265
Log:
Abstract out and probe compiler/shell syntax, and add options so that
Charmonizer ought to work properly with MSVC.

Modified:
    lucene/lucy/trunk/charmonizer/charmonize.c
    lucene/lucy/trunk/charmonizer/src/Charmonizer.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm

Modified: lucene/lucy/trunk/charmonizer/charmonize.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/charmonize.c?view=diff&rev=465265&r1=465264&r2=465265
==============================================================================
--- lucene/lucy/trunk/charmonizer/charmonize.c (original)
+++ lucene/lucy/trunk/charmonizer/charmonize.c Wed Oct 18 07:48:10 2006
@@ -85,10 +85,10 @@
     outpath  = extract_delim(infile_contents, infile_len, "charm_outpath");
     
     /* set up Charmonizer */
-    chaz_init();
     chaz_set_prefixes("LUCY_", "Lucy_", "lucy_", "lucy_");
     chaz_set_compiler(compiler);
     chaz_set_ccflags(ccflags);
+    chaz_init();
 
     /* clean up */
     free(infile_contents);

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer.charm?view=diff&rev=465265&r1=465264&r2=465265
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer.charm Wed Oct 18 07:48:10 2006
@@ -9,6 +9,7 @@
 void
 chaz_init()
 {
+    probe_compiler_syntax();
     write_charm_h();
 }
 

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm?view=diff&rev=465265&r1=465264&r2=465265
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm Wed Oct 18 07:48:10 2006
@@ -6,6 +6,15 @@
 #include <stdlib.h>
 #include <string.h>
 
+static void
+grow_command_buf(size_t len);
+
+static chaz_bool_t 
+compile_and_verify(char *source, size_t source_len);
+
+static void
+clean_up_try();
+
 char *compiler        = NULL;
 char *ccflags         = NULL;
 char *constant_prefix = NULL;
@@ -17,6 +26,9 @@
 
 static char *command_buf = NULL;
 static size_t command_buf_len = 0;
+static char *local_command_start = NULL;
+static char *object_flag = NULL;
+static char *include_flag = NULL;
 
 static char charm_h_code[] = METAQUOTE
     #ifndef CHARM_H
@@ -30,6 +42,17 @@
     #endif
 METAQUOTE;
 
+static void
+grow_command_buf(size_t command_len)
+{
+    if (command_len > command_buf_len) {
+        command_buf = realloc(command_buf, command_len);
+        if (command_buf == NULL) 
+            die("allocation of command_buf failed");
+        command_buf_len = command_len;
+    }
+}
+
 void
 write_charm_h()
 {
@@ -41,12 +64,71 @@
         die("Error when closing '_charm.h': %s", strerror(errno));
 }
 
+
+void
+chaz_Core_probe_compiler_syntax()
+{
+    char *o_flags[] = { " -o ", " /o ", NULL };
+    char *i_flags[] = { "-I", "/I", NULL };
+    char *command_starts[] = { "./", ".\\", "", NULL };
+    int o_i, i_i, cs_i;
+    const char *ret_one_code = "int main() { return 1; }";
+    const int ret_one_len = strlen(ret_one_code);
+
+    if (verbosity) {
+        printf("Probing compiler/shell syntax... "
+            "you may see some errors...\n");
+        fflush(stdout);
+    }
+
+    for (o_i = 0; o_flags[o_i] != NULL; o_i++) {
+        free(object_flag);
+        object_flag = strdup(o_flags[o_i]);
+        for (i_i = 0; i_flags[i_i] != NULL; i_i++) {
+            free(include_flag);
+            include_flag = strdup(i_flags[i_i]);
+            for (cs_i = 0; command_starts[cs_i] != NULL; cs_i++) {
+                free(local_command_start);
+                local_command_start = strdup(command_starts[cs_i]);
+                if (compile_and_verify(ret_one_code, ret_one_len)) {
+                    if (verbosity) {
+                        printf("... found a syntax that works.\n");
+                        fflush(stdout);
+                    }
+                    return;
+                }
+            }
+        }
+    }
+    die("Couldn't divine syntax for compiler");
+}
+
 char*
 capture_output(char *source, size_t source_len, size_t *output_len) 
 {
     char *captured_output = NULL;
+    chaz_bool_t compile_succeeded = compile_and_verify(source, source_len);
+
+    if (compile_succeeded) {
+        /* run the app, slurp the output */
+        grow_command_buf(strlen(local_command_start) + strlen(TRY_APP_PATH));
+        sprintf(command_buf, "%s%s", local_command_start, TRY_APP_PATH);
+        system(command_buf);
+        captured_output = slurp_file(TARGET_PATH, output_len);
+    }
+    else {
+        *output_len = 0;
+    }
+
+    clean_up_try();
+
+    return captured_output;
+}
+
+static chaz_bool_t 
+compile_and_verify(char *source, size_t source_len)
+{
     FILE *app_source_fh, *garbage_fh;
-    size_t command_len = 0;
     int   successful;
     
     /* clear out previous versions and test to make sure removal worked */
@@ -69,21 +151,15 @@
         die("compiler has not been set");
     if (ccflags == NULL)
         die("ccflags has not been set");
-    command_len = strlen(compiler) + strlen(ccflags) + 200;
-    if (command_len > command_buf_len) {
-        command_buf = realloc(command_buf, command_len);
-        if (command_buf == NULL) 
-            die("allocation of command_buf failed");
-        command_buf_len = command_len;
-    }
+    grow_command_buf( strlen(compiler) + strlen(ccflags) + 200 );
 
     /* Compile the source.
      * Unfortunately there's no completely portable way to redirect stderr
      * yet be able to recover it later, so we're stuck seeing the
      * errors when something fails.
      */
-    sprintf(command_buf, "%s " TRY_SOURCE_PATH " -o " TRY_APP_PATH " -I. %s", 
-        compiler, ccflags);
+    sprintf(command_buf, "%s " TRY_SOURCE_PATH " %s " TRY_APP_PATH " %s. %s", 
+        compiler, object_flag, include_flag, ccflags);
     system(command_buf);
 
     /* see if compilation was successful */
@@ -92,22 +168,15 @@
     if (garbage_fh != NULL && fclose(garbage_fh)) 
         die("Error closing file '%s': %s", TRY_SOURCE_PATH, strerror(errno));
 
-    /* if compilation was successful... */
-    if (successful) {
-        /* run the app, slurp the output and write it to our fh */
-        system("./" TRY_APP_PATH);
-        captured_output = slurp_file(TARGET_PATH, output_len);
-    }
-    else {
-        *output_len = 0;
-    }
+    return successful;
+}
 
-    /* clean up */
+static void
+clean_up_try()
+{
     remove(TRY_SOURCE_PATH);
     remove(TRY_APP_PATH);
     remove(TARGET_PATH);
-
-    return captured_output;
 }
 
 void

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm?view=diff&rev=465265&r1=465264&r2=465265
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.harm Wed Oct 18 07:48:10 2006
@@ -35,6 +35,13 @@
 void
 chaz_Core_write_charm_h();
 
+/* Determine whether commands should be issued as ./do_stuff, .\do_stuff, or
+ * something else.  See how the compiler wants its object argument specified:
+ * '-o', '/o'.  Etc...
+ */
+void
+chaz_Core_probe_compiler_syntax();
+
 /* Attempt to compile the supplied source code.  If successful, capture the 
  * output of the program and return a pointer to a newly allocated buffer.
  * If the compilation fails, return NULL.  The length of the captured 
@@ -134,6 +141,7 @@
 # define verbosity              chaz_Core_verbosity
 # define want_short_names       chaz_Core_want_short_names
 # define write_charm_h          chaz_Core_write_charm_h
+# define probe_compiler_syntax  chaz_Core_probe_compiler_syntax 
 # define capture_output         chaz_Core_capture_output 
 # define shorten_constant       chaz_Core_shorten_constant
 # define shorten_macro          chaz_Core_shorten_macro



Mime
View raw message