build r458 - in trunk: . win32



Author: tml
Date: Mon May 19 12:35:39 2008
New Revision: 458
URL: http://svn.gnome.org/viewvc/build?rev=458&view=rev

Log:
2008-05-19  Tor Lillqvist  <tml novell com>

	Actually, this module isn't fully dead yet. win32/cl-wrapper.c
	might potentially be useful some day...

	* win32/cl-wrapper.c: Spent some time on this again. Some clear
	improvements.

	Don't use fixed file names for the a "forced" header file with
	pragmas, or dummy source file. Instead use mktemp() to generate
	unique names, so that running several instances of the wrapper at
	the same time works.

	Make -E and -P work properly.

	Dynamically grow the string arrays as needed.

	Still no attempt to implement the -Wl,--whole-archive flag though.

	Now attempting to build GLib with CC=gcc-cl proceeds a bit
	further. (gcc-cl.exe is what I call the result of building
	cl-wrapper.c, because having the name start with "gcc" makes the
	configury work a bit better. Or at least that is what I think.)
	Anyway, the libglib build now proceeds until libtool complains it
	can't find a "real file for library -lws2_32". Sigh...



Modified:
   trunk/ChangeLog
   trunk/win32/cl-wrapper.c

Modified: trunk/win32/cl-wrapper.c
==============================================================================
--- trunk/win32/cl-wrapper.c	(original)
+++ trunk/win32/cl-wrapper.c	Mon May 19 12:35:39 2008
@@ -23,35 +23,57 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <mbstring.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <io.h>
 
+#define WIN32_LEAN_AND_MEAN
+#define STRICT
+#include <windows.h>
+#undef WIN32_LEAN_AND_MEAN
+#undef STRICT
+
+static char *cl_wrapper_lib;
+
 static char *cmdline;
 static const char *cmdline_compiler;
 static const char *cmdline_args;
-static const char **libraries;
+
+static const char **link_flags;
+static int link_flag_ix = 0, link_flag_n = 0;
+
+static const char **libs;
+static int lib_ix = 0, lib_n = 0;
+
 static const char **libdirs;
+static int libdir_ix = 0, libdir_n = 0;
+
 static const char **objects;
+static int object_ix = 0, object_n = 0;
+
 static const char *output_executable = NULL;
 static char *output_object = NULL;
 static const char *executable_type = NULL;
 static const char *source = NULL;
 static const char *def_file = NULL;
-static int lib_ix = 0;
-static int libdir_ix = 0;
-static int object_ix = 0;
 
-static int compileonly = 0, debug = 0, output = 0, verbose = 0, version = 0;
+static int compileonly = 0;
+static int debug = 0;
+static int output = 0;
+static int verbose = 0;
+static int version = 0;
 static int nsources = 0;
 
 static float cl_version;
 
+static char *force_header_filename = NULL;
 static FILE *force_header = NULL;
 
-#define DUMMY_C_FILE "__dummy__.c"
-#define INDIRECT_CMDLINE_FILE "__cl_wrapper.at"
-#define FORCE_HEADER "__force_header.h"
+static char *dummy_source_filename = NULL;
+
+static int E_flag = 0;
+static int P_flag = 0;
 
 #if 0
 static int MD_flag = 0;
@@ -103,10 +125,6 @@
   "advapi32",
   "shell32",
   "oldnames",
-  /* mingw has dirent functions in its mingwex library, silly,
-   * but bite the bucket and link with the dirent library
-   */
-  "dirent",
   NULL
 };
 
@@ -124,6 +142,26 @@
   { NULL, NULL }
 };
 
+static void
+charv_append (const char ***p,
+	      int          *ix,
+	      int          *n,
+	      const char   *s)
+{
+  if (*n == 0)
+    {
+      *n = 10;
+      *p = malloc (*n * sizeof (const char *));
+    }
+  else if (*ix == *n)
+    {
+      *n *= 2;
+      *p = realloc (*p, *n * (sizeof (char *)));
+    }
+  (*p)[*ix] = s;
+  (*ix)++;
+}
+
 static char *
 backslashify (const char *string)
 {
@@ -190,17 +228,47 @@
   if (force_header != NULL)
     return;
 
-  force_header = fopen (FORCE_HEADER, "wt");
+  force_header_filename = malloc (strlen ("__cl-wrapper-force-header-XXXXXX.h") + 1);
+  strcpy (force_header_filename, "__cl-wrapper-force-header-XXXXXX");
+  mktemp (force_header_filename);
+  strcat (force_header_filename, ".h");
+
+  force_header = fopen (force_header_filename, "wt");
 
   if (force_header == NULL)
     {
-      fprintf (stderr, "Could not open temporary file " FORCE_HEADER " for writing: %s\n", strerror (errno));
+      fprintf (stderr, "Could not open temporary file %s for writing: %s\n",
+	       force_header_filename, strerror (errno));
       exit (1);
     }
 }
 
 static void
