Skip to content

Commit

Permalink
Simplify linking to system libraries
Browse files Browse the repository at this point in the history
The LLVMSupport library implementation consolidates all dependencies on
system libraries.  Move the logic gathering system libraries out of
'cmake/modules/LLVM-Config.cmake' and into 'lib/Support/CMakeLists.txt'.
Use the target_link_libraries() command there to tell CMake about the
link dependencies of the LLVMSupport implementation.  CMake will
automatically propagate this to all targets that link LLVMSupport
directly or indirectly.

We still need to build knowledge of system library dependencies into
'llvm-config'.  Store the list of libraries needed in a property on
LLVMSupport and teach 'tools/llvm-config/CMakeLists.txt' to retrieve it
from there.

Drop all calls to 'link_system_libs' and 'get_system_libs' from our
CMake code.  Replace their implementations with a warning that explains
the calls are no longer necessary.  Also drop from 'LLVMConfig.cmake'
the HAVE_* and related variables that were published there only to allow
'get_system_libs' to run outside our build process.

Contributed by Brad King.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201969 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
chapuni committed Feb 23, 2014
1 parent be66089 commit 043cc54
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 50 deletions.
35 changes: 5 additions & 30 deletions cmake/modules/LLVM-Config.cmake
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
function(get_system_libs return_var)
# Returns in `return_var' a list of system libraries used by LLVM.
if( NOT MSVC )
if( MINGW )
set(system_libs ${system_libs} imagehlp psapi shell32)
elseif( CMAKE_HOST_UNIX )
if( HAVE_LIBRT )
set(system_libs ${system_libs} rt)
endif()
if( HAVE_LIBDL )
set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
endif()
if(LLVM_ENABLE_TERMINFO)
if(HAVE_TERMINFO)
set(system_libs ${system_libs} ${TERMINFO_LIBS})
endif()
endif()
if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
set(system_libs ${system_libs} pthread)
endif()
if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
set(system_libs ${system_libs} z)
endif()
endif( MINGW )
endif( NOT MSVC )
set(${return_var} ${system_libs} PARENT_SCOPE)
endfunction(get_system_libs)
message(AUTHOR_WARNING "get_system_libs no longer needed")
set(${return_var} "" PARENT_SCOPE)
endfunction()


function(link_system_libs target)
get_system_libs(llvm_system_libs)
target_link_libraries(${target} ${llvm_system_libs})
endfunction(link_system_libs)
message(AUTHOR_WARNING "link_system_libs no longer needed")
endfunction()


function(is_llvm_target_library library return_var)
Expand Down Expand Up @@ -70,7 +46,6 @@ endfunction(explicit_llvm_config)
# This is a variant intended for the final user:
function(llvm_map_components_to_libraries OUT_VAR)
explicit_map_components_to_libraries(result ${ARGN})
get_system_libs(sys_result)
set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
endfunction(llvm_map_components_to_libraries)

Expand Down
5 changes: 0 additions & 5 deletions cmake/modules/LLVMConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ set(LLVM_NATIVE_ARCH @LLVM_NATIVE_ARCH@)

set(LLVM_ENABLE_PIC @LLVM_ENABLE_PIC@)

set(HAVE_TERMINFO @HAVE_TERMINFO@)
set(TERMINFO_LIBS @TERMINFO_LIBS@)
set(HAVE_LIBDL @HAVE_LIBDL@)
set(HAVE_LIBPTHREAD @HAVE_LIBPTHREAD@)
set(HAVE_LIBZ @HAVE_LIBZ@)
set(LLVM_ON_UNIX @LLVM_ON_UNIX@)
set(LLVM_ON_WIN32 @LLVM_ON_WIN32@)

Expand Down
14 changes: 2 additions & 12 deletions cmake/modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ PROJ_cmake := $(DESTDIR)$(PROJ_prefix)/share/llvm/cmake

OBJMODS := LLVMConfig.cmake LLVMConfigVersion.cmake LLVMExports.cmake

# TODO: Teach LLVM-Config.cmake to work without explicit terminfo libs.
TERMINFO_LIBS := tinfo terminfo curses ncurses ncursesw
TERMINFO_LIBS := $(filter $(TERMINFO_LIBS),$(subst -l,,$(LIBS)))

