Skip to content

Commit

Permalink
Initial setup (#1)
Browse files Browse the repository at this point in the history
* Update README
* Add a build system
* Use Clang
* Fix CMake to find clang
* Add a C
  • Loading branch information
certik authored Jan 5, 2023
1 parent 745ae40 commit e59f0f3
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 1 deletion.
46 changes: 46 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI

on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
branches:
- main


jobs:
Build:
name: LFortran CI (${{ matrix.python-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: mamba-org/provision-with-micromamba@main
with:
environment-file: environment_unix.yml

- uses: hendrikmuhs/ccache-action@main
with:
variant: sccache
key: ${{ github.job }}-${{ matrix.os }}

- name: Build (Linux / macOS)
shell: bash -l {0}
if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
run: |
./build.sh
- name: Test (Linux / macOS)
shell: bash -l {0}
if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
run: |
./src/lc --ast-dump examples/test.cpp
127 changes: 127 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(lc LANGUAGES C CXX)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release
CACHE STRING "Build type (Debug, Release)" FORCE)
endif ()
if (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR
CMAKE_BUILD_TYPE STREQUAL "Release"))
message("${CMAKE_BUILD_TYPE}")
message(FATAL_ERROR "CMAKE_BUILD_TYPE must be one of: Debug, Release (current value: '${CMAKE_BUILD_TYPE}')")
endif ()

if (NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17
CACHE STRING "C++ standard" FORCE)
endif ()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# LLVM
set(WITH_LLVM no CACHE BOOL "Build with LLVM support")
set(WITH_TARGET_AARCH64 no CACHE BOOL "Enable target AARCH64")
set(WITH_TARGET_X86 no CACHE BOOL "Enable target X86")
set(WITH_TARGET_WASM no CACHE BOOL "Enable target WebAssembly")
if (WITH_LLVM)
set(LFORTRAN_LLVM_COMPONENTS core support mcjit orcjit native asmparser asmprinter)
find_package(LLVM REQUIRED)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Always enable the native target
if ("${LLVM_NATIVE_ARCH}" STREQUAL "AArch64")
set(WITH_TARGET_AARCH64 yes)
endif()

if ("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
set(WITH_TARGET_X86 yes)
endif()

if (WITH_TARGET_AARCH64)
if (NOT ("${LLVM_TARGETS_TO_BUILD}" MATCHES "AArch64"))
message(FATAL_ERROR "The selected LLVM library doesn't have support for AArch64 targets")
endif()

list(APPEND LFORTRAN_LLVM_COMPONENTS aarch64info aarch64utils aarch64desc aarch64asmparser aarch64codegen aarch64disassembler)
add_definitions("-DHAVE_TARGET_AARCH64=1")
endif()

if (WITH_TARGET_X86)
if (NOT ("${LLVM_TARGETS_TO_BUILD}" MATCHES "X86"))
message(FATAL_ERROR "The selected LLVM library doesn't have support for X86 targets")
endif()

list(APPEND LFORTRAN_LLVM_COMPONENTS x86info x86desc x86codegen x86asmparser x86disassembler)
add_definitions("-DHAVE_TARGET_X86=1")
endif()

if (WITH_TARGET_WASM)
if (NOT ("${LLVM_TARGETS_TO_BUILD}" MATCHES "WebAssembly"))
message(FATAL_ERROR "The selected LLVM library doesn't have support for WebAssembly targets")
endif()

list(APPEND LFORTRAN_LLVM_COMPONENTS webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo)
add_definitions("-DHAVE_TARGET_WASM=1")
endif()

# Clang dependencies
list(APPEND LFORTRAN_LLVM_COMPONENTS windowsdriver)

llvm_map_components_to_libnames(llvm_libs ${LFORTRAN_LLVM_COMPONENTS})
unset(LFORTRAN_LLVM_COMPONENTS)

add_library(p::llvm INTERFACE IMPORTED)
set_property(TARGET p::llvm PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${LLVM_INCLUDE_DIRS})
#set_property(TARGET p::llvm PROPERTY INTERFACE_COMPILE_DEFINITIONS
# ${LLVM_DEFINITIONS})
#set_property(TARGET p::llvm PROPERTY INTERFACE_COMPILE_OPTIONS
# ${LLVM_DEFINITIONS})
set_property(TARGET p::llvm PROPERTY INTERFACE_COMPILE_OPTIONS
$<$<COMPILE_LANGUAGE:CXX>:${LFORTRAN_CXX_NO_RTTI_FLAG}>)
set_property(TARGET p::llvm PROPERTY INTERFACE_LINK_LIBRARIES
${llvm_libs})
if (MSVC)
# LLVM on Windows appends zlib shared library and we must provide
# a path to find it:
get_filename_component(mypath ${ZLIB_LIBRARY} DIRECTORY)
target_link_directories(p::llvm BEFORE INTERFACE ${mypath})
message(STATUS "ZLIB LIBRARY PATH: ${mypath}")
endif()
set(HAVE_LFORTRAN_LLVM yes)

find_package(clang 15 REQUIRED)
add_library(p::clang INTERFACE IMPORTED)
set_property(TARGET p::clang PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${CLANG_INCLUDE_DIRS})
set_property(TARGET p::clang PROPERTY INTERFACE_LINK_LIBRARIES
${CLANG_LIBRARIES})
endif()

enable_testing()

message("\n")
message("Configuration results")
message("---------------------")
message("C compiler : ${CMAKE_C_COMPILER}")
message("C++ compiler : ${CMAKE_CXX_COMPILER}")
message("Build type: ${CMAKE_BUILD_TYPE}")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
message("C compiler flags : ${CMAKE_C_FLAGS_DEBUG}")
message("C++ compiler flags : ${CMAKE_CXX_FLAGS_DEBUG}")
else ()
message("C compiler flags : ${CMAKE_C_FLAGS_RELEASE}")
message("C++ compiler flags : ${CMAKE_CXX_FLAGS_RELEASE}")
endif ()
message("Installation prefix: ${CMAKE_INSTALL_PREFIX}")
message("WITH_LLVM: ${WITH_LLVM}")
message("WITH_TARGET_AARCH64: ${WITH_TARGET_AARCH64}")
message("WITH_TARGET_X86: ${WITH_TARGET_X86}")
message("WITH_TARGET_WASM: ${WITH_TARGET_WASM}")


add_subdirectory(src)
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# lc
# lc

LC is the C frontend to LCompilers.

# Build

mamba env create -f environment_unix.yml
./build.sh
./src/lc --ast-dump examples/test.cpp
9 changes: 9 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -ex

cmake \
-DCMAKE_BUILD_TYPE=Debug \
-DWITH_LLVM=yes \
.
cmake --build . -j16
51 changes: 51 additions & 0 deletions cmake/Findclang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) 2016 Andrew Kelley
# This file is MIT licensed.
# See http://opensource.org/licenses/MIT

# CLANG_FOUND
# CLANG_INCLUDE_DIRS
# CLANG_LIBRARIES
# CLANG_LIBDIRS

find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h)

macro(FIND_AND_ADD_CLANG_LIB _libname_)
string(TOUPPER ${_libname_} _prettylibname_)
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_} NAMES_PER_DIR)
if(CLANG_${_prettylibname_}_LIB)
set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})
endif()
endmacro(FIND_AND_ADD_CLANG_LIB)

