Skip to content

Avoid object libraries in the VS IDE #93519

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

Merged
merged 1 commit into from
Jun 19, 2024
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
6 changes: 5 additions & 1 deletion clang/cmake/modules/AddClang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ macro(add_clang_library name)
else()
set(LIBTYPE STATIC)
endif()
if(NOT XCODE)
if(NOT XCODE AND NOT MSVC_IDE)
# The Xcode generator doesn't handle object libraries correctly.
# The Visual Studio CMake generator does handle object libraries
# correctly, but it is preferable to list the libraries with their
# source files (instead of the object files and the source files in
# a separate target in the "Object Libraries" folder)
list(APPEND LIBTYPE OBJECT)
endif()
set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(clangSupport_sources

add_clang_library(clangSupport ${clangSupport_sources})

if (NOT XCODE)
if (TARGET obj.clangSupport)
add_library(clangSupport_tablegen ALIAS obj.clangSupport)
elseif (NOT LLVM_LINK_LLVM_DYLIB)
add_library(clangSupport_tablegen ALIAS clangSupport)
Expand Down
21 changes: 15 additions & 6 deletions flang/cmake/modules/AddFlang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,28 @@ function(add_flang_library name)

endif()

if (ARG_SHARED)
if(ARG_SHARED AND ARG_STATIC)
set(LIBTYPE SHARED STATIC)
elseif(ARG_SHARED)
set(LIBTYPE SHARED)
else()
# llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
# so we need to handle it here.
if (BUILD_SHARED_LIBS AND NOT ARG_STATIC)
set(LIBTYPE SHARED OBJECT)
if(BUILD_SHARED_LIBS)
set(LIBTYPE SHARED)
else()
set(LIBTYPE STATIC OBJECT)
set(LIBTYPE STATIC)
endif()
set_property(GLOBAL APPEND PROPERTY FLANG_STATIC_LIBS ${name})
if(NOT XCODE AND NOT MSVC_IDE)
# The Xcode generator doesn't handle object libraries correctly.
# The Visual Studio CMake generator does handle object libraries
# correctly, but it is preferable to list the libraries with their
# source files (instead of the object files and the source files in
# a separate target in the "Object Libraries" folder)
list(APPEND LIBTYPE OBJECT)
endif()
set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
endif()

llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})

clang_target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
Expand Down
62 changes: 42 additions & 20 deletions mlir/cmake/modules/AddMLIR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,23 @@ endfunction()
# Don't include this library in libMLIR.so. This option should be used
# for test libraries, executable-specific libraries, or rarely used libraries
# with large dependencies.
# OBJECT
# The library's object library is referenced using "obj.${name}". For this to
# work reliably, this flag ensures that the OBJECT library exists.
# ENABLE_AGGREGATION
# Forces generation of an OBJECT library, exports additional metadata,
# Exports additional metadata,
# and installs additional object files needed to include this as part of an
# aggregate shared library.
# TODO: Make this the default for all MLIR libraries once all libraries
# are compatible with building an object library.
function(add_mlir_library name)
cmake_parse_arguments(ARG
"SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION"
"SHARED;INSTALL_WITH_TOOLCHAIN;EXCLUDE_FROM_LIBMLIR;DISABLE_INSTALL;ENABLE_AGGREGATION;OBJECT"
""
"ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS"
${ARGN})
_set_mlir_additional_headers_as_srcs(${ARG_ADDITIONAL_HEADERS})

# Is an object library needed.
set(NEEDS_OBJECT_LIB OFF)
if(ARG_ENABLE_AGGREGATION)
set(NEEDS_OBJECT_LIB ON)
endif()

# Determine type of library.
if(ARG_SHARED)
set(LIBTYPE SHARED)
Expand All @@ -337,18 +334,39 @@ function(add_mlir_library name)
else()
set(LIBTYPE STATIC)
endif()
# Test libraries and such shouldn't be include in libMLIR.so
if(NOT ARG_EXCLUDE_FROM_LIBMLIR)
set(NEEDS_OBJECT_LIB ON)
set_property(GLOBAL APPEND PROPERTY MLIR_STATIC_LIBS ${name})
set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
endif()
endif()

