ltmain.sh 277 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4001

4002
4003
  # Parse options
  func_parse_lt_options \"\$0\" \${1+\"\$@\"}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4004

4005
4006
4007
  # Find the directory that this script lives in.
  thisdir=\`\$ECHO \"\$file\" | $SED 's%/[^/]*$%%'\`
  test \"x\$thisdir\" = \"x\$file\" && thisdir=.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4008

4009
4010
4011
4012
  # Follow symbolic links until we get to the real thisdir.
  file=\`ls -ld \"\$file\" | $SED -n 's/.*-> //p'\`
  while test -n \"\$file\"; do
    destdir=\`\$ECHO \"\$file\" | $SED 's%/[^/]*\$%%'\`
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4013

4014
4015
4016
4017
4018
    # If there was a directory component, then change thisdir.
    if test \"x\$destdir\" != \"x\$file\"; then
      case \"\$destdir\" in
      [\\\\/]* | [A-Za-z]:[\\\\/]*) thisdir=\"\$destdir\" ;;
      *) thisdir=\"\$thisdir/\$destdir\" ;;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4019
      esac
4020
    fi
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4021

4022
4023
4024
    file=\`\$ECHO \"\$file\" | $SED 's%^.*/%%'\`
    file=\`ls -ld \"\$thisdir/\$file\" | $SED -n 's/.*-> //p'\`
  done
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4025

4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
  # Usually 'no', except on cygwin/mingw when embedded into
  # the cwrapper.
  WRAPPER_SCRIPT_BELONGS_IN_OBJDIR=$func_emit_wrapper_arg1
  if test \"\$WRAPPER_SCRIPT_BELONGS_IN_OBJDIR\" = \"yes\"; then
    # special case for '.'
    if test \"\$thisdir\" = \".\"; then
      thisdir=\`pwd\`
    fi
    # remove .libs from thisdir
    case \"\$thisdir\" in
    *[\\\\/]$objdir ) thisdir=\`\$ECHO \"\$thisdir\" | $SED 's%[\\\\/][^\\\\/]*$%%'\` ;;
    $objdir )   thisdir=. ;;
    esac
  fi
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4040

4041
4042
4043
4044
  # Try to get the absolute directory name.
  absdir=\`cd \"\$thisdir\" && pwd\`
  test -n \"\$absdir\" && thisdir=\"\$absdir\"
"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4045

4046
4047
4048
4049
	if test "$fast_install" = yes; then
	  $ECHO "\
  program=lt-'$outputname'$exeext
  progdir=\"\$thisdir/$objdir\"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4050

4051
4052
4053
  if test ! -f \"\$progdir/\$program\" ||
     { file=\`ls -1dt \"\$progdir/\$program\" \"\$progdir/../\$program\" 2>/dev/null | ${SED} 1q\`; \\
       test \"X\$file\" != \"X\$progdir/\$program\"; }; then
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4054

4055
    file=\"\$\$-\$program\"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4056

4057
4058
4059
4060
4061
    if test ! -d \"\$progdir\"; then
      $MKDIR \"\$progdir\"
    else
      $RM \"\$progdir/\$file\"
    fi"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4062

4063
4064
4065
4066
4067
4068
4069
4070
4071
	  $ECHO "\

    # relink executable if necessary
    if test -n \"\$relink_command\"; then
      if relink_command_output=\`eval \$relink_command 2>&1\`; then :
      else
	$ECHO \"\$relink_command_output\" >&2
	$RM \"\$progdir/\$file\"
	exit 1
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4072
      fi
4073
    fi
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4074

4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
    $MV \"\$progdir/\$file\" \"\$progdir/\$program\" 2>/dev/null ||
    { $RM \"\$progdir/\$program\";
      $MV \"\$progdir/\$file\" \"\$progdir/\$program\"; }
    $RM \"\$progdir/\$file\"
  fi"
	else
	  $ECHO "\
  program='$outputname'
  progdir=\"\$thisdir/$objdir\"
"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4085
4086
	fi

4087
	$ECHO "\
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4088

4089
  if test -f \"\$progdir/\$program\"; then"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4090

4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
	# fixup the dll searchpath if we need to.
	#
	# Fix the DLL searchpath if we need to.  Do this before prepending
	# to shlibpath, because on Windows, both are PATH and uninstalled
	# libraries must come first.
	if test -n "$dllsearchpath"; then
	  $ECHO "\
    # Add the dll search path components to the executable PATH
    PATH=$dllsearchpath:\$PATH
"
	fi
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4102

4103
4104
4105
4106
4107
	# Export our shlibpath_var if we have one.
	if test "$shlibpath_overrides_runpath" = yes && test -n "$shlibpath_var" && test -n "$temp_rpath"; then
	  $ECHO "\
    # Add our own library path to $shlibpath_var
    $shlibpath_var=\"$temp_rpath\$$shlibpath_var\"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4108

4109
4110
4111
    # Some systems cannot cope with colon-terminated $shlibpath_var
    # The second colon is a workaround for a bug in BeOS R4 sed
    $shlibpath_var=\`\$ECHO \"\$$shlibpath_var\" | $SED 's/::*\$//'\`
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4112

4113
4114
    export $shlibpath_var
"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4115
4116
	fi

4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
	$ECHO "\
    if test \"\$libtool_execute_magic\" != \"$magic\"; then
      # Run the actual program with our arguments.
      func_exec_program \${1+\"\$@\"}
    fi
  else
    # The program doesn't exist.
    \$ECHO \"\$0: error: \\\`\$progdir/\$program' does not exist\" 1>&2
    \$ECHO \"This script is just a wrapper for \$program.\" 1>&2
    \$ECHO \"See the $PACKAGE documentation for more information.\" 1>&2
    exit 1
  fi
fi\
"
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4132
4133


4134
4135
4136
4137
4138
4139
4140
# func_emit_cwrapperexe_src
# emit the source code for a wrapper executable on stdout
# Must ONLY be called from within func_mode_link because
# it depends on a number of variable set therein.
func_emit_cwrapperexe_src ()
{
	cat <<EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4141

4142
4143
/* $cwrappersource - temporary wrapper executable for $objdir/$outputname
   Generated by $PROGRAM (GNU $PACKAGE$TIMESTAMP) $VERSION
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4144

4145
4146
   The $output program cannot be directly executed until all the libtool
   libraries that it depends on are installed.
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4147

4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
   This wrapper executable should never be moved out of the build directory.
   If it is, it will not operate correctly.
*/
EOF
	    cat <<"EOF"
#ifdef _MSC_VER
# define _CRT_SECURE_NO_DEPRECATE 1
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
# include <direct.h>
# include <process.h>
# include <io.h>
#else
# include <unistd.h>
# include <stdint.h>
# ifdef __CYGWIN__
#  include <io.h>
# endif
#endif
#include <malloc.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4177

4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
/* declarations of non-ANSI functions */
#if defined(__MINGW32__)
# ifdef __STRICT_ANSI__
int _putenv (const char *);
# endif
#elif defined(__CYGWIN__)
# ifdef __STRICT_ANSI__
char *realpath (const char *, char *);
int putenv (char *);
int setenv (const char *, const char *, int);
# endif
/* #elif defined (other platforms) ... */
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4191

4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
/* portability defines, excluding path handling macros */
#if defined(_MSC_VER)
# define setmode _setmode
# define stat    _stat
# define chmod   _chmod
# define getcwd  _getcwd
# define putenv  _putenv
# define S_IXUSR _S_IEXEC
# ifndef _INTPTR_T_DEFINED
#  define _INTPTR_T_DEFINED
#  define intptr_t int
# endif
#elif defined(__MINGW32__)
# define setmode _setmode
# define stat    _stat
# define chmod   _chmod
# define getcwd  _getcwd
# define putenv  _putenv
#elif defined(__CYGWIN__)
# define HAVE_SETENV
# define FOPEN_WB "wb"
/* #elif defined (other platforms) ... */
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4215

4216
4217
4218
4219
4220
4221
4222
#if defined(PATH_MAX)
# define LT_PATHMAX PATH_MAX
#elif defined(MAXPATHLEN)
# define LT_PATHMAX MAXPATHLEN
#else
# define LT_PATHMAX 1024
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4223

4224
4225
4226
4227
4228
4229
#ifndef S_IXOTH
# define S_IXOTH 0
#endif
#ifndef S_IXGRP
# define S_IXGRP 0
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4230

4231
4232
4233
4234
4235
/* path handling portability macros */
#ifndef DIR_SEPARATOR
# define DIR_SEPARATOR '/'
# define PATH_SEPARATOR ':'
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4236

4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
#if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
  defined (__OS2__)
# define HAVE_DOS_BASED_FILE_SYSTEM
# define FOPEN_WB "wb"
# ifndef DIR_SEPARATOR_2
#  define DIR_SEPARATOR_2 '\\'
# endif
# ifndef PATH_SEPARATOR_2
#  define PATH_SEPARATOR_2 ';'
# endif
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4248

4249
4250
4251
4252
4253
4254
#ifndef DIR_SEPARATOR_2
# define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
#else /* DIR_SEPARATOR_2 */
# define IS_DIR_SEPARATOR(ch) \
	(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
#endif /* DIR_SEPARATOR_2 */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4255

4256
4257
4258
4259
4260
#ifndef PATH_SEPARATOR_2
# define IS_PATH_SEPARATOR(ch) ((ch) == PATH_SEPARATOR)
#else /* PATH_SEPARATOR_2 */
# define IS_PATH_SEPARATOR(ch) ((ch) == PATH_SEPARATOR_2)
#endif /* PATH_SEPARATOR_2 */
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4261

4262
4263
4264
4265
4266
#ifndef FOPEN_WB
# define FOPEN_WB "w"
#endif
#ifndef _O_BINARY
# define _O_BINARY 0
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4267
4268
#endif

4269
4270
4271
4272
#define XMALLOC(type, num)      ((type *) xmalloc ((num) * sizeof(type)))
#define XFREE(stale) do { \
  if (stale) { free ((void *) stale); stale = 0; } \
} while (0)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4273

4274
4275
4276
4277
4278
#if defined(LT_DEBUGWRAPPER)
static int lt_debug = 1;
#else
static int lt_debug = 0;
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4279

4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
const char *program_name = "libtool-wrapper"; /* in case xstrdup fails */

void *xmalloc (size_t num);
char *xstrdup (const char *string);
const char *base_name (const char *name);
char *find_executable (const char *wrapper);
char *chase_symlinks (const char *pathspec);
int make_executable (const char *path);
int check_executable (const char *path);
char *strendzap (char *str, const char *pat);
void lt_debugprintf (const char *file, int line, const char *fmt, ...);
void lt_fatal (const char *file, int line, const char *message, ...);
static const char *nonnull (const char *s);
static const char *nonempty (const char *s);
void lt_setenv (const char *name, const char *value);
char *lt_extend_str (const char *orig_value, const char *add, int to_end);
void lt_update_exe_path (const char *name, const char *value);
void lt_update_lib_path (const char *name, const char *value);
char **prepare_spawn (char **argv);
void lt_dump_script (FILE *f);
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4301

4302
4303
4304
4305
	    cat <<EOF
volatile const char * MAGIC_EXE = "$magic_exe";
const char * LIB_PATH_VARNAME = "$shlibpath_var";
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4306

4307
4308
4309
4310
4311
4312
4313
4314
4315
	    if test "$shlibpath_overrides_runpath" = yes && test -n "$shlibpath_var" && test -n "$temp_rpath"; then
              func_to_host_path "$temp_rpath"
	      cat <<EOF
const char * LIB_PATH_VALUE   = "$func_to_host_path_result";
EOF
	    else
	      cat <<"EOF"
const char * LIB_PATH_VALUE   = "";
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4316
4317
	    fi

4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
	    if test -n "$dllsearchpath"; then
              func_to_host_path "$dllsearchpath:"
	      cat <<EOF
const char * EXE_PATH_VARNAME = "PATH";
const char * EXE_PATH_VALUE   = "$func_to_host_path_result";
EOF
	    else
	      cat <<"EOF"
const char * EXE_PATH_VARNAME = "";
const char * EXE_PATH_VALUE   = "";
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4329
4330
	    fi

4331
4332
4333
4334
	    if test "$fast_install" = yes; then
	      cat <<EOF
const char * TARGET_PROGRAM_NAME = "lt-$outputname"; /* hopefully, no .exe */
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4335
	    else
4336
4337
4338
	      cat <<EOF
const char * TARGET_PROGRAM_NAME = "$outputname"; /* hopefully, no .exe */
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4339
4340
4341
	    fi


4342
	    cat <<"EOF"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4343

4344
#define LTWRAPPER_OPTION_PREFIX         "--lt-"
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4345

4346
4347
4348
static const char *ltwrapper_option_prefix = LTWRAPPER_OPTION_PREFIX;
static const char *dumpscript_opt       = LTWRAPPER_OPTION_PREFIX "dump-script";
static const char *debug_opt            = LTWRAPPER_OPTION_PREFIX "debug";
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4349

4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
int
main (int argc, char *argv[])
{
  char **newargz;
  int  newargc;
  char *tmp_pathspec;
  char *actual_cwrapper_path;
  char *actual_cwrapper_name;
  char *target_name;
  char *lt_argv_zero;
  intptr_t rval = 127;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4361

4362
  int i;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4363

4364
4365
  program_name = (char *) xstrdup (base_name (argv[0]));
  newargz = XMALLOC (char *, argc + 1);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4366

4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
  /* very simple arg parsing; don't want to rely on getopt
   * also, copy all non cwrapper options to newargz, except
   * argz[0], which is handled differently
   */
  newargc=0;
  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], dumpscript_opt) == 0)
	{
EOF
	    case "$host" in
	      *mingw* | *cygwin* )
		# make stdout use "unix" line endings
		echo "          setmode(1,_O_BINARY);"
		;;
	      esac
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4383

4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
	    cat <<"EOF"
	  lt_dump_script (stdout);
	  return 0;
	}
      if (strcmp (argv[i], debug_opt) == 0)
	{
          lt_debug = 1;
          continue;
	}
      if (strcmp (argv[i], ltwrapper_option_prefix) == 0)
        {
          /* however, if there is an option in the LTWRAPPER_OPTION_PREFIX
             namespace, but it is not one of the ones we know about and
             have already dealt with, above (inluding dump-script), then
             report an error. Otherwise, targets might begin to believe
             they are allowed to use options in the LTWRAPPER_OPTION_PREFIX
             namespace. The first time any user complains about this, we'll
             need to make LTWRAPPER_OPTION_PREFIX a configure-time option
             or a configure.ac-settable value.
           */
          lt_fatal (__FILE__, __LINE__,
		    "unrecognized %s option: '%s'",
                    ltwrapper_option_prefix, argv[i]);
        }
      /* otherwise ... */
      newargz[++newargc] = xstrdup (argv[i]);
    }
  newargz[++newargc] = NULL;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4412

4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
EOF
	    cat <<EOF
  /* The GNU banner must be the first non-error debug message */
  lt_debugprintf (__FILE__, __LINE__, "libtool wrapper (GNU $PACKAGE$TIMESTAMP) $VERSION\n");
EOF
	    cat <<"EOF"
  lt_debugprintf (__FILE__, __LINE__, "(main) argv[0]: %s\n", argv[0]);
  lt_debugprintf (__FILE__, __LINE__, "(main) program_name: %s\n", program_name);

  tmp_pathspec = find_executable (argv[0]);
  if (tmp_pathspec == NULL)
    lt_fatal (__FILE__, __LINE__, "couldn't find %s", argv[0]);
  lt_debugprintf (__FILE__, __LINE__,
                  "(main) found exe (before symlink chase) at: %s\n",
		  tmp_pathspec);

  actual_cwrapper_path = chase_symlinks (tmp_pathspec);
  lt_debugprintf (__FILE__, __LINE__,
                  "(main) found exe (after symlink chase) at: %s\n",
		  actual_cwrapper_path);
  XFREE (tmp_pathspec);

  actual_cwrapper_name = xstrdup (base_name (actual_cwrapper_path));
  strendzap (actual_cwrapper_path, actual_cwrapper_name);

  /* wrapper name transforms */
  strendzap (actual_cwrapper_name, ".exe");
  tmp_pathspec = lt_extend_str (actual_cwrapper_name, ".exe", 1);
  XFREE (actual_cwrapper_name);
  actual_cwrapper_name = tmp_pathspec;
  tmp_pathspec = 0;

  /* target_name transforms -- use actual target program name; might have lt- prefix */
  target_name = xstrdup (base_name (TARGET_PROGRAM_NAME));
  strendzap (target_name, ".exe");
  tmp_pathspec = lt_extend_str (target_name, ".exe", 1);
  XFREE (target_name);
  target_name = tmp_pathspec;
  tmp_pathspec = 0;

  lt_debugprintf (__FILE__, __LINE__,
		  "(main) libtool target name: %s\n",
		  target_name);
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4457

4458
4459
4460
4461
4462
4463
4464
4465
	    cat <<EOF
  newargz[0] =
    XMALLOC (char, (strlen (actual_cwrapper_path) +
		    strlen ("$objdir") + 1 + strlen (actual_cwrapper_name) + 1));
  strcpy (newargz[0], actual_cwrapper_path);
  strcat (newargz[0], "$objdir");
  strcat (newargz[0], "/");
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4466

4467
4468
4469
	    cat <<"EOF"
  /* stop here, and copy so we don't have to do this twice */
  tmp_pathspec = xstrdup (newargz[0]);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4470

4471
4472
  /* do NOT want the lt- prefix here, so use actual_cwrapper_name */
  strcat (newargz[0], actual_cwrapper_name);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4473

4474
4475
4476
4477
4478
  /* DO want the lt- prefix here if it exists, so use target_name */
  lt_argv_zero = lt_extend_str (tmp_pathspec, target_name, 1);
  XFREE (tmp_pathspec);
  tmp_pathspec = NULL;
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4479

4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
	    case $host_os in
	      mingw*)
	    cat <<"EOF"
  {
    char* p;
    while ((p = strchr (newargz[0], '\\')) != NULL)
      {
	*p = '/';
      }
    while ((p = strchr (lt_argv_zero, '\\')) != NULL)
      {
	*p = '/';
      }
  }
EOF
	    ;;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4496
4497
	    esac

4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
	    cat <<"EOF"
  XFREE (target_name);
  XFREE (actual_cwrapper_path);
  XFREE (actual_cwrapper_name);

  lt_setenv ("BIN_SH", "xpg4"); /* for Tru64 */
  lt_setenv ("DUALCASE", "1");  /* for MSK sh */
  /* Update the DLL searchpath.  EXE_PATH_VALUE ($dllsearchpath) must
     be prepended before (that is, appear after) LIB_PATH_VALUE ($temp_rpath)
     because on Windows, both *_VARNAMEs are PATH but uninstalled
     libraries must come first. */
  lt_update_exe_path (EXE_PATH_VARNAME, EXE_PATH_VALUE);
  lt_update_lib_path (LIB_PATH_VARNAME, LIB_PATH_VALUE);

  lt_debugprintf (__FILE__, __LINE__, "(main) lt_argv_zero: %s\n",
		  nonnull (lt_argv_zero));
  for (i = 0; i < newargc; i++)
    {
      lt_debugprintf (__FILE__, __LINE__, "(main) newargz[%d]: %s\n",
		      i, nonnull (newargz[i]));
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4519

4520
EOF
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4521

4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
	    case $host_os in
	      mingw*)
		cat <<"EOF"
  /* execv doesn't actually work on mingw as expected on unix */
  newargz = prepare_spawn (newargz);
  rval = _spawnv (_P_WAIT, lt_argv_zero, (const char * const *) newargz);
  if (rval == -1)
    {
      /* failed to start process */
      lt_debugprintf (__FILE__, __LINE__,
		      "(main) failed to launch target \"%s\": %s\n",
		      lt_argv_zero, nonnull (strerror (errno)));
      return 127;
    }
  return rval;
EOF
		;;
	      *)
		cat <<"EOF"
  execv (lt_argv_zero, newargz);
  return rval; /* =127, but avoids unused variable warning */
EOF
		;;
	    esac
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4546

4547
4548
	    cat <<"EOF"
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4549

4550
4551
4552
4553
4554
4555
void *
xmalloc (size_t num)
{
  void *p = (void *) malloc (num);
  if (!p)
    lt_fatal (__FILE__, __LINE__, "memory exhausted");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4556

4557
4558
  return p;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4559

4560
4561
4562
4563
4564
4565
char *
xstrdup (const char *string)
{
  return string ? strcpy ((char *) xmalloc (strlen (string) + 1),
			  string) : NULL;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4566

4567
4568
4569
4570
const char *
base_name (const char *name)
{
  const char *base;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4571

4572
4573
4574
4575
4576
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
  /* Skip over the disk name in MSDOS pathnames. */
  if (isalpha ((unsigned char) name[0]) && name[1] == ':')
    name += 2;
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4577

4578
4579
4580
4581
4582
  for (base = name; *name; name++)
    if (IS_DIR_SEPARATOR (*name))
      base = name + 1;
  return base;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4583

4584
4585
4586
4587
int
check_executable (const char *path)
{
  struct stat st;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4588

4589
4590
4591
4592
  lt_debugprintf (__FILE__, __LINE__, "(check_executable): %s\n",
                  nonempty (path));
  if ((!path) || (!*path))
    return 0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4593

4594
4595
4596
4597
4598
4599
  if ((stat (path, &st) >= 0)
      && (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
    return 1;
  else
    return 0;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4600

4601
4602
4603
4604
4605
int
make_executable (const char *path)
{
  int rval = 0;
  struct stat st;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4606

4607
4608
4609
4610
  lt_debugprintf (__FILE__, __LINE__, "(make_executable): %s\n",
                  nonempty (path));
  if ((!path) || (!*path))
    return 0;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4611

4612
4613
4614
4615
4616
4617
  if (stat (path, &st) >= 0)
    {
      rval = chmod (path, st.st_mode | S_IXOTH | S_IXGRP | S_IXUSR);
    }
  return rval;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4618

4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
/* Searches for the full path of the wrapper.  Returns
   newly allocated full path name if found, NULL otherwise
   Does not chase symlinks, even on platforms that support them.
*/
char *
find_executable (const char *wrapper)
{
  int has_slash = 0;
  const char *p;
  const char *p_next;
  /* static buffer for getcwd */
  char tmp[LT_PATHMAX + 1];
  int tmp_len;
  char *concat_name;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4633

4634
4635
  lt_debugprintf (__FILE__, __LINE__, "(find_executable): %s\n",
                  nonempty (wrapper));
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4636

4637
4638
  if ((wrapper == NULL) || (*wrapper == '\0'))
    return NULL;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4639

4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
  /* Absolute path? */
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
  if (isalpha ((unsigned char) wrapper[0]) && wrapper[1] == ':')
    {
      concat_name = xstrdup (wrapper);
      if (check_executable (concat_name))
	return concat_name;
      XFREE (concat_name);
    }
  else
    {
#endif
      if (IS_DIR_SEPARATOR (wrapper[0]))
	{
	  concat_name = xstrdup (wrapper);
	  if (check_executable (concat_name))
	    return concat_name;
	  XFREE (concat_name);
	}
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
    }
#endif
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4662

4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
  for (p = wrapper; *p; p++)
    if (*p == '/')
      {
	has_slash = 1;
	break;
      }
  if (!has_slash)
    {
      /* no slashes; search PATH */
      const char *path = getenv ("PATH");
      if (path != NULL)
	{
	  for (p = path; *p; p = p_next)
	    {
	      const char *q;
	      size_t p_len;
	      for (q = p; *q; q++)
		if (IS_PATH_SEPARATOR (*q))
		  break;
	      p_len = q - p;
	      p_next = (*q == '\0' ? q : q + 1);
	      if (p_len == 0)
		{
		  /* empty path: current directory */
		  if (getcwd (tmp, LT_PATHMAX) == NULL)
		    lt_fatal (__FILE__, __LINE__, "getcwd failed: %s",
                              nonnull (strerror (errno)));
		  tmp_len = strlen (tmp);
		  concat_name =
		    XMALLOC (char, tmp_len + 1 + strlen (wrapper) + 1);
		  memcpy (concat_name, tmp, tmp_len);
		  concat_name[tmp_len] = '/';
		  strcpy (concat_name + tmp_len + 1, wrapper);
		}
	      else
		{
		  concat_name =
		    XMALLOC (char, p_len + 1 + strlen (wrapper) + 1);
		  memcpy (concat_name, p, p_len);
		  concat_name[p_len] = '/';
		  strcpy (concat_name + p_len + 1, wrapper);
		}
	      if (check_executable (concat_name))
		return concat_name;
	      XFREE (concat_name);
	    }
	}
      /* not found in PATH; assume curdir */
    }
  /* Relative path | not found in path: prepend cwd */
  if (getcwd (tmp, LT_PATHMAX) == NULL)
    lt_fatal (__FILE__, __LINE__, "getcwd failed: %s",
              nonnull (strerror (errno)));
  tmp_len = strlen (tmp);
  concat_name = XMALLOC (char, tmp_len + 1 + strlen (wrapper) + 1);
  memcpy (concat_name, tmp, tmp_len);
  concat_name[tmp_len] = '/';
  strcpy (concat_name + tmp_len + 1, wrapper);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4721

4722
4723
4724
4725
4726
  if (check_executable (concat_name))
    return concat_name;
  XFREE (concat_name);
  return NULL;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4727

4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
char *
chase_symlinks (const char *pathspec)
{
#ifndef S_ISLNK
  return xstrdup (pathspec);
#else
  char buf[LT_PATHMAX];
  struct stat s;
  char *tmp_pathspec = xstrdup (pathspec);
  char *p;
  int has_symlinks = 0;
  while (strlen (tmp_pathspec) && !has_symlinks)
    {
      lt_debugprintf (__FILE__, __LINE__,
		      "checking path component for symlinks: %s\n",
		      tmp_pathspec);
      if (lstat (tmp_pathspec, &s) == 0)
	{
	  if (S_ISLNK (s.st_mode) != 0)
	    {
	      has_symlinks = 1;
	      break;
	    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4751

4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
	  /* search backwards for last DIR_SEPARATOR */
	  p = tmp_pathspec + strlen (tmp_pathspec) - 1;
	  while ((p > tmp_pathspec) && (!IS_DIR_SEPARATOR (*p)))
	    p--;
	  if ((p == tmp_pathspec) && (!IS_DIR_SEPARATOR (*p)))
	    {
	      /* no more DIR_SEPARATORS left */
	      break;
	    }
	  *p = '\0';
	}
      else
	{
	  lt_fatal (__FILE__, __LINE__,
		    "error accessing file \"%s\": %s",
		    tmp_pathspec, nonnull (strerror (errno)));
	}
    }
  XFREE (tmp_pathspec);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4771

4772
4773
4774
4775
  if (!has_symlinks)
    {
      return xstrdup (pathspec);
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4776

4777
4778
4779
4780
4781
4782
4783
  tmp_pathspec = realpath (pathspec, buf);
  if (tmp_pathspec == 0)
    {
      lt_fatal (__FILE__, __LINE__,
		"could not follow symlinks for %s", pathspec);
    }
  return xstrdup (tmp_pathspec);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4784
#endif
4785
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4786

4787
4788
char *
strendzap (char *str, const char *pat)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4789
{
4790
  size_t len, patlen;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4791

4792
4793
  assert (str != NULL);
  assert (pat != NULL);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4794

4795
4796
  len = strlen (str);
  patlen = strlen (pat);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4797

4798
4799
4800
4801
4802
4803
4804
4805
  if (patlen <= len)
    {
      str += len - patlen;
      if (strcmp (str, pat) == 0)
	*str = '\0';
    }
  return str;
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4806

4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
void
lt_debugprintf (const char *file, int line, const char *fmt, ...)
{
  va_list args;
  if (lt_debug)
    {
      (void) fprintf (stderr, "%s:%s:%d: ", program_name, file, line);
      va_start (args, fmt);
      (void) vfprintf (stderr, fmt, args);
      va_end (args);
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4818
4819
}

4820
4821
4822
4823
static void
lt_error_core (int exit_status, const char *file,
	       int line, const char *mode,
	       const char *message, va_list ap)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4824
{
4825
4826
4827
  fprintf (stderr, "%s:%s:%d: %s: ", program_name, file, line, mode);
  vfprintf (stderr, message, ap);
  fprintf (stderr, ".\n");
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4828

4829
4830
  if (exit_status >= 0)
    exit (exit_status);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4831
4832
}

4833
4834
void
lt_fatal (const char *file, int line, const char *message, ...)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4835
{
4836
4837
4838
4839
  va_list ap;
  va_start (ap, message);
  lt_error_core (EXIT_FAILURE, file, line, "FATAL", message, ap);
  va_end (ap);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4840
4841
}

4842
4843
static const char *
nonnull (const char *s)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4844
{
4845
  return s ? s : "(null)";
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4846
4847
}

4848
4849
static const char *
nonempty (const char *s)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4850
{
4851
4852
  return (s && !*s) ? "(empty)" : nonnull (s);
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4853

4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
void
lt_setenv (const char *name, const char *value)
{
  lt_debugprintf (__FILE__, __LINE__,
		  "(lt_setenv) setting '%s' to '%s'\n",
                  nonnull (name), nonnull (value));
  {
#ifdef HAVE_SETENV
    /* always make a copy, for consistency with !HAVE_SETENV */
    char *str = xstrdup (value);
    setenv (name, str, 1);
#else
    int len = strlen (name) + 1 + strlen (value) + 1;
    char *str = XMALLOC (char, len);
    sprintf (str, "%s=%s", name, value);
    if (putenv (str) != EXIT_SUCCESS)
      {
        XFREE (str);
      }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4873
#endif
4874
  }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4875
4876
4877
}

char *
4878
lt_extend_str (const char *orig_value, const char *add, int to_end)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4879
{
4880
4881
  char *new_value;
  if (orig_value && *orig_value)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4882
    {
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
      int orig_value_len = strlen (orig_value);
      int add_len = strlen (add);
      new_value = XMALLOC (char, add_len + orig_value_len + 1);
      if (to_end)
        {
          strcpy (new_value, orig_value);
          strcpy (new_value + orig_value_len, add);
        }
      else
        {
          strcpy (new_value, add);
          strcpy (new_value + add_len, orig_value);
        }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4896
    }
4897
  else
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4898
    {
4899
      new_value = xstrdup (add);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4900
    }
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
  return new_value;
}

void
lt_update_exe_path (const char *name, const char *value)
{
  lt_debugprintf (__FILE__, __LINE__,
		  "(lt_update_exe_path) modifying '%s' by prepending '%s'\n",
                  nonnull (name), nonnull (value));

  if (name && *name && value && *value)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4912
    {
4913
4914
4915
4916
      char *new_value = lt_extend_str (getenv (name), value, 0);
      /* some systems can't cope with a ':'-terminated path #' */
      int len = strlen (new_value);
      while (((len = strlen (new_value)) > 0) && IS_PATH_SEPARATOR (new_value[len-1]))
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4917
        {
4918
          new_value[len-1] = '\0';
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4919
        }
4920
4921
      lt_setenv (name, new_value);
      XFREE (new_value);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4922
    }
4923
}
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4924

4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
void
lt_update_lib_path (const char *name, const char *value)
{
  lt_debugprintf (__FILE__, __LINE__,
		  "(lt_update_lib_path) modifying '%s' by prepending '%s'\n",
                  nonnull (name), nonnull (value));

  if (name && *name && value && *value)
    {
      char *new_value = lt_extend_str (getenv (name), value, 0);
      lt_setenv (name, new_value);
      XFREE (new_value);
    }
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4938
4939
}

4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
EOF
	    case $host_os in
	      mingw*)
		cat <<"EOF"

/* Prepares an argument vector before calling spawn().
   Note that spawn() does not by itself call the command interpreter
     (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
      ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
         GetVersionEx(&v);
         v.dwPlatformId == VER_PLATFORM_WIN32_NT;
      }) ? "cmd.exe" : "command.com").
   Instead it simply concatenates the arguments, separated by ' ', and calls
   CreateProcess().  We must quote the arguments since Win32 CreateProcess()
   interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
   special way:
   - Space and tab are interpreted as delimiters. They are not treated as
     delimiters if they are surrounded by double quotes: "...".
   - Unescaped double quotes are removed from the input. Their only effect is
     that within double quotes, space and tab are treated like normal
     characters.
   - Backslashes not followed by double quotes are not special.
   - But 2*n+1 backslashes followed by a double quote become
     n backslashes followed by a double quote (n >= 0):
       \" -> "
       \\\" -> \"
       \\\\\" -> \\"
 */
#define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
#define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
char **
prepare_spawn (char **argv)
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4972
{
4973
4974
4975
  size_t argc;
  char **new_argv;
  size_t i;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4976

4977
4978
4979
  /* Count number of arguments.  */
  for (argc = 0; argv[argc] != NULL; argc++)
    ;
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4980

4981
4982
  /* Allocate new argument vector.  */
  new_argv = XMALLOC (char *, argc + 1);
Emmanuel Bertin's avatar
Emmanuel Bertin committed
4983

4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
  /* Put quoted arguments into the new argument vector.  */
  for (i = 0; i < argc; i++)
    {
      const char *string = argv[i];

      if (string[0] == '\0')
	new_argv[i] = xstrdup ("\"\"");
      else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
	{
	  int quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
	  size_t length;
	  unsigned int backslashes;
	  const char *s;
	  char *quoted_string;
	  char *p;

	  length = 0;
For faster browsing, not all history is shown. View entire blame