Skip to content

Commit

Permalink
Build functioning dev packages for IREECompiler and IREERuntime. (#16008
Browse files Browse the repository at this point in the history
)

With this patch, external projects can do:

```
find_package(IREECompiler)
find_package(IREERuntime)
```

and access our targets by adding either the build or install tree
`lib/cmake/IREE` to the CMake package search path.

There are still some ergonomic issues that need to be addressed going
forward:

* cpuinfo breaks the one downstream I am testing this against. We really
should just finish killing that, so I am setting
-DIREE_ENABLE_CPUINFO=OFF in the dist package.
* We are not yet re-establishing the pretty `iree::runtime:...` aliases.
The underscore versions are available (`iree_runtime_...`).
* I want to further isolate the remaining bundled deps (yaml and
flatcc).
* New convenience install targets do not have full dependency
information. Need to do an `all` build first.

As part of this, I did some longstanding cleanups:

* Normalized the install component namespace. Defined
`IREECMakeExports`, `IREEDevLibraries-Compiler`,
`IREEDevLibraries-Runtime`, `IREERuntimeLibraries-Compiler`,
`IREEBundledLibraries`, `IREETools-Runtime`.
* Added convenience `iree-install-*` targets. Each includes a
`-stripped` variant as well.
* Moved MLIRInterop.h to the bindings/c directory where it should have
been all along and straightened the deps.
* Install the mlir-c headers to support libIREECompiler.so dev.
* Added the "busybox" tool binaries to the install distribution
(iree-lld, etc).
* Stopped bundling upstream `lld` and `clang` by default because they
bloat the size. `lld` is now bundled as `iree-lld` just like in the
Python packages. Clang and llvm-link can be gotten by installing the new
`IREETools-CompilerExtra` component if folks need those.

Note that this only installs public headers and libraries needed to
build against the compiler and runtime. Installing private things needed
to develop parts of the system are out of scope.
  • Loading branch information
stellaraccident authored Dec 29, 2023
1 parent b0e8f3c commit 15c306f
Show file tree
Hide file tree
Showing 37 changed files with 593 additions and 74 deletions.
113 changes: 109 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ include(iree_cc_binary)
include(iree_cc_library)
include(iree_cc_test)
include(iree_import_binary)
include(iree_install_support)
include(iree_external_cmake_options)
include(iree_tablegen_library)
include(iree_tablegen_doc)
Expand Down Expand Up @@ -854,10 +855,16 @@ else()
# Also add a library that can be depended on to get LLVM includes setup
# properly. bazel_to_cmake targets this for some header only pseudo deps.
add_library(IREELLVMIncludeSetup INTERFACE)
target_include_directories(IREELLVMIncludeSetup INTERFACE
${LLVM_INCLUDE_DIRS}
${MLIR_INCLUDE_DIRS}
${LLD_INCLUDE_DIRS}
foreach(_d ${LLVM_INCLUDE_DIRS} ${MLIR_INCLUDE_DIRS} ${LLD_INCLUDE_DIRS})
# BUILD_INTERFACE only works one at a time.
target_include_directories(IREELLVMIncludeSetup INTERFACE
$<BUILD_INTERFACE:${_d}>
)
endforeach()
iree_install_targets(
TARGETS IREELLVMIncludeSetup
COMPONENT IREEPublicLibraries-Compiler
EXPORT_SET Compiler
)

# Splice the includes setup into base LLVM libraries so that using them
Expand Down Expand Up @@ -895,24 +902,49 @@ endif()

#-------------------------------------------------------------------------------
# Other dependencies
# By default we bundle a number of dependencies needed to build the project.
# When bundled like this, they are installed into the IREEBundledLibraries
# component and exported to their subsystem which requires them (if a static
# dep from the public API): "Runtime" or "Compiler".
#
# Some deps have a usable CMake build, and we add_subdirectory these, manually
# using iree_install_targets to include them in our installation. Others require
# custom CMake and these are in the build_tools/third_party directory.
#
# TODO: We should have a mode that purely uses find_package for OS friendly
# packaging/externalizing deps.
#-------------------------------------------------------------------------------

include(external_cc_library)
include(flatbuffer_c_library)

# libyaml
add_subdirectory(build_tools/third_party/libyaml EXCLUDE_FROM_ALL)

add_subdirectory(build_tools/third_party/llvm-project EXCLUDE_FROM_ALL)
add_subdirectory(build_tools/third_party/tracy_client EXCLUDE_FROM_ALL)

iree_set_googletest_cmake_options()
add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)

