2016-01-10 03:51:34 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* tools/cnvwindeps.c
|
|
|
|
*
|
2021-02-19 18:57:46 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-01-10 03:51:34 +08:00
|
|
|
*
|
2021-02-19 18:57:46 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-10 03:51:34 +08:00
|
|
|
*
|
2021-02-19 18:57:46 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-01-10 03:51:34 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-01-11 03:38:41 +08:00
|
|
|
#ifdef HOST_CYGWIN
|
|
|
|
|
2016-01-10 03:51:34 +08:00
|
|
|
#include <sys/cygwin.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define MAX_LINE 1024
|
|
|
|
#define MAX_PATH 1024
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Global Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static unsigned long g_lineno;
|
|
|
|
static char g_line[MAX_LINE];
|
|
|
|
static char g_dequoted[MAX_PATH];
|
|
|
|
static char g_posix[MAX_PATH];
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static char *skip_spaces(char *ptr)
|
|
|
|
{
|
|
|
|
while (*ptr && isspace((int)*ptr)) ptr++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *find_spaces(char *ptr)
|
|
|
|
{
|
|
|
|
bool quoted = false;
|
|
|
|
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (ptr[0] == '\\' && isspace((int)ptr[1]))
|
|
|
|
{
|
|
|
|
quoted = true;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else if (!quoted && isspace((int)*ptr))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quoted = false;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:01:42 +08:00
|
|
|
static bool scour_path(const char *path)
|
|
|
|
{
|
|
|
|
/* KLUDGE: GNU make cannot handle dependencies with spaces in them.
|
|
|
|
* There may be addition characters that cause problems too.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return strchr(path, ' ') != NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-10 03:51:34 +08:00
|
|
|
static bool dequote_path(const char *winpath)
|
|
|
|
{
|
|
|
|
char *dest = g_dequoted;
|
|
|
|
const char *src = winpath;
|
|
|
|
int len = 0;
|
|
|
|
bool quoted = false;
|
|
|
|
|
|
|
|
while (*src && len < MAX_PATH)
|
|
|
|
{
|
2021-02-19 18:29:43 +08:00
|
|
|
if (src[0] != '\\' || (src[1] != ' ' &&
|
|
|
|
src[1] != '(' && src[1] != ')'))
|
2016-01-10 03:51:34 +08:00
|
|
|
{
|
|
|
|
*dest++ = *src;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quoted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src || len >= MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%lu: Line truncated\n", g_lineno);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
return quoted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool convert_path(const char *winpath)
|
|
|
|
{
|
|
|
|
ssize_t size;
|
|
|
|
ssize_t ret;
|
|
|
|
bool quoted;
|
|
|
|
|
|
|
|
quoted = dequote_path(winpath);
|
|
|
|
|
2021-02-19 18:29:43 +08:00
|
|
|
size = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
|
|
|
|
g_dequoted, NULL, 0);
|
2016-01-10 03:51:34 +08:00
|
|
|
if (size > MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%lu: POSIX path too long: %lu\n",
|
|
|
|
g_lineno, (unsigned long)size);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:29:43 +08:00
|
|
|
ret = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE,
|
|
|
|
g_dequoted, g_posix, MAX_PATH);
|
2016-01-10 03:51:34 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%lu: cygwin_conv_path '%s' failed: %s\n",
|
|
|
|
g_lineno, g_dequoted, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return quoted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_usage(const char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "USAGE: %s <path-to-deps-file>\n", progname);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
char *next;
|
|
|
|
FILE *stream;
|
|
|
|
bool begin;
|
|
|
|
bool quoted;
|
2016-01-11 22:01:42 +08:00
|
|
|
bool scouring;
|
2016-01-10 03:51:34 +08:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unexpected number of arguments\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = fopen(argv[1], "r");
|
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "open %s failed: %s\n", argv[1], strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:01:42 +08:00
|
|
|
begin = true;
|
|
|
|
scouring = false;
|
2016-01-10 03:51:34 +08:00
|
|
|
g_lineno = 0;
|
|
|
|
|
|
|
|
while (fgets(g_line, MAX_LINE, stream) != NULL)
|
|
|
|
{
|
|
|
|
g_lineno++;
|
|
|
|
next = g_line;
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
if (begin)
|
|
|
|
{
|
|
|
|
path = skip_spaces(next);
|
2016-01-27 08:27:37 +08:00
|
|
|
if (*path == '#')
|
|
|
|
{
|
2016-01-27 22:22:44 +08:00
|
|
|
/* The reset of the line is comment */
|
2016-01-27 08:27:37 +08:00
|
|
|
|
2016-01-27 22:22:44 +08:00
|
|
|
puts(path);
|
2016-01-27 08:27:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-10 03:51:34 +08:00
|
|
|
next = strchr(path, ':');
|
|
|
|
if (!next)
|
|
|
|
{
|
2016-01-27 08:27:37 +08:00
|
|
|
fprintf(stderr, "%lu: Expected colon\n", g_lineno);
|
2016-01-10 03:51:34 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*next != '\0')
|
|
|
|
{
|
|
|
|
*next++ = '\0';
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:01:42 +08:00
|
|
|
scouring = scour_path(path);
|
|
|
|
if (!scouring)
|
2016-01-10 03:51:34 +08:00
|
|
|
{
|
2016-01-11 22:01:42 +08:00
|
|
|
quoted = convert_path(path);
|
|
|
|
if (quoted)
|
|
|
|
{
|
|
|
|
printf("\"%s\":", g_posix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("%s:", g_posix);
|
|
|
|
}
|
2021-02-19 18:29:43 +08:00
|
|
|
}
|
2016-01-10 03:51:34 +08:00
|
|
|
|
|
|
|
begin = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = skip_spaces(next);
|
|
|
|
next = find_spaces(path);
|
|
|
|
|
|
|
|
if (path[0] == '\\')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (strcmp(path, "") == 0)
|
|
|
|
{
|
|
|
|
printf("\n\n");
|
2016-01-11 22:01:42 +08:00
|
|
|
begin = true;
|
|
|
|
scouring = false;
|
2016-01-10 03:51:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*next != '\0')
|
|
|
|
{
|
|
|
|
*next++ = '\0';
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:01:42 +08:00
|
|
|
if (!scouring && !scour_path(path))
|
2016-01-10 03:51:34 +08:00
|
|
|
{
|
2016-01-11 22:01:42 +08:00
|
|
|
quoted = convert_path(path);
|
|
|
|
if (quoted)
|
|
|
|
{
|
|
|
|
printf(" \\\n\t\"%s\"", g_posix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(" \\\n\t%s", g_posix);
|
|
|
|
}
|
2016-01-12 07:14:34 +08:00
|
|
|
}
|
2016-01-10 03:51:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(stream);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-11 03:38:41 +08:00
|
|
|
|
|
|
|
#else /* HOST_CYGWIN */
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
2016-01-11 22:10:08 +08:00
|
|
|
fprintf(stderr, "ERROR: This tool is only available under Cygwin\n");
|
2016-01-11 03:38:41 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HOST_CYGWIN */
|