Skip to content

Commit

Permalink
Enable Link Time Optimization (LTO) with clang
Browse files Browse the repository at this point in the history
* Change the LLVM formula to build the LLVMgold plugin
* Pass "-flto" for osquery and all dependencies when building with clang on Unix
* Pass "-flto" to modules when they are built
* Set `ranlib` and `ar` to their LLVM verions when building on Linux
  • Loading branch information
artemdinaburg committed Jan 18, 2017
1 parent f9599d6 commit 32074b5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CMake/CMakeLibs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ macro(ADD_OSQUERY_MODULE TARGET)
# However it is only provided as an example for unit testing.
target_link_libraries(${TARGET} "-static-libstdc++")
endif()
if(NOT WINDOWS AND CMAKE_CXX_COMPILER MATCHES "clang")
#enable LTO builds of modules when building with clang on Unix
target_link_libraries(${TARGET} "-flto")
endif()
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CXX_COMPILE_FLAGS}")
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME ${TARGET})
endmacro(ADD_OSQUERY_MODULE)
Expand Down
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ if(NOT WIN32)
-fdata-sections
-ffunction-sections
)
if (CMAKE_CXX_COMPILER MATCHES "clang")
# Only enale LTO builds when using clang on Unix, for now
add_compile_options(
-flto
)
endif()
endif()

# osquery additional compiler flags added by CMake.
Expand Down Expand Up @@ -142,6 +148,20 @@ else()
endif()
endif()

if(UNIX AND NOT APPLE)
if (CMAKE_CXX_COMPILER MATCHES "clang")
# Use the LLVM versions of ar and ranlib to support LTO builds
find_program(LLVMRANLIB_EXECUTABLE "llvm-ranlib" ENV PATH)
find_program(LLVMAR_EXECUTABLE "llvm-ar" ENV PATH)
set(CMAKE_AR "${LLVMAR_EXECUTABLE}")
set(CMAKE_RANLIB "${LLVMRANLIB_EXECUTABLE}")
set(CMAKE_C_ARCHIVE_CREATE "${LLVMAR_EXECUTABLE} qc <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_CREATE "${LLVMAR_EXECUTABLE} qc <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_C_ARCHIVE_FINISH "${LLVMRANLIB_EXECUTABLE} <TARGET>")
set(CMAKE_CXX_ARCHIVE_FINISH "${CMAKE_C_ARCHIVE_FINISH}")
endif()
endif()

# make debug (environment variable from Makefile)
if(DEFINED ENV{DEBUG})
set(DEBUG TRUE)
Expand Down Expand Up @@ -267,6 +287,12 @@ if(WINDOWS)
endforeach(flag_var)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LTCG")
else()
if (CMAKE_CXX_COMPILER MATCHES "clang")
# Clang on Unix uses LTO; we also need to pass -flto when linking
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
endif()

endif()

if(NOT IS_DIRECTORY ${CMAKE_SOURCE_DIR}/third-party/sqlite3)
Expand Down
12 changes: 12 additions & 0 deletions osquery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ else()
-Wno-unused-parameter
-Wno-gnu-case-range
)
if (CMAKE_CXX_COMPILER MATCHES "clang")
# Enable LTO when building with clang on Unix
add_compile_options(
-flto
)
endif()
endif()

if(WINDOWS)
Expand Down Expand Up @@ -106,6 +112,12 @@ endif()

# The platform-specific SDK + core linker flags.
if(NOT WINDOWS)
if (CMAKE_CXX_COMPILER MATCHES "clang")
# When doing a clang on Unix LTO build, the link step also needs
# -flto on the comand line
ADD_OSQUERY_LINK_CORE("-flto")
ADD_OSQUERY_LINK_ADDITIONAL("-flto")
endif()
ADD_OSQUERY_LINK_CORE("-rdynamic")
endif()
if(APPLE)
Expand Down
19 changes: 14 additions & 5 deletions tools/provision/formula/llvm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class Llvm < AbstractOsqueryFormula
end
end

bottle do
root_url "https://osquery-packages.s3.amazonaws.com/bottles"
cellar :any_skip_relocation
sha256 "349e1f8f4831665c87245cdc727c34b23f7bbd6b6fccbd7c39a3c3a89b002767" => :x86_64_linux
end
# The current bottle does not build LLVMgold.so
#bottle do
# root_url "https://osquery-packages.s3.amazonaws.com/bottles"
# cellar :any_skip_relocation
# sha256 "349e1f8f4831665c87245cdc727c34b23f7bbd6b6fccbd7c39a3c3a89b002767" => :x86_64_linux
#end

head do
url "http://llvm.org/git/llvm.git"
Expand Down Expand Up @@ -87,6 +88,8 @@ class Llvm < AbstractOsqueryFormula

deprecated_option "rtti" => "with-rtti"

depends_on "binutils" if build.with? "clang"

# Apple's libstdc++ is too old to build LLVM
fails_with :gcc
fails_with :llvm
Expand Down Expand Up @@ -123,6 +126,12 @@ def install
args << "-DLLVM_ENABLE_EH=ON"
end

if build.with? "clang"
# build the LLVMGold plugin
binutils = Formula["binutils"].prefix/"include"
args << "-DLLVM_BINUTILS_INCDIR=#{binutils}"
end

args << "-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON" if build.with? "compiler-rt"
args << "-DLLVM_INSTALL_UTILS=On" if build.with? "utils"
args << "-DGCC_INSTALL_PREFIX=#{Formula["gcc"].prefix}"
Expand Down

0 comments on commit 32074b5

Please sign in to comment.