Skip to content

Commit

Permalink
Merge pull request #50 from dsLeks/fix-the-builders
Browse files Browse the repository at this point in the history
Fix the builders
  • Loading branch information
PiJoules authored Mar 13, 2024
2 parents fa777e2 + cf69019 commit ac5f1d8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 18 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/build-sanitized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,33 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: apt-get dependencies
run: |
sudo apt-get update
sudo apt-get -y install unzip wget cmake lsb-release software-properties-common gnupg
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 16
sudo apt-get -y install clang-format-16
- name: Get googletest
run: |
wget https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
unzip release-1.12.1.zip
cd googletest-release-1.12.1
mkdir build
cd build
cmake ..
cmake .. -DCMAKE_CXX_COMPILER=clang++-16 -DCMAKE_C_COMPILER=clang-16
make
- name: Build main executable
run: make EXTRA_CPPFLAGS="-fsanitize=address -fsanitize=undefined"
run: make LLVM_CONFIG=llvm-config-16 CXX=clang++-16 EXTRA_CPPFLAGS="-fsanitize=address -fsanitize=undefined"
- name: Build test executable
run: make test GTEST_HDR=googletest-release-1.12.1/googletest/include GTEST_LIB=googletest-release-1.12.1/build/lib EXTRA_CPPFLAGS="-fsanitize=address -fsanitize=undefined"
run: make test GTEST_HDR=googletest-release-1.12.1/googletest/include GTEST_LIB=googletest-release-1.12.1/build/lib LLVM_CONFIG=llvm-config-16 CXX=clang++-16 EXTRA_CPPFLAGS="-fsanitize=address -fsanitize=undefined"
- name: Run test executable
run: ./test
- name: Check formatting
run: make format-check LLVM_CONFIG=llvm-config-16 CLANG_FORMAT=clang-format-16
- name: Check main executable
run: |
./main test_input.txt | llc-16 -filetype=obj -o /tmp/out.o --relocation-model=pic
clang-16 /tmp/out.o
if [ "$(./a.out)" != "Hi! I am ALIVE!" ]; then echo "Output is: $(./a.out)'"exit 1; fi
21 changes: 17 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: apt-get dependencies
run: |
sudo apt-get update
sudo apt-get -y install unzip wget cmake lsb-release software-properties-common gnupg
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 16
sudo apt-get -y install clang-format-16
- name: Get googletest
run: |
wget https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
unzip release-1.12.1.zip
cd googletest-release-1.12.1
mkdir build
cd build
cmake ..
cmake .. -DCMAKE_CXX_COMPILER=clang++-16 -DCMAKE_C_COMPILER=clang-16
make
- name: Build main executable
run: make
run: make LLVM_CONFIG=llvm-config-16 CXX=clang++-16
- name: Build test executable
run: make test GTEST_HDR=googletest-release-1.12.1/googletest/include GTEST_LIB=googletest-release-1.12.1/build/lib
run: make test GTEST_HDR=googletest-release-1.12.1/googletest/include GTEST_LIB=googletest-release-1.12.1/build/lib LLVM_CONFIG=llvm-config-16 CXX=clang++-16
- name: Run test executable
run: ./test
- name: Check formatting
run: make format-check
run: make format-check LLVM_CONFIG=llvm-config-16 CLANG_FORMAT=clang-format-16
- name: Check main executable
run: |
./main test_input.txt | llc-16 -filetype=obj -o /tmp/out.o --relocation-model=pic
clang-16 /tmp/out.o
if [ "$(./a.out)" != "Hi! I am ALIVE!" ]; then echo "Output is: $(./a.out)'"exit 1; fi
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,35 @@ This could potentially build with earlier versions. These just happen to be the
ones tested with locally.

