Skip to content

Commit a5b9e48

Browse files
committed
compiler_archs: gcc: Use main 'gcc' if is same arch
1 parent 584d416 commit a5b9e48

6 files changed

Lines changed: 46 additions & 15 deletions

File tree

shellblocks/compiler_archs/__init__.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from subprocess import check_output, CalledProcessError
12
from enum import Enum
23
from shellblocks.compiler_archs.mips import CompilerArchMIPSBE, CompilerArchMIPSLE
34
from shellblocks.compiler_archs.arm import CompilerArchARMLE
@@ -14,17 +15,42 @@ class CompilerArchOption(Enum):
1415
X86_64 = "x86_64"
1516

1617

18+
def get_current_platform():
19+
try:
20+
machine_bytes = check_output(["gcc", "-dumpmachine"])
21+
except FileNotFoundError:
22+
return None
23+
except CalledProcessError:
24+
return None
25+
26+
machine = machine_bytes.decode()
27+
28+
if "mips-" in machine:
29+
return CompilerArchOption.MIPSBE
30+
if "mipsel-" in machine:
31+
return CompilerArchOption.MIPSLE
32+
if "arm-" in machine:
33+
return CompilerArchOption.ARMLE
34+
if "x86_64-" in machine:
35+
return CompilerArchOption.X86_64
36+
37+
return None
38+
39+
1740
def compiler_arch_to_object(arch: CompilerArchOption) -> CompilerArch:
41+
current_platform = get_current_platform()
42+
use_main_gcc = (current_platform == arch)
43+
1844
if arch == CompilerArchOption.MIPSBE:
19-
return CompilerArchMIPSBE()
45+
return CompilerArchMIPSBE(use_main_gcc)
2046
elif arch == CompilerArchOption.MIPSLE:
21-
return CompilerArchMIPSLE()
47+
return CompilerArchMIPSLE(use_main_gcc)
2248
elif arch == CompilerArchOption.ARMLE:
23-
return CompilerArchARMLE()
49+
return CompilerArchARMLE(use_main_gcc)
2450
elif arch == CompilerArchOption.X86:
25-
return CompilerArchX86()
51+
return CompilerArchX86(CompilerArchOption.X86_64 == current_platform)
2652
elif arch == CompilerArchOption.X86_64:
27-
return CompilerArchX86_64()
53+
return CompilerArchX86_64(use_main_gcc)
2854

2955
raise NotImplementedError()
3056

shellblocks/compiler_archs/arm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
class CompilerArchARM(CompilerArchGCC):
8-
def __init__(self):
9-
super().__init__()
8+
def __init__(self, use_main_gcc: bool):
9+
super().__init__(use_main_gcc)
1010

1111
def get_headers(self) -> List[str]:
1212
return ["arch/arm/utils.h"]

shellblocks/compiler_archs/gcc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33

44
class CompilerArchGCC(CompilerArch):
5-
def __init__(self):
5+
def __init__(self, use_main_gcc: bool):
66
super().__init__()
77

8-
self.compiler_path = self.get_compiler_path()
8+
self.use_main_gcc = use_main_gcc
9+
10+
if self.use_main_gcc:
11+
self.compiler_path = "gcc"
12+
else:
13+
self.compiler_path = self.get_compiler_path()
914

1015
def get_compiler_path(self):
1116
raise NotImplementedError()

shellblocks/compiler_archs/mips.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
class CompilerArchMIPS(CompilerArchGCC):
8-
def __init__(self):
9-
super().__init__()
8+
def __init__(self, use_main_gcc: bool):
9+
super().__init__(use_main_gcc)
1010

1111
def get_gcc_flags(self):
1212
return super().get_gcc_flags() + [

shellblocks/compiler_archs/x86.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
class CompilerArchX86(CompilerArchX86_64):
5-
def __init__(self):
6-
super().__init__()
5+
def __init__(self, use_main_gcc: bool):
6+
super().__init__(use_main_gcc)
77

88
def get_gcc_flags(self):
99
return super().get_gcc_flags() + [

shellblocks/compiler_archs/x86_64.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
class CompilerArchX86_64(CompilerArchGCC):
8-
def __init__(self):
9-
super().__init__()
8+
def __init__(self, use_main_gcc: bool):
9+
super().__init__(use_main_gcc)
1010

1111
def get_headers(self) -> List[str]:
1212
return ["arch/x86/utils.h"]

0 commit comments

Comments
 (0)