Skip to content

Commit 4c04ed3

Browse files
fabianbs96Martin Mory
andauthored
Fix Build (#684)
* Add split-dwarf to improve linker resource consumption in incremental build * More target-oriented cmake + verify that llvm small libs exist when not using the fat lib * Single export set + rename install components * minor fix * Auto-detect whether we should link against the fat LLVM lib * Fix in-tree build * fix llvm small library search * make output of non-found small llvm libs pretty --------- Co-authored-by: Martin Mory <linuxfan91@googlemail.com>
1 parent ae8c161 commit 4c04ed3

4 files changed

Lines changed: 130 additions & 64 deletions

File tree

CMakeLists.txt

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ string(APPEND CMAKE_CXX_FLAGS_RELEASE "")
7070

7171
option(CMAKE_VISIBILITY_INLINES_HIDDEN "Hide inlined functions from the DSO table (default ON)" ON)
7272

73+
include(CheckCXXCompilerFlag)
74+
75+
# Handle memory issues with linking
76+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
77+
string(APPEND CMAKE_CXX_FLAGS_DEBUG " -gsplit-dwarf")
78+
string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -gsplit-dwarf")
79+
set(LINKER_FLAGS_SAVE ${CMAKE_EXE_LINKER_FLAGS})
80+
81+
# See LLVM_USE_SPLIT_DWARF in LLVM
82+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gdb-index")
83+
cmake_policy(PUSH)
84+
cmake_policy(SET CMP0056 NEW)
85+
check_cxx_compiler_flag("" GDB_INDEX_SUPPORTED)
86+
cmake_policy(POP)
87+
set(CMAKE_EXE_LINKER_FLAGS ${LINKER_FLAGS_SAVE})
88+
89+
if(GDB_INDEX_SUPPORTED)
90+
link_libraries(debug "-Wl,--gdb-index")
91+
endif()
92+
endif()
93+
7394
# march=native
7495

7596
# NOTE: Use gcc -march=native -Q --help=target | grep -- '-march=' | cut -f3
@@ -87,7 +108,6 @@ elseif("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
87108
endif()
88109

89110
if (NOT "${PHASAR_TARGET_ARCH_INTERNAL}" STREQUAL "")
90-
include(CheckCXXCompilerFlag)
91111
check_cxx_compiler_flag("-march=${PHASAR_TARGET_ARCH_INTERNAL}" MARCH_SUPPORTED)
92112
if (MARCH_SUPPORTED)
93113
message(STATUS "Target architecture '${PHASAR_TARGET_ARCH_INTERNAL}' enabled")
@@ -204,6 +224,17 @@ else()
204224
set(PHASAR_CONFIG_INSTALL_DIR "${PHASAR_CUSTOM_CONFIG_INSTALL_DIR}")
205225
endif()
206226

227+
228+
# Headers
229+
230+
add_library(phasar_interface INTERFACE)
231+
target_include_directories(phasar_interface
232+
INTERFACE
233+
$<BUILD_INTERFACE:${PHASAR_SRC_DIR}/include/> # The regular include folder
234+
$<BUILD_INTERFACE:${PHASAR_BINARY_DIR}/include/> # The location of phasar-config.h
235+
$<INSTALL_INTERFACE:include/> # The installed include folder
236+
)
237+
207238
### Adding external libraries
208239

209240
# Threads
@@ -260,7 +291,6 @@ option(USE_LLVM_FAT_LIB "Link against libLLVM.so instead of the individual LLVM
260291
if (NOT PHASAR_IN_TREE)
261292
# Only search for LLVM if we build out of tree
262293
find_package(LLVM 14 REQUIRED CONFIG)
263-
264294
find_library(LLVM_LIBRARY NAMES LLVM PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH)
265295

266296
if(USE_LLVM_FAT_LIB AND ${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND")
@@ -270,7 +300,38 @@ if (NOT PHASAR_IN_TREE)
270300
message(STATUS "Found consolidated shared LLVM lib ${LLVM_LIBRARY} that will be linked against.")
271301
set(USE_LLVM_FAT_LIB ON)
272302
endif()
273-
endif()
303+
304+
if (NOT USE_LLVM_FAT_LIB)
305+
message(STATUS "Link against individual LLVM modules")
306+
set(LLVM_REQUIRED_LIBRARIES
307+
Core
308+
Support
309+
BitWriter
310+
Analysis
311+
Passes
312+
Demangle
313+
Analysis
314+
IRReader
315+
Linker
316+
)
317+
foreach(lib ${LLVM_REQUIRED_LIBRARIES})
318+
find_library(LLVM_SMALL_LIB${lib} NAMES LLVM${lib} PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH)
319+
if(LLVM_SMALL_LIB${lib} MATCHES "NOTFOUND$")
320+
list(APPEND LLVM_SMALL_LIB_NOTFOUND "LLVM${lib}")
321+
endif()
322+
endforeach()
323+
324+
if(DEFINED LLVM_SMALL_LIB_NOTFOUND)
325+
if(${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND")
326+
message(FATAL_ERROR "Did not find a complete version of LLVM: Did not find the fat lib libLLVM.so, but also did not find the individual modules ${LLVM_SMALL_LIB_NOTFOUND}.")
327+
else()
328+
set(USE_LLVM_FAT_LIB ON)
329+
list(JOIN LLVM_SMALL_LIB_NOTFOUND ", " LLVM_SMALL_LIB_NOTFOUND_PRETTY)
330+
message(WARNING "Did not find the LLVM modules ${LLVM_SMALL_LIB_NOTFOUND_PRETTY}. Fallback to link against ${LLVM_LIBRARY}. To silence this warning, set -DUSE_LLVM_FAT_LIB=ON in the cmake invocation.")
331+
endif()
332+
endif(DEFINED LLVM_SMALL_LIB_NOTFOUND)
333+
endif(NOT USE_LLVM_FAT_LIB)
334+
endif(NOT PHASAR_IN_TREE)
274335

275336
if(NOT LLVM_ENABLE_RTTI AND NOT PHASAR_IN_TREE)
276337
message(FATAL_ERROR "PhASAR requires a LLVM version that is built with RTTI")
@@ -315,7 +376,7 @@ if(BUILD_PHASAR_CLANG)
315376

316377
if (PHASAR_IN_TREE)
317378
# Phasar needs clang headers, specificaly some that are generated by clangs table-gen
318-
include_directories(SYSTEM
379+
include_directories(
319380
${CLANG_INCLUDE_DIR}
320381
${PHASAR_SRC_DIR}/../clang/include
321382
${PROJECT_BINARY_DIR}/tools/clang/include
@@ -335,13 +396,8 @@ endif ()
335396

336397
# Library Dependency Dirs
337398
if(NOT PHASAR_IN_TREE)
338-
include_directories(${LLVM_INCLUDE_DIRS})
339-
link_directories(${LLVM_LIB_PATH} ${LLVM_LIBRARY_DIRS})
340399
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
341400
add_definitions(${LLVM_DEFINITIONS_LIST})
342-
if (BUILD_PHASAR_CLANG)
343-
link_directories(${CLANG_LIB_PATH})
344-
endif()
345401
endif()
346402

347403
# Installed config
@@ -413,17 +469,30 @@ install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/phasar/Config/
413469
PATTERN "*.h"
414470
)
415471

416-
# Install the header only json container
472+
if(NOT PHASAR_IN_TREE)
473+
install(TARGETS phasar_interface
474+
EXPORT PhasarExports
475+
)
476+
477+
# Install the export-set containing all the phasar targets
478+
install(EXPORT PhasarExports
479+
FILE PhasarExports.cmake
480+
NAMESPACE phasar::
481+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/phasar"
482+
)
483+
else()
484+
install(TARGETS phasar_interface
485+
EXPORT LLVMExports
486+
)
487+
set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS phasar_interface)
488+
endif()
489+
490+
# Install the header only json container ### TODO Fix this!
417491
install(DIRECTORY external/json/single_include/
418492
DESTINATION include
419493
FILES_MATCHING PATTERN "*.hpp"
420494
)
421495

422-
# Install the gtest header files (TODO this installation dependency should be eliminated)
423-
install(DIRECTORY external/googletest/googletest/include/gtest/
424-
DESTINATION include/gtest
425-
)
426-
427496
# Install Phasar utils helper scripts
428497
install(DIRECTORY utils/
429498
DESTINATION bin

Config.cmake.in

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ find_package(LLVM 14 REQUIRED CONFIG)
1414
set(PHASAR_USE_LLVM_FAT_LIB @USE_LLVM_FAT_LIB@)
1515
set(PHASAR_BUILD_DYNLIB @PHASAR_BUILD_DYNLIB@)
1616

17-
# TODO: The order seems to be important in the include'ing loop below. Fix this!
18-
1917
set(PHASAR_COMPONENTS
2018
utils
2119
passes
@@ -40,30 +38,35 @@ set(PHASAR_COMPONENTS
4038

4139
list(REMOVE_DUPLICATES phasar_FIND_COMPONENTS)
4240

41+
set(PHASAR_NEEDED_LIBS)
42+
43+
include("${CMAKE_CURRENT_LIST_DIR}/PhasarExports.cmake")
44+
4345
foreach(component ${phasar_FIND_COMPONENTS})
4446
if(NOT ${component} IN_LIST PHASAR_COMPONENTS)
4547
set(phasar_FOUND false)
46-
set(phasar_NOT_FOUND_MESSAGE "Unsupported component: ${component}. valid components are: ${PHASAR_COMPONENTS}")
48+
set(phasar_NOT_FOUND_MESSAGE "Unsupported component: ${component}. Valid components are: ${PHASAR_COMPONENTS}")
4749
endif()
48-
endforeach()
4950

50-
foreach(component ${PHASAR_COMPONENTS})
51-
include("${CMAKE_CURRENT_LIST_DIR}/phasar_${component}-targets.cmake")
51+
list(APPEND PHASAR_NEEDED_LIBS phasar::${component})
5252
endforeach()
53-
include("${CMAKE_CURRENT_LIST_DIR}/phasar-targets.cmake")
5453

55-
if (NOT phasar_FIND_COMPONENTS)
56-
list(APPEND PHASAR_NEEDED_LIBS phasar::phasar)
57-
set(phasar_FIND_COMPONENTS ${PHASAR_NEEDED_LIBS})
58-
endif()
54+
if (phasar_FOUND)
55+
foreach(component ${phasar_FIND_COMPONENTS})
56+
# For backwards compatibility -- will be removed with next release
57+
add_library(phasar::phasar_${component} ALIAS phasar::${component})
58+
endforeach()
5959

60-
foreach(component ${phasar_FIND_COMPONENTS})
61-
list(APPEND PHASAR_NEEDED_LIBS phasar::phasar_${component})
62-
endforeach()
60+
if (NOT phasar_FIND_COMPONENTS)
61+
list(APPEND PHASAR_NEEDED_LIBS phasar::phasar)
62+
# Default target
63+
add_library(phasar ALIAS phasar::phasar)
64+
endif()
6365

64-
function(phasar_config executable)
65-
target_link_libraries(${executable}
66-
PUBLIC
67-
${PHASAR_NEEDED_LIBS}
68-
)
69-
endfunction()
66+
function(phasar_config executable)
67+
target_link_libraries(${executable}
68+
PUBLIC
69+
${PHASAR_NEEDED_LIBS}
70+
)
71+
endfunction()
72+
endif()

cmake/phasar_macros.cmake

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,14 @@ function(add_phasar_library name)
179179
set(libkind STATIC)
180180
endif()
181181

182+
# cut off prefix phasar_ for convenient component names
183+
string(REGEX REPLACE phasar_ "" component_name ${name})
184+
182185
add_library(${name} ${libkind} ${srcs})
183-
add_library(phasar::${name} ALIAS ${name})
186+
add_library(phasar::${component_name} ALIAS ${name})
187+
set_target_properties(${name} PROPERTIES
188+
EXPORT_NAME ${component_name}
189+
)
184190

185191
if(LLVM_COMMON_DEPENDS)
186192
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
@@ -192,22 +198,20 @@ function(add_phasar_library name)
192198
target_link_libraries(${name} PUBLIC ${PHASAR_LIB_LINKS})
193199
endif()
194200

195-
target_link_libraries(${name} PUBLIC ${PHASAR_LIB_LINK_PUBLIC})
201+
target_link_libraries(${name} PUBLIC phasar_interface ${PHASAR_LIB_LINK_PUBLIC})
196202
target_link_libraries(${name} PRIVATE ${PHASAR_LIB_LINK_PRIVATE})
197203

198204
phasar_link_llvm(${name} ${PHASAR_LIB_LLVM_LINK_COMPONENTS})
199205

200-
target_include_directories(${name}
201-
PUBLIC
202-
$<BUILD_INTERFACE:${PHASAR_SRC_DIR}/include/> # The regular include folder
203-
$<BUILD_INTERFACE:${PHASAR_BINARY_DIR}/include/> # The location of phasar-config.h
204-
)
205-
206-
# Set the target property such that installed PhASAR knows where to find its includes (must be relative paths in this case in contrast to non-installed PhASAR!)
207-
set_property(TARGET ${name} APPEND
208-
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
209-
$<INSTALL_INTERFACE:include/>
210-
)
206+
# Library Dependency Dirs
207+
if(NOT PHASAR_IN_TREE)
208+
target_include_directories(${name} PUBLIC
209+
${LLVM_INCLUDE_DIRS}
210+
)
211+
target_link_directories(${name} PUBLIC
212+
${LLVM_LIB_PATH} ${LLVM_LIBRARY_DIRS}
213+
)
214+
endif()
211215

212216
if(MSVC)
213217
get_target_property(cflag ${name} COMPILE_FLAGS)
@@ -220,31 +224,19 @@ function(add_phasar_library name)
220224
set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
221225
endif(MSVC)
222226

223-
# cut off prefix phasar_ for convenient component names
224-
string(REGEX REPLACE phasar_ "" component_name ${name})
225-
226227
if(PHASAR_IN_TREE)
227228
install(TARGETS ${name}
228229
EXPORT LLVMExports
229230
LIBRARY DESTINATION lib
230-
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
231+
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
232+
)
231233
else()
232234
install(TARGETS ${name}
233-
EXPORT ${name}-targets
234-
COMPONENT ${component_name}
235-
236-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
237-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
235+
EXPORT PhasarExports
238236

239237
# NOTE: Library, archive and runtime destination are automatically set by
240238
# GNUInstallDirs which is included in the top-level CMakeLists.txt
241239
)
242-
install(EXPORT ${name}-targets
243-
FILE ${name}-targets.cmake
244-
NAMESPACE phasar::
245-
DESTINATION lib/cmake/phasar
246-
COMPONENT ${component_name}
247-
)
248240
endif()
249241

250242
set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})

examples/use-phasar-as-library/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ add_executable(myphasartool
2121
# phasar_config(myphasartool)
2222

2323
# New way using target_link_libraries:
24-
target_link_libraries(myphasartool phasar::phasar_llvm_ifdside)
24+
target_link_libraries(myphasartool phasar::llvm_ifdside)
2525

2626
# If find_package did not specify components:
2727
# target_link_libraries(myphasartool phasar::phasar)
28+
# alternatively using the default target:
29+
# target_link_libraries(myphasartool phasar)
2830

2931
install(TARGETS myphasartool
3032
RUNTIME DESTINATION bin

0 commit comments

Comments
 (0)