make/src/native/fixpath.c
author erikj
Wed, 02 Jan 2019 12:59:26 +0100
changeset 53110 50677f43ac3d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215445: Enable building for Windows in WSL Reviewed-by: ihse Contributed-by: andrewluotechnologies@outlook.com, erik.joelsson@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     1
/*
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
     2
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     4
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    10
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    15
 * accompanied this code).
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    16
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    20
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    23
 * questions.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    24
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    25
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    26
#include <Windows.h>
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    27
#include <stdbool.h>
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    28
#include <io.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    29
#include <stdio.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    30
#include <string.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    31
#include <malloc.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    32
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    33
void report_error(char const * msg)
15058
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    34
{
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    35
  LPVOID lpMsgBuf;
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    36
  DWORD dw = GetLastError();
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    37
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    38
  FormatMessage(
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    39
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    40
      FORMAT_MESSAGE_FROM_SYSTEM |
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    41
      FORMAT_MESSAGE_IGNORE_INSERTS,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    42
      NULL,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    43
      dw,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    44
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    45
      (LPTSTR) &lpMsgBuf,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    46
      0,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    47
      NULL);
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    48
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    49
  fprintf(stderr,
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    50
          "%s  Failed with error %d: %s\n",
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    51
          msg, dw, lpMsgBuf);
15058
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    52
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    53
  LocalFree(lpMsgBuf);
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    54
}
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    55
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    56
/*
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    57
 * Test if pos points to /prefix/_/ where _ can
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    58
 * be any character.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    59
 */
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    60
int is_prefix_here(int pos, char const *in, int len, const char* prefix)
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    61
{
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    62
  // Length of c/ is 2
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    63
  int prefix_size = strlen(prefix);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    64
  if (pos+prefix_size+2 > len) return 0;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    65
  if (in[pos+prefix_size+1]=='/') {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    66
    return strncmp(in + pos, prefix, prefix_size) == 0;
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    67
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    68
  return 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    69
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    70
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    71
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    72
 * Replace /cygdrive/_/ with _:/
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    73
 * Works in place since drive letter is always
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    74
 * shorter than /cygdrive/
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    75
 */
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    76
char *replace_cygdrive_cygwin(char const *in)
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    77
{
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    78
  size_t len = strlen(in);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    79
  char *out = (char*) malloc(len+1);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    80
  int i,j;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    81
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    82
  if (len < 12) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    83
    memmove(out, in, len + 1);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    84
    return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    85
  }
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    86
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    87
  for (i = 0, j = 0; i<len;) {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
    88
    if (is_prefix_here(i, in, len, "/cygdrive/")) {
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    89
      out[j++] = in[i+10];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    90
      out[j++] = ':';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    91
      i+=11;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    92
    } else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    93
      out[j] = in[i];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    94
      i++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    95
      j++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    96
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    97
  }
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
    98
  out[j] = '\0';
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    99
  return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   100
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   101
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   102
void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   103
{
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   104
  while ((addlen+*u+1) > *bl) {
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   105
    *bl *= 2;
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   106
    *b = (char*) realloc(*b, *bl);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   107
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   108
  memcpy(*b+*u, add, addlen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   109
  *u += addlen;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   110
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   112
/*
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   113
 * Creates a new string from in where the first occurrence of sub is
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   114
 * replaced by rep.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   115
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   116
char *replace_substring(char *in, char *sub, char *rep)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   117
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   118
  int in_len = strlen(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   119
  int sub_len = strlen(sub);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   120
  int rep_len = strlen(rep);
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   121
  char *out = (char *) malloc(in_len - sub_len + rep_len + 1);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   122
  char *p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   123
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   124
  if (!(p = strstr(in, sub))) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   125
    // If sub isn't a substring of in, just return in.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   126
    return in;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   127
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   128
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   129
  // Copy characters from beginning of in to start of sub.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   130
  strncpy(out, in, p - in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   131
  out[p - in] = '\0';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   132
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   133
  sprintf(out + (p - in), "%s%s", rep, p + sub_len);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   134
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   135
  return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   136
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   137
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   138
char* msys_path_list; // @-separated list of paths prefix to look for
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   139
char* msys_path_list_end; // Points to last \0 in msys_path_list.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   140
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   141
void setup_msys_path_list(char const * argument)
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   142
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   143
  char* p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   144
  char* drive_letter_pos;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   145
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   146
  msys_path_list = strdup(&argument[2]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   147
  msys_path_list_end = &msys_path_list[strlen(msys_path_list)];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   148
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   149
  // Convert all at-sign (@) in path list to \0.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   150
  // @ was chosen as separator to minimize risk of other tools messing around with it
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   151
  p = msys_path_list;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   152
  do {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   153
    if (p[1] == ':') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   154
      // msys has mangled our path list, restore it from c:/... to /c/...
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   155
      drive_letter_pos = p+1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   156
      *drive_letter_pos = *p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   157
      *p = '/';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   158
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   159
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   160
    // Look for an @ in the list
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   161
    p = strchr(p, '@');
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   162
    if (p != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   163
      *p = '\0';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   164
      p++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   165
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   166
  } while (p != NULL);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   167
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   168
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   169
char *replace_cygdrive_msys(char const *in)
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   170
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   171
  char* str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   172
  char* prefix;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   173
  char* p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   174
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   175
  str = strdup(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   176
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   177
  // For each prefix in the path list, search for it and replace /c/... with c:/...
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   178
  for (prefix = msys_path_list; prefix < msys_path_list_end && prefix != NULL; prefix += strlen(prefix)+1) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   179
    p=str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   180
    while ((p = strstr(p, prefix))) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   181
      char* drive_letter = p+1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   182
      *p = *drive_letter;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   183
      *drive_letter = ':';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   184
      p++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   185
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   186
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   187
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   188
  return str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   189
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   190
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   191
/*
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   192
 * Replace /mnt/_/ with _:/
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   193
 * Works in place since drive letter is always
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   194
 * shorter than /mnt/
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   195
 */
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   196
char *replace_cygdrive_wsl(char const *in)
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   197
{
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   198
  size_t len = strlen(in);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   199
  char *out = (char*) malloc(len+1);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   200
  int i,j;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   201
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   202
  if (len < 7) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   203
    memmove(out, in, len + 1);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   204
    return out;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   205
  }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   206
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   207
  for (i = 0, j = 0; i<len;) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   208
    if (is_prefix_here(i, in, len, "/mnt/")) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   209
      out[j++] = in[i+5];
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   210
      out[j++] = ':';
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   211
      i+=6;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   212
    } else {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   213
      out[j] = in[i];
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   214
      i++;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   215
      j++;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   216
    }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   217
  }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   218
  out[j] = '\0';
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   219
  return out;
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   220
}
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   221
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   222
char*(*replace_cygdrive)(char const *in) = NULL;
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   223
bool debug_fixpath = false;
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   224
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   225
char *files_to_delete[1024];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   226
int num_files_to_delete = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   227
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   228
char *fix_at_file(char const *in)
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   229
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   230
  char *tmpdir;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   231
  char name[2048];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   232
  char *atname;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   233
  char *buffer;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   234
  size_t buflen=65536;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   235
  size_t used=0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   236
  size_t len;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   237
  int rc;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   238
  FILE *atout;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   239
  FILE *atin;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   240
  char block[2048];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   241
  size_t blocklen;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   242
  char *fixed;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   243
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   244
  atin = fopen(in+1, "r");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   245
  if (atin == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   246
    fprintf(stderr, "Could not read at file %s\n", in+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   247
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   248
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   249
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   250
  tmpdir = getenv("TEMP");
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   251
  if (tmpdir == NULL) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   252
#if _WIN64
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   253
    tmpdir = "c:/cygwin64/tmp";
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   254
#else
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   255
    tmpdir = "c:/cygwin/tmp";
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   256
#endif
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   257
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   258
  _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   259
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   260
  rc = _mktemp_s(name, strlen(name)+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   261
  if (rc) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   262
    fprintf(stderr, "Could not create temporary file name for at file!\n");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   263
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   264
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   265
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   266
  atout = fopen(name, "w");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   267
  if (atout == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   268
    fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   269
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   270
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   271
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   272
  buffer = (char*) malloc(buflen);
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   273
  while ((blocklen = fread(block, 1, sizeof(block), atin)) > 0) {
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   274
    append(&buffer, &buflen, &used, block, blocklen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   275
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   276
  buffer[used] = 0;
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   277
  if (debug_fixpath) {
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   278
    fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   279
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   280
  fixed = replace_cygdrive(buffer);
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   281
  if (debug_fixpath) {
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   282
    fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   283
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   284
  fwrite(fixed, strlen(fixed), 1, atout);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   285
  fclose(atin);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   286
  fclose(atout);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   287
  free(fixed);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   288
  free(buffer);
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   289
  files_to_delete[num_files_to_delete] = (char*) malloc(strlen(name)+1);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   290
  strcpy(files_to_delete[num_files_to_delete], name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   291
  num_files_to_delete++;
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   292
  atname = (char*) malloc(strlen(name)+2);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   293
  atname[0] = '@';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   294
  strcpy(atname+1, name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   295
  return atname;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   296
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   297
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   298
// given an argument, convert it to the windows command line safe quoted version
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   299
// using rules from:
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   300
// http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   301
// caller is responsible for freeing both input and output.
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   302
char * quote_arg(char const * in_arg) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   303
  char *quoted = NULL;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   304
  char *current = quoted;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   305
  int pass;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   306
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   307
  if (strlen(in_arg) == 0) {
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   308
     // empty string? explicitly quote it.
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   309
     return _strdup("\"\"");
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   310
  }
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   311
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   312
  if (strpbrk(in_arg, " \t\n\v\r\\\"") == NULL) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   313
     return _strdup(in_arg);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   314
  }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   315
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   316
  // process the arg twice. Once to calculate the size and then to copy it.
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   317
  for (pass=1; pass<=2; pass++) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   318
    char const *arg = in_arg;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   319
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   320
    // initial "
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   321
    if (pass == 2) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   322
      *current = '\"';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   323
    }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   324
    current++;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   325
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   326
    // process string to be quoted until NUL
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   327
    do {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   328
      int escapes = 0;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   329
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   330
      while (*arg == '\\') {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   331
        // count escapes.
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   332
        escapes++;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   333
        arg++;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   334
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   335
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   336
      if (*arg == '\0') {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   337
         // escape the escapes before final "
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   338
         escapes *= 2;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   339
      } else if (*arg == '"') {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   340
        // escape the escapes and the "
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   341
        escapes = escapes * 2 + 1;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   342
      } else {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   343
         // escapes aren't special, just echo them.
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   344
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   345
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   346
      // emit some escapes
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   347
      while (escapes > 0) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   348
        if (pass == 2) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   349
          *current = '\\';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   350
        }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   351
        current++;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   352
        escapes--;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   353
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   354
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   355
      // and the current char
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   356
      if (pass == 2) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   357
        *current = *arg;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   358
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   359
      current++;
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   360
    } while (*arg++ != '\0');
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   361
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   362
    // allocate the buffer
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   363
    if (pass == 1) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   364
      size_t alloc = (size_t) (current - quoted + (ptrdiff_t) 2);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   365
      current = quoted = (char*) calloc(alloc, sizeof(char));
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   366
    }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   367
  }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   368
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   369
  // final " and \0
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   370
  *(current - 1) = '"';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   371
  *current = '\0';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   372
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   373
  return quoted;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   374
}
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   375
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   376
int main(int argc, char const ** argv)
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   377
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   378
    STARTUPINFO si;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   379
    PROCESS_INFORMATION pi;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   380
    unsigned short rc;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   381
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   382
    char *line;
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   383
    char *current;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   384
    int i, cmd;
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   385
    DWORD exitCode = 0;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   386
    DWORD processFlags = 0;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   387
    BOOL processInheritHandles = TRUE;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   388
    BOOL waitForChild = TRUE;
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   389
    char* fixpathPath;
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   390
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   391
    debug_fixpath = (getenv("DEBUG_FIXPATH") != NULL);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   392
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   393
    if (argc<2 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm' && argv[1][1] != 'w')) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   394
        fprintf(stderr, "Usage: fixpath -c|m|w<path@path@...> [--detach] /cygdrive/c/WINDOWS/notepad.exe [/cygdrive/c/x/test.txt|@/cygdrive/c/x/atfile]\n");
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   395
        exit(0);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   396
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   397
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   398
    if (debug_fixpath) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   399
      char const * cmdline = GetCommandLine();
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   400
      fprintf(stderr, "fixpath input line >%s<\n", strstr(cmdline, argv[1]));
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   401
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   402
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   403
    if (argv[1][1] == 'c' && argv[1][2] == '\0') {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   404
      if (debug_fixpath) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   405
        fprintf(stderr, "fixpath using cygwin mode\n");
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   406
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   407
      replace_cygdrive = replace_cygdrive_cygwin;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   408
    } else if (argv[1][1] == 'm') {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   409
      if (debug_fixpath) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   410
        fprintf(stderr, "fixpath using msys mode, with path list: %s\n", &argv[1][2]);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   411
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   412
      setup_msys_path_list(argv[1]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   413
      replace_cygdrive = replace_cygdrive_msys;
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   414
    } else if (argv[1][1] == 'w') {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   415
      if (debug_fixpath) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   416
        fprintf(stderr, "fixpath using wsl mode, with path list: %s\n", &argv[1][2]);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   417
      }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   418
      replace_cygdrive = replace_cygdrive_wsl;
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   419
    } else {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   420
      fprintf(stderr, "fixpath Unknown mode: %s\n", argv[1]);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   421
      exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   422
    }
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   423
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   424
    if (argv[2][0] == '-') {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   425
      if (strcmp(argv[2], "--detach") == 0) {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   426
        if (debug_fixpath) {
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   427
          fprintf(stderr, "fixpath in detached mode\n");
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   428
        }
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   429
        processFlags |= DETACHED_PROCESS;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   430
        processInheritHandles = FALSE;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   431
        waitForChild = FALSE;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   432
      } else {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   433
        fprintf(stderr, "fixpath Unknown argument: %s\n", argv[2]);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   434
        exit(-1);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   435
      }
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   436
      i = 3;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   437
    } else {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   438
      i = 2;
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   439
    }
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   440
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   441
    // handle assignments
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   442
    while (i < argc) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   443
      char const * assignment = strchr(argv[i], '=');
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   444
      if (assignment != NULL && assignment != argv[i]) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   445
        size_t var_len = (size_t) (assignment - argv[i] + (ptrdiff_t) 1);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   446
        char *var = (char *) calloc(var_len, sizeof(char));
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   447
        char *val = replace_cygdrive(assignment + 1);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   448
        memmove(var, argv[i], var_len);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   449
        var[var_len - 1] = '\0';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   450
        strupr(var);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   451
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   452
        if (debug_fixpath) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   453
          fprintf(stderr, "fixpath setting var >%s< to >%s<\n", var, val);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   454
        }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   455
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   456
        rc = SetEnvironmentVariable(var, val);
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   457
        if (!rc) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   458
          // Could not set var for some reason.  Try to report why.
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   459
          const int msg_len = 80 + var_len + strlen(val);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   460
          char * msg = (char *) alloca(msg_len);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   461
          _snprintf_s(msg, msg_len, _TRUNCATE, "Could not set environment variable [%s=%s]", var, val);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   462
          report_error(msg);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   463
          exit(1);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   464
        }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   465
        free(var);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   466
        free(val);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   467
      } else {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   468
        // no more assignments;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   469
        break;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   470
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   471
      i++;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   472
    }
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   473
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   474
    // remember index of the command
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   475
    cmd = i;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   476
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   477
    // handle command and it's args.
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   478
    while (i < argc) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   479
      char const *replaced = replace_cygdrive(argv[i]);
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   480
      if (replaced[0] == '@') {
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   481
        if (waitForChild == FALSE) {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   482
          fprintf(stderr, "fixpath Cannot use @-files in detached mode: %s\n", replaced);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   483
          exit(1);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   484
        }
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   485
        // Found at-file! Fix it!
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   486
        replaced = fix_at_file(replaced);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   487
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   488
      argv[i] = quote_arg(replaced);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   489
      i++;
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   490
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   491
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   492
    // determine the length of the line
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   493
    line = NULL;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   494
    // args
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   495
    for (i = cmd; i < argc; i++) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   496
      line += (ptrdiff_t) strlen(argv[i]);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   497
    }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   498
    // spaces and null
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   499
    line += (ptrdiff_t) (argc - cmd + 1);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   500
    // allocate
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   501
    line = (char*) calloc(line - (char*) NULL, sizeof(char));
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   502
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   503
    // copy in args.
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   504
    current = line;
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   505
    for (i = cmd; i < argc; i++) {
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   506
      ptrdiff_t len = strlen(argv[i]);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   507
      if (i != cmd) {
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   508
        *current++ = ' ';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   509
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   510
      memmove(current, argv[i], len);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   511
      current += len;
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   512
    }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   513
    *current = '\0';
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   514
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   515
    if (debug_fixpath) {
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   516
      fprintf(stderr, "fixpath converted line >%s<\n", line);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   517
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   518
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   519
    if (cmd == argc) {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   520
      if (debug_fixpath) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   521
        fprintf(stderr, "fixpath no command provided!\n");
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   522
      }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   523
      exit(0);
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   524
    }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   525
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   526
    ZeroMemory(&si, sizeof(si));
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   527
    si.cb=sizeof(si);
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   528
    ZeroMemory(&pi, sizeof(pi));
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   529
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   530
    fflush(stderr);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   531
    fflush(stdout);
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   532
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   533
    fixpathPath = calloc(32767, sizeof(char));
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   534
    rc = GetEnvironmentVariable("FIXPATH_PATH", fixpathPath, 32767);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   535
    if (rc) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   536
      if (debug_fixpath) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   537
        fprintf(stderr, "Setting Path to FIXPATH_PATH: %s\n", fixpathPath);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   538
      }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   539
      rc = SetEnvironmentVariable("Path", fixpathPath);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   540
      if (!rc) {
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   541
        // Could not set Path for some reason.  Try to report why.
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   542
        const int msg_len = 80 + strlen(fixpathPath);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   543
        char * msg = (char *)alloca(msg_len);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   544
        _snprintf_s(msg, msg_len, _TRUNCATE, "Could not set environment variable [Path=%s]", fixpathPath);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   545
        report_error(msg);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   546
        exit(1);
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   547
      }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   548
    }
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   549
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   550
    rc = CreateProcess(NULL,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   551
                       line,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   552
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   553
                       0,
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   554
                       processInheritHandles,
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   555
                       processFlags,
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   556
                       NULL,
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   557
                       NULL,
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   558
                       &si,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   559
                       &pi);
23837
6f3e6fee7b44 8040332: fixpath must explicitly quote empty string parameters.
mduigou
parents: 23435
diff changeset
   560
    if (!rc) {
15058
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
   561
      // Could not start process for some reason.  Try to report why:
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   562
      report_error("Could not start process!");
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   563
      exit(126);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   564
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   565
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   566
    if (waitForChild == TRUE) {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   567
      WaitForSingleObject(pi.hProcess, INFINITE);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   568
      GetExitCodeProcess(pi.hProcess, &exitCode);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   569
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   570
      if (debug_fixpath) {
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   571
        for (i=0; i<num_files_to_delete; ++i) {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   572
          fprintf(stderr, "fixpath Not deleting temporary file %s\n",
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   573
                  files_to_delete[i]);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   574
        }
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   575
      } else {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   576
        for (i=0; i<num_files_to_delete; ++i) {
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   577
          remove(files_to_delete[i]);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   578
        }
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   579
      }
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   580
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   581
      if (exitCode != 0) {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   582
        if (debug_fixpath) {
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   583
          fprintf(stderr, "fixpath exit code %d\n",
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   584
                  exitCode);
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   585
        }
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   586
      }
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   587
    } else {
53110
50677f43ac3d 8215445: Enable building for Windows in WSL
erikj
parents: 47216
diff changeset
   588
      if (debug_fixpath) {
34101
a1db3b482137 8143296: javac-server/sjavac not compatible with LogFailures on Windows
erikj
parents: 23837
diff changeset
   589
        fprintf(stderr, "fixpath Not waiting for child process");
23435
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   590
      }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   591
    }
098340eccdcb 8039411: Enhance fixpath to allow environment variable assignments.
mduigou
parents: 15058
diff changeset
   592
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   593
    exit(exitCode);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   594
}