Skip to content

Commit

Permalink
Converted sizeof check to work also when cross compiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed Sep 5, 2015
1 parent 2c56884 commit ad5795e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
44 changes: 24 additions & 20 deletions compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,20 @@ def has_header(self, hname):
'''
return self.compiles(templ % hname)

def compiles(self, code):
mlog.debug('Running compile test:\n\n', code)
def compiles(self, code, extra_args = []):
suflen = len(self.default_suffix)
(fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix)
os.close(fd)
ofile = open(srcname, 'w')
ofile.write(code)
ofile.close()
commands = self.get_exelist()
commands += extra_args
commands += self.get_compile_only_args()
commands.append(srcname)
mlog.debug('Running compile test.')
mlog.debug('Command line: ', ' '.join(commands))
mlog.debug('Code:\n', code)
p = subprocess.Popen(commands, cwd=os.path.split(srcname)[0], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stde, stdo) = p.communicate()
stde = stde.decode()
Expand Down Expand Up @@ -325,7 +328,24 @@ def run(self, code):
os.remove(exename)
return RunResult(True, pe.returncode, so, se)

def cross_sizeof(self, element, prefix, env):
templ = '''%s
int temparray[%d-sizeof(%s)];
'''
extra_args = []
try:
extra_args = env.cross_info.config['properties'][self.language + '_args']
except KeyError:
pass
for i in range(1, 1024):
code = templ % (prefix, i, element)
if self.compiles(code, extra_args):
return i
raise EnvironmentException('Cross checking sizeof overflowed.')

def sizeof(self, element, prefix, env):
if self.is_cross:
return self.cross_sizeof(element, prefix, env)
templ = '''#include<stdio.h>
%s
Expand All @@ -334,23 +354,7 @@ def sizeof(self, element, prefix, env):
return 0;
};
'''
varname = 'sizeof ' + element
varname = varname.replace(' ', '_')
if self.is_cross:
val = env.cross_info.config['properties'][varname]
if val is not None:
if isinstance(val, int):
return val
raise EnvironmentException('Cross variable {0} is not an integer.'.format(varname))
cross_failed = False
try:
res = self.run(templ % (prefix, element))
except CrossNoRunException:
cross_failed = True
if cross_failed:
message = '''Can not determine size of {0} because cross compiled binaries are not runnable.
Please define the corresponding variable {1} in your cross compilation definition file.'''.format(element, varname)
raise EnvironmentException(message)
res = self.run(templ % (prefix, element))
if not res.compiled:
raise EnvironmentException('Could not compile sizeof test.')
if res.returncode != 0:
Expand Down Expand Up @@ -414,7 +418,7 @@ def has_function(self, funcname, prefix, env):
if val is not None:
if isinstance(val, bool):
return val
raise EnvironmentException('Cross variable {0} is not an boolean.'.format(varname))
raise EnvironmentException('Cross variable {0} is not a boolean.'.format(varname))
return self.compiles(templ % (prefix, funcname))

def has_member(self, typename, membername, prefix):
Expand Down
4 changes: 0 additions & 4 deletions cross/iphone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ cpp_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Appli
c_link_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk']
cpp_link_args = ['-arch', 'armv7', '-miphoneos-version-min=8.0', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk']

sizeof_int = 4
sizeof_wchar_t = 4
sizeof_void* = 4

alignment_char = 1
alignment_void* = 4
alignment_double = 4 # Don't know if this is correct...
Expand Down
4 changes: 0 additions & 4 deletions cross/ubuntu-armhf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ pkgconfig = '/usr/bin/arm-linux-gnueabihf-pkg-config'
[properties]
root = '/usr/arm-linux-gnueabihf'

sizeof_int = 4
sizeof_wchar_t = 4
sizeof_void* = 4

alignment_char = 1
alignment_void* = 4
alignment_double = 4 # Don't know if this is correct...
Expand Down

0 comments on commit ad5795e

Please sign in to comment.