FIND_AND_ADD_CLANG_LIB(clangFrontendTool)
FIND_AND_ADD_CLANG_LIB(clangCodeGen)
FIND_AND_ADD_CLANG_LIB(clangFrontend)
FIND_AND_ADD_CLANG_LIB(clangDriver)
FIND_AND_ADD_CLANG_LIB(clangSerialization)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerFrontend)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCheckers)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCore)
FIND_AND_ADD_CLANG_LIB(clangAnalysis)
FIND_AND_ADD_CLANG_LIB(clangASTMatchers)
FIND_AND_ADD_CLANG_LIB(clangCrossTU)
FIND_AND_ADD_CLANG_LIB(clangAST)
FIND_AND_ADD_CLANG_LIB(clangParse)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangBasic)
FIND_AND_ADD_CLANG_LIB(clangEdit)
FIND_AND_ADD_CLANG_LIB(clangLex)
FIND_AND_ADD_CLANG_LIB(clangARCMigrate)
FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend)
FIND_AND_ADD_CLANG_LIB(clangRewrite)
FIND_AND_ADD_CLANG_LIB(clangIndex)
FIND_AND_ADD_CLANG_LIB(clangToolingCore)
FIND_AND_ADD_CLANG_LIB(clangToolingSyntax)
FIND_AND_ADD_CLANG_LIB(clangTooling)
FIND_AND_ADD_CLANG_LIB(clangExtractAPI)
FIND_AND_ADD_CLANG_LIB(clangSupport)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(clang DEFAULT_MSG CLANG_LIBRARIES CLANG_INCLUDE_DIRS)

mark_as_advanced(CLANG_INCLUDE_DIRS CLANG_LIBRARIES CLANG_LIBDIRS)
10 changes: 10 additions & 0 deletions environment_unix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: lc
channels:
- conda-forge
dependencies:
- cmake
- llvmdev=15.0.6
- clangdev=15.0.6
- make
- zlib
- git
4 changes: 4 additions & 0 deletions examples/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int f(int x) {
int result = (x / 42);
return result;
}
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(lc lc.cpp)
target_link_libraries(lc p::clang p::llvm)
Loading

0 comments on commit e59f0f3

Please sign in to comment.