Skip to content

Commit

Permalink
Simplify cc_flags_filter, fix clean subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng committed Oct 4, 2023
1 parent b68dae3 commit b3ad432
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
7 changes: 6 additions & 1 deletion src/blade/build_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ def _test(self):
def _remove_paths(paths):
# The rm command can delete a large number of files at once, which is much faster than
# using python's own remove functions (only supports deleting a single path at a time).
subprocess.call(['rm', '-fr'] + paths)
if os.name == 'posix':
subprocess.call(['rm', '-fr'] + paths)
return
import shutil
for path in paths:
shutil.rmtree(path, ignore_errors=True)

def clean(self):
"""Clean specific generated target files or directories"""
Expand Down
5 changes: 4 additions & 1 deletion src/blade/load_build_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ def _check_under_skipped_dir(dirname):
if os.path.exists(filepath):
cache[dirname] = filepath
return filepath
result = _check_under_skipped_dir(os.path.dirname(dirname))
parent = os.path.dirname(dirname)
if parent == dirname:
return ''
result = _check_under_skipped_dir(parent)
cache[dirname] = result
return result

Expand Down
40 changes: 15 additions & 25 deletions src/blade/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,36 +412,26 @@ def filter_cc_flags(self, flag_list, language='c'):
flag_list = var_to_list(flag_list)
valid_flags, unrecognized_flags = [], []

# Put compilation output into test.o instead of /dev/null
# because the command line with '--coverage' below exit
# with status 1 which makes '--coverage' unsupported
# echo "int main() { return 0; }" | gcc -o /dev/null -c -x c --coverage - > /dev/null 2>&1
suffix = language
if suffix == 'c++':
suffix = 'cpp'
fd, src = tempfile.mkstemp('.' + suffix, 'filter_cc_flags_test')
os.write(fd, b"int main() { return 0; }\n")
os.close(fd)

# cl.exe only report the first error option, so we must test flags one by one.
for flag in flag_list:
# Example error messages:
# Command line error D8021 : invalid numeric argument '/Wzzz'
if flag.startswith('-'):
testflag = '/' + flag[1:]
else:
testflag = flag
cmd = ('"%s" /nologo /FoNUL /c /WX %s "%s"' % (self.cc, testflag, src))
returncode, stdout, stderr = run_command(cmd, shell=True)
message = stdout + stderr
if "'%s'" % testflag in message:
unrecognized_flags.append(flag)
else:
valid_flags.append(flag)
try:
# In case of error, the `.o` file will be deleted by the compiler
os.remove(src)
except OSError:
pass
cmd = ('"%s" /nologo /E /Zs /c /WX %s' % (self.cc, testflag))
try:
returncode, stdout, stderr = run_command(cmd, shell=False)
if returncode == 0:
continue
output = stdout + stderr
# Example error messages:
# Command line error D8021 : invalid numeric argument '/Wzzz'
if "'%s'" % testflag in output:
unrecognized_flags.append(flag)
else:
valid_flags.append(flag)
except OSError as e:
console.fatal("filter_cc_flags error, cmd='%s', error='%s'" % (cmd, e))

if unrecognized_flags:
console.warning('config: Unrecognized %s flags: %s' % (
Expand Down

0 comments on commit b3ad432

Please sign in to comment.