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

Ship with LTO default libs on Linux & macOS #2640

Merged
merged 7 commits into from
Apr 17, 2018
Merged
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
16 changes: 12 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,21 @@ commonSteps: &commonSteps
cd ..
altLibSuffix="32"
if [ "$CI_OS" = "osx" ]; then
altLibSuffix=""
altLibSuffix=""
fi
echo 'void main() { import std.stdio; writeln("Hello world, ", size_t.sizeof * 8, " bits"); }' > hello.d
$LDC_INSTALL_DIR/bin/ldc2 hello.d -m64 -of=hello64 -link-defaultlib-shared -L-Wl,-rpath,$LDC_INSTALL_DIR/lib
$LDC_INSTALL_DIR/bin/ldc2 hello.d -m32 -of=hello32 -link-defaultlib-shared -L-Wl,-rpath,$LDC_INSTALL_DIR/lib$altLibSuffix
./hello64
$LDC_INSTALL_DIR/bin/ldc2 hello.d -m32 -of=hello32 -link-defaultlib-shared -L-Wl,-rpath,$LDC_INSTALL_DIR/lib$altLibSuffix
./hello32
- run:
name: Hello world integration test with LTO
command: |
cd ..
$LDC_INSTALL_DIR/bin/ldc2 hello.d -of=hello_thin -flto=thin -defaultlib=phobos2-ldc-lto,druntime-ldc-lto
./hello_thin
$LDC_INSTALL_DIR/bin/ldc2 hello.d -of=hello_full -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto
./hello_full
- run:
name: Build dub
command: |
Expand Down Expand Up @@ -245,7 +253,7 @@ jobs:
- CI_OS: linux
- LLVM_VERSION: 6.0.0
- HOST_LDC_VERSION: 1.8.0
- EXTRA_CMAKE_FLAGS: "-DMULTILIB=ON -DCMAKE_EXE_LINKER_FLAGS=-static-libstdc++ -DLDC_INSTALL_LTOPLUGIN=ON -DLDC_INSTALL_LLVM_RUNTIME_LIBS=ON"
- EXTRA_CMAKE_FLAGS: "-DMULTILIB=ON -DBUILD_LTO_LIBS=ON -DD_FLAGS='-w;-flto=thin' -DCMAKE_EXE_LINKER_FLAGS=-static-libstdc++ -DLDC_INSTALL_LTOPLUGIN=ON -DLDC_INSTALL_LLVM_RUNTIME_LIBS=ON"
- DUB_VERSION: v1.7.2
build-osx:
<<: *commonSteps
Expand All @@ -258,7 +266,7 @@ jobs:
- LLVM_VERSION: 6.0.0
- HOST_LDC_VERSION: 1.8.0
- BOOTSTRAP_CMAKE_FLAGS: "-DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS=-lc++"
- EXTRA_CMAKE_FLAGS: "-DMULTILIB=ON -DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS=-lc++"
- EXTRA_CMAKE_FLAGS: "-DMULTILIB=ON -DBUILD_LTO_LIBS=ON -DD_FLAGS='-w;-flto=thin' -DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS=-lc++"
- DUB_VERSION: v1.7.2

workflows:
Expand Down
11 changes: 7 additions & 4 deletions driver/toobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,12 @@ void writeModule(llvm::Module *m, const char *filename) {
};

