Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate CDT to LLVM 16.0.6 #327

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "cdt-llvm"]
path = cdt-llvm
url = https://github.com/AntelopeIO/cdt-llvm
[submodule "libraries/libc/cdt-musl"]
path = libraries/libc/cdt-musl
url = https://github.com/AntelopeIO/cdt-musl
Expand All @@ -10,3 +7,7 @@
[submodule "libraries/native/softfloat"]
path = libraries/native/softfloat
url = https://github.com/AntelopeIO/berkeley-softfloat-3
[submodule "llvm"]
path = llvm
url = https://github.com/llvm/llvm-project
branch = release/16.x
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.21)
project(cdt VERSION 4.2.0)


# Sanity check our source directory to make sure that we are not trying to
# generate an in-source build, and to make
Expand All @@ -11,10 +13,8 @@ This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
Please delete them.")
endif()

project(cdt)

set(VERSION_MAJOR 4)
set(VERSION_MINOR 2)
set(VERSION_MAJOR 5)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION_SUFFIX "dev")

Expand Down Expand Up @@ -45,6 +45,7 @@ endif()

include(modules/ClangExternalProject.txt)
include(modules/ToolsExternalProject.txt)
include(cdt-llvm-extensions/LLVMPatch.txt)

set(WASM_SDK_BUILD true)

Expand Down Expand Up @@ -89,7 +90,7 @@ configure_file(${CMAKE_SOURCE_DIR}/scripts/generate_rpm.sh ${CMAKE_BINARY_DIR}/p
configure_file(${CMAKE_SOURCE_DIR}/scripts/generate_tarball.sh ${CMAKE_BINARY_DIR}/packages/generate_tarball.sh COPYONLY)

# add licenses
configure_file(${CMAKE_SOURCE_DIR}/cdt-llvm/LICENSE.TXT ${CMAKE_BINARY_DIR}/licenses/llvm.license COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/llvm/LICENSE.TXT ${CMAKE_BINARY_DIR}/licenses/llvm.license COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/libraries/boost/boost.license ${CMAKE_BINARY_DIR}/licenses/boost.license COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/libraries/meta_refl/LICENSE ${CMAKE_BINARY_DIR}/licenses/meta_refl.license COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/tools/external/wabt/LICENSE ${CMAKE_BINARY_DIR}/licenses/wabt.license COPYONLY)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Contract Development Toolkit (CDT) is a C/C++ toolchain targeting WebAssembly (WASM) and a set of tools to facilitate development of smart contracts written in C/C++ that are meant to be deployed to an [Antelope](https://github.com/AntelopeIO/) blockchain.

In addition to being a general purpose WebAssembly toolchain, specific features and optimizations are available to support building Antelope-based smart contracts. This new toolchain is built around [Clang 9](https://github.com/AntelopeIO/cdt-llvm), which means that CDT inherits the optimizations and analyses from that version of LLVM, but as the WASM target is still considered experimental, some optimizations are incomplete or not available.
In addition to being a general purpose WebAssembly toolchain, specific features and optimizations are available to support building Antelope-based smart contracts. This new toolchain is built around [Clang 16](https://github.com/llvm/llvm-project/tree/release/16.x), which means that CDT inherits the optimizations and analyses from that version of LLVM, but as the WASM target is still considered experimental, some optimizations are incomplete or not available.

## Repo organization

Expand Down
1 change: 0 additions & 1 deletion cdt-llvm
Submodule cdt-llvm deleted from 6650e8
118 changes: 118 additions & 0 deletions cdt-llvm-extensions/LLVMPatch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
cmake_minimum_required(VERSION 3.21)

set(EXTENSION_SOURCE ${CMAKE_SOURCE_DIR}/cdt-llvm-extensions)
set(LLVM_DEST ${CMAKE_SOURCE_DIR}/llvm)

# Get LLVM Major Version
# Function to extract version from a line
function(parse_version_line file_path version_string output_var)
if(EXISTS ${file_path})
file(READ ${file_path} input_text)
string(REGEX MATCH "set\\(${version_string} ([0-9]+)\\)" match "${input_text}")
if(match)
set(${output_var} "${CMAKE_MATCH_1}" PARENT_SCOPE)
else()
message(FATAL_ERROR "Failed to parse LLVM major version")
endif()
endif()
endfunction()

# function call to get version number
parse_version_line(${LLVM_DEST}/llvm/CMakeLists.txt LLVM_VERSION_MAJOR PARSE_LLVM_MAJOR_VERSION)

if(NOT PARSE_LLVM_MAJOR_VERSION EQUAL 16)
message(FATAL_ERROR, "Only Supporting LLVM version 16, detected version ${PARSE_LLVM_MAJOR_VERSION} exiting")
endif()

message("-- Starting LLVM patches (LLVMPatch.txt)")

### Function to set experimental targets
include(${EXTENSION_SOURCE}/SetLLVMTargets.txt)
set_llvm_targets()

### Patches CLANG
# adds definition and declaration for Eosio Types
# support for C and C++
file(COPY_FILE ${EXTENSION_SOURCE}/clang/include/clang/AST/DeclCXX.h ${LLVM_DEST}/clang/include/clang/AST/DeclCXX.h)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/include/clang/AST/Decl.h ${LLVM_DEST}/clang/include/clang/AST/Decl.h)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/include/clang/Basic/AttrDocs.td ${LLVM_DEST}/clang/include/clang/Basic/AttrDocs.td)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/include/clang/Basic/Attr.td ${LLVM_DEST}/clang/include/clang/Basic/Attr.td)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/lib/AST/Decl.cpp ${LLVM_DEST}/clang/lib/AST/Decl.cpp)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/lib/Basic/Targets/WebAssembly.h ${LLVM_DEST}/clang/lib/Basic/Targets/WebAssembly.h)

