Skip to content

Commit d6e4dc7

Browse files
committed
fix: cross build bug
1 parent fb1e739 commit d6e4dc7

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

src/build/build.c

+34-29
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void elf_custom_links() {
7878
ct_fndef_size = collect_fndef_list(ctx);
7979
ct_fndef_data = fndefs_serialize();
8080
elf_put_data(ctx->data_fndef_section, ct_fndef_data, ct_fndef_size);
81-
sym = (Elf64_Sym){
81+
sym = (Elf64_Sym) {
8282
.st_shndx = ctx->data_fndef_section->sh_index,
8383
.st_value = 0,
8484
.st_other = 0,
@@ -91,7 +91,7 @@ static void elf_custom_links() {
9191
// caller - --------------------------------------------------------------------------
9292
ct_caller_data = callers_serialize();
9393
elf_put_data(ctx->data_caller_section, ct_caller_data, ct_caller_list->length * sizeof(caller_t));
94-
sym = (Elf64_Sym){
94+
sym = (Elf64_Sym) {
9595
.st_shndx = ctx->data_caller_section->sh_index,
9696
.st_value = 0,
9797
.st_other = 0,
@@ -106,7 +106,7 @@ static void elf_custom_links() {
106106
ct_symdef_size = collect_symdef_list(ctx);
107107
ct_symdef_data = symdefs_serialize();
108108
elf_put_data(ctx->data_symdef_section, ct_symdef_data, ct_symdef_size);
109-
sym = (Elf64_Sym){
109+
sym = (Elf64_Sym) {
110110
.st_shndx = ctx->data_symdef_section->sh_index,
111111
.st_value = 0,
112112
.st_other = 0,
@@ -143,11 +143,11 @@ static void mach_custom_links() {
143143
ct_rtype_data = rtypes_serialize();
144144
mach_put_data(ctx->data_rtype_section, ct_rtype_data, ct_rtype_size);
145145
// 创建符号指向自定义数据段 __data.rtype
146-
mach_put_sym(ctx->symtab_command, &(struct nlist_64){
147-
.n_type = N_SECT | N_EXT,
148-
.n_sect = ctx->data_rtype_section->sh_index,
149-
.n_value = 0, // in section data offset
150-
},
146+
mach_put_sym(ctx->symtab_command, &(struct nlist_64) {
147+
.n_type = N_SECT | N_EXT,
148+
.n_sect = ctx->data_rtype_section->sh_index,
149+
.n_value = 0, // in section data offset
150+
},
151151
SYMBOL_RTYPE_DATA);
152152

153153
macho_put_global_symbol(ctx, SYMBOL_RTYPE_COUNT, &ct_rtype_count, QWORD);
@@ -157,11 +157,11 @@ static void mach_custom_links() {
157157
ct_fndef_data = fndefs_serialize();
158158
mach_put_data(ctx->data_fndef_section, ct_fndef_data, ct_fndef_size);
159159

160-
mach_put_sym(ctx->symtab_command, &(struct nlist_64){
161-
.n_type = N_SECT | N_EXT,
162-
.n_sect = ctx->data_fndef_section->sh_index,
163-
.n_value = 0, // in section data offset
164-
},
160+
mach_put_sym(ctx->symtab_command, &(struct nlist_64) {
161+
.n_type = N_SECT | N_EXT,
162+
.n_sect = ctx->data_fndef_section->sh_index,
163+
.n_value = 0, // in section data offset
164+
},
165165
SYMBOL_FNDEF_DATA);
166166
macho_put_global_symbol(ctx, SYMBOL_FNDEF_COUNT, &ct_fndef_count, QWORD);
167167

@@ -170,11 +170,11 @@ static void mach_custom_links() {
170170
ct_caller_data = callers_serialize();
171171
mach_put_data(ctx->data_caller_section, ct_caller_data, ct_caller_list->length * sizeof(caller_t));
172172
// 注册段名称与 runtime 中的符号进行绑定
173-
mach_put_sym(ctx->symtab_command, &(struct nlist_64){
174-
.n_type = N_SECT | N_EXT,
175-
.n_sect = ctx->data_caller_section->sh_index,
176-
.n_value = 0, // in section data offset
177-
},
173+
mach_put_sym(ctx->symtab_command, &(struct nlist_64) {
174+
.n_type = N_SECT | N_EXT,
175+
.n_sect = ctx->data_caller_section->sh_index,
176+
.n_value = 0, // in section data offset
177+
},
178178
SYMBOL_CALLER_DATA);
179179
macho_put_global_symbol(ctx, SYMBOL_CALLER_COUNT, &ct_caller_list->length, QWORD);
180180

@@ -184,11 +184,11 @@ static void mach_custom_links() {
184184
ct_symdef_data = symdefs_serialize();
185185
mach_put_data(ctx->data_symdef_section, ct_symdef_data, ct_symdef_size);
186186

187-
mach_put_sym(ctx->symtab_command, &(struct nlist_64){
188-
.n_type = N_SECT | N_EXT,
189-
.n_sect = ctx->data_symdef_section->sh_index,
190-
.n_value = 0, // in section data offset
191-
},
187+
mach_put_sym(ctx->symtab_command, &(struct nlist_64) {
188+
.n_type = N_SECT | N_EXT,
189+
.n_sect = ctx->data_symdef_section->sh_index,
190+
.n_value = 0, // in section data offset
191+
},
192192
SYMBOL_SYMDEF_DATA);
193193
macho_put_global_symbol(ctx, SYMBOL_SYMDEF_COUNT, &ct_symdef_count, QWORD);
194194

@@ -278,11 +278,11 @@ static void mach_assembler_module(module_t *m) {
278278
uint64_t offset = mach_put_data(ctx->data_section, symbol->value, symbol->size);
279279

280280
// 写入符号表
281-
mach_put_sym(ctx->symtab_command, &(struct nlist_64){
282-
.n_type = N_SECT | N_EXT,
283-
.n_sect = ctx->data_section->sh_index,
284-
.n_value = offset, // in section data offset
285-
},
281+
mach_put_sym(ctx->symtab_command, &(struct nlist_64) {
282+
.n_type = N_SECT | N_EXT,
283+
.n_sect = ctx->data_section->sh_index,
284+
.n_value = offset, // in section data offset
285+
},
286286
symbol->name);
287287
}
288288

@@ -396,7 +396,7 @@ static int command_exists(const char *cmd) {
396396
int exists = 0;
397397

398398
while (dir != NULL) {
399-
char full_path[1024];
399+
char full_path[8211];
400400
snprintf(full_path, sizeof(full_path), "%s/%s", dir, cmd);
401401

402402
if (stat(full_path, &st) == 0) {
@@ -649,6 +649,11 @@ static void build_init(char *build_entry) {
649649
assertf(false, "entry file='%s' not found", build_entry);
650650
}
651651

652+
// darwin 默认使用 ld 链接
653+
if (BUILD_OS == OS_DARWIN && strlen(USE_LD) == 0) {
654+
strcpy(USE_LD, "ld");
655+
}
656+
652657
// copy
653658
strcpy(SOURCE_PATH, temp_path);
654659
assertf(file_exists(SOURCE_PATH), "full entry file=%s not found", SOURCE_PATH);

src/build/config.c

+2-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@ char SOURCE_PATH[PATH_MAX] = "";
2424
char BUILD_OUTPUT_DIR[PATH_MAX] = "";
2525
char BUILD_OUTPUT[PATH_MAX] = "";
2626

27-
#ifdef __LINUX
28-
char USE_LD[PATH_MAX] = "";
29-
#else
30-
char USE_LD[PATH_MAX] = "ld";
31-
#endif
32-
33-
char LDFLAGS[PATH_MAX] = "";
27+
char USE_LD[1024] = "";
28+
char LDFLAGS[1024] = "";
3429

3530
char *WORKDIR; // 执行 shell 命令所在的目录(import 搜索将会基于该目录进行文件搜索)
3631
char *BASE_NS; // 最后一级目录的名称,也可以自定义

src/build/config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ extern char *WORKDIR; // 执行 shell 命令所在的目录(import 搜索将会
2929
extern char *BASE_NS; // 最后一级目录的名称,也可以自定义
3030
extern char *TEMP_DIR;// 链接临时目录
3131

32-
extern char USE_LD[PATH_MAX]; // 自定义链接器
33-
extern char LDFLAGS[PATH_MAX]; // 自定义链接器参数
32+
extern char USE_LD[1024]; // 自定义链接器
33+
extern char LDFLAGS[1024]; // 自定义链接器参数
3434

3535
extern char *BUILD_ENTRY; // nature build {test/main.n} 花括号包起来的这部分
3636
extern char SOURCE_PATH[PATH_MAX];// /opt/test/main.n 的绝对路径

tests/features/20250418_00_playground.c

+2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ char *http_get(const char *path) {
2222
}
2323

2424
char *code1 = "import fs\n"
25+
"import co"
2526
"\n"
2627
"fn main() {\n"
2728
" println(\"hello world\")\n"
29+
" co.sleep(100)\n"
2830
" var stderr = fs.stderr()\n"
2931
" stderr.write(\"hello error\\n\" as [u8])\n"
3032
"}";

0 commit comments

Comments
 (0)