Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cw from illumos 9899 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 145 additions & 40 deletions usr/src/tools/cw/cw.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2019 Joyent, Inc.
*/

/*
Expand Down Expand Up @@ -260,6 +262,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>

#include <sys/param.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -291,7 +294,8 @@ struct aelist {

typedef enum {
GNU,
SUN
SUN,
SMATCH
} compiler_style_t;

typedef struct {
Expand All @@ -308,7 +312,7 @@ typedef struct cw_ictx {
int i_oldargc;
char **i_oldargv;
pid_t i_pid;
char *i_discard;
char *i_tmpdir;
char *i_stderr;
} cw_ictx_t;

Expand Down Expand Up @@ -559,29 +563,59 @@ xlate(struct aelist *h, const char *xarg, const char **table)
* extension to be .o, otherwise we match the extension of the caller.
*/
static char *
discard_file_name(const char *path)
discard_file_name(cw_ictx_t *ctx, const char *path)
{
char *ret, *ext, *file;
char *ret, *ext;
char tmpl[] = "cwXXXXXX";

if (path == NULL) {
ext = ".o";
} else {
ext = strrchr(path, '.');
}

if ((ret = calloc(MAXPATHLEN, sizeof (char))) == NULL)
/*
* We need absolute control over where the temporary file goes, since
* we rely on it for cleanup so tempnam(3C) and tmpnam(3C) are
* inappropriate (they use TMPDIR, preferentially).
*
* mkstemp(3C) doesn't actually help us, since the temporary file
* isn't used by us, only its name.
*/
if (mktemp(tmpl) == NULL)
nomem();

if ((file = tempnam(NULL, ".cw")) == NULL)
(void) asprintf(&ret, "%s/%s%s", ctx->i_tmpdir, tmpl,
(ext != NULL) ? ext : "");

if (ret == NULL)
nomem();

(void) strlcpy(ret, file, MAXPATHLEN);
if (ext != NULL)
(void) strlcat(ret, ext, MAXPATHLEN);
free(file);
return (ret);
}

static boolean_t
is_source_file(const char *path)
{
char *ext = strrchr(path, '.');

if ((ext == NULL) || (*(ext + 1) == '\0'))
return (B_FALSE);

ext += 1;

if ((strcasecmp(ext, "c") == 0) ||
(strcmp(ext, "cc") == 0) ||
(strcmp(ext, "i") == 0) ||
(strcasecmp(ext, "s") == 0) ||
(strcmp(ext, "cpp") == 0)) {
return (B_TRUE);
}

return (B_FALSE);
}


static void
do_gcc(cw_ictx_t *ctx)
{
Expand Down Expand Up @@ -647,10 +681,7 @@ do_gcc(cw_ictx_t *ctx)
strcmp(arg + arglen - 3, ".il") == 0)
continue;

if (!in_output && arglen > 2 &&
arg[arglen - 2] == '.' &&
(arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
if (!in_output && is_source_file(arg))
c_files++;

/*
Expand All @@ -660,8 +691,7 @@ do_gcc(cw_ictx_t *ctx)
* compiler.
*/
if ((ctx->i_flags & CW_F_SHADOW) && in_output) {
ctx->i_discard = discard_file_name(arg);
newae(ctx->i_ae, ctx->i_discard);
newae(ctx->i_ae, discard_file_name(ctx, arg));
} else {
newae(ctx->i_ae, arg);
}
Expand Down Expand Up @@ -780,8 +810,7 @@ do_gcc(cw_ictx_t *ctx)
newae(ctx->i_ae, arg);
} else if (ctx->i_flags & CW_F_SHADOW) {
newae(ctx->i_ae, "-o");
ctx->i_discard = discard_file_name(arg);
newae(ctx->i_ae, ctx->i_discard);
newae(ctx->i_ae, discard_file_name(ctx, arg));
} else {
newae(ctx->i_ae, arg);
}
Expand Down Expand Up @@ -1308,12 +1337,37 @@ do_gcc(cw_ictx_t *ctx)
if (!nolibc)
newae(ctx->i_ae, "-lc");
if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
ctx->i_discard = discard_file_name(NULL);
newae(ctx->i_ae, "-o");
newae(ctx->i_ae, ctx->i_discard);
newae(ctx->i_ae, discard_file_name(ctx, NULL));
}
}

static void
do_smatch(cw_ictx_t *ctx)
{
if (ctx->i_flags & CW_F_PROG) {
newae(ctx->i_ae, "--version");
return;
}

/*
* Some sources shouldn't run smatch at all.
*/
for (int i = 0; i < ctx->i_oldargc; i++) {
char *arg = ctx->i_oldargv[i];

if (strcmp(arg, "-_smatch=off") == 0) {
ctx->i_flags &= ~ (CW_F_EXEC | CW_F_ECHO);
return;
}
}

/*
* smatch can handle gcc's options.
*/
do_gcc(ctx);
}