### Patches CLANG
# implements decl-related attribute processing
# coordinates the per-module state used while generating code
# top level handling of macro expansion for the preprocessor
file(COPY_FILE ${EXTENSION_SOURCE}/clang/lib/CodeGen/CodeGenModule.cpp ${LLVM_DEST}/clang/lib/CodeGen/CodeGenModule.cpp)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/lib/Lex/PPMacroExpansion.cpp ${LLVM_DEST}/clang/lib/Lex/PPMacroExpansion.cpp)
file(COPY_FILE ${EXTENSION_SOURCE}/clang/lib/Sema/SemaDeclAttr.cpp ${LLVM_DEST}/clang/lib/Sema/SemaDeclAttr.cpp)

### Patches WASM
# creates, allocates, and populates structures needed to generate WASM
# includes eosioABI eosioNotify eosioACtions
file(COPY_FILE ${EXTENSION_SOURCE}/lld/wasm/InputFiles.cpp ${LLVM_DEST}/lld/wasm/InputFiles.cpp)
file(COPY_FILE ${EXTENSION_SOURCE}/lld/wasm/InputFiles.h ${LLVM_DEST}/lld/wasm/InputFiles.h)

### Patches LLVM
# defines all attributes for LLVM
# defines WASM Object
# entry point to LTO (link time optimization)
file(COPY_FILE ${EXTENSION_SOURCE}/llvm/include/llvm/IR/Attributes.td ${LLVM_DEST}/llvm/include/llvm/IR/Attributes.td)
file(COPY_FILE ${EXTENSION_SOURCE}/llvm/include/llvm/LTO/LTO.h ${LLVM_DEST}/llvm/include/llvm/LTO/LTO.h)
file(COPY_FILE ${EXTENSION_SOURCE}/llvm/include/llvm/Object/Wasm.h ${LLVM_DEST}/llvm/include/llvm/Object/Wasm.h)

### Patches WebAssembly
# WebAssembly Object
# WebAssembly LLVM assembly writer
file(COPY_FILE ${EXTENSION_SOURCE}/llvm/lib/Object/WasmObjectFile.cpp ${LLVM_DEST}/llvm/lib/Object/WasmObjectFile.cpp)
file(COPY_FILE ${EXTENSION_SOURCE}/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp ${LLVM_DEST}/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp)

### Add Customer Eosio Parsers into LLVM source
# adds directories to Transforms cmake
# registered as added step to parsing
# Impliment as PassManger to upgrade to LLVM 17+
if(EXISTS ${LLVM_DEST}/llvm/lib/Transforms)
file(COPY ${EXTENSION_SOURCE}/llvm/lib/Transforms/EosioApply DESTINATION ${LLVM_DEST}/llvm/lib/Transforms )
file(COPY ${EXTENSION_SOURCE}/llvm/lib/Transforms/EosioSoftfloat DESTINATION ${LLVM_DEST}/llvm/lib/Transforms )
# Add new targets to cmake if needed
set(LLVM_LIB_TRANSFORMS_CMAKE ${LLVM_DEST}/llvm/lib/Transforms/CMakeLists.txt)
file(READ ${LLVM_LIB_TRANSFORMS_CMAKE} LLVM_LIB_CMAKE_CONTENTS)
if(NOT LLVM_LIB_CMAKE_CONTENTS MATCHES "EosioApply")
file(APPEND ${LLVM_LIB_TRANSFORMS_CMAKE} "add_subdirectory(EosioApply)\n")
endif()
if(NOT LLVM_LIB_CMAKE_CONTENTS MATCHES "EosioSoftfloat")
file(APPEND ${LLVM_LIB_TRANSFORMS_CMAKE} "add_subdirectory(EosioSoftfloat)\n")
endif()
else()
message(FATAL_ERROR "Cannot find LLVM source please load git submodules and try again")
endif()