// write LLVM bitcode
if (global.params.output_bc || (doLTO && outputObj)) {
std::string bcpath =
(doLTO && outputObj) ? filename : replaceExtensionWith(global.bc_ext);
const bool emitBitcodeAsObjectFile =
doLTO && outputObj && !global.params.output_bc;
if (global.params.output_bc || emitBitcodeAsObjectFile) {
std::string bcpath = emitBitcodeAsObjectFile
? filename
: replaceExtensionWith(global.bc_ext);
Logger::println("Writing LLVM bitcode to: %s\n", bcpath.c_str());
std::error_code errinfo;
llvm::raw_fd_ostream bos(bcpath.c_str(), errinfo, llvm::sys::fs::F_None);
Expand Down Expand Up @@ -436,7 +439,7 @@ void writeModule(llvm::Module *m, const char *filename) {
}
}

if (outputObj && !doLTO) {
if (outputObj && !emitBitcodeAsObjectFile) {
writeObjectFile(m, filename);
if (useIR2ObjCache) {
cache::cacheObjectFile(filename, moduleHash);
Expand Down
103 changes: 52 additions & 51 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ endif()
set(DMDFE_VERSION ${D_VERSION}.${DMDFE_MINOR_VERSION}.${DMDFE_PATCH_VERSION})

set(MULTILIB OFF CACHE BOOL "Build both 32/64 bit runtime libraries")
set(BUILD_BC_LIBS OFF CACHE BOOL "Build the runtime as LLVM bitcode libraries")
set(BUILD_LTO_LIBS OFF CACHE BOOL "Also build the runtime as LLVM bitcode libraries for LTO")
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include/d CACHE PATH "Path to install D modules to")
set(BUILD_SHARED_LIBS AUTO CACHE STRING "Whether to build the runtime as a shared library (ON|OFF|BOTH)")
set(D_FLAGS -w CACHE STRING "Runtime D compiler flags, separated by ';'")
Expand Down Expand Up @@ -346,7 +346,7 @@ endforeach()
# The paths of the output files are appended to outlist_o and outlist_bc, respectively.
macro(dc src_files src_basedir d_flags output_basedir all_at_once outlist_o outlist_bc)
set(dc_flags -c --output-o ${d_flags})
if(BUILD_BC_LIBS)
if(BUILD_LTO_LIBS)
list(APPEND dc_flags --output-bc)
endif()

Expand All @@ -368,33 +368,42 @@ macro(dc src_files src_basedir d_flags output_basedir all_at_once outlist_o outl
endif()

set(output_o ${output_basedir}/${output_root}${CMAKE_C_OUTPUT_EXTENSION})
set(output_bc ${output_basedir}/${output_root}.bc)
set(output_bc ${output_basedir}/${output_root}.bc${CMAKE_C_OUTPUT_EXTENSION})
list(APPEND ${outlist_o} ${output_o})
if(BUILD_BC_LIBS)
if(BUILD_LTO_LIBS)
list(APPEND ${outlist_bc} ${output_bc})
endif()

if(${all_at_once})
list(APPEND relative_src_files ${relative_src_file})
else()
set(outfiles ${output_o})
if(BUILD_BC_LIBS)
set(renameCommand "")
if(BUILD_LTO_LIBS)
set(renameCommand mv ${output_basedir}/${output_root}.bc ${output_bc})
list(APPEND outfiles ${output_bc})
endif()

add_custom_command(
OUTPUT ${outfiles}
COMMAND ${LDC_EXE_FULL} ${dc_flags} -of=${output_o} ${relative_src_file}
COMMAND ${renameCommand}
WORKING_DIRECTORY ${src_basedir}
DEPENDS ${f} ${dc_deps}
)
endif()
endforeach()

if(${all_at_once})
set(renameCommand "")
if(BUILD_LTO_LIBS)
set(renameCommand find ${output_basedir} -iname '*.bc' -exec mv '{}' '{}${CMAKE_C_OUTPUT_EXTENSION}' "\\\\;")
endif()

add_custom_command(
OUTPUT ${${outlist_o}} ${${outlist_bc}}
COMMAND ${LDC_EXE_FULL} ${dc_flags} -od=${output_basedir} -op ${relative_src_files}
COMMAND ${renameCommand}
WORKING_DIRECTORY ${src_basedir}
DEPENDS ${src_files} ${dc_deps}
)
Expand Down Expand Up @@ -453,12 +462,18 @@ foreach(f ${D_FLAGS})
endforeach()

# Sets the common properties for all library targets.
function(set_common_library_properties target is_shared)
function(set_common_library_properties target name output_dir c_flags ld_flags is_shared)
set_target_properties(${target} PROPERTIES
VERSION ${DMDFE_VERSION}
SOVERSION ${DMDFE_PATCH_VERSION}
LINKER_LANGUAGE C
MACOSX_RPATH ON
OUTPUT_NAME ${name}
ARCHIVE_OUTPUT_DIRECTORY ${output_dir}
LIBRARY_OUTPUT_DIRECTORY ${output_dir}
RUNTIME_OUTPUT_DIRECTORY ${output_dir}
COMPILE_FLAGS "${c_flags}"
LINK_FLAGS "${ld_flags}"
VERSION ${DMDFE_VERSION}
SOVERSION ${DMDFE_PATCH_VERSION}
LINKER_LANGUAGE C
MACOSX_RPATH ON
)
if(RT_ARCHIVE_WITH_LDC AND NOT is_shared)
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE D)
Expand Down Expand Up @@ -488,16 +503,10 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
get_target_suffix("${lib_suffix}" "${path_suffix}" target_suffix)
add_library(druntime-ldc${target_suffix} ${library_type}
${druntime_o} ${DRUNTIME_C} ${DRUNTIME_ASM})
set_target_properties(
druntime-ldc${target_suffix} PROPERTIES
OUTPUT_NAME druntime-ldc${lib_suffix}
ARCHIVE_OUTPUT_DIRECTORY ${output_path}
LIBRARY_OUTPUT_DIRECTORY ${output_path}
RUNTIME_OUTPUT_DIRECTORY ${output_path}
COMPILE_FLAGS "${c_flags}"
LINK_FLAGS "${ld_flags}"
set_common_library_properties(druntime-ldc${target_suffix}
druntime-ldc${lib_suffix} ${output_path}
"${c_flags}" "${ld_flags}" ${is_shared}
)
set_common_library_properties(druntime-ldc${target_suffix} "${is_shared}")

# When building a shared library, we need to link in all the default
# libraries otherwise implicitly added by LDC to make it loadable from
Expand All @@ -511,16 +520,10 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
if(PHOBOS2_DIR)
add_library(phobos2-ldc${target_suffix} ${library_type}
${phobos2_o} ${PHOBOS2_C})
set_target_properties(
phobos2-ldc${target_suffix} PROPERTIES
OUTPUT_NAME phobos2-ldc${lib_suffix}
ARCHIVE_OUTPUT_DIRECTORY ${output_path}
LIBRARY_OUTPUT_DIRECTORY ${output_path}
RUNTIME_OUTPUT_DIRECTORY ${output_path}
COMPILE_FLAGS "${c_flags}"
LINK_FLAGS "${ld_flags}"
set_common_library_properties(phobos2-ldc${target_suffix}
phobos2-ldc${lib_suffix} ${output_path}
"${c_flags}" "${ld_flags}" ${is_shared}
)
set_common_library_properties(phobos2-ldc${target_suffix} "${is_shared}")

if("${is_shared}" STREQUAL "ON")
# Make sure to link shared unittest-Phobos against shared NON-unittest-druntime
Expand All @@ -530,34 +533,23 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
druntime-ldc${target_suffix_without_unittest} ${C_SYSTEM_LIBS})
endif()

list(APPEND ${outlist_targets} "phobos2-ldc${target_suffix}")
list(APPEND ${outlist_targets} phobos2-ldc${target_suffix})
endif()

if(BUILD_BC_LIBS)
find_program(LLVM_AR_EXE llvm-ar
HINTS ${LLVM_ROOT_DIR}/bin
DOC "path to llvm-ar tool"
if(BUILD_LTO_LIBS AND "${is_shared}" STREQUAL "OFF" AND NOT "${lib_suffix}" MATCHES "-unittest")
add_library(druntime-ldc-lto${target_suffix} STATIC
${druntime_bc} ${DRUNTIME_C} ${DRUNTIME_ASM})
set_common_library_properties(druntime-ldc-lto${target_suffix}
druntime-ldc-lto${lib_suffix} ${output_path}
"${c_flags}" "${ld_flags}" OFF
)
if(NOT LLVM_AR_EXE)
message(SEND_ERROR "llvm-ar not found")
endif()

set(bclibs
${output_path}/libdruntime-ldc${lib_suffix}-bc.a
${output_path}/libphobos2-ldc${lib_suffix}-bc.a
add_library(phobos2-ldc-lto${target_suffix} STATIC
${phobos2_bc} ${PHOBOS2_C})
set_common_library_properties(phobos2-ldc-lto${target_suffix}
phobos2-ldc-lto${lib_suffix} ${output_path}
"${c_flags}" "${ld_flags}" OFF
)
add_custom_command(
OUTPUT ${bclibs}
COMMAND ${LLVM_AR_EXE} rs libdruntime-ldc${lib_suffix}-bc.a ${druntime_bc}
COMMAND ${LLVM_AR_EXE} rs libphobos2-ldc${lib_suffix}-bc.a ${phobos2_bc}
WORKING_DIRECTORY ${output_path}
DEPENDS
${druntime_bc}
${phobos2_bc}
VERBATIM
)

add_custom_target(bitcode-libraries${target_suffix} ALL DEPENDS ${bclibs})
endif()
endmacro()

Expand Down Expand Up @@ -662,6 +654,10 @@ if(MULTILIB AND "${TARGET_SYSTEM}" MATCHES "APPLE")
list(APPEND libs_to_install druntime-ldc.a druntime-ldc-debug.a
phobos2-ldc.a phobos2-ldc-debug.a
)
# Only install the release versions of the (static-only) bitcode libraries.
if(BUILD_LTO_LIBS)
list(APPEND libs_to_install druntime-ldc-lto.a phobos2-ldc-lto.a)
endif()
endif()
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
set(suffix ${SHARED_LIB_SUFFIX}.dylib)
Expand Down Expand Up @@ -715,6 +711,11 @@ else()
build_runtime_variants("-m${MULTILIB_SUFFIX}" "-m${MULTILIB_SUFFIX} ${RT_CFLAGS}" "-m${MULTILIB_SUFFIX} ${LD_FLAGS}" "${MULTILIB_SUFFIX}" dummy)
endif()

# Only install the release versions of the (static-only) bitcode libraries.
if(BUILD_LTO_LIBS)
list(APPEND libs_to_install druntime-ldc-lto phobos2-ldc-lto)
endif()

foreach(libname ${libs_to_install})
install(TARGETS ${libname}
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX})
Expand Down
28 changes: 10 additions & 18 deletions runtime/jit-rt/DefineBuildJitRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ if(LDC_DYNAMIC_COMPILE)
set(output_path ${CMAKE_BINARY_DIR}/lib${path_suffix})

add_library(ldc-jit-rt-so${target_suffix} SHARED ${LDC_JITRT_SO_CXX} ${LDC_JITRT_SO_H})
set_target_properties(
ldc-jit-rt-so${target_suffix} PROPERTIES
OUTPUT_NAME ldc-jit
ARCHIVE_OUTPUT_DIRECTORY ${output_path}
LIBRARY_OUTPUT_DIRECTORY ${output_path}
RUNTIME_OUTPUT_DIRECTORY ${output_path}
COMPILE_FLAGS "${c_flags} ${LDC_CXXFLAGS} ${LLVM_CXXFLAGS} ${JITRT_EXTRA_FLAGS}"
LINK_FLAGS "${ld_flags} ${JITRT_EXTRA_LDFLAGS}"
set_common_library_properties(ldc-jit-rt-so${target_suffix}
ldc-jit ${output_path}
"${c_flags} ${LDC_CXXFLAGS} ${LLVM_CXXFLAGS} ${JITRT_EXTRA_FLAGS}"
"${ld_flags} ${JITRT_EXTRA_LDFLAGS}"
ON
)
set_common_library_properties(ldc-jit-rt-so${target_suffix} ON)

target_link_libraries(ldc-jit-rt-so${target_suffix} ${JITRT_LLVM_LIBS})

Expand All @@ -76,16 +72,12 @@ if(LDC_DYNAMIC_COMPILE)
compile_jit_rt_D("-enable-dynamic-compile;${d_flags}" "" "${path_suffix}" "${COMPILE_ALL_D_FILES_AT_ONCE}" jitrt_d_o jitrt_d_bc)

add_library(ldc-jit-rt${target_suffix} STATIC ${jitrt_d_o} ${LDC_JITRT_CXX} ${LDC_JITRT_H})
set_target_properties(
ldc-jit-rt${target_suffix} PROPERTIES
OUTPUT_NAME ldc-jit-rt
ARCHIVE_OUTPUT_DIRECTORY ${output_path}
LIBRARY_OUTPUT_DIRECTORY ${output_path}
RUNTIME_OUTPUT_DIRECTORY ${output_path}
COMPILE_FLAGS "${c_flags} ${JITRT_EXTRA_FLAGS}"
LINK_FLAGS "${ld_flags} ${JITRT_EXTRA_LDFLAGS}"
set_common_library_properties(ldc-jit-rt${target_suffix}
ldc-jit-rt ${output_path}
"${c_flags} ${JITRT_EXTRA_FLAGS}"
"${ld_flags} ${JITRT_EXTRA_LDFLAGS}"
OFF
)
set_common_library_properties(ldc-jit-rt${target_suffix} OFF)

target_link_libraries(ldc-jit-rt${target_suffix} ldc-jit-rt-so${target_suffix})

Expand Down