diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..16ecc41 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + # Workflow files stored in the + # default location of `.github/workflows` + directory: "/" + schedule: + interval: "monthly" + reviewers: + - "lzaoral" diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml new file mode 100644 index 0000000..db38eed --- /dev/null +++ b/.github/workflows/linux.yml @@ -0,0 +1,138 @@ +--- +name: Linux CI +on: + # TODO: rename to main when we remove the binary blobs from the git history + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + name: Ubuntu + strategy: + fail-fast: false + matrix: + include: + # Linux with GCC + - {os: 20.04, llvm: '6.0', compiler: gcc} + - {os: 20.04, llvm: 7, compiler: gcc} + - {os: 20.04, llvm: 8, compiler: gcc} + - {os: 20.04, llvm: 9, compiler: gcc} + - {os: 20.04, llvm: 10, compiler: gcc} + - {os: 20.04, llvm: 11, compiler: gcc} + - {os: 20.04, llvm: 12, compiler: gcc} + - {os: 22.04, llvm: 13, compiler: gcc} + - {os: 22.04, llvm: 14, compiler: gcc} + - {os: 22.04, llvm: 14, compiler: gcc, type: Debug} + + # Linux with Clang + - {os: 20.04, llvm: '6.0', compiler: clang} + - {os: 20.04, llvm: 7, compiler: clang} + - {os: 20.04, llvm: 8, compiler: clang} + - {os: 20.04, llvm: 9, compiler: clang} + - {os: 20.04, llvm: 10, compiler: clang} + - {os: 20.04, llvm: 11, compiler: clang} + - {os: 20.04, llvm: 12, compiler: clang} + - {os: 22.04, llvm: 13, compiler: clang} + - {os: 22.04, llvm: 14, compiler: clang} + - {os: 22.04, llvm: 14, compiler: clang, type: Debug} + + runs-on: ubuntu-${{matrix.os}} + env: + # for colours in ninja + CLICOLOR_FORCE: 1 + + steps: + - name: Checkout llvm2c + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + sudo apt update + sudo apt install cmake ninja-build clang-${{matrix.llvm}} \ + llvm-${{matrix.llvm}}-dev + + - name: Set the environment + id: env + run: | + # Set buildtype + if [[ "${{matrix.type}}" != "" ]]; then + echo "BUILD_TYPE=${{matrix.type}}" >> $GITHUB_ENV + else + echo "BUILD_TYPE=RelWithDebInfo" >> $GITHUB_ENV + fi + + # TODO: add sanitizer support to llvm2c's CMakeLists.txt + + # Build with sanitizers + CFLAGS="-fsanitize=address,undefined -g -fno-omit-frame-pointer" + CXXFLAGS="-fsanitize=address,undefined -g -fno-omit-frame-pointer" + LDFLAGS="-fsanitize=address,undefined" + + # Fail on UBSAN + CFLAGS="-fno-sanitize-recover=all $CFLAGS" + CXXFLAGS="-fno-sanitize-recover=all $CXXFLAGS" + + # Make UBSAN print whole stack traces + UBSAN_OPTIONS="print_stacktrace=1" + + # Fail on any compiler warning + CFLAGS="-Werror $CFLAGS" + CXXFLAGS="-Werror $CXXFLAGS" + + # Select compiler and set compiler flags + if [[ "${{matrix.compiler}}" = "clang" ]]; then + # Clang + CC="clang-${{matrix.llvm}}" + CXX="clang++-${{matrix.llvm}}" + + # Force coloured output + CFLAGS="-fcolor-diagnostics $CFLAGS" + CXXFLAGS="-fcolor-diagnostics $CXXFLAGS" + else + # GCC + CC="gcc" + CXX="g++" + + # Force coloured output + CFLAGS="-fdiagnostics-color $CFLAGS" + CXXFLAGS="-fdiagnostics-color $CXXFLAGS" + fi + + # Save the environment + echo "CC=$CC" >> $GITHUB_ENV + echo "CXX=$CXX" >> $GITHUB_ENV + echo "CFLAGS=$CFLAGS" >> $GITHUB_ENV + echo "CXXFLAGS=$CXXFLAGS" >> $GITHUB_ENV + echo "LDFLAGS=$LDFLAGS" >> $GITHUB_ENV + echo "UBSAN_OPTIONS=$UBSAN_OPTIONS" >> $GITHUB_ENV + + - name: '[Dynamic LLVM] Configure CMake project' + run: | + cmake -S. \ + -B_build \ + -GNinja \ + -DCMAKE_BUILD_TYPE:STRING="${{matrix.type}}" \ + -DLLVM_DIR:PATH="$(llvm-config-${{matrix.llvm}} --cmakedir)" + + - name: '[Dynamic LLVM] Build' + run: cmake --build _build + + - name: '[Dynamic LLVM] Run tests' + run: cmake --build _build --target check + + - name: '[Static LLVM] Re-configure CMake project' + run: | + cmake -S. \ + -B_build \ + -DLLVM_LINK_DYLIB:BOOL=OFF + cmake --build _build --target clean + + - name: '[Static LLVM] Build' + run: cmake --build _build + + - name: '[Static LLVM] Run tests' + run: cmake --build _build --target check diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml new file mode 100644 index 0000000..54ab499 --- /dev/null +++ b/.github/workflows/macos.yml @@ -0,0 +1,92 @@ +--- +name: macOS CI +on: + # TODO: rename to main when we remove the binary blobs from the git history + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + name: 'macOS (llvm: ${{matrix.llvm}}, build: ${{matrix.build}})' + strategy: + fail-fast: false + matrix: + llvm: [14] + build: [Debug, RelWithDebInfo] + + runs-on: macos-latest + env: + # for colours in ninja + CLICOLOR_FORCE: 1 + + steps: + - name: Checkout llvm2c + uses: actions/checkout@v3 + + - name: Install dependencies + run: brew install ninja llvm@${{matrix.llvm}} + + - name: Set environment + id: env + run: | + # TODO: add sanitizer support to llvm2c's CMakeLists.txt + + # Build with sanitizers + CFLAGS="-fsanitize=address,undefined -g -fno-omit-frame-pointer" + CXXFLAGS="-fsanitize=address,undefined -g -fno-omit-frame-pointer" + LDFLAGS="-fsanitize=address,undefined" + + # Fail on UBSAN + CFLAGS="-fno-sanitize-recover=all $CFLAGS" + CXXFLAGS="-fno-sanitize-recover=all $CXXFLAGS" + + # Make UBSAN print whole stack traces + UBSAN_OPTIONS="print_stacktrace=1" + + # Fail on any compiler warning + CFLAGS="-Werror $CFLAGS" + CXXFLAGS="-Werror $CXXFLAGS" + + # Force coloured output + CFLAGS="-fcolor-diagnostics $CFLAGS" + CXXFLAGS="-fcolor-diagnostics $CXXFLAGS" + + # Save the environment + echo "CC=$CC" >> $GITHUB_ENV + echo "CXX=$CXX" >> $GITHUB_ENV + echo "CFLAGS=$CFLAGS" >> $GITHUB_ENV + echo "CXXFLAGS=$CXXFLAGS" >> $GITHUB_ENV + echo "LDFLAGS=$LDFLAGS" >> $GITHUB_ENV + echo "UBSAN_OPTIONS=$UBSAN_OPTIONS" >> $GITHUB_ENV + + - name: '[Dynamic LLVM] Configure CMake project' + run: | + LLVM_CONFIG="$(brew --prefix llvm@${{matrix.llvm}})/bin/llvm-config" + cmake -S. \ + -B_build \ + -GNinja \ + -DCMAKE_BUILD_TYPE:STRING=${{matrix.build}} \ + -DLLVM_DIR:PATH="$("$LLVM_CONFIG" --cmakedir)" + + - name: '[Dynamic LLVM] Build' + run: cmake --build _build + + - name: '[Dynamic LLVM] Run tests' + run: cmake --build _build --target check + + - name: '[Static LLVM] Re-configure CMake project' + run: | + cmake -S. \ + -B_build \ + -DLLVM_LINK_DYLIB:BOOL=OFF + cmake --build _build --target clean + + - name: '[Static LLVM] Build' + run: cmake --build _build + + - name: '[Static LLVM] Run tests' + run: cmake --build _build --target check diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml new file mode 100644 index 0000000..eda9c2d --- /dev/null +++ b/.github/workflows/windows.yml @@ -0,0 +1,88 @@ +--- +name: Windows CI +on: + # TODO: rename to main when we remove the binary blobs from the git history + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + name: 'Windows (llvm: ${{matrix.llvm}}, build: ${{matrix.build}})' + strategy: + fail-fast: false + matrix: + llvm: [13] + build: [RelWithDebInfo] + + runs-on: windows-latest + steps: + - name: Checkout llvm2c + uses: actions/checkout@v3 + + - name: Install dependencies + run: | + # Use LLVM build from the authors of the zig language: + # https://github.com/ziglang/zig/wiki/Building-Zig-on-Windows#option-1-use-the-windows-zig-compiler-dev-kit + curl -o llvm.tar.xz "https://ziglang.org/deps/llvm%2bclang%2blld-${{matrix.llvm}}.0.1-x86_64-windows-msvc-release-mt.tar.xz" + + # workaround for https://github.com/actions/runner-images/issues/282 + $env:Path = "C:\\Program Files\\Git\\usr\\bin;" + $env:Path + + # extract twice to deal with dangling symlinks + tar xvf .\llvm.tar.xz + tar xvf .\llvm.tar.xz + + - name: Set environment + run: | + # TODO: add sanitizer support to llvm2c's CMakeLists.txt + + # Build with sanitizers (TODO: add UBSAN when MSVC supports it) + $CFLAGS = "/fsanitize=address" + $CXXFLAGS = "/fsanitize=address" + + # Make UBSAN print whole stack traces + $UBSAN_OPTIONS = "print_stacktrace=1" + + # Fail on any compiler warning + $CFLAGS += " /WX" + $CXXFLAGS += " /WX" + + # Save the environment + "CFLAGS=$CFLAGS" >> $env:GITHUB_ENV + "CXXFLAGS=$CXXFLAGS" >> $env:GITHUB_ENV + "UBSAN_OPTIONS=$UBSAN_OPTIONS" >> $env:GITHUB_ENV + + # TODO: LLVM cannot be build as a DLL on Windows. Uncomment when it is + # possible. + # + #- name: '[Dynamic LLVM] Configure CMake project' + # run: | + # $LLVM_CONFIG = Resolve-Path 'llvm*\bin\llvm-config.exe' + # cmake -S. ` + # -B_build ` + # -DLLVM_DIR:PATH="$(& "$LLVM_CONFIG" --cmakedir)" + # + #- name: '[Dynamic LLVM] Build' + # run: cmake --build _build --config ${{matrix.build}} + # + #- name: '[Dynamic LLVM] Run tests' + # run: cmake --build _build --target check + + - name: '[Static LLVM] Re-configure CMake project' + run: | + $LLVM_CONFIG = Resolve-Path 'llvm*\bin\llvm-config.exe' + cmake -S. ` + -B_build ` + -DLLVM_LINK_DYLIB:BOOL=OFF ` + -DLLVM_DIR:PATH="$(& "$LLVM_CONFIG" --cmakedir)" + + - name: '[Static LLVM] Build' + run: cmake --build _build --config ${{matrix.build}} + + # FIXME: enable tests on Windows + #- name: '[Static LLVM] Run tests' + # run: cmake --build _build --target check diff --git a/CMakeLists.txt b/CMakeLists.txt index b921002..1e4f027 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,11 @@ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "RelWithDebInfo" # -------------------------------------------------- set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -fno-rtti") +if(MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++14 /W4 /GR-") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -fno-rtti") +endif() # -------------------------------------------------- # LLVM @@ -46,6 +50,11 @@ include_directories(SYSTEM ${LLVM_INCLUDE_DIRS}) link_directories(${LLVM_LIBRARY_DIRS}) option(LLVM_LINK_DYLIB "Link with LLVM dynamically" ON) +if(MSVC) + message(WARNING "It is not possible to link with LLVM dynamically on Windows") + set(LLVM_LINK_DYLIB OFF) +endif() + if(LLVM_LINK_DYLIB) message(STATUS "LLVM linking: dynamic") set(llvm_libs LLVM) @@ -53,8 +62,12 @@ else() message(STATUS "LLVM linking: static") # Find the libraries that correspond to the LLVM components # that we wish to use - llvm_map_components_to_libnames(llvm_libs bitwriter core irreader linker - support) + llvm_map_components_to_libnames(llvm_libs irreader) + + if(MSVC) + add_compile_options($<$:/MTd> + $<$>:/MT>) + endif() endif() # -------------------------------------------------- @@ -64,25 +77,17 @@ set(llvm2c_sources main.cpp core/Block.cpp - core/Block.h core/Func.cpp - core/Func.h core/Program.cpp - core/Program.h expr/BinaryExpr.cpp - expr/BinaryExpr.h expr/Expr.cpp - expr/Expr.h expr/UnaryExpr.cpp - expr/UnaryExpr.h parser/ProgramParser.cpp - parser/ProgramParser.h parser/SimplifyingExprVisitor.cpp parser/addSignCasts.cpp parser/arrowify.cpp - parser/cfunc.h parser/computeGlobalVarsOrder.cpp parser/constval.cpp parser/createAllocas.cpp @@ -108,16 +113,12 @@ set(llvm2c_sources parser/parseMetadataTypes.cpp parser/parseStructDeclarations.cpp parser/parseStructItems.cpp - parser/passes.h parser/prepareBitcastUnion.cpp parser/refDeref.cpp parser/toinst.cpp - parser/toinst.h type/Type.cpp - type/Type.h type/TypeHandler.cpp - type/TypeHandler.h writer/CWriter.cpp writer/ExprWriter.cpp diff --git a/parser/createExpressions.cpp b/parser/createExpressions.cpp index 94751da..45eb97d 100644 --- a/parser/createExpressions.cpp +++ b/parser/createExpressions.cpp @@ -57,10 +57,6 @@ Expr* parseLLVMInstruction(const llvm::Instruction& ins, Program& program); static void parseInlineASM(const llvm::Instruction& ins, Func* func, Block* block); // FIXME: Remove this when LLVM 8 is the minimal version for LLVM2C! -static inline llvm::iterator_range args_wrapper(llvm::CallInst *CI) { - return make_range(CI->arg_begin(), CI->arg_end()); -} - static inline llvm::iterator_range args_wrapper(const llvm::CallInst *CI) { return make_range(CI->arg_begin(), CI->arg_end()); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5ec7948..31de37c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,7 +9,7 @@ if(N EQUAL 0) set(N 1) endif() -set(CTEST_OPTS -j${N} --output-on-failure --progress ${CTEST_OPTS}) +set(CTEST_OPTS -j${N} --output-on-failure --progress ${CTEST_OPTS} -C $) add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} ${CTEST_OPTS} USES_TERMINAL) add_dependencies(check llvm2c) @@ -34,10 +34,7 @@ endforeach() # -------------------------------------------------- # Tests # -------------------------------------------------- -set(LLVM2C $) -configure_file(run.in run.imd @ONLY) -file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/run - INPUT ${CMAKE_CURRENT_BINARY_DIR}/run.imd) +configure_file(run.in run @ONLY) set(tests asm @@ -56,4 +53,5 @@ set(tests foreach(test ${tests}) add_test(NAME ${test} COMMAND ./run ${test}) + set_tests_properties(${test} PROPERTIES ENVIRONMENT LLVM2C=$) endforeach() diff --git a/test/run.in b/test/run.in index 9dc0d2a..302974e 100755 --- a/test/run.in +++ b/test/run.in @@ -8,7 +8,7 @@ fi LABEL="$1" FOLDER=$LABEL -if ! [[ -e @LLVM2C@ ]]; then +if ! [[ -e "$LLVM2C" ]]; then echo "llvm2c not found!" exit 1 fi @@ -37,7 +37,7 @@ for f in @CMAKE_CURRENT_SOURCE_DIR@/inputs/$FOLDER/*.c; do @OPT@ -mem2reg $TEMPDIR/temp.ll -o $TEMPDIR/temp.ll fi - @LLVM2C@ $TEMPDIR/temp.ll --o $TEMPDIR/temp.c # >> /dev/null + "$LLVM2C" $TEMPDIR/temp.ll --o $TEMPDIR/temp.c # >> /dev/null if [[ $? != 0 ]]; then echo -e "\n\t[NOK] llvm2c failed to translate $f!"