From bbddcc7ef7eb59af125636562cb670fd86ac5be5 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 2 Jan 2026 16:12:09 +0800 Subject: [PATCH] fix #419 --- amalgamate.py | 28 +++++++++++++++++++++++----- include/pocketpy/pocketpy.h | 2 ++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index 1688d558..c4f18d5e 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -80,7 +80,14 @@ for entry in os.listdir(ROOT): headers[f'{entry}/{file}'] = Header(f'{entry}/{file}') def merge_c_files(): - c_files = [COPYRIGHT, '\n', '#include "pocketpy.h"', '\n'] + c_files = [ + COPYRIGHT, + '\n', + '#define PK_IS_AMALGAMATED_C', + '\n', + '#include "pocketpy.h"', + '\n' + ] # merge internal headers internal_h = [] @@ -123,7 +130,12 @@ def merge_c_files(): return ''.join(c_files) def merge_h_files(): - h_files = [COPYRIGHT, '#pragma once'] + h_files = [ + COPYRIGHT, + '#pragma once', + '\n', + '#define PK_IS_PUBLIC_INCLUDE', + ] def _replace(m): path = m.group(1) @@ -148,9 +160,15 @@ write_file('amalgamated/pocketpy.h', merge_h_files()) shutil.copy("src2/main.c", "amalgamated/main.c") +def checked_sh(cmd): + ok = os.system(cmd) + assert ok == 0, f"command failed: {cmd}" + if sys.platform in ['linux', 'darwin']: - ok = os.system("gcc -o main amalgamated/pocketpy.c amalgamated/main.c -O1 --std=c11 -lm -ldl -lpthread") - if ok == 0: - print("Test build success!") + common_flags = "-O1 --std=c11 -lm -ldl -lpthread -Iamalgamated" + checked_sh(f"gcc -o main amalgamated/pocketpy.c src2/example.c {common_flags}") + checked_sh("./main && rm -f ./main") + checked_sh(f"gcc -o main amalgamated/pocketpy.c amalgamated/main.c {common_flags}") + print("amalgamated/pocketpy.h") diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 6eac3fcf..8fa0296f 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -28,6 +28,7 @@ typedef double py_f64; /// A generic destructor function. typedef void (*py_Dtor)(void*); +#ifndef PK_IS_AMALGAMATED_C #ifdef PK_IS_PUBLIC_INCLUDE typedef struct py_TValue { py_Type type; @@ -40,6 +41,7 @@ typedef struct py_TValue { }; } py_TValue; #endif +#endif /// A string view type. It is helpful for passing strings which are not null-terminated. typedef struct c11_sv {