static void
do_cc(cw_ictx_t *ctx)
{
Expand All @@ -1331,26 +1385,21 @@ do_cc(cw_ictx_t *ctx)

while (--ctx->i_oldargc > 0) {
char *arg = *++ctx->i_oldargv;
size_t arglen = strlen(arg);

if (strncmp(arg, "-_CC=", 5) == 0) {
newae(ctx->i_ae, strchr(arg, '=') + 1);
continue;
}

if (*arg != '-') {
if (!in_output && arglen > 2 &&
arg[arglen - 2] == '.' &&
(arg[arglen - 1] == 'S' || arg[arglen - 1] == 's' ||
arg[arglen - 1] == 'c' || arg[arglen - 1] == 'i'))
if (!in_output && is_source_file(arg))
c_files++;

if (in_output == 0 || !(ctx->i_flags & CW_F_SHADOW)) {
newae(ctx->i_ae, arg);
} else {
in_output = 0;
ctx->i_discard = discard_file_name(arg);
newae(ctx->i_ae, ctx->i_discard);
newae(ctx->i_ae, discard_file_name(ctx, arg));
}
continue;
}
Expand All @@ -1374,8 +1423,7 @@ do_cc(cw_ictx_t *ctx)
newae(ctx->i_ae, arg);
} else if (ctx->i_flags & CW_F_SHADOW) {
newae(ctx->i_ae, "-o");
ctx->i_discard = discard_file_name(arg);
newae(ctx->i_ae, ctx->i_discard);
newae(ctx->i_ae, discard_file_name(ctx, arg));
} else {
newae(ctx->i_ae, arg);
}
Expand Down Expand Up @@ -1413,8 +1461,7 @@ do_cc(cw_ictx_t *ctx)

if (!seen_o && (ctx->i_flags & CW_F_SHADOW)) {
newae(ctx->i_ae, "-o");
ctx->i_discard = discard_file_name(NULL);
newae(ctx->i_ae, ctx->i_discard);
newae(ctx->i_ae, discard_file_name(ctx, NULL));
}
}

Expand All @@ -1439,6 +1486,9 @@ prepctx(cw_ictx_t *ctx)
case GNU:
do_gcc(ctx);
break;
case SMATCH:
do_smatch(ctx);
break;
}
}

Expand Down Expand Up @@ -1522,9 +1572,6 @@ reap(cw_ictx_t *ctx)
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));

(void) unlink(ctx->i_discard);
free(ctx->i_discard);

if (stat(ctx->i_stderr, &s) < 0) {
warn("stat failed on child cleanup");
return (-1);
Expand Down Expand Up @@ -1554,7 +1601,7 @@ reap(cw_ictx_t *ctx)
static int
exec_ctx(cw_ictx_t *ctx, int block)
{
if ((ctx->i_stderr = tempnam(NULL, ".cw")) == NULL) {
if ((ctx->i_stderr = tempnam(ctx->i_tmpdir, "cw")) == NULL) {
nomem();
return (-1);
}
Expand Down Expand Up @@ -1610,18 +1657,64 @@ parse_compiler(const char *spec, cw_compiler_t *compiler)
errx(1, "Compiler is missing a style: %s", spec);

if ((strcasecmp(token, "gnu") == 0) ||
(strcasecmp(token, "gcc") == 0))
(strcasecmp(token, "gcc") == 0)) {
compiler->c_style = GNU;
else if ((strcasecmp(token, "sun") == 0) ||
(strcasecmp(token, "cc") == 0))
} else if ((strcasecmp(token, "sun") == 0) ||
(strcasecmp(token, "cc") == 0)) {
compiler->c_style = SUN;
else
} else if ((strcasecmp(token, "smatch") == 0)) {
compiler->c_style = SMATCH;
} else {
errx(1, "unknown compiler style: %s", token);
}

if (tspec != NULL)
errx(1, "Excess tokens in compiler: %s", spec);
}

static void
cleanup(cw_ictx_t *ctx)
{
DIR *dirp;
struct dirent *dp;
char buf[MAXPATHLEN];

if ((dirp = opendir(ctx->i_tmpdir)) == NULL) {
if (errno != ENOENT) {
err(1, "couldn't open temp directory: %s",
ctx->i_tmpdir);
} else {
return;
}
}

errno = 0;
while ((dp = readdir(dirp)) != NULL) {
(void) snprintf(buf, MAXPATHLEN, "%s/%s", ctx->i_tmpdir,
dp->d_name);

if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0) {
continue;
}

if (unlink(buf) == -1)
err(1, "failed to unlink temp file: %s", dp->d_name);
errno = 0;
}

if (errno != 0) {
err(1, "failed to read temporary directory: %s",
ctx->i_tmpdir);
}

(void) closedir(dirp);
if (rmdir(ctx->i_tmpdir) != 0) {
err(1, "failed to unlink temporary directory: %s",
ctx->i_tmpdir);
}
}

int
main(int argc, char **argv)
{
Expand All @@ -1636,6 +1729,7 @@ main(int argc, char **argv)
boolean_t Cflg = B_FALSE;
boolean_t cflg = B_FALSE;
boolean_t nflg = B_FALSE;
char *tmpdir;

cw_ictx_t *main_ctx;

Expand Down Expand Up @@ -1723,6 +1817,16 @@ main(int argc, char **argv)
do_serial = 1;
}

tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = "/tmp";

if (asprintf(&main_ctx->i_tmpdir, "%s/cw.XXXXXX", tmpdir) == -1)
nomem();

if ((main_ctx->i_tmpdir = mkdtemp(main_ctx->i_tmpdir)) == NULL)
errx(1, "failed to create temporary directory");

ret |= exec_ctx(main_ctx, do_serial);

for (int i = 0; i < nshadows; i++) {
Expand All @@ -1732,7 +1836,7 @@ main(int argc, char **argv)
if ((shadow_ctx = newictx()) == NULL)
nomem();

memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));
(void) memcpy(shadow_ctx, main_ctx, sizeof (cw_ictx_t));

shadow_ctx->i_flags |= CW_F_SHADOW;
shadow_ctx->i_compiler = &shadows[i];
Expand All @@ -1754,5 +1858,6 @@ main(int argc, char **argv)
}
}

cleanup(main_ctx);
return (ret);
}