Skip to content

Commit

Permalink
EXPORTED_SYMBOL_FILE support for cmake
Browse files Browse the repository at this point in the history
The cmake build didn't support EXPORTED_SYMBOL_FILE. Instead, it had a
Windows-only implementation in tools/lto/CMakeLists.txt, a linux-only
implementation in tools/gold/CMakeLists.txt, and a darwin-only implementation
in tools/clang/tools/libclang/CMakeLists.txt.

This attempts to consolidate these one-offs into a single place. Clients can now
just set LLVM_EXPORTED_SYMBOL_FILE and things (hopefully) Just Work, like in
the make build.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198136 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
nico committed Dec 28, 2013
1 parent 621bd8d commit b6fe25c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 45 deletions.
79 changes: 79 additions & 0 deletions cmake/modules/AddLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,72 @@ include(LLVMParseArguments)
include(LLVMProcessSources)
include(LLVM-Config)

function(add_llvm_symbol_exports target_name export_file)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(native_export_file "symbol.exports")
add_custom_command(OUTPUT symbol.exports
COMMAND sed -e "s/^/_/" < ${export_file} > symbol.exports
DEPENDS ${export_file}
VERBATIM
COMMENT "Creating export file for ${target_name}")
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/symbol.exports")
elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
# Gold and BFD ld require a version script rather than a plain list.
set(native_export_file "symbol.exports")
# FIXME: Don't write the "local:" line on OpenBSD.
add_custom_command(OUTPUT symbol.exports
COMMAND echo "{" > symbol.exports
COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> symbol.exports || :
COMMAND sed -e "s/$$/;/" -e "s/^/ /" < ${export_file} >> symbol.exports
COMMAND echo " local: *;" >> symbol.exports
COMMAND echo "};" >> symbol.exports
DEPENDS ${export_file}
VERBATIM
COMMENT "Creating export file for ${target_name}")
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/symbol.exports")
else()
set(native_export_file "symbol.def")

set(CAT "type")
if(CYGWIN)
set(CAT "cat")
endif()

add_custom_command(OUTPUT symbol.def
COMMAND cmake -E echo "EXPORTS" > symbol.def
COMMAND ${CAT} < ${export_file} >> symbol.def
DEPENDS ${export_file}
VERBATIM
COMMENT "Creating export file for ${target_name}")
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/symbol.def")
endif()

add_custom_target(${target_name}_exports DEPENDS ${native_export_file})

get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
foreach(src ${srcs})
get_filename_component(extension ${src} EXT)
if(extension STREQUAL ".cpp")
set(first_source_file ${src})
break()
endif()
endforeach()

# Force re-linking when the exports file changes. Actually, it
# forces recompilation of the source file. The LINK_DEPENDS target
# property only works for makefile-based generators.
set_property(SOURCE ${first_source_file} APPEND PROPERTY
OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})

set_property(DIRECTORY APPEND
PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})

add_dependencies(${target_name} ${target_name}_exports)
endfunction(add_llvm_symbol_exports)

macro(add_llvm_library name)
llvm_process_sources( ALL_FILES ${ARGN} )
add_library( ${name} ${ALL_FILES} )
Expand All @@ -19,6 +85,10 @@ macro(add_llvm_library name)
endif ()
endif()

if (LLVM_EXPORTED_SYMBOL_FILE)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif()

# Ensure that the system libraries always comes last on the
# list. Without this, linking the unit tests on MinGW fails.
link_system_libs( ${name} )
Expand Down Expand Up @@ -57,6 +127,10 @@ ${name} ignored.")
set(libkind SHARED)
endif()

if (LLVM_EXPORTED_SYMBOL_FILE)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif(LLVM_EXPORTED_SYMBOL_FILE)

add_library( ${name} ${libkind} ${ALL_FILES} )
set_target_properties( ${name} PROPERTIES PREFIX "" )

Expand Down Expand Up @@ -91,6 +165,11 @@ macro(add_llvm_executable name)
else()
add_executable(${name} ${ALL_FILES})
endif()

if (LLVM_EXPORTED_SYMBOL_FILE)
add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
endif(LLVM_EXPORTED_SYMBOL_FILE)

