chelper: Rework check for building C code

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-04-21 11:22:00 -04:00
parent 08eec0da86
commit 73b78af6b2
1 changed files with 26 additions and 22 deletions

View File

@ -178,31 +178,26 @@ defs_all = [
defs_kin_winch, defs_kin_extruder, defs_kin_shaper, defs_kin_winch, defs_kin_extruder, defs_kin_shaper,
] ]
# Update filenames to an absolute path
def get_abs_files(srcdir, filelist):
return [os.path.join(srcdir, fname) for fname in filelist]
# Return the list of file modification times # Return the list of file modification times
def get_mtimes(srcdir, filelist): def get_mtimes(filelist):
out = [] out = []
for filename in filelist: for filename in filelist:
pathname = os.path.join(srcdir, filename)
try: try:
t = os.path.getmtime(pathname) t = os.path.getmtime(filename)
except os.error: except os.error:
continue continue
out.append(t) out.append(t)
return out return out
# Check if the code needs to be compiled # Check if the code needs to be compiled
def check_build_code(srcdir, target, sources, cmd, other_files=[]): def check_build_code(sources, target):
src_times = get_mtimes(srcdir, sources + other_files + [__file__]) src_times = get_mtimes(sources)
obj_times = get_mtimes(srcdir, [target]) obj_times = get_mtimes([target])
if not obj_times or max(src_times) > min(obj_times): return not obj_times or max(src_times) > min(obj_times)
logging.info("Building C code module %s", target)
srcfiles = [os.path.join(srcdir, fname) for fname in sources]
destlib = os.path.join(srcdir, target)
res = os.system(cmd % (destlib, ' '.join(srcfiles)))
if res:
msg = "Unable to build C code module (error=%s)" % (res,)
logging.error(msg)
raise Exception(msg)
def check_gcc_option(option): def check_gcc_option(option):
cmd = "%s %s -S -o /dev/null -xc /dev/null > /dev/null 2>&1" % ( cmd = "%s %s -S -o /dev/null -xc /dev/null > /dev/null 2>&1" % (
@ -219,15 +214,20 @@ def get_ffi():
global FFI_main, FFI_lib, pyhelper_logging_callback global FFI_main, FFI_lib, pyhelper_logging_callback
if FFI_lib is None: if FFI_lib is None:
srcdir = os.path.dirname(os.path.realpath(__file__)) srcdir = os.path.dirname(os.path.realpath(__file__))
if check_gcc_option(SSE_FLAGS): srcfiles = get_abs_files(srcdir, SOURCE_FILES)
cmd = "%s %s %s" % (GCC_CMD, SSE_FLAGS, COMPILE_ARGS) ofiles = get_abs_files(srcdir, OTHER_FILES)
else: destlib = get_abs_files(srcdir, [DEST_LIB])[0]
cmd = "%s %s" % (GCC_CMD, COMPILE_ARGS) if check_build_code(srcfiles+ofiles+[__file__], destlib):
check_build_code(srcdir, DEST_LIB, SOURCE_FILES, cmd, OTHER_FILES) if check_gcc_option(SSE_FLAGS):
cmd = "%s %s %s" % (GCC_CMD, SSE_FLAGS, COMPILE_ARGS)
else:
cmd = "%s %s" % (GCC_CMD, COMPILE_ARGS)
logging.info("Building C code module %s", DEST_LIB)
os.system(cmd % (destlib, ' '.join(srcfiles)))
FFI_main = cffi.FFI() FFI_main = cffi.FFI()
for d in defs_all: for d in defs_all:
FFI_main.cdef(d) FFI_main.cdef(d)
FFI_lib = FFI_main.dlopen(os.path.join(srcdir, DEST_LIB)) FFI_lib = FFI_main.dlopen(destlib)
# Setup error logging # Setup error logging
def logging_callback(msg): def logging_callback(msg):
logging.error(FFI_main.string(msg)) logging.error(FFI_main.string(msg))
@ -250,7 +250,11 @@ HC_CMD = "sudo %s/hub-ctrl -h 0 -P 2 -p %d"
def run_hub_ctrl(enable_power): def run_hub_ctrl(enable_power):
srcdir = os.path.dirname(os.path.realpath(__file__)) srcdir = os.path.dirname(os.path.realpath(__file__))
hubdir = os.path.join(srcdir, HC_SOURCE_DIR) hubdir = os.path.join(srcdir, HC_SOURCE_DIR)
check_build_code(hubdir, HC_TARGET, HC_SOURCE_FILES, HC_COMPILE_CMD) srcfiles = get_abs_files(hubdir, HC_SOURCE_FILES)
destlib = get_abs_files(hubdir, [HC_TARGET])[0]
if check_build_code(srcfiles, destlib):
logging.info("Building C code module %s", HC_TARGET)
os.system(HC_COMPILE_CMD % (destlib, ' '.join(srcfiles)))
os.system(HC_CMD % (hubdir, enable_power)) os.system(HC_CMD % (hubdir, enable_power))