From ae010215e7a37469e88b77fab5bd585b981567a4 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 16 Aug 2025 11:29:29 -0400 Subject: [PATCH] chelper: Build library first in temporary file and then rename Try to avoid cases where an incomplete library build causes confusing future failures. Signed-off-by: Kevin O'Connor --- klippy/chelper/__init__.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/klippy/chelper/__init__.py b/klippy/chelper/__init__.py index 6ed6ed5b..60ba91e7 100644 --- a/klippy/chelper/__init__.py +++ b/klippy/chelper/__init__.py @@ -274,6 +274,28 @@ def do_build_code(cmd): logging.error(msg) raise Exception(msg) +# Build the main c_helper.so c code library +def check_build_c_library(): + srcdir = os.path.dirname(os.path.realpath(__file__)) + srcfiles = get_abs_files(srcdir, SOURCE_FILES) + ofiles = get_abs_files(srcdir, OTHER_FILES) + destlib = get_abs_files(srcdir, [DEST_LIB])[0] + if not check_build_code(srcfiles+ofiles+[__file__], destlib): + # Code already built + return destlib + # Select command line options + if check_gcc_option(SSE_FLAGS): + cmd = "%s %s %s" % (GCC_CMD, SSE_FLAGS, COMPILE_ARGS) + else: + cmd = "%s %s" % (GCC_CMD, COMPILE_ARGS) + # Invoke compiler + logging.info("Building C code module %s", DEST_LIB) + tempdestlib = get_abs_files(srcdir, ["_temp_" + DEST_LIB])[0] + do_build_code(cmd % (tempdestlib, ' '.join(srcfiles))) + # Rename from temporary file to final file name + os.rename(tempdestlib, destlib) + return destlib + FFI_main = None FFI_lib = None pyhelper_logging_callback = None @@ -286,17 +308,9 @@ def logging_callback(msg): def get_ffi(): global FFI_main, FFI_lib, pyhelper_logging_callback if FFI_lib is None: - srcdir = os.path.dirname(os.path.realpath(__file__)) - srcfiles = get_abs_files(srcdir, SOURCE_FILES) - ofiles = get_abs_files(srcdir, OTHER_FILES) - destlib = get_abs_files(srcdir, [DEST_LIB])[0] - if check_build_code(srcfiles+ofiles+[__file__], destlib): - 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) - do_build_code(cmd % (destlib, ' '.join(srcfiles))) + # Check if library needs to be built, and build if so + destlib = check_build_c_library() + # Open library FFI_main = cffi.FFI() for d in defs_all: FFI_main.cdef(d)