if(IREE_ENABLE_THREADING)
# Benchmark.
iree_set_benchmark_cmake_options()
add_subdirectory(third_party/benchmark EXCLUDE_FROM_ALL)
iree_install_targets(
TARGETS benchmark
COMPONENT IREEBundledLibraries
EXPORT_SET Runtime
)

if(IREE_ENABLE_CPUINFO)
iree_set_cpuinfo_cmake_options()
add_subdirectory(third_party/cpuinfo EXCLUDE_FROM_ALL)
iree_install_targets(
TARGETS cpuinfo
COMPONENT IREEBundledLibraries
EXPORT_SET Runtime
)
endif()
endif()

Expand All @@ -925,6 +957,11 @@ endif()

if(IREE_HAL_DRIVER_VULKAN)
add_subdirectory(third_party/vulkan_headers EXCLUDE_FROM_ALL)
iree_install_targets(
TARGETS Vulkan-Headers
COMPONENT IREEBundledLibraries
EXPORT_SET Runtime
)
endif()

if(IREE_BUILD_COMPILER)
Expand Down Expand Up @@ -1056,3 +1093,71 @@ endif()
if(IREE_BUILD_TESTS)
iree_create_ctest_customization()
endif()

#-------------------------------------------------------------------------------
# Install/exports
#-------------------------------------------------------------------------------

add_subdirectory(build_tools/cmake ${IREE_BINARY_DIR}/lib/cmake/IREE)

# See runtime/src/CMakeLists.txt
iree_generate_export_targets(
EXPORT_SET Runtime
)

# See compiler/bindings/c/CMakeLists.txt
iree_generate_export_targets(
EXPORT_SET Compiler
)

# Convenience installation targets.
iree_add_install_target(NAME iree-install-dev-libraries)
iree_add_install_target(NAME iree-install-runtime-libraries)
iree_add_install_target(NAME iree-install-tools)

iree_add_install_target(
NAME iree-install-cmake-exports
COMPONENT IREECMakeExports
ADD_TO iree-install-dev-libraries
)

iree_add_install_target(
NAME iree-install-dev-libraries-runtime
COMPONENT IREEDevLibraries-Runtime
ADD_TO iree-install-dev-libraries
)

iree_add_install_target(
NAME iree-install-bundled-libraries
COMPONENT IREEBundledLibraries
ADD_TO iree-install-dev-libraries-runtime
)

if(IREE_BUILD_COMPILER)
iree_add_install_target(
NAME iree-install-dev-libraries-compiler
COMPONENT IREEDevLibraries-Compiler
ADD_TO iree-install-dev-libraries
)

iree_add_install_target(
NAME iree-install-tools-compiler
COMPONENT IREETools-Compiler
ADD_TO iree-install-tools
)

iree_add_install_target(
NAME iree-install-runtime-libraries-compiler
COMPONENT IREERuntimeLibraries-Compiler
ADD_TO
iree-install-runtime-libraries
iree-install-tools-compiler
iree-install-dev-libraries-compiler
)
endif()

iree_add_install_target(
NAME iree-install-tools-runtime
COMPONENT IREETools-Runtime
ADD_TO iree-install-tools
)
31 changes: 31 additions & 0 deletions build_tools/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set(_cmake_installdir "${CMAKE_INSTALL_LIBDIR}/cmake/IREE")

