@@ -487,31 +487,39 @@ static int call_eaccess(const char *pathname, int mode) {
487487 return result ;
488488}
489489
490- int eaccess (const char * pathname , int mode ) {
491- int ret = call_eaccess (pathname , mode );
492- if (ret == 0 ) {
493- return 0 ;
494- }
495-
496- int nvcc_available = 0 ;
490+ int is_nvcc_available (const char * pathname ) {
497491 char * value_compile = getenv ("INTERCEPT_COMPILE_PATH" );
498492 char * value_env = getenv ("IS_INTERCEPT_COMPILE_PATH_FROM_ENV_PATH" );
499493
500494 // Consider tool chain is avaialbe only when it is available from env path.
501495 if (value_env && * value_env == '1' && value_compile ) {
502- nvcc_available = 1 ;
496+ return 1 ;
503497 }
498+ return 0 ;
499+ }
504500
501+ int is_nvcc_cmd (const char * pathname ) {
505502 int len = strlen (pathname );
506- if (! nvcc_available && len == 4 && pathname [3 ] == 'c' && pathname [2 ] == 'c' &&
503+ if (len == 4 && pathname [3 ] == 'c' && pathname [2 ] == 'c' &&
507504 pathname [1 ] == 'v' && pathname [0 ] == 'n' ) {
508505 // To handle case like "nvcc foo.cu ..."
509- return 0 ;
506+ return 1 ;
510507 }
511- if (! nvcc_available && len > 4 && pathname [len - 1 ] == 'c' &&
512- pathname [len - 2 ] == 'c ' && pathname [len - 3 ] == 'v ' &&
513- pathname [len - 4 ] == 'n' && pathname [ len - 5 ] == '/' ) {
508+ if (len > 4 && pathname [ len - 1 ] == 'c' && pathname [len - 2 ] == 'c' &&
509+ pathname [len - 3 ] == 'v ' && pathname [len - 4 ] == 'n ' &&
510+ pathname [len - 5 ] == '/' ) {
514511 // To handle case like "/path/to/nvcc foo.cu ..."
512+ return 1 ;
513+ }
514+ return 0 ;
515+ }
516+
517+ int eaccess (const char * pathname , int mode ) {
518+ int ret = call_eaccess (pathname , mode );
519+ if (ret == 0 ) {
520+ return 0 ;
521+ }
522+ if (!is_nvcc_available (pathname ) && is_nvcc_cmd (pathname )) {
515523 return 0 ;
516524 }
517525 return ret ;
@@ -541,35 +549,8 @@ int stat(const char *pathname, struct stat *statbuf) {
541549 if (ret == 0 ) {
542550 return 0 ;
543551 }
544- int len = strlen (pathname );
545- if (len == 4 && pathname [3 ] == 'c' && pathname [2 ] == 'c' &&
546- pathname [1 ] == 'v' && pathname [0 ] == 'n' ) {
547- // To handle case like "nvcc foo.cu ..."
548-
549- const char * nvcc_path = getenv ("INTERCEPT_COMPILE_PATH" );
550- if (nvcc_path ) {
551- call_stat (nvcc_path , statbuf );
552- return 0 ;
553- }
554-
555- pathname = get_intercept_stub_path ();
556- call_stat (pathname , statbuf );
557- return 0 ;
558- }
559552
560- if (len > 4 && pathname [len - 1 ] == 'c' && pathname [len - 2 ] == 'c' &&
561- pathname [len - 3 ] == 'v' && pathname [len - 4 ] == 'n' &&
562- pathname [len - 5 ] == '/' ) {
563- // To handle case like "/path/to/nvcc foo.cu ..."
564-
565- const char * nvcc_path = getenv ("INTERCEPT_COMPILE_PATH" );
566- if (nvcc_path ) {
567- call_stat (nvcc_path , statbuf );
568- return 0 ;
569- }
570-
571- pathname = get_intercept_stub_path ();
572- call_stat (pathname , statbuf );
553+ if (!is_nvcc_available (pathname ) && is_nvcc_cmd (pathname )) {
573554 return 0 ;
574555 }
575556 return ret ;
@@ -1727,36 +1708,10 @@ char *replace_binary_name(const char *src, const char *pos, int compiler_idx,
17271708int is_tool_available (char const * argv [], size_t const argc ) {
17281709 const char * pathname = argv [0 ];
17291710 int len = strlen (pathname );
1730- int is_nvcc = 0 ;
1731- int is_nvcc_available = 0 ;
1732-
1733- int nvcc_available = 0 ;
1734- char * value_compile = getenv ("INTERCEPT_COMPILE_PATH" );
1735- char * value_env = getenv ("IS_INTERCEPT_COMPILE_PATH_FROM_ENV_PATH" );
1736-
1737- // Consider tool chain is avaialbe only when it is available from env path.
1738- if (value_env && * value_env == '1' && value_compile ) {
1739- nvcc_available = 1 ;
1740- }
17411711
1742- if (len == 4 && pathname [3 ] == 'c' && pathname [2 ] == 'c' &&
1743- pathname [1 ] == 'v' && pathname [0 ] == 'n' ) {
1744- // To handle case like "nvcc"
1745- is_nvcc = 1 ;
1746- if (nvcc_available == '0' ) {
1747- return 0 ;
1748- }
1749- }
1750- if (len > 4 && pathname [len - 1 ] == 'c' && pathname [len - 2 ] == 'c' &&
1751- pathname [len - 3 ] == 'v' && pathname [len - 4 ] == 'n' &&
1752- pathname [len - 5 ] == '/' ) {
1753- // To handle case like "/path/to/nvcc"
1754- is_nvcc = 1 ;
1755- }
1756- if (is_nvcc ) {
1757- if (is_nvcc_available ) {
1712+ if (is_nvcc_cmd (pathname )) {
1713+ if (is_nvcc_available (pathname ))
17581714 return 1 ;
1759- }
17601715 return 0 ;
17611716 }
17621717
@@ -1771,7 +1726,7 @@ int is_tool_available(char const *argv[], size_t const argc) {
17711726 is_ld = 1 ;
17721727 }
17731728 if (is_ld ) {
1774- if (!is_nvcc_available ) {
1729+ if (!is_nvcc_available ( pathname ) ) {
17751730 for (size_t idx = 0 ; idx < argc ; idx ++ ) {
17761731 // if ld linker command uses cuda libarary like libcuda.so or
17771732 // libcudart.so, then the ld command should be intercepted.
@@ -1782,12 +1737,12 @@ int is_tool_available(char const *argv[], size_t const argc) {
17821737 }
17831738 }
17841739
1785- if (!is_nvcc_available && argc == 3 ) {
1740+ if (!is_nvcc_available ( pathname ) && argc == 3 ) {
17861741 // To handle case like "/bin/[sh/bash] -c '[echo or something]
17871742 // [/path/to/]nvcc -c foo.cu -o foo.o'" on the environment where tool chain
17881743 // is not available.
17891744 int is_bash = 0 ;
1790- is_nvcc = 0 ;
1745+ int is_nvcc_cmd = 0 ;
17911746 const char * pathname = argv [0 ];
17921747 len = strlen (pathname );
17931748
@@ -1811,7 +1766,7 @@ int is_tool_available(char const *argv[], size_t const argc) {
18111766 }
18121767
18131768 if (pos ) {
1814- is_nvcc =
1769+ is_nvcc_cmd =
18151770 pos > argv [2 ]
18161771 ? strlen (pos ) >= 4 && isspace (* (pos + 4 )) &&
18171772 (* (pos - 1 ) == '/' || * (pos - 1 ) == ';' ||
@@ -1822,7 +1777,7 @@ int is_tool_available(char const *argv[], size_t const argc) {
18221777 // sure it is a compiler command.
18231778 }
18241779
1825- if (is_bash && strcmp (argv [1 ], "-c" ) == 0 && is_nvcc ) {
1780+ if (is_bash && strcmp (argv [1 ], "-c" ) == 0 && is_nvcc_cmd ) {
18261781 return 0 ;
18271782 }
18281783 }
0 commit comments