set(EXCLUDE_FROM_ALL OFF)
llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
if( LLVM_COMMON_DEPENDS )
Expand Down
3 changes: 3 additions & 0 deletions cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ else()
endif()

if(WIN32)
set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
if(CYGWIN)
set(LLVM_ON_WIN32 0)
set(LLVM_ON_UNIX 1)
Expand All @@ -57,8 +58,10 @@ else(WIN32)
set(LLVM_ON_WIN32 0)
set(LLVM_ON_UNIX 1)
if(APPLE)
set(LLVM_HAVE_LINK_VERSION_SCRIPT 0)
set(LTDL_SHLIB_EXT ".dylib")
else(APPLE)
set(LLVM_HAVE_LINK_VERSION_SCRIPT 1)
set(LTDL_SHLIB_EXT ".so")
endif(APPLE)
set(EXEEXT "")
Expand Down
28 changes: 2 additions & 26 deletions tools/gold/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(LLVM_BINUTILS_INCDIR "" CACHE PATH
"PATH to binutils/include containing plugin-api.h for gold plugin.")

set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/gold.exports)

if( NOT LLVM_BINUTILS_INCDIR )
# Nothing to say.
elseif( NOT EXISTS "${LLVM_BINUTILS_INCDIR}/plugin-api.h" )
Expand All @@ -17,30 +19,4 @@ else()
add_llvm_loadable_module(LLVMgold
gold-plugin.cpp
)

# Makefile.rules contains a special cases for OpenBSD, Darwin and
# Windows. We restrict ourselves to Linux for the time being.
set(srcexp ${CMAKE_CURRENT_SOURCE_DIR}/gold.exports)
add_custom_command(OUTPUT exportsfile
COMMAND echo "{" > exportsfile
COMMAND grep -q "\\<" ${srcexp} && echo " global:" >> exportsfile || :
COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${srcexp} >> exportsfile
COMMAND echo " local: *;" >> exportsfile
COMMAND echo "};" >> exportsfile
DEPENDS ${srcexp}
VERBATIM
COMMENT "Creating export file for gold plugin")
add_custom_target(gold_exports DEPENDS exportsfile)
set_property(DIRECTORY APPEND
PROPERTY ADDITIONAL_MAKE_CLEAN_FILES exportsfile)

# Force re-linking when the exports file changes. Actually, it
# forces recompilation of gold-plugin.cpp. The LINK_DEPENDS target
# property only works for makefile-based generators.
set_property(SOURCE gold-plugin.cpp APPEND PROPERTY
OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/exportsfile)

target_link_libraries(LLVMgold LTO
-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/exportsfile)
add_dependencies(LLVMgold gold_exports)
endif()
22 changes: 3 additions & 19 deletions tools/lto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,10 @@ set(SOURCES
lto.cpp
)

if( NOT CYGWIN AND LLVM_ENABLE_PIC )
if ( WIN32 )
# Create .def file containing a list of exports preceeded by
# 'EXPORTS'. The file "lto.exports" already contains the list, so we
# massage it into the correct format here to create "lto.exports.def".
set(LTO_EXPORTS_DEF ${CMAKE_CURRENT_BINARY_DIR}/lto.exports.def)
set(LTO_EXPORTS_DEF_TEMP ${LTO_EXPORTS_DEF}.txt)
file(READ "lto.exports" exports_list)
file(WRITE ${LTO_EXPORTS_DEF_TEMP} "LIBRARY LTO\n")
file(APPEND ${LTO_EXPORTS_DEF_TEMP} "EXPORTS\n")
file(APPEND ${LTO_EXPORTS_DEF_TEMP} ${exports_list})

# Copy the file only if it has changed.
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${LTO_EXPORTS_DEF_TEMP} ${LTO_EXPORTS_DEF})
set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/lto.exports)

set(SHARED_LIB_SOURCES ${SOURCES} ${LTO_EXPORTS_DEF})
else()
set(SHARED_LIB_SOURCES ${SOURCES})
endif()
if( NOT CYGWIN AND LLVM_ENABLE_PIC )
set(SHARED_LIB_SOURCES ${SOURCES})

set(bsl ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS ON)
Expand Down

0 comments on commit b6fe25c

Please sign in to comment.