if(IREE_BUILD_COMPILER)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/IREECompilerConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/IREECompilerConfig.cmake
@ONLY
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/IREECompilerConfig.cmake
DESTINATION ${_cmake_installdir}
COMPONENT IREECMakeExports
)
endif()

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/IREERuntimeConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/IREERuntimeConfig.cmake
@ONLY
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/IREERuntimeConfig.cmake
DESTINATION ${_cmake_installdir}
COMPONENT IREECMakeExports
)
6 changes: 6 additions & 0 deletions build_tools/cmake/IREECompilerConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This allows users to call find_package(IREECompiler) and access our targets.

include("${CMAKE_CURRENT_LIST_DIR}/IREETargets-Compiler.cmake")

# TODO: Define some properties to reference all libraries and config options.
# TODO: Iterate over all exported libraries and re-alias them.
6 changes: 6 additions & 0 deletions build_tools/cmake/IREERuntimeConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This allows users to call find_package(IREERuntime) and access our targets.

include("${CMAKE_CURRENT_LIST_DIR}/IREETargets-Runtime.cmake")

# TODO: Define some properties to reference all libraries and config options.
# TODO: Iterate over all exported libraries and re-alias them.
10 changes: 7 additions & 3 deletions build_tools/cmake/external_cc_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,17 @@ function(external_cc_library)
)
endif()

add_library(${_RULE_PACKAGE}::${_RULE_NAME} ALIAS ${_NAME})
iree_install_targets(
TARGETS ${_NAME}
HDRS ${_RULE_HDRS}
)

iree_add_alias_library(${_RULE_PACKAGE}::${_RULE_NAME} ${_NAME})
# If the library name matches the final component of the package then treat it
# as a default. For example, 'foo::bar' library 'bar' would end up as
# 'foo::bar'.
string(REGEX REPLACE "^.*::" "" _PACKAGE_DIR ${_RULE_PACKAGE})
if(${_PACKAGE_DIR} STREQUAL ${_RULE_NAME})

add_library(${_RULE_PACKAGE} ALIAS ${_NAME})
iree_add_alias_library(${_RULE_PACKAGE} ${_NAME})
endif()
endfunction()
9 changes: 6 additions & 3 deletions build_tools/cmake/flatbuffer_c_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function(flatbuffer_c_library)
add_dependencies(${_NAME} ${_GEN_TARGET})
target_include_directories(${_NAME} SYSTEM
INTERFACE
${CMAKE_CURRENT_BINARY_DIR}
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
)
target_link_libraries(${_NAME}
INTERFACE
Expand All @@ -127,10 +127,13 @@ function(flatbuffer_c_library)
"-I${IREE_ROOT_DIR}/third_party/flatcc/include/"
"-I${IREE_ROOT_DIR}/third_party/flatcc/include/flatcc/reflection/"
)

iree_install_targets(
TARGETS ${_NAME}
)

# Alias the iree_package_name library to iree::package::name.
# This lets us more clearly map to Bazel and makes it possible to
# disambiguate the underscores in paths vs. the separators.
iree_package_ns(_PACKAGE_NS)
add_library(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME})
iree_add_alias_library(${_PACKAGE_NS}::${_RULE_NAME} ${_NAME})
endfunction()
13 changes: 9 additions & 4 deletions build_tools/cmake/iree_cc_binary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include(CMakeParseArguments)
# HOSTONLY: host only; compile using host toolchain when cross-compiling
# SETUP_INSTALL_RPATH: Sets an install RPATH which assumes the standard
# directory layout (to be used if linking against installed shared libs).
#
# INSTALL_COMPONENT: CMake install component (Defaults to "IREETool-${_RULE_NAME}").
# Note:
# iree_cc_binary will create a binary called ${PACKAGE_NAME}_${NAME}, e.g.
# iree_base_foo with an alias to ${PACKAGE_NS}::${NAME}.
Expand Down Expand Up @@ -55,7 +55,7 @@ function(iree_cc_binary)
cmake_parse_arguments(
_RULE
"EXCLUDE_FROM_ALL;HOSTONLY;TESTONLY;SETUP_INSTALL_RPATH;DISABLE_LLVM_LINK_LLVM_DYLIB"
"NAME"
"NAME;INSTALL_COMPONENT"
"SRCS;COPTS;DEFINES;LINKOPTS;DATA;DEPS"
${ARGN}
)
Expand Down Expand Up @@ -155,18 +155,23 @@ function(iree_cc_binary)
set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${IREE_CXX_STANDARD})
set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)