if(NEEDS_OBJECT_LIB AND NOT XCODE)
# The Xcode generator doesn't handle object libraries correctly.
# We special case xcode when building aggregates.
# Is an object library needed...?
# Note that the XCode generator doesn't handle object libraries correctly and
# usability is degraded in the Visual Studio solution generators.
# llvm_add_library may also itself decide to create an object library.
set(NEEDS_OBJECT_LIB OFF)
if(ARG_OBJECT)
# Yes, because the target "obj.${name}" is referenced.
set(NEEDS_OBJECT_LIB ON)
endif ()
if(LLVM_BUILD_LLVM_DYLIB AND NOT ARG_EXCLUDE_FROM_LIBMLIR AND NOT XCODE)
# Yes, because in addition to the shared library, the object files are
# needed for linking into libMLIR.so (see mlir/tools/mlir-shlib/CMakeLists.txt).
# For XCode, -force_load is used instead.
# Windows is not supported (LLVM_BUILD_LLVM_DYLIB=ON will cause an error).
set(NEEDS_OBJECT_LIB ON)
set_property(GLOBAL APPEND PROPERTY MLIR_STATIC_LIBS ${name})
set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
set_property(GLOBAL APPEND PROPERTY MLIR_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
endif ()
if(ARG_ENABLE_AGGREGATION AND NOT XCODE)
# Yes, because this library is added to an aggergate library such as
# libMLIR-C.so which is links together all the object files.
# For XCode, -force_load is used instead.
set(NEEDS_OBJECT_LIB ON)
endif()
if (NOT ARG_SHARED AND NOT ARG_EXCLUDE_FROM_LIBMLIR AND NOT XCODE AND NOT MSVC_IDE)
# Yes, but only for legacy reasons. Also avoid object libraries for
# Visual Studio solutions.
set(NEEDS_OBJECT_LIB ON)
endif()
if(NEEDS_OBJECT_LIB)
list(APPEND LIBTYPE OBJECT)
endif()

Expand Down Expand Up @@ -380,9 +398,12 @@ function(add_mlir_library name)
# XCode has limited support for object libraries. Instead, add dep flags
# that force the entire library to be embedded.
list(APPEND AGGREGATE_DEPS "-force_load" "${name}")
else()
elseif(TARGET obj.${name})
# FIXME: *.obj can also be added via target_link_libraries since CMake 3.12.
list(APPEND AGGREGATE_OBJECTS "$<TARGET_OBJECTS:obj.${name}>")
list(APPEND AGGREGATE_OBJECT_LIB "obj.${name}")
else()
message(SEND_ERROR "Aggregate library not supported on this platform")
endif()

# For each declared dependency, transform it into a generator expression
Expand All @@ -402,7 +423,7 @@ function(add_mlir_library name)

# In order for out-of-tree projects to build aggregates of this library,
# we need to install the OBJECT library.
if(MLIR_INSTALL_AGGREGATE_OBJECTS AND NOT ARG_DISABLE_INSTALL)
if(TARGET "obj.${name}" AND MLIR_INSTALL_AGGREGATE_OBJECTS AND NOT ARG_DISABLE_INSTALL)
add_mlir_library_install(obj.${name})
endif()
endif()
Expand Down Expand Up @@ -615,6 +636,7 @@ endfunction()
function(add_mlir_public_c_api_library name)
add_mlir_library(${name}
${ARGN}
OBJECT
EXCLUDE_FROM_LIBMLIR
ENABLE_AGGREGATION
ADDITIONAL_HEADER_DIRS
Expand Down
2 changes: 2 additions & 0 deletions mlir/lib/Dialect/GPU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ add_mlir_dialect_library(MLIRGPUTransforms
Transforms/SPIRVAttachTarget.cpp
Transforms/SubgroupReduceLowering.cpp
Transforms/Utils.cpp

OBJECT

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/GPU
Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/Target/LLVM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ endif()
add_mlir_dialect_library(MLIRNVVMTarget
NVVM/Target.cpp

OBJECT

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR

Expand Down Expand Up @@ -109,6 +111,8 @@ endif()
add_mlir_dialect_library(MLIRROCDLTarget
ROCDL/Target.cpp

OBJECT

LINK_COMPONENTS
MCParser
${AMDGPU_LIBS}
Expand Down
1 change: 1 addition & 0 deletions mlir/tools/mlir-shlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if(LLVM_BUILD_LLVM_DYLIB)
add_mlir_library(
MLIR
SHARED
EXCLUDE_FROM_LIBMLIR
${INSTALL_WITH_TOOLCHAIN}
mlir-shlib.cpp
${_OBJECTS}
Expand Down
Loading