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

[SYCLomatc][Intercept-build] Refine intercept-build to work as normal compilation if the toolchain is available #2386

Open
wants to merge 5 commits into
base: SYCLomatic
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
105 changes: 80 additions & 25 deletions clang/tools/scan-build-py/lib/libear/ear.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* -*- coding: utf-8 -*-
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down Expand Up @@ -53,6 +54,7 @@ extern char **environ;

#ifdef SYCLomatic_CUSTOMIZATION
#include <ctype.h>
#include <sys/stat.h>
#define PATH_MAX 4096
#endif // SYCLomatic_CUSTOMIZATION

Expand Down Expand Up @@ -492,35 +494,14 @@ int eaccess(const char *pathname, int mode) {
pathname[1] == 'v' && pathname[0] == 'n') {
// To handle case like "nvcc foo.cu ..."
return 0;
} else if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
pathname[len - 5] == '/') {
// To handle case like "/path/to/nvcc foo.cu ..."
return 0;
}
return call_eaccess(pathname, mode);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we keep it, just do a little refinement.

static int call_stat(const char *pathname, struct stat *statbuf) {
typedef int (*func)(const char *, struct statbuf *);
DLSYM(func, fp, "stat");
int const result = (*fp)(pathname, statbuf);
return result;
}

int stat(const char *pathname, struct stat *statbuf) {
int len = strlen(pathname);
if (len == 4 && pathname[3] == 'c' && pathname[2] == 'c' &&
pathname[1] == 'v' && pathname[0] == 'n') {
// To handle case like "nvcc foo.cu ..."
return 0;
} else if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
pathname[len - 5] == '/') {
if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
pathname[len - 5] == '/') {
// To handle case like "/path/to/nvcc foo.cu ..."
return 0;
}
return call_stat(pathname, statbuf);
return call_eaccess(pathname, mode);
}

/*
Expand Down Expand Up @@ -1706,6 +1687,60 @@ char *replace_binary_name(const char *src, const char *pos, int compiler_idx,
/* this method is to write log about the process creation. */

#ifdef SYCLomatic_CUSTOMIZATION
int file_exists(const char *pathname) {

int len = strlen(pathname);
int is_nvcc = 0;
if (len == 4 && pathname[3] == 'c' && pathname[2] == 'c' &&
pathname[1] == 'v' && pathname[0] == 'n') {
// To handle case like "nvcc"
is_nvcc = 1;
}

if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
pathname[len - 5] == '/') {
// To handle case like "/path/to/nvcc"
is_nvcc = 0;
}

if (is_nvcc) {
int idx = len - 4;
for (; idx > 0 && !isspace(pathname[idx]); idx--)
;
struct stat buffer;
if (stat(pathname + idx, &buffer) == 0){
return 1;
}
return 0;
}

int is_ld = 0;
if (len == 2 && pathname[1] == 'd' && pathname[0] == 'l') {
// To handle case like "ld"
is_ld = 1;
}

if (len > 2 && pathname[len - 1] == 'd' && pathname[len - 2] == 'l' &&
pathname[len - 3] == '/') {
// To handle case like "/path/to/ld"
is_ld = 0;
}

if (is_ld) {
int idx = len - 2;
for (; idx > 0 && !isspace(pathname[idx]); idx--)
;
struct stat buffer;
if (stat(pathname + idx, &buffer) == 0){
return 1;
}
return 0;
}

return 1;
}

// This method parses the command execution issued by the build tool make to
// write log for the compile options and fake the expecting outcome for the
// command. It returns whether intercept-stub is used to take over the command
Expand Down Expand Up @@ -1759,6 +1794,26 @@ static void bear_report_call(char const *fun, char const *const argv[]) {

emit_cmake_warning(argv, argc);

int is_tool_available = 0;
if (file_exists(argv[0])) {
is_tool_available = 1;
}

if(is_tool_available) {
for (size_t it = 0; it < argc; ++it) {
fprintf(fd, "%s%c", argv[it], US);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug purpose printf?

Copy link
Contributor Author

@tomflinda tomflinda Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is used to collect options for current command.

}
fprintf(fd, "%c", GS);
if (fclose(fd)) {
perror("bear: fclose");
pthread_mutex_unlock(&mutex);
exit(EXIT_FAILURE);
}
free((void *)cwd);
pthread_mutex_unlock(&mutex);
return 0;
}

// compiler list should be intercepted.
const char *const compiler_array[] = {"nvcc", "clang++"};
// Current compiler index intercepted.
Expand Down
Loading