Skip to content

Commit

Permalink
Add support for M1 Mac
Browse files Browse the repository at this point in the history
* wref to issue IUCompilerCourse/public-student-support-code#13
* We do this with the help of inbuilt arch command "Rosetta 2", For now Rosetta is here to stay
* and we can make use of it's capabilities to compile x86 instructions on arm machine
  • Loading branch information
vjspranav committed May 2, 2022
1 parent e88d53a commit bea9b9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
CC = gcc

UNAME_S := $(shell uname -s)
UNAME_P := $(shell uname -p)
ifeq ($(UNAME_S),Darwin)
ifeq ($(UNAME_P),arm)
$(info M1 MacOSX detected)
CC := arch -x86_64 $(CC)
endif
endif

.PHONY: all test

Expand All @@ -8,10 +18,10 @@ test: runtime.o
racket run-tests.rkt

runtime.o: runtime.c runtime.h
gcc -std=c11 -c $^
$(CC) -std=c11 -c $^

fake_prog: fake_prog.c runtime.o
gcc -std=c11 $^ -o $@
$(CC) -std=c11 $^ -o $@

clean:
rm -rf *~ fake_prog runtime.o runtime.h.gch ./compiled tests/*.s tests/*.out tests/*.dSYM tests/*~ *.dot *.png log.* *.log
10 changes: 9 additions & 1 deletion utilities.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,14 @@ Changelog:
(close-output-port out)
result)]))

(define (get-arch)
(match (system-type 'arch)
[ 'aarch64
(match (system-type 'os)
[ 'macosx "arch -x86_64 "]
[ else "" ])]
[ else "" ]))

(define (compiler-tests-suite name typechecker passes test-family test-nums)
(let ([compiler (compile-file typechecker passes)])
(make-test-suite
Expand All @@ -2237,7 +2245,7 @@ Changelog:
(test-case "typecheck" (check-false typechecks "Expected expression to fail typechecking"))
(if (not typechecks) (fail "Expected expression to typecheck")
(test-case "code generation"
(let ([gcc-output (system (format "gcc -g -march=x86-64 -std=c99 runtime.o ./tests/~a.s -o ./tests/~a.out" test-name test-name))])
(let ([gcc-output (system (format (string-append (get-arch) "gcc -g -std=c99 runtime.o ./tests/~a.s -o ./tests/~a.out") test-name test-name))])
(if (not gcc-output) (fail "Failed during assembly")
(let ([input (if (file-exists? (format "./tests/~a.in" test-name))
(format " < ./tests/~a.in" test-name)
Expand Down

0 comments on commit bea9b9b

Please sign in to comment.