Skip to content

Commit 4aa022e

Browse files
author
Matthew MacGregor
committed
Initial for alpine linux support.
1 parent 90d1f7f commit 4aa022e

File tree

7 files changed

+56
-6
lines changed

7 files changed

+56
-6
lines changed

.vscode/settings.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"files.associations": {
3+
"array": "c",
4+
"atomic": "c",
5+
"complex": "c",
6+
"chrono": "c",
7+
"ratio": "c",
8+
"string_view": "c",
9+
"system_error": "c",
10+
"type_traits": "c",
11+
"functional": "c",
12+
"istream": "c",
13+
"ostream": "c",
14+
"sstream": "c",
15+
"streambuf": "c"
16+
},
17+
"C_Cpp.errorSquiggles": "disabled"
18+
}

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ chibicc: $(OBJS)
1313

1414
$(OBJS): chibicc.h
1515

16+
# Note: -include include/stdarg.h resolves missing va_list with musl, but
17+
# is not needed for platforms with glibc
1618
test/%.exe: chibicc test/%.c
17-
./chibicc -Iinclude -Itest -c -o test/$*.o test/$*.c
19+
./chibicc -include include/stdarg.h -Iinclude -Itest -c -o test/$*.o test/$*.c
1820
$(CC) -pthread -o $@ test/$*.o -xc test/common
1921

2022
test: $(TESTS)

chibicc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ noreturn void error(char *fmt, ...) __attribute__((format(printf, 1, 2)));
9595
noreturn void error_at(char *loc, char *fmt, ...) __attribute__((format(printf, 2, 3)));
9696
noreturn void error_tok(Token *tok, char *fmt, ...) __attribute__((format(printf, 2, 3)));
9797
void warn_tok(Token *tok, char *fmt, ...) __attribute__((format(printf, 2, 3)));
98+
void print_tok(Token *tok, char *fmt, ...) __attribute__((format(printf, 2, 3)));
9899
bool equal(Token *tok, char *op);
99100
Token *skip(Token *tok, char *op);
100101
bool consume(Token **rest, Token *tok, char *str);

include/stdarg.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ static void *__va_arg_fp(__va_elem *ap, int sz, int align) {
5454
#define __GNUC_VA_LIST 1
5555
typedef va_list __gnuc_va_list;
5656

57+
#ifndef va_list
58+
// TODO: this is necessary on Alpine linux; there must be a better condition
59+
// to check than if we have va_list defined at this point.
60+
typedef va_list __builtin_va_list;
61+
#endif
62+
5763
#endif

main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ static char *find_libpath(void) {
593593
return "/usr/lib/x86_64-linux-gnu";
594594
if (file_exists("/usr/lib64/crti.o"))
595595
return "/usr/lib64";
596+
if (file_exists("/usr/lib/crti.o"))
597+
return "/usr/lib";
596598
error("library path is not found");
597599
}
598600

@@ -601,6 +603,7 @@ static char *find_gcc_libpath(void) {
601603
"/usr/lib/gcc/x86_64-linux-gnu/*/crtbegin.o",
602604
"/usr/lib/gcc/x86_64-pc-linux-gnu/*/crtbegin.o", // For Gentoo
603605
"/usr/lib/gcc/x86_64-redhat-linux/*/crtbegin.o", // For Fedora
606+
"/usr/lib/gcc/x86_64-alpine-linux-musl/*/crtbegin.o" // For Alpine
604607
};
605608

606609
for (int i = 0; i < sizeof(paths) / sizeof(*paths); i++) {
@@ -645,7 +648,11 @@ static void run_linker(StringArray *inputs, char *output) {
645648

646649
if (!opt_static) {
647650
strarray_push(&arr, "-dynamic-linker");
648-
strarray_push(&arr, "/lib64/ld-linux-x86-64.so.2");
651+
if (file_exists("/lib64/ld-linux-x86-64.so.2")) {
652+
strarray_push(&arr, "/lib64/ld-linux-x86-64.so.2"); // ubuntu 20.04
653+
} else if (file_exists("/lib/ld-musl-x86_64.so.1")) {
654+
strarray_push(&arr, "/lib/ld-musl-x86_64.so.1"); // alpine
655+
}
649656
}
650657

651658
for (int i = 0; i < ld_extra_args.len; i++)

parse.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,8 +1506,9 @@ static bool is_typename(Token *tok) {
15061506
"_Thread_local", "__thread", "_Atomic",
15071507
};
15081508

1509-
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++)
1509+
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) {
15101510
hashmap_put(&map, kw[i], (void *)1);
1511+
}
15111512
}
15121513

15131514
return hashmap_get2(&map, tok->loc, tok->len) || find_typedef(tok);
@@ -3131,13 +3132,16 @@ static Token *parse_typedef(Token *tok, Type *basety) {
31313132
bool first = true;
31323133

31333134
while (!consume(&tok, tok, ";")) {
3134-
if (!first)
3135+
if (!first) {
31353136
tok = skip(tok, ",");
3137+
}
31363138
first = false;
31373139

31383140
Type *ty = declarator(&tok, tok, basety);
3139-
if (!ty->name)
3141+
if (!ty->name) {
31403142
error_tok(ty->name_pos, "typedef name omitted");
3143+
}
3144+
31413145
push_scope(get_ident(ty->name))->type_def = ty;
31423146
}
31433147
return tok;

tokenize.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,27 @@ void warn_tok(Token *tok, char *fmt, ...) {
7575
va_end(ap);
7676
}
7777

78+
void print_tok(Token *tok, char *fmt, ...) {
79+
va_list ap;
80+
va_start(ap, fmt);
81+
verror_at(tok->file->name, tok->file->contents, tok->line_no, tok->loc, fmt, ap);
82+
va_end(ap);
83+
}
84+
7885
// Consumes the current token if it matches `op`.
7986
bool equal(Token *tok, char *op) {
8087
return memcmp(tok->loc, op, tok->len) == 0 && op[tok->len] == '\0';
8188
}
8289

8390
// Ensure that the current token is `op`.
8491
Token *skip(Token *tok, char *op) {
85-
if (!equal(tok, op))
92+
if (!equal(tok, op)) {
93+
char tmp[256] = {0};
94+
memcpy(tmp, tok->loc, tok->len);
95+
printf("tok: %s, op: %s\n", tmp, op);
8696
error_tok(tok, "expected '%s'", op);
97+
}
98+
8799
return tok->next;
88100
}
89101

0 commit comments

Comments
 (0)