-process_argv (int argc,
+process_ld_option (const char **argv,
+		   int         *argi)
+{
+  if (strncmp (argv[*argi], "-Wl,--out-implib,", strlen ("-Wl,--out-implib,")) == 0)
+    {
+      charv_append (&link_flags, &link_flag_ix, &link_flag_n, "foo");
+    }
+  else if (strncmp (argv[*argi], "-Wl,--whole-archive,", strlen ("-Wl,--whole-archive,")) == 0)
+    {
+    }
+}
+
+static void
+preprocess_argv (int          argc,
+		 const char **argv)
+{
+  int i;
+
+  for (i = 1; i < argc; i++)
+    if (strcmp (argv[i], "-P") == 0)
+      P_flag = 1;
+}
+
+static void
+process_argv (int          argc,
 	      const char **argv)
 {
   int i, k;
@@ -228,7 +296,17 @@
 	strcat (cmdline, argv[i]);
       }
     else if (strcmp (argv[i], "-E") == 0)
-      strcat (cmdline, " -E");
+      {
+	E_flag = 1;
+	if (P_flag)
+	  strcat (cmdline, " -EP");
+	else
+	  strcat (cmdline, " -E");
+      }
+    else if (strcmp (argv[i], "-P") == 0)
+      {
+	/* Already handled in preprocess_argv() */
+      }
     else if (strcmp (argv[i], "-g") == 0)
       debug++;
     else if (strncmp (argv[i], "-I", 2) == 0)
@@ -286,18 +364,18 @@
       {
 	/* Ignore -lm */
 	if (strcmp (argv[i], "-lm") != 0)
-	  libraries[lib_ix++] = argv[i] + 2;
+	  charv_append (&libs, &lib_ix, &lib_n, argv[i] + 2);
       }
     else if (strncmp (argv[i], "-L", 2) == 0)
       {
 	if (strlen (argv[i]) > 2)
 	  {
-	    libdirs[libdir_ix++] = backslashify (argv[i] + 2);
+	    charv_append (&libdirs, &libdir_ix, &libdir_n, backslashify (argv[i] + 2));
 	  }
 	else
 	  {
 	    i++;
-	    libdirs[libdir_ix++] = backslashify (argv[i]);
+	    charv_append (&libdirs, &libdir_ix, &libdir_n, backslashify (argv[i]));
 	  }
       }
     else if (strcmp (argv[i], "-shared") == 0)
@@ -338,6 +416,9 @@
       }
     else if (strncmp (argv[i], "-O", 2) == 0)
       strcat (cmdline, " -O2");
