From 68dee1250d0d2d7d8914a7b9cfdbebbdca33679b Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Mon, 24 Jun 2019 10:35:23 +0200 Subject: [PATCH] Port the build scripts to CMake 3.5, refactor them. * Use the official LLVMConfig rather than our FindLLVM. * Add target rule for Ragel. * Remove useless dependencies by only compiling for the native target. * Use target rules. --- CMakeLists.txt | 26 +++---------------- cmake/FindLLVM.cmake | 54 ---------------------------------------- cmake/FindRagel.cmake | 26 +++++++++++++++++++ cmake/MonicelliDoc.cmake | 22 ++++++++++++++++ src/CMakeLists.txt | 52 ++++++++++++++------------------------ src/asmgen.cpp | 8 +++--- 6 files changed, 73 insertions(+), 115 deletions(-) delete mode 100644 cmake/FindLLVM.cmake create mode 100644 cmake/MonicelliDoc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 75063b4..dc89a43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,31 +1,13 @@ # Copyright 2017 the Monicelli project authors. All rights reserved. # Use of this source code is governed by a GPLv3 license, see LICENSE.txt. -cmake_minimum_required(VERSION 3.0) -project(Monicelli) +cmake_minimum_required(VERSION 3.5) +project(Monicelli CXX) -set(CMAKE_BUILD_TYPE Release) -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") +list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") -find_package(Doxygen) +include(MonicelliDoc) add_subdirectory(src) -if (DOXYGEN_FOUND) - set(DOXYGEN_CONFIG ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) - - configure_file( - ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in - ${DOXYGEN_CONFIG} - @ONLY - ) - - add_custom_target(doc - DEPENDS ${DOXYGEN_CONFIG} - COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_CONFIG} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - VERBATIM - ) -endif() - install(FILES README.md LICENSE.txt DESTINATION doc/monicelli) diff --git a/cmake/FindLLVM.cmake b/cmake/FindLLVM.cmake deleted file mode 100644 index 118ced0..0000000 --- a/cmake/FindLLVM.cmake +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2017 the Monicelli project authors. All rights reserved. -# Use of this source code is governed by a GPLv3 license, see LICENSE.txt. - -find_program(LLVM_CONFIG llvm-config) - -if (LLVM_CONFIG STREQUAL "LLVM_CONFIG-NOTFOUND") - message(FATAL_ERROR "Please install the LLVM dev package to compile Monicelli.") -else() - message(STATUS "Found llvm-config: ${LLVM_CONFIG}") -endif() - -execute_process( - COMMAND ${LLVM_CONFIG} --version - OUTPUT_VARIABLE LLVM_VERSION - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -set(TARGET_LLVM_VERSION "6.0.0") - -if (NOT LLVM_VERSION STREQUAL ${TARGET_LLVM_VERSION}) - message(WARNING "Expected LLVM ${TARGET_LLVM_VERSION}, found ${LLVM_VERSION}, build may fail.") -endif() - -execute_process( - COMMAND ${LLVM_CONFIG} --includedir - OUTPUT_VARIABLE LLVM_INCLUDE_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -execute_process( - COMMAND ${LLVM_CONFIG} --libdir - OUTPUT_VARIABLE LLVM_LIBRARY_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -execute_process( - COMMAND ${LLVM_CONFIG} --cxxflags - OUTPUT_VARIABLE LLVM_CXXFLAGS - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -execute_process( - COMMAND ${LLVM_CONFIG} --libs all - OUTPUT_VARIABLE LLVM_MODULE_LIBS - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -execute_process( - COMMAND ${LLVM_CONFIG} --system-libs - OUTPUT_VARIABLE LLVM_SYSTEM_LIBS - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -set(LLVM_LIBS ${LLVM_MODULE_LIBS} ${LLVM_SYSTEM_LIBS}) diff --git a/cmake/FindRagel.cmake b/cmake/FindRagel.cmake index d50216e..df71b5e 100644 --- a/cmake/FindRagel.cmake +++ b/cmake/FindRagel.cmake @@ -8,3 +8,29 @@ if (${RAGEL} STREQUAL "RAGEL-NOTFOUND") else() message(STATUS "Found ragel: ${RAGEL}") endif() + +function(add_ragel_library name source header) + set(generated_cpp "${source}.cpp") + + add_custom_command( + OUTPUT "${generated_cpp}" + MAIN_DEPENDENCY "${source}" + DEPENDS "${header}" + COMMAND ${RAGEL} -G2 "${CMAKE_CURRENT_SOURCE_DIR}/${source}" -o "${generated_cpp}" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + VERBATIM + ) + + add_library(${name} "${generated_cpp}") + + set_target_properties(${name} + PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED true + ) + + # The lexer uses implicit fallthroughs all over, but it's OK. + target_compile_options(${name} PRIVATE -Wno-implicit-fallthrough) + + target_include_directories(${name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) +endfunction() diff --git a/cmake/MonicelliDoc.cmake b/cmake/MonicelliDoc.cmake new file mode 100644 index 0000000..07ce835 --- /dev/null +++ b/cmake/MonicelliDoc.cmake @@ -0,0 +1,22 @@ +# Copyright 2019 the Monicelli project authors. All rights reserved. +# Use of this source code is governed by a GPLv3 license, see LICENSE.txt. + +find_package(Doxygen) + +if (DOXYGEN_FOUND) + set(MONICELLI_DOXYGEN_CONFIG ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile) + + configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in + ${MONICELLI_DOXYGEN_CONFIG} + @ONLY + ) + + add_custom_target(doc + DEPENDS ${MONICELLI_DOXYGEN_CONFIG} + COMMAND ${DOXYGEN_EXECUTABLE} ${MONICELLI_DOXYGEN_CONFIG} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + VERBATIM + ) +endif() + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 90875a5..20be759 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,41 +1,12 @@ # Copyright 2017 the Monicelli project authors. All rights reserved. # Use of this source code is governed by a GPLv3 license, see LICENSE.txt. -find_package(LLVM REQUIRED) +find_package(LLVM REQUIRED CONFIG) find_package(Ragel REQUIRED) -option(ENABLE_LINKER "Enable the Monicelli linker. Requires POSIX." ON) +option(MONICELLI_ENABLE_LINKER "Enable the Monicelli linker. Requires POSIX." ON) -if (ENABLE_LINKER) - add_definitions(-DMONICELLI_ENABLE_LINKER) -endif() - -add_definitions( - -std=c++11 - -Wall -Wextra -Werror - # The lexer uses implicit fallthroughs all over, but it's OK. - -Wno-implicit-fallthrough - ${LLVM_CXXFLAGS} - -g -O2 -UNDEBUG -) - -include_directories( - ${CMAKE_CURRENT_SOURCE_DIR} - ${LLVM_INCLUDE_DIR} -) - -link_directories( - ${LLVM_LIBRARY_DIR} -) - -add_custom_command( - OUTPUT lexer.rl.cpp - MAIN_DEPENDENCY lexer.rl - DEPENDS lexer.h - COMMAND ${RAGEL} -G2 "${CMAKE_CURRENT_SOURCE_DIR}/lexer.rl" -o lexer.rl.cpp - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" - VERBATIM -) +add_ragel_library(lexer lexer.rl lexer.h) add_executable(mcc main.cpp @@ -49,7 +20,6 @@ add_executable(mcc parser.cpp lexer.cpp lexer.def - "${CMAKE_CURRENT_BINARY_DIR}/lexer.rl.cpp" options.cpp errors.cpp support.cpp @@ -59,6 +29,20 @@ add_executable(mcc operators.def ) -target_link_libraries(mcc ${LLVM_LIBS}) +set_target_properties(mcc + PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED true +) + +target_compile_options(mcc PRIVATE -Wall -Wextra -Werror) + +if (MONICELLI_ENABLE_LINKER) + target_compile_definitions(mcc PRIVATE MONICELLI_ENABLE_LINKER) +endif() + +# TODO expand this line to support compiling on other architectures and cross-compilation. +llvm_config(mcc core support x86codegen x86asmparser) +target_link_libraries(mcc PRIVATE lexer) install(TARGETS mcc RUNTIME DESTINATION bin) diff --git a/src/asmgen.cpp b/src/asmgen.cpp index 7b4a2a2..7b69d2b 100644 --- a/src/asmgen.cpp +++ b/src/asmgen.cpp @@ -24,11 +24,9 @@ namespace monicelli { void registerTargets() { - llvm::InitializeAllTargetInfos(); - llvm::InitializeAllTargets(); - llvm::InitializeAllTargetMCs(); - llvm::InitializeAllAsmParsers(); - llvm::InitializeAllAsmPrinters(); + llvm::InitializeNativeTarget(); + llvm::InitializeNativeTargetAsmParser(); + llvm::InitializeNativeTargetAsmPrinter(); } llvm::TargetMachine* getTargetMachine(const std::string& triple, const std::string& cpu,