Skip to content

Commit

Permalink
Recognize .a and .so files
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 8d130ab commit d56dd2f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
17 changes: 11 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "chibicc.h"

typedef enum { FILE_NONE, FILE_C, FILE_ASM, FILE_OBJ } FileType;
typedef enum {
FILE_NONE, FILE_C, FILE_ASM, FILE_OBJ, FILE_AR, FILE_DSO,
} FileType;

StringArray include_paths;
bool opt_fcommon = true;
Expand Down Expand Up @@ -477,12 +479,15 @@ static void run_linker(StringArray *inputs, char *output) {
}

static FileType get_file_type(char *filename) {
if (endswith(filename, ".o"))
return FILE_OBJ;

if (opt_x != FILE_NONE)
return opt_x;

if (endswith(filename, ".a"))
return FILE_AR;
if (endswith(filename, ".so"))
return FILE_DSO;
if (endswith(filename, ".o"))
return FILE_OBJ;
if (endswith(filename, ".c"))
return FILE_C;
if (endswith(filename, ".s"))
Expand Down Expand Up @@ -525,8 +530,8 @@ int main(int argc, char **argv) {

FileType type = get_file_type(input);

// Handle .o
if (type == FILE_OBJ) {
// Handle .o or .a
if (type == FILE_OBJ || type == FILE_AR || type == FILE_DSO) {
strarray_push(&ld_args, input);
continue;
}
Expand Down
16 changes: 16 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,20 @@ check '-x none'
echo foo | $chibicc -E - | grep -q foo
check -E

# .a file
echo 'void foo() {}' | $chibicc -c -xc -o $tmp/foo.o -
echo 'void bar() {}' | $chibicc -c -xc -o $tmp/bar.o -
ar rcs $tmp/foo.a $tmp/foo.o $tmp/bar.o
echo 'void foo(); void bar(); int main() { foo(); bar(); }' > $tmp/main.c
$chibicc -o $tmp/foo $tmp/main.c $tmp/foo.a
check '.a'

# .so file
echo 'void foo() {}' | cc -fPIC -c -xc -o $tmp/foo.o -
echo 'void bar() {}' | cc -fPIC -c -xc -o $tmp/bar.o -
cc -shared -o $tmp/foo.so $tmp/foo.o $tmp/bar.o
echo 'void foo(); void bar(); int main() { foo(); bar(); }' > $tmp/main.c
$chibicc -o $tmp/foo $tmp/main.c $tmp/foo.so
check '.so'

echo OK

0 comments on commit d56dd2f

Please sign in to comment.