Skip to content

Commit

Permalink
Fix unnecessary file creation bug (#1)
Browse files Browse the repository at this point in the history
Cxbuild no longer makes __test_dummy_source__.o
  • Loading branch information
HansolChoe committed Nov 9, 2021
1 parent d24807b commit a6b8373
Showing 1 changed file with 43 additions and 48 deletions.
91 changes: 43 additions & 48 deletions compiler/_gnu_compiler_tool.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
import os
import re
import cslib
import subprocess
import libcsbuild
from .icompiler_tool import *


class _GnuCompilerTool(ICompilerTool):
def __init__(self, compiler_path: str, command: list):
self.toolchain_name = "GnuCompilerTool"
self.command = command
self.compiler_path = compiler_path

def get_include_option_name(self):
return "-I"

def split_command(self):
sources = [command for command in self.command if cslib.is_source_file(command)]
options = [command for command in self.command if command not in sources]
return options, sources

def get_system_include_list(self):
"""find system includes from 'gcc -c -v __test_dummy_source__.c'"""
dummy_src_path = libcsbuild.get_new_file_path("__test_dummy_source__.c")
redirect_path = libcsbuild.get_new_file_path("__redirect__.stdout")
with open(dummy_src_path, "w") as f:
f.flush()
pass

with open(redirect_path, "w") as f:
f.flush()
subprocess.call([self.compiler_path, '-v', '-c', dummy_src_path], stdout=f, stderr=f)

collect = False
collected = []
with open(redirect_path, "r") as f:

for line in f.readlines():
if line.strip().startswith("#include <...>"):
collect = True
elif line.strip().startswith("End of"):
collect = False

if collect:
collected.append(line.strip())
# remove 0 index cuz, collected[0] == "#include <...>"
return collected[1:]
import os
import re
import cslib
import subprocess
import libcsbuild

from .icompiler_tool import *


class _GnuCompilerTool(ICompilerTool):
def __init__(self, compiler_path: str, command: list):
self.toolchain_name = "GnuCompilerTool"
self.command = command
self.compiler_path = compiler_path

def get_include_option_name(self):
return "-I"

def split_command(self):
sources = [command for command in self.command if cslib.is_source_file(command)]
options = [command for command in self.command if command not in sources]
return options, sources

def get_system_include_list(self):
compiler_kind = os.path.basename(self.compiler_path)
if compiler_kind == "gcc":
command = "gcc -E -Wp,-v -xc /dev/null"
elif compiler_kind == "g++" or compiler_kind == "c++":
command = "g++ -E -Wp,-v -xc++ /dev/null"
else:
# TODO: Make no such compiler exception, and Exit gracefully
raise Exception("No Such Compiler Exception")
output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode("utf-8")
collect = False
collected = []
for line in output.splitlines():
if line.strip().startswith("#include <...>"):
collect = True
elif line.strip().startswith("End of"):
collect = False
if collect:
collected.append(line.strip())
return collected[1:]

0 comments on commit a6b8373

Please sign in to comment.