set(_INSTALL_COMPONENT "${_RULE_INSTALL_COMPONENT}")
if(NOT _INSTALL_COMPONENT)
set(_INSTALL_COMPONENT "IREETool-${_RULE_NAME}")
endif()

if(_RULE_EXCLUDE_FROM_ALL)
set_property(TARGET ${_NAME} PROPERTY EXCLUDE_FROM_ALL ON)
install(TARGETS ${_NAME}
RENAME ${_RULE_NAME}
COMPONENT ${_RULE_NAME}
COMPONENT ${_INSTALL_COMPONENT}
RUNTIME DESTINATION bin
BUNDLE DESTINATION bin
EXCLUDE_FROM_ALL)
else()
install(TARGETS ${_NAME}
RENAME ${_RULE_NAME}
COMPONENT ${_RULE_NAME}
COMPONENT ${_INSTALL_COMPONENT}
RUNTIME DESTINATION bin
BUNDLE DESTINATION bin)
endif()
Expand Down
19 changes: 15 additions & 4 deletions build_tools/cmake/iree_cc_library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,28 @@ function(iree_cc_library)
)
endif()

if(NOT _RULE_TESTONLY)
iree_install_targets(
TARGETS ${_NAME}
HDRS ${_RULE_HDRS} ${_RULE_TEXTUAL_HDRS}
)
endif()

# Alias the iree_package_name library to iree::package::name.
# This lets us more clearly map to Bazel and makes it possible to
# disambiguate the underscores in paths vs. the separators.
if(_DEBUG_IREE_PACKAGE_NAME)
message(STATUS " + alias ${_PACKAGE_NS}::${_RULE_NAME}")
endif()
add_library(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME})
iree_add_alias_library(${_PACKAGE_NS}::${_RULE_NAME} ${_NAME})

if(NOT "${_PACKAGE_NS}" STREQUAL "")
# If the library name matches the final component of the package then treat
# it as a default. For example, foo/bar/ library 'bar' would end up as
# 'foo::bar'.
iree_package_dir(_PACKAGE_DIR)
if("${_RULE_NAME}" STREQUAL "${_PACKAGE_DIR}")
add_library(${_PACKAGE_NS} ALIAS ${_NAME})
iree_add_alias_library(${_PACKAGE_NS} ${_NAME})
endif()
endif()
endfunction()
Expand Down Expand Up @@ -428,17 +435,21 @@ function(iree_cc_unified_library)
$<TARGET_PROPERTY:${_RULE_ROOT},INTERFACE_LINK_LIBRARIES>
)

iree_install_targets(
TARGETS ${_NAME}
)

# Alias the iree_package_name library to iree::package::name.
# This lets us more clearly map to Bazel and makes it possible to
# disambiguate the underscores in paths vs. the separators.
add_library(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME})
iree_add_alias_library(${_PACKAGE_NS}::${_RULE_NAME} ${_NAME})

# If the library name matches the final component of the package then treat
# it as a default. For example, foo/bar/ library 'bar' would end up as
# 'foo::bar'.
iree_package_dir(_PACKAGE_DIR)
if(${_RULE_NAME} STREQUAL ${_PACKAGE_DIR})
add_library(${_PACKAGE_NS} ALIAS ${_NAME})
iree_add_alias_library(${_PACKAGE_NS} ${_NAME})
endif()
endfunction()

Expand Down
Loading

0 comments on commit 15c306f

Please sign in to comment.