diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a335ee..ebcd1bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.12) project(GazpreaBase) # Ensure tool dependencies. @@ -6,8 +6,8 @@ include(ExternalProject) # Required to download and build external projects (i.e find_package(Git REQUIRED) # Need git to download ANTLR through ExternalProject. find_package(Java COMPONENTS Runtime REQUIRED) # Need java to run ANTLR, but only the runtime. -# Ensure we have LLVM. -include("${CMAKE_SOURCE_DIR}/cmake/get_llvm.cmake") +# Ensure we have MLIR. +include("${CMAKE_SOURCE_DIR}/cmake/get_mlir.cmake") # Link against the pthreads library to make std::call_once # in generated ANTLR code to run without producing system errors diff --git a/LICENSE.md b/LICENSE.md index 2461d68..e22db37 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ CMPUT 415 Student Submission License (Version 1.0) -Copyright 2022 `student name` +Copyright 2023 `student name` Unauthorized redistribution is forbidden in all circumstances. Use of this software without explicit authorization from the author **or** CMPUT 415 diff --git a/README.md b/README.md index 2bdea5e..6774432 100644 --- a/README.md +++ b/README.md @@ -4,69 +4,18 @@ The base cmake setup for Gazprea assignment. Author: Braedy Kuzma (braedy@ualberta.ca) Updated by: Deric Cheung (dacheung@ualberta.ca) -# Usage -## Installing LLVM -You will be working with LLVM in this project. Due to the complex nature (and -size) of the project we did not want to include LLVM as a subproject. -Therefore, there is some additional setup required to get your build up and -running. +# Building +CMake will use the environment variables ANTLR_INS, ANTLR_JAR, and MLIR_DIR +to find locally installed builds of ANTLR and MLIR. +See the [setup document](https://webdocs.cs.ualberta.ca/~c415/setup/) +to find out how to install these packages on your machine. -### On a personal machine -The first thing you should do is have a look into the `configureLLVM.sh` script -in the scripts folder and understand as much as possible. We'll touch some -things briefly here that are better explained there. The steps here will expect -you have some knowledge of what's going on inside the script. - - 1. Checkout LLVM to a to your home directory from - [OUR FORK](https://github.com/cmput415/llvm-project), checkout the 10.0.0 - release, and change the directory in your script. - 1. `cd $HOME` - 1. `git clone git@github.com:cmput415/llvm-project.git` - 1. `cd llvm` - 1. `git checkout llvmorg-10.0.0` - 1. Add these configuration lines to your `~/.bashrc` on linux or - `~/.bash_profile` on MacOS to setup the next steps. You should restart your - terminal after editing these files. - ```bash - export LLVM_INS="$HOME/llvm-ins/" # Change me if you really want. - export LLVM_DIR="$LLVM_INS/lib/cmake/llvm/" # Don't change me. - export PATH="$LLVM_INS/bin:$PATH" # Don't change me - ``` - 1. If you want a debug build you may change this by looking at Step 2 in the - `scripts/configureLLVM.sh` script. - 1. Run `configureLLVM.sh`. - 1. `cd $HOME/llvm-project/build` - 1. `make -j x` where x is the number of compilation threads. `4` is safe if - you have >= 8gb RAM. Haven't experimented much here. The script is set up - to try to use a better linker, but if you end up with your system linker - the memory usage can balloon quickly and paging can become a problem. If - you're having problems with hanging or system lag while compiling you - might want to kill this (you won't lose your progress if `ctrl+c` works) - and run with less threads. - 1. Run `make install`. - 1. CMake should automatically pick up your built llvm now. You should return - to the assignment specification to complete setup. - -### On university machines -You won't be building LLVM on the university machines: AICT wouldn't be very -happy with you. Instead, we are providing a **RELEASE** build available for -everyone. - 1. Follow the instructions on the [setup - page](https://webdocs.cs.ualberta.ca/~c415/setup/) for the CS computers and - LLVM will be available to you. - -## Building -### Linux - 1. Install git, java (only the runtime is necessary), and cmake (>= v3.0). - - Until now, cmake has found the dependencies without issues. If you - encounter an issue, let a TA know and we can fix it. 1. Make a directory that you intend to build the project in and change into that directory. - 1. Run `cmake `. - 1. Run `make`. - 1. Done. + 2. Run `cmake `. + 3. Run `make`. -## Pulling in upstream changes +# Pulling in upstream changes If there are updates to your assignment you can retrieve them using the instructions here. 1. Add the upstream as a remote using `git remote add upstream `. diff --git a/cmake/get_antlr.cmake b/cmake/get_antlr.cmake index d7b65f0..e5b6d90 100644 --- a/cmake/get_antlr.cmake +++ b/cmake/get_antlr.cmake @@ -1,17 +1,16 @@ -# A module to obtain the ANTLR C++ runtime. It attempts to find a locally installed copy of the -# runtime pointed to by an environment variable called ANTLR_INS. Can swap in get_antlr_manual -# if you'd rather have it auto-install a copy just for this project (more space). - -# CMake module that finds a previously installed copy of the ANTLR C++ runtime required when linking -# a generated lexer/parser. Creates the variable ANTLR_INCLUDE_DIRS to add to your target's -# include directories, adds the antlr library path to the project, allows your target to link -# against antlr4-runtime, creates ANTLR_JAR for generating grammars, and creates an antlr target -# that you can add via add_dependencies to make sure everything has happened already. +# A module to obtain the ANTLR C++ runtime. It attempts to find a locally installed +# copy of the runtime pointed to by an environment variable called ANTLR_INS. +# The module also looks for the environment variable ANTLR_JAR, which is used +# to generate the lexer and parser. +# Creates the variable ANTLR_INCLUDE_DIRS to add to your target's include +# directories, adds the antlr library path to the project, allows your target +# to link against antlr4-runtime, and creates an antlr target that you can add +# via add_dependencies to make sure everything has happened already. # Get the environment variable that tells us where the manual install was. if (NOT DEFINED ENV{ANTLR_INS}) - message(FATAL_ERROR "Did you forget to install ANTLR? The ANTLR_INS environment variable was " - "not set.") + message(FATAL_ERROR "Did you forget to install ANTLR?" + "The ANTLR_INS environment variable was not set.") endif() file(TO_CMAKE_PATH "$ENV{ANTLR_INS}" _ANTLR_DIR) @@ -19,19 +18,11 @@ file(TO_CMAKE_PATH "$ENV{ANTLR_INS}" _ANTLR_DIR) file(TO_CMAKE_PATH "${_ANTLR_DIR}/bin" BIN_DIR) # Join dir. set(BIN_DIR ${BIN_DIR} CACHE PATH "ANTLR jar directory.") # Set for internal use. -# Download ANTLR executable, saves us from ensuring people have java build tools (e.g. Maven)... -file(TO_CMAKE_PATH "${BIN_DIR}/antlr-4.10.1-complete.jar" ANTLR_JAR) -if (NOT EXISTS "${ANTLR_JAR}") - message(STATUS "Downloading ANTLR generator...") - file( - DOWNLOAD - http://www.antlr.org/download/antlr-4.10.1-complete.jar - "${ANTLR_JAR}" - SHOW_PROGRESS - ) - file(TO_NATIVE_PATH "${BIN_DIR}" BIN_DIR_NATIVE) # Transform for display. - message(STATUS "Downloaded ANTLR jar destination: ${BIN_DIR_NATIVE}") +# Find the ANTLR binary +if (NOT DEFINED ENV{ANTLR_JAR}) + message(FATAL_ERROR "The ANTLR_JAR environment variable is not set") endif() +set(ANTLR_JAR "$ENV{ANTLR_JAR}") # Check that the base include path exists. if (NOT EXISTS "${_ANTLR_DIR}/include/antlr4-runtime/") diff --git a/cmake/get_antlr_manual.cmake b/cmake/get_antlr_manual.cmake deleted file mode 100644 index 61aa049..0000000 --- a/cmake/get_antlr_manual.cmake +++ /dev/null @@ -1,79 +0,0 @@ -# Another method to obtain the ANTLR C++ runtime. This will clone the github repository for ANTLR -# and build its own copy of the runtime to use (roughly .5-.75GB extra space). Completely swappable -# with finding your person install of ANTLR. - -# CMake module that downloads the antlr source and builds the C++ runtime required when linking -# a generated lexer/parser. Creates the variable ANTLR_INCLUDE_DIRS to add to your target's -# include directories, adds the antlr library path to the project, allows your target to link -# against antlr4-runtime, creates ANTLR_JAR for generating grammars, and creates an antlr target -# that you can add via add_dependencies to make sure everything has happened already. - -# Set the directory for binaries. -file(TO_CMAKE_PATH "${CMAKE_BINARY_DIR}/bin" BIN_DIR) # Join dir. -set(BIN_DIR ${BIN_DIR} CACHE PATH "ANTLR jar directory.") # Set for internal use. - -# Download ANTLR executable, saves us from ensuring people have java build tools (e.g. Maven)... -file(TO_CMAKE_PATH "${BIN_DIR}/antlr.jar" ANTLR_JAR) -if (NOT EXISTS "${ANTLR_JAR}") - message(STATUS "Downloading ANTLR generator...") - file( - DOWNLOAD - http://www.antlr.org/download/antlr-4.10.1-complete.jar - "${ANTLR_JAR}" - SHOW_PROGRESS - ) - file(TO_NATIVE_PATH "${BIN_DIR}" BIN_DIR_NATIVE) # Transform for display. - message(STATUS "Downloaded ANTLR jar destination: ${BIN_DIR_NATIVE}") -endif() - -# Download ANTLR source to get the runtime... -# Turn on logging as necessary. -ExternalProject_Add( - antlr - - # Set up custom paths. - PREFIX "${CMAKE_BINARY_DIR}" - TMP_DIR "${CMAKE_BINARY_DIR}/antlr-tmp" - STAMP_DIR "${CMAKE_BINARY_DIR}/antlr-stamp" - DOWNLOAD_DIR "${CMAKE_BINARY_DIR}" - SOURCE_DIR "${CMAKE_BINARY_DIR}/antlr" - BINARY_DIR "${CMAKE_BINARY_DIR}/antlr-build" - INSTALL_DIR "${CMAKE_BINARY_DIR}/antlr-install" - - # ANTLR repository and tag. - GIT_REPOSITORY https://github.com/antlr/antlr4.git - GIT_TAG "4.10.1" - UPDATE_COMMAND "" - PATCH_COMMAND "" - TIMEOUT 10 - #LOG_DOWNLOAD ON - - # Modify the configure command to always build release mode, both shared and static libs, notify - # of jar location, and then set custom paths for the build and install. - CONFIGURE_COMMAND - ${CMAKE_COMMAND} - -DCMAKE_BUILD_TYPE=Release - -DBUILD_SHARED_LIBS=ON -BUILD_TESTS=OFF - -DCMAKE_INSTALL_PREFIX:PATH= - /runtime/Cpp - #LOG_CONFIGURE ON - - # No need to edit the build command, the autogenerated one works perfectly. - #LOG_BUILD ON - - # No need to edit the install command, the autogenerated one works perfectly. - #LOG_INSTALL ON -) - -# Grab the install directory so we can get the include directories and built libs. -ExternalProject_Get_Property(antlr INSTALL_DIR) - -# Create includes paths. -list(APPEND ANTLR_INCLUDE_DIRS ${INSTALL_DIR}/include/antlr4-runtime) -foreach(src_path misc atn dfa tree support) - list(APPEND ANTLR_INCLUDE_DIRS ${INSTALL_DIR}/include/antlr4-runtime/${src_path}) -endforeach(src_path) - -# Create libs path and then add it to the linker paths. -set(_ANTLR_LIB_DIRS "${INSTALL_DIR}/lib") -link_directories("${_ANTLR_LIB_DIRS}") diff --git a/cmake/get_llvm.cmake b/cmake/get_llvm.cmake deleted file mode 100644 index 838f308..0000000 --- a/cmake/get_llvm.cmake +++ /dev/null @@ -1,19 +0,0 @@ -# Need LLVM so we can link against it. Either we automatically find this in the "default" place or -# it's found because we set up $LLVM_DIR. There should be no additions necessary here. -find_package(LLVM REQUIRED CONFIG) - -# Status messages about LLVM found. -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") -message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") - -# Ensure we found our own specified version at 10.0.0. We don't want Ohaton's 3.x.x or another local -# build we don't know. -if(NOT ("${LLVM_VERSION_MAJOR}" EQUAL 10 AND - "${LLVM_VERSION_MINOR}" EQUAL 0 AND - "${LLVM_VERSION_PATCH}" EQUAL 0)) - message(FATAL_ERROR "LLVM version incompatible.") -endif() - -# Add llvm specific pieces to our build. -include_directories("${LLVM_INCLUDE_DIRS}") -add_definitions("${LLVM_DEFINITIONS}") diff --git a/cmake/get_mlir.cmake b/cmake/get_mlir.cmake new file mode 100644 index 0000000..0b3d584 --- /dev/null +++ b/cmake/get_mlir.cmake @@ -0,0 +1,12 @@ +# Need MLIR so we can link against it. Either we automatically find this in the "default" place or +# it's found because we set up $MLIR_DIR. There should be no additions necessary here. +find_package(MLIR REQUIRED CONFIG) + +# Status messages about LLVM found. +message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") +message(STATUS "Using Config.cmake in: ${MLIR_DIR}") + +# Add mlir specific pieces to our build. +include_directories("${MLIR_INCLUDE_DIRS}") +include_directories("${LLVM_INCLUDE_DIRS}") +add_definitions("${MLIR_DEFINITIONS}") diff --git a/scripts/configureLLVM.sh b/scripts/configureLLVM.sh deleted file mode 100755 index 962346f..0000000 --- a/scripts/configureLLVM.sh +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/env bash -# CMPUT 415 LLVM configure script -# Author: Braedy Kuzma (braedy AT ualberta.ca) -# This script is designed to make sure that you have an llvm install compatible -# with what we'll be marking you against. I recommend editing this file and -# committing your changes so you guys have identical environments (helps with -# peer support). There's nothing wrong with having diverging environments and -# it doesn't affect me because I won't be using your version. - -# http://llvm.org/docs/CMake.html#llvm-specific-variables -# The above link has many options available in it. Here are some relevant ones -# chosen rather hastily that you might care about. -# LLVM_BUILD_TOOLS: -# Builds all of the tools by default in the "all" target. I've disabled it -# here so as to not clutter your build. -# LLVM_ENABLE_ASSERTIONS: -# Potential slow down, usually disabled in non-debug builds. I've enabled -# them permanently because they're useful for catching things. Feel free to -# disable if you want, but I'll be running with them on. -# LLVM_ENABLE_RTTI: -# Potential slow down, disabled in all builds by default. Enables run time -# type information. (e.g. dynamic_cast, typeid). Feel free to enable this if -# you need it for a bit of debugging but I'll be running with it off. See -# here: https://en.wikipedia.org/wiki/Run-time_type_information -# LLVM_PARALLEL_COMPILE_JOBS: -# Defines the maximum number of parallel compiling threads. Useful if your -# computer can't handle the compilation (linking is usually the issue and I'm -# not sure if this overrides the -j argument to make) -# LLVM_PARALLEL_LINK_JOBS: -# Defines the maximum number of parallel linking threads. Useful if your -# computer can't handle all of the linking (if your computer is lagging/ -# hanging, linking is probably the issue). -# LLVM_ENABLE_DOXYGEN: -# Enables local docs building. Requires doxygen? -LLVM_OPTIONS="-DLLVM_BUILD_TOOLS=ON -DLLVM_BUILD_TESTS=OFF -DLLVM_INCLUDE_TESTS=OFF -DLLVM_BUILD_EXAMPLES=OFF -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_OPTIMIZED_TABLEGEN=ON" - -# ------------------------- -# STEP 1: CHOOSE PATHS. -# Ideally these are absolute paths so you can run this script from anywhere. -# Read the note below the sudo trap (the trap is the whole if statement) -# about how the install directory will be used. -BAS_DIR="$HOME/llvm-project" -SRC_DIR="$BAS_DIR/llvm" # The directory inside where you've checked - # out llvm. -BLD_DIR="$BAS_DIR/build" # The directory llvm will be built to. -INS_DIR="$LLVM_INS" # DON'T TOUCH THIS. SEE README. - -# Sudo trap. -# This is here because I don't want you creating files as root and possibly -# mucking up something. I don't want to be responsible for that. -if [ "$EUID" -eq 0 ] - then echo "Don't run me as root. I create files you want to be able to delete." - exit -fi - -mkdir -p "$BLD_DIR" -cd "$BLD_DIR" - -# Personally I'm not a fan of installing this to my / partition because it can -# be large (debug can be >20GB depending on options) and cleanup is hard due to -# the lack of an uninstall target and root permissions. I've included the -# ability to use a custom install destination (INS_DIR -> LLVM_DIR) prefix for -# the llvm installation and one that uses the default prefix (i.e. -# /usr/{bin,lib}). The first one will not require root permissions (you own -# the install directory) but requires additional setup for your own project. -# The second one should install the required files somewhere that cmake can -# automatically find them in your personal project. - -# ------------------------- -# STEP 2: CHOOSE BUILD TYPE. -# UNCOMMENT ONLY ONE BUILD TYPE. I recommend release. - -# DEBUG BUILD (full debug build for use with debugger, quite large and -# unoptimised). -# BUILD="DEBUG" - -# MINSIZEREL BUILD (minimum size release build, faster, but prioritises small -# size). -# BUILD="MINSIZEREL" - -# RELWITHDEBINFO BUILD (builds optimised code with debug info, gives the -# debugger a chance but whole section can be optimised away). -# BUILD="RELWITHDEBINFO" - -# RELEASE BUILD (builds optimised code, you can still use this with a debugger -# but any time you step into llvm the compiler will be lost until you return). -BUILD="RELEASE" - -# ------------------------- -# STEP 3: CHOOSE INSTALL METHOD. -# If you want to install to system directories you will need to swap the comment -# below. This is at *your own risk* and is truly not necessary. -USE_INSTALL="-DCMAKE_INSTALL_PREFIX=$INS_DIR" -# USE_INSTALL="" - -# ------------------------- -# STEP 4: IGNORE THIS. -# These are things that need to be done without you thinking about it. - -# If we're on MacOS, we can't use the gold linker. It's an ELF-only tool. -USE_GOLD="" -unamestr=`uname` -if [[ "$unamestr" == 'Linux' ]]; then - USE_GOLD="-DLLVM_USE_LINKER=gold" -fi - -cmake "$SRC_DIR" -DCMAKE_BUILD_TYPE="$BUILD" -DLLVM_TARGETS_TO_BUILD=X86 $USE_GOLD $USE_INSTALL $LLVM_OPTIONS diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d552376..b2d6b2a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,10 +13,17 @@ add_dependencies(gazc antlr) # Find the libraries that correspond to the LLVM components # that we wish to use +set(LLVM_LINK_COMPONENTS Core Support) llvm_map_components_to_libnames(llvm_libs core) +get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) -# Add the antlr runtime and parser as libraries to link. -target_link_libraries(gazc parser antlr4-runtime ${llvm_libs}) +# Add the MLIR, LLVM, antlr runtime and parser as libraries to link. +target_link_libraries(gazc PRIVATE + parser + antlr4-runtime + ${llvm_libs} + ${dialect_libs} + ) # Symbolic link our executable to the base directory so we don't have to go searching for it. symlink_to_bin("gazc")