- gcc v13
- Can also use clang v16
- make v4
- [googletest v1.12.1](https://github.com/google/googletest/releases/tag/release-1.12.1) (only needed for testing)
- [LLVM v16](https://github.com/llvm/llvm-project/releases/tag/llvmorg-16.0.0)
- NOTE: If using a prebuilt for x86_64 linux, use the
[16.04](https://github.com/llvm/llvm-project/releases/tag/llvmorg-16.0.4)
prebuilt. The normal v16.0.0 version seems to be built incorrectly since I
run into a segfault with the prebuilt in
`llvm::ConstantExpr::getGetElementPtr(llvm::Type*, llvm::Constant*,
llvm::ArrayRef<llvm::Value*>, bool, std::optional<unsigned int>,
llvm::Type*)` but don't hit this when I build from source.

## Building + Running

```sh
$ make
./main
$ make # Creates ./main
$ ./main test_input.txt
; ModuleID = 'my cool jit'
source_filename = "my cool jit"

@0 = private unnamed_addr constant [16 x i8] c"Hi! I am ALIVE!\00", align 1

define void @main() {
entry:
%0 = call i32 @printf(ptr @0)
ret void
}

declare i32 @printf(ptr %0)
```

Define `EXTRA_CPPFLAGS` to pass flags to the build. For example, to build with
Expand All @@ -28,8 +49,28 @@ sanitizers:
$ make EXTRA_CPPFLAGS="-fsanitize=address -fsanitize=undefined"
```

## Using the compiler

```sh
$ ./main test_input.txt |& llc -filetype=obj -o /tmp/out.o --relocation-model=pic # Creates a.out
$ ./a.out
Hi! I am ALIVE!
```

## Testing

```sh
$ make test GTEST_HDR=<path to include dir> GTEST_LIB=<path to lib dir>
```

### Reproducing the github actions builders

The github actions can also be tested locally via docker containers using
[act](https://github.com/nektos/act).

**NOTE**: Docker containers don't have `sudo` by default, so you'll want to
remove all the `sudo`s in the build file you're running.

```
$ act --workflows .github/workflows/build.yml
```
2 changes: 1 addition & 1 deletion compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Function *Compiler::FunctionAST(const ast::Func &func) {
for (auto &Arg : TheFunction->args())
NamedValues[std::string(Arg.getName())] = &Arg;

if (Value *RetVal = OutExprAST(func.getBody())) {
if ([[maybe_unused]] Value *RetVal = OutExprAST(func.getBody())) {
// Finish off the function.
// Builder->CreateRet(RetVal);
Builder->CreateRetVoid();
Expand Down
3 changes: 1 addition & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ int main(int argc, char **argv) {

std::ifstream input(argv[1]);
xos::Lexer lexer(input);
// xos::Result<xos::Token> tok = lexer.Lex();
xos::Parser parser(lexer);
auto func = parser.parseFunc();
xos::Compiler compiler;
compiler.compile(*func);
compiler.TheModule->dump();
compiler.TheModule->print(llvm::outs(), nullptr);
std::cout << std::endl;
return 0;
}
22 changes: 16 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
CC = gcc # version?
CXX = g++
CPPFLAGS = -std=c++17 -Wall -Wextra -Wconversion -Werror -fno-rtti -fno-exceptions -fdata-sections -ffunction-sections $(EXTRA_CPPFLAGS) -g
LLVM_CONFIG ?= /Users/alekhya/Projects/llvm/build/bin/llvm-config
CPPFLAGS = -std=c++17 -Wall -Wextra -Wconversion -Werror -fno-rtti -fno-exceptions -fdata-sections -ffunction-sections -g $(EXTRA_CPPFLAGS)
CLANG_FORMAT ?= clang-format
LLVM_CONFIG ?= llvm-config
LLVM_CONFIG_CXX_FLAGS = $(shell $(LLVM_CONFIG) --cxxflags)
LLVM_CONFIG_LD_FLAGS = $(shell $(LLVM_CONFIG) --ldflags)
LLVM_CONFIG_SYSTEM_LIBS = $(shell $(LLVM_CONFIG) --system-libs)
LLVM_CONFIG_LIBS = $(shell $(LLVM_CONFIG) --libs core)

UNAME = $(shell uname -s)
ifeq ($(UNAME),Linux)
PLATFORM_SPECIFIC_LDFLAGS = -Wl,--gc-sections
endif
ifeq ($(UNAME),Darwin)
PLATFORM_SPECIFIC_LDFLAGS = -Wl,dead_strip
endif

CPPFLAGS := $(LLVM_CONFIG_CXX_FLAGS) $(CPPFLAGS)
LDFLAGS := $(LLVM_CONFIG_LD_FLAGS) $(LLVM_CONFIG_SYSTEM_LIBS) $(LLVM_CONFIG_LIBS) $(LDFLAGS) -Wl,-dead_strip
LDFLAGS := $(LLVM_CONFIG_LD_FLAGS) $(LLVM_CONFIG_SYSTEM_LIBS) $(LLVM_CONFIG_LIBS) $(LDFLAGS) $(PLATFORM_SPECIFIC_LDFLAGS)

# Add new source files here.
SRCS = lexer.cpp parser.cpp compiler.cpp
Expand All @@ -34,14 +43,15 @@ test.o: test.cpp $(HDRS)
# This is run with:
#
# $ make test GTEST_HDR=<path to include dir> GTEST_LIB=<path to lib dir>
#
$(TEST): test.o $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@ -lgtest_main -lgtest -L$(GTEST_LIB) $(LDFLAGS)
$(CXX) $(CPPFLAGS) $^ -o $@ -lgtest_main -lgtest -L$(GTEST_LIB) $(LDFLAGS) -lpthread

clean:
rm -rf $(EXE) *.o $(TEST)

format:
clang-format --style=google -i $(SRCS) main.cpp test.cpp $(HDRS)
$(CLANG_FORMAT) --style=google -i $(SRCS) main.cpp test.cpp $(HDRS)

format-check:
clang-format --style=google $(SRCS) main.cpp test.cpp $(HDRS) --dry-run -Werror
$(CLANG_FORMAT) --style=google $(SRCS) main.cpp test.cpp $(HDRS) --dry-run -Werror

0 comments on commit ac5f1d8

Please sign in to comment.