$(PROJ_OBJ_DIR)/LLVMConfig.cmake: LLVMConfig.cmake.in $(LLVMBuildCMakeFrag)
$(Echo) 'Generating LLVM CMake package config file'
$(Verb) ( \
Expand All @@ -40,11 +36,6 @@ $(PROJ_OBJ_DIR)/LLVMConfig.cmake: LLVMConfig.cmake.in $(LLVMBuildCMakeFrag)
-e 's/@LLVM_ENABLE_ZLIB@/'"$(ENABLE_ZLIB)"'/' \
-e 's/@LLVM_NATIVE_ARCH@/'"$(LLVM_NATIVE_ARCH)"'/' \
-e 's/@LLVM_ENABLE_PIC@/'"$(ENABLE_PIC)"'/' \
-e 's/@HAVE_TERMINFO@/'"$(HAVE_TERMINFO)"'/' \
-e 's/@TERMINFO_LIBS@/'"$(TERMINFO_LIBS)"'/' \
-e 's/@HAVE_LIBDL@/'"$(HAVE_DLOPEN)"'/' \
-e 's/@HAVE_LIBPTHREAD@/'"$(HAVE_PTHREAD)"'/' \
-e 's/@HAVE_LIBZ@/'"$(HAVE_LIBZ)"'/' \
-e 's/@LLVM_ON_UNIX@/'"$(LLVM_ON_UNIX)"'/' \
-e 's/@LLVM_ON_WIN32@/'"$(LLVM_ON_WIN32)"'/' \
-e 's/@LLVM_CONFIG_INCLUDE_DIRS@/'"$(subst /,\/,$(PROJ_includedir))"'/' \
Expand All @@ -70,9 +61,8 @@ $(PROJ_OBJ_DIR)/LLVMExports.cmake: $(LLVMBuildCMakeExportsFrag)
echo 'add_library('"$$lib"' STATIC IMPORTED)' && \
echo 'set_property(TARGET '"$$lib"' PROPERTY IMPORTED_LOCATION "'"$(PROJ_libdir)/lib$$lib.a"'")' ; \
done && \
cat "$(LLVMBuildCMakeExportsFrag)" \
&& echo '# System libs depend on LLVMSupport.' \
&& echo 'set_property(TARGET LLVMSupport PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES $(patsubst -l%,%,$(LIBS)))' \
cat "$(LLVMBuildCMakeExportsFrag)" && \
echo 'set_property(TARGET LLVMSupport APPEND PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES '"$(subst -l,,$(LIBS))"')' \
) | grep -v gtest > $@

all-local:: $(addprefix $(PROJ_OBJ_DIR)/, $(OBJMODS))
Expand Down
28 changes: 26 additions & 2 deletions lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,29 @@ add_llvm_library(LLVMSupport
Windows/TimeValue.inc
Windows/Watchdog.inc
)

link_system_libs(LLVMSupport)
set(system_libs)
if( NOT MSVC )
if( MINGW )
set(system_libs ${system_libs} imagehlp psapi shell32)
elseif( CMAKE_HOST_UNIX )
if( HAVE_LIBRT )
set(system_libs ${system_libs} rt)
endif()
if( HAVE_LIBDL )
set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
endif()
if(LLVM_ENABLE_TERMINFO)
if(HAVE_TERMINFO)
set(system_libs ${system_libs} ${TERMINFO_LIBS})
endif()
endif()
if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
set(system_libs ${system_libs} pthread)
endif()
if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
set(system_libs ${system_libs} z)
endif()
endif( MINGW )
endif( NOT MSVC )
target_link_libraries(LLVMSupport ${system_libs})
set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${system_libs}")
2 changes: 1 addition & 1 deletion tools/llvm-config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(BUILDVARIABLES_SRCPATH ${CMAKE_CURRENT_SOURCE_DIR}/BuildVariables.inc.in)
set(BUILDVARIABLES_OBJPATH ${CMAKE_CURRENT_BINARY_DIR}/BuildVariables.inc)

# Compute the substitution values for various items.
get_system_libs(LLVM_SYSTEM_LIBS_LIST)
get_property(LLVM_SYSTEM_LIBS_LIST TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS)
foreach(l ${LLVM_SYSTEM_LIBS_LIST})
set(SYSTEM_LIBS ${SYSTEM_LIBS} "-l${l}")
endforeach()
Expand Down

0 comments on commit 043cc54

Please sign in to comment.