1
1
#
2
2
# preprocessor.py
3
3
#
4
- import subprocess
4
+ import subprocess , os
5
5
6
6
nocache = 1
7
7
verbose = 0
@@ -53,12 +53,14 @@ def run_preprocessor(env, fn=None):
53
53
# Find a compiler, considering the OS
54
54
#
55
55
def search_compiler (env ):
56
+ global nocache
56
57
57
58
from pathlib import Path , PurePath
58
59
59
60
ENV_BUILD_PATH = Path (env ['PROJECT_BUILD_DIR' ], env ['PIOENV' ])
60
61
GCC_PATH_CACHE = ENV_BUILD_PATH / ".gcc_path"
61
62
63
+ gccpath = None
62
64
try :
63
65
gccpath = env .GetProjectOption ('custom_gcc' )
64
66
blab ("Getting compiler from env" )
@@ -71,24 +73,42 @@ def search_compiler(env):
71
73
blab ("Getting g++ path from cache" )
72
74
return GCC_PATH_CACHE .read_text ()
73
75
74
- # Use any item in $PATH corresponding to a platformio toolchain bin folder
75
76
path_separator = ':'
76
77
gcc_exe = '*g++'
77
- if env ['PLATFORM' ] == 'win32' :
78
+
79
+ sysname = os .uname ().sysname
80
+ if sysname == 'Windows' :
78
81
path_separator = ';'
79
82
gcc_exe += ".exe"
80
83
84
+ envpath = map (Path , env ['ENV' ]['PATH' ].split (path_separator ))
85
+
81
86
# Search for the compiler in PATH
82
- for ppath in map (Path , env ['ENV' ]['PATH' ].split (path_separator )):
87
+ for ppath in envpath :
88
+ # Use any item in $PATH corresponding to a platformio toolchain bin folder
83
89
if ppath .match (env ['PROJECT_PACKAGES_DIR' ] + "/**/bin" ):
84
90
for gpath in ppath .glob (gcc_exe ):
85
- gccpath = str (gpath .resolve ())
86
- # Cache the g++ path to no search always
87
- if not nocache and ENV_BUILD_PATH .exists ():
88
- blab ("Caching g++ for current env" )
89
- GCC_PATH_CACHE .write_text (gccpath )
90
- return gccpath
91
-
92
- gccpath = env .get ('CXX' )
93
- blab ("Couldn't find a compiler! Fallback to %s" % gccpath )
91
+ # Skip '*-elf-g++' (crosstool-NG)
92
+ if not gpath .stem .endswith ('-elf-g++' ):
93
+ gccpath = str (gpath .resolve ())
94
+ break
95
+
96
+ if not gccpath :
97
+ for ppath in envpath :
98
+ for gpath in ppath .glob (gcc_exe ):
99
+ # Skip macOS Clang
100
+ if gpath != 'usr/bin/g++' or sysname != 'Darwin' :
101
+ gccpath = str (gpath .resolve ())
102
+ break
103
+
104
+ if not gccpath :
105
+ gccpath = env .get ('CXX' )
106
+ blab ("Couldn't find a compiler! Fallback to '%s'" % gccpath )
107
+ nocache = 1
108
+
109
+ # Cache the g++ path to speed up the next build
110
+ if not nocache and gccpath and ENV_BUILD_PATH .exists ():
111
+ blab ("Caching g++ for current env" )
112
+ GCC_PATH_CACHE .write_text (gccpath )
113
+
94
114
return gccpath
0 commit comments