+    else if (strcmp (argv[i], "-mthreads") == 0)
+      {
+      }
     else if (strncmp (argv[i], "-mtune=", 7) == 0)
       {
 	if (cl_version <= 13)
@@ -383,7 +464,12 @@
     else if (strncmp (argv[i], "-W", 2) == 0)
       {
 	if (strlen (argv[i]) > 3 && argv[i][3] == ',')
-	  fprintf (stderr, "Ignored subprocess option %s\n", argv[i]);
+	  {
+	    if (strncmp (argv[i], "-Wl,-", 5) == 0)
+	      process_ld_option (argv, &i);
+	    else
+	      fprintf (stderr, "Ignored flag %s\n", argv[i]);
+	  }
 	else if (strcmp (argv[i], "-Wall") == 0)
 	  {
 	    if (cl_version >= 13)
@@ -444,9 +530,39 @@
     else if (strcmp (argv[i], "-print-search-dirs") == 0 ||
 	     strcmp (argv[i], "--print-search-dirs") == 0)
       {
-	printf ("libraries: %s\n", slashify (getenv ("LIB")));
+	printf ("libs: %s\n", slashify (getenv ("LIB")));
 	exit (0);
       }
+    else if (strcmp (argv[i], "-") == 0)
+      {
+	dummy_source_filename = malloc (strlen ("__cl-wrapper-dummy-XXXXXX.c") + 1);
+	char buf[1000];
+	FILE *dummy_source;
+	int n;
+
+	strcpy (dummy_source_filename, "__cl-wrapper-dummy-XXXXXX");
+	mktemp (dummy_source_filename);
+	strcat (dummy_source_filename, ".c");
+
+	dummy_source = fopen (dummy_source_filename, "wt");
+
+	if (dummy_source == NULL)
+	  {
+	    fprintf (stderr, "Could not open temporary file %s for writing: %s\n",
+		     dummy_source_filename, strerror (errno));
+	    exit (1);
+	  }
+
+	while ((n = fread (buf, 1, sizeof (buf), stdin)) > 0)
+	  fwrite (buf, 1, n, dummy_source);
+
+	fclose (dummy_source);
+	
+	nsources++;
+	strcat (cmdline, " ");
+	strcat (cmdline, dummy_source_filename);
+	source = dummy_source_filename;
+      }
     else if (argv[i][0] == '-')
       fprintf (stderr, "Ignored flag %s\n", argv[i]);
     else
@@ -465,7 +581,7 @@
 	  }
 	else if (lastdot != NULL && (stricmp (lastdot, ".obj") == 0 ||
 				     stricmp (lastdot, ".o") == 0))
-	  objects[object_ix++] = argv[i];
+	  charv_append (&objects, &object_ix, &object_n, argv[i]);
 	else if (lastdot != NULL && stricmp (lastdot, ".a") == 0)
 	  {
 	    /* Copy .a file to .lib. Or what? Rename temporarily?
@@ -509,9 +625,22 @@
   int i, j, k;
   char *p;
 
-  libraries = malloc (argc * sizeof (char *));
-  libdirs = malloc ((argc+10) * sizeof (char *));
-  objects = malloc (argc * sizeof (char *));
+  cl_wrapper_lib = getenv ("CL_WRAPPER_LIB");
+  if (cl_wrapper_lib == NULL)
+    {
+      char exepath[2000];
+      char *backslash;
+      
+      /* Assume that this executable is in foo/bin, with its other stuff
+       * then in foo/lib/cl-wrapper.
+       */
+      GetModuleFileName (NULL, exepath, sizeof (exepath));
+      backslash = _mbsrchr (exepath, '\\');
+      *backslash = '\0';
+      backslash = _mbsrchr (exepath, '\\');
+      strcpy (backslash+1, "lib\\cl-wrapper");
+      cl_wrapper_lib = strdup (exepath);
+    }
 
   for (k = 0, i = 1; i < argc; i++)
     k += strlen (argv[i]);
@@ -545,6 +674,8 @@
   cmdline_args = cmdline + strlen (cmdline);
   strcat (cmdline, " -MD -Zm500");
 
+  preprocess_argv (argc, (const char **) argv);
+
   /* Make cl more like gcc. */
   open_force_header ();
   for (k = 0; error_warnings[k] != 0; k++)
@@ -562,9 +693,9 @@
     strcat (cmdline, " -c nul.c");
   else
     {
-      if (nsources == 1 && output_object == NULL)
+      if (nsources == 1 && output_object == NULL && !E_flag)
 	{
-	  const char *base = strrchr (source, '\\');
+	  const char *base = _mbsrchr (source, '\\');
 	  char *dot;
 
 	  if (base == NULL)
@@ -593,22 +724,28 @@
 
       if (nsources == 0)
 	{
-	  FILE *dummy = fopen (DUMMY_C_FILE, "w");
-	  fprintf (dummy, "static int foobar = 42;\n");
-	  fclose (dummy);
-
-	  strcat (cmdline, " " DUMMY_C_FILE);
+	  strcat (cmdline, " nul.c");
 	}
 
-      strcat (cmdline, " -FI" FORCE_HEADER);
+      if (!P_flag)
+	{
+	  strcat (cmdline, " -FI");
+	  strcat (cmdline, force_header_filename);
+	}
 
-      if (!output && !compileonly)
+      if (!output && !compileonly &&!E_flag)
 	strcat (cmdline, " -Fea.exe");
 
       if (!compileonly)
 	{
 	  strcat (cmdline, " -link");
 
+	  for (i = 0; i < link_flag_ix; i++)
+	    {
+	      strcat (cmdline, " ");
+	      strcat (cmdline, link_flags[i]);
+	    }
+
 	  for (i = 0; i < object_ix; i++)
 	    {
 	      strcat (cmdline, " ");
@@ -621,14 +758,14 @@
 		{
 		  char b[1000];
 
-		  sprintf (b, "%s\\%s.lib", libdirs[j], libraries[i]);
+		  sprintf (b, "%s\\%s.lib", libdirs[j], libs[i]);
 		  if (access (b, 4) == 0)
 		    {
 		      strcat (cmdline, " ");
 		      strcat (cmdline, b);
 		      break;
 		    }
-		  sprintf (b, "%s\\lib%s.lib", libdirs[j], libraries[i]);
+		  sprintf (b, "%s\\lib%s.lib", libdirs[j], libs[i]);
 		  if (access (b, 4) == 0)
 		    {
 		      strcat (cmdline, " ");
@@ -640,11 +777,19 @@
 	      if (j == libdir_ix)
 		{
 		  strcat (cmdline, " ");
-		  strcat (cmdline, libraries[i]);
+		  strcat (cmdline, libs[i]);
 		  strcat (cmdline, ".lib");
 		}
 	    }
-
+#if 0
+	  /* mingw has dirent functions in its mingwex library that is
+	   * automatically used, silly, so bite the bucket and link with the
+	   * dirent library
+	   */
+	  strcat (cmdline, " ");
+	  strcat (cmdline, cl_wrapper_lib);
+	  strcat (cmdline, "\\dirent.lib");
+#endif
 	  for (k = 0; default_libs[k]; k++)
 	    {
 	      strcat (cmdline, " ");
@@ -658,40 +803,49 @@
 
   if (strlen (cmdline) >= 200)	/* Real limit unknown */
     {
-      FILE *atfile = fopen (INDIRECT_CMDLINE_FILE, "wt");
-      char *indirect_cmdline = malloc (strlen (cmdline_compiler) +
-				       strlen (INDIRECT_CMDLINE_FILE) + 10);
+      char *atfile_name = malloc (strlen ("__cl-wrapper-XXXXXX") + 1);
+      FILE *atfile;
+      char *indirect_cmdline;
+
+      strcpy (atfile_name, "__cl-wrapper-XXXXXX");
+      mktemp (atfile_name);
+
+      atfile = fopen (atfile_name, "wt");
 
       if (atfile == NULL)
 	{
 	  fprintf (stderr, "Could not open %s for writing: %s\n",
-		   INDIRECT_CMDLINE_FILE, strerror (errno));
+		   atfile_name, strerror (errno));
 	  exit (-1);
 	}
 
       fprintf (atfile, "%s\n", cmdline_args);
       fclose (atfile);
 
-      sprintf (indirect_cmdline, "%s @" INDIRECT_CMDLINE_FILE,
-	       cmdline_compiler);
-      dup2 (2, 1);
+      indirect_cmdline = malloc (strlen (cmdline_compiler) +
+				 strlen (atfile_name) + 4);
+      strcpy (indirect_cmdline, cmdline_compiler);
+      strcat (indirect_cmdline, " @");
+      strcat (indirect_cmdline, atfile_name);
+
+      if (!E_flag)
+	dup2 (2, 1);
       retval = system (indirect_cmdline);
-#if 0
-      remove (INDIRECT_CMDLINE_FILE);
-#endif
+
+      remove (atfile_name);
     }
   else
     {
-      dup2 (2, 1);
+      if (!E_flag)
+	dup2 (2, 1);
       retval = system (cmdline);
     }
 
-  if (nsources == 0)
-    remove (DUMMY_C_FILE);
+  if (getenv ("DONTREMOVE") == NULL && force_header_filename != NULL)
+    remove (force_header_filename);
 
-#if 0
-  remove (FORCE_HEADER);
-#endif
+  if (getenv ("DONTREMOVE") == NULL && dummy_source_filename != NULL)
+    remove (dummy_source_filename);
 
 #if 0
   /* Produce a dummy make dependency file if asked to... Perhaps it



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]