### Modifies AddLLD cmake targeting cdt tooling
# must be a better way to inlcude these
# - read in AddLLD cmake
# - add new targets
# - then write back file
if (EXISTS ${LLVM_DEST}/lld/cmake/modules/AddLLD.cmake)
file(READ ${LLVM_DEST}/lld/cmake/modules/AddLLD.cmake INPUT_ADDLLD_CMAKE)
string(FIND "${INPUT_ADDLLD_CMAKE}"
"set_target_properties(\${name} PROPERTIES FOLDER \"lld libraries\")"
IDX_ADDLLD_TARGET_PROPS)
string(FIND ${INPUT_ADDLLD_CMAKE}
"\${CDT_TOOLS_SOURCE_DIR}/jsoncons/include"
IDX_ADDLLD_TOOLS_INCLUDE)
# Found target properties did not found tools/jsoncons/include
# Only add if target string exists
# AND do not add if tools/jsonccons/include already exists
if(IDX_ADDLLD_TARGET_PROPS GREATER 0 AND IDX_ADDLLD_TOOLS_INCLUDE LESS 0)
# Two space nesting
# not sure about relative paths
string(REPLACE
" set_target_properties(\${name} PROPERTIES FOLDER \"lld libraries\")"
" set_target_properties(\${name} PROPERTIES FOLDER \"lld libraries\")\n set(CDT_TOOLS_SOURCE_DIR \"${CMAKE_SOURCE_DIR}/tools\")\n target_compile_options(\${name} PUBLIC -fexceptions -Wno-reorder -Wno-sign-compare -Wno-unused-local-typedefs -fno-omit-frame-pointer)\n target_include_directories(\${name} PUBLIC \${CDT_TOOLS_SOURCE_DIR}/jsoncons/include)\n target_include_directories(\${name} PUBLIC \${CDT_TOOLS_SOURCE_DIR}/include)\n"
OUTPUT_ADDLLD_CMAKE
"${INPUT_ADDLLD_CMAKE}")
file(WRITE ${LLVM_DEST}/lld/cmake/modules/AddLLD.cmake "${OUTPUT_ADDLLD_CMAKE}")
endif()
endif()
message("-- Completed LLVM patches")
46 changes: 46 additions & 0 deletions cdt-llvm-extensions/SetLLVMTargets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### set targets
# passing as arguments to cmake doesn't work with current build setup
# when passing as argument LLVM tries to build WebAssembly target that doesn't exist
# this happens as LLVM is looking for WebAssembly in the wrong step
function(set_llvm_targets)
if(EXISTS ${LLVM_DEST}/llvm/CMakeLists.txt)
file(READ ${LLVM_DEST}/llvm/CMakeLists.txt ROOT_LLVM_CMAKE_CONTENTS)
# validate strings we want to replace exist
string(FIND "${ROOT_LLVM_CMAKE_CONTENTS}"
"set(LLVM_TARGETS_TO_BUILD \"all\""
IDX_LLVM_BUILD_TARGETS)
string(FIND "${ROOT_LLVM_CMAKE_CONTENTS}"
"set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD \"\""
IDX_LLVM_EXP_TARGETS)

# debug purposes check if replacements already made
string(FIND "${ROOT_LLVM_CMAKE_CONTENTS}"
"set(LLVM_TARGETS_TO_BUILD \"X86\""
IDX_RESOLVED_LLVM_BUILD_TARGETS)
string(FIND "${ROOT_LLVM_CMAKE_CONTENTS}"
"set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD \"WebAssembly\""
IDX_RESOLVED_LLVM_EXP_TARGETS)

# only replace if strings match and replace needed
if(IDX_LLVM_BUILD_TARGETS GREATER 0 AND IDX_LLVM_EXP_TARGETS GREATER 0)
string(REPLACE
"set(LLVM_TARGETS_TO_BUILD \"all\""
"set(LLVM_TARGETS_TO_BUILD \"X86\""
NEW_ROOT_LLVM_CMAKE_CONTENTS
"${ROOT_LLVM_CMAKE_CONTENTS}")
string(REPLACE
"set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD \"\""
"set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD \"WebAssembly\""
NEW_ROOT_LLVM_CMAKE_CONTENTS
"${NEW_ROOT_LLVM_CMAKE_CONTENTS}")
file(WRITE ${LLVM_DEST}/llvm/CMakeLists.txt "${NEW_ROOT_LLVM_CMAKE_CONTENTS}")
else()
# cannot find strings to replace, already replaced
if(NOT IDX_RESOLVED_LLVM_BUILD_TARGETS GREATER 0 AND NOT IDX_RESOLVED_LLVM_EXP_TARGETS GREATER 0)
message(FATAL_ERROR,"Cannot find EXPERIMENTAL Targets in ${LLVM_DEST}/llvm/CMakeLists.txt")
endif()
endif()
else()
message(FATAL_ERROR,"Need to set targets, yet file does not exist ${LLVM_DEST}/llvm/CMakeLists.txt")
endif()
endfunction()
Loading
Loading