From 4e8892b61bab91ee951018de6e72b9b4fa0f50c9 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental@gmail.com>
Date: Sun, 18 Aug 2024 03:25:46 -0500
Subject: [PATCH] add boost fetch-content

---
 .github/workflows/ci.yml                      | 11 ----
 cmake/iree_aie_xrt.cmake                      | 66 +++++++++----------
 .../iree-amd-aie/Target/CMakeLists.txt        |  8 +--
 iree_compiler_plugin.cmake                    |  2 -
 4 files changed, 31 insertions(+), 56 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0ffe5b87c5..3bd990a83f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -64,17 +64,6 @@ jobs:
           ninja: true
           ccache: true
 
-      - name: Install boost
-        if: ${{ matrix.os == 'windows' }}
-        uses: MarkusJx/install-boost@v2.4.5
-        id: install-boost
-        with:
-          boost_install_dir: ${{ github.workspace }}
-          boost_version: 1.78.0
-          platform_version: 2019
-          toolset: msvc
-          link: shared
-
       - uses: actions/setup-python@v4
         if: ${{ matrix.os == 'windows' }}
         with:
diff --git a/cmake/iree_aie_xrt.cmake b/cmake/iree_aie_xrt.cmake
index 950111af61..c70f1b2b17 100644
--- a/cmake/iree_aie_xrt.cmake
+++ b/cmake/iree_aie_xrt.cmake
@@ -4,23 +4,38 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-include(${CMAKE_CURRENT_LIST_DIR}/iree_aie_utils.cmake)
-
 if(TARGET iree-aie-xclbinutil)
   return()
 endif()
 
-if(WIN32)
-  set(STATIC_OR_SHARED ON)
-else()
-  # R_X86_64_PC32 relocation at offset 0x684 against symbol
-  #`boost::system::detail::cat_holder<void>::system_category_instance' can not be used; recompile with -fPIC
-  set(STATIC_OR_SHARED OFF)
-endif()
-set(Boost_USE_STATIC_LIBS ${SHARED_OR_STATIC} CACHE BOOL "" FORCE)
+include(${CMAKE_CURRENT_LIST_DIR}/iree_aie_utils.cmake)
+
+include(FetchContent)
 find_package(Threads REQUIRED)
-find_package(Boost REQUIRED COMPONENTS filesystem program_options system)
-message(STATUS "Boost include directories:" ${Boost_INCLUDE_DIRS})
+set(Boost_USE_STATIC_LIBS ON)
+set(BOOST_ENABLE_CMAKE ON)
+set(FETCHCONTENT_QUIET FALSE) # Needed to print downloading progress
+FetchContent_Declare(
+  Boost
+  URL https://github.com/boostorg/boost/releases/download/boost-1.81.0/boost-1.81.0.7z
+  USES_TERMINAL_DOWNLOAD TRUE
+  GIT_PROGRESS TRUE
+  DOWNLOAD_NO_EXTRACT FALSE)
+FetchContent_MakeAvailable(Boost)
+set(_boost_libs
+    any
+    algorithm
+    asio
+    format
+    functional
+    lexical_cast
+    process
+    program_options
+    property_tree
+    tokenizer
+    tuple
+    uuid)
+list(TRANSFORM _boost_libs PREPEND Boost::)
 
 set(IREE_XRT_SOURCE_DIR "${IREE_AMD_AIE_SOURCE_DIR}/third_party/XRT/src")
 
@@ -28,9 +43,6 @@ if(NOT WIN32)
   find_package(RapidJSON REQUIRED)
 endif()
 
-# Note: we do not simply add the subdirectory and use the imported target because
-# XRT will build in its entirety when we do `ninja install` for IREE.
-
 # obv we have python but XRT uses this var to look for an ancient version of pybind (and fail)
 replace_string_in_file(${IREE_XRT_SOURCE_DIR}/python/pybind11/CMakeLists.txt
                        "if (HAS_PYTHON)" "if (FALSE)")
@@ -123,22 +135,11 @@ file(
   "${_xclbinutil_source_dir}/XclBinUtilMain.cxx"
 )
 
-if(WIN32)
-  set(STATIC_OR_SHARED STATIC)
-else()
-  # R_X86_64_PC32 relocation at offset 0x684 against symbol
-  #`boost::system::detail::cat_holder<void>::system_category_instance' can not be used; recompile with -fPIC
-  set(STATIC_OR_SHARED SHARED)
-endif()
-add_library(iree-aie-xclbinutil ${STATIC_OR_SHARED} ${_xclbinutil_srcs})
+add_library(iree-aie-xclbinutil STATIC ${_xclbinutil_srcs})
 target_compile_options(iree-aie-xclbinutil PRIVATE -fexceptions -frtti)
 
 set(THREADS_PREFER_PTHREAD_FLAG ON)
-set(_xclbin_libs
-    Boost::filesystem
-    Boost::program_options
-    Boost::system
-    Threads::Threads)
+set(_xclbin_libs $<BUILD_LOCAL_INTERFACE:${_boost_libs}> Threads::Threads)
 set(_xclbinutil_compile_definitions
     -DBOOST_BIND_GLOBAL_PLACEHOLDERS
     # prevents collision with bootgen's Section class
@@ -146,9 +147,6 @@ set(_xclbinutil_compile_definitions
 
 if(WIN32)
   list(APPEND _xclbinutil_compile_definitions -D"/EHsc")
-  # Uncomment if you get LINK : fatal error LNK1104: cannot open file
-  # 'libboost_filesystem-vc142-mt-gd-x64-1_74.lib'
-  # target_compile_definitions(iree-aie-xclbinutil PUBLIC BOOST_ALL_DYN_LINK)
 else()
   list(APPEND _xclbinutil_compile_definitions -DENABLE_JSON_SCHEMA_VALIDATION)
   list(APPEND _xclbin_libs $<BUILD_LOCAL_INTERFACE:transformcdo>)
@@ -160,7 +158,6 @@ target_link_libraries(iree-aie-xclbinutil
                       PRIVATE ${_xclbin_libs})
 target_include_directories(iree-aie-xclbinutil
                            PRIVATE ${XRT_BINARY_DIR}/gen
-                                   ${Boost_INCLUDE_DIRS}
                                    ${IREE_XRT_SOURCE_DIR}/runtime_src/core/include
                                    ${_xclbinutil_source_dir})
 # for some reason windows doesn't respect the standard output path without this
@@ -203,8 +200,5 @@ foreach(_core_lib IN LISTS _core_libs)
   target_include_directories(${_core_lib} SYSTEM PUBLIC
                              ${IREE_XRT_SOURCE_DIR}/runtime_src/core/common/elf)
   target_compile_definitions(${_core_lib} PRIVATE -DBOOST_BIND_GLOBAL_PLACEHOLDERS)
-  target_link_libraries(${_core_lib} PRIVATE
-                                     Boost::filesystem
-                                     Boost::program_options
-                                     Boost::system)
+  target_link_libraries(${_core_lib} PRIVATE $<BUILD_LOCAL_INTERFACE:${_boost_libs}>)
 endforeach()
diff --git a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Target/CMakeLists.txt b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Target/CMakeLists.txt
index b487003b81..1328c15618 100644
--- a/compiler/plugins/target/AMD-AIE/iree-amd-aie/Target/CMakeLists.txt
+++ b/compiler/plugins/target/AMD-AIE/iree-amd-aie/Target/CMakeLists.txt
@@ -18,15 +18,9 @@ iree_cc_library(
     iree-amd-aie::aie_runtime::iree_aie_runtime_static
     iree::target::amd-aie::Transforms
     iree-aie-bootgen
+    iree-aie-xclbinutil
 )
 
-if(IREE_AMD_AIE_ENABLE_XRT_DRIVER)
-  # on windows for some reason `DEPS` above doesn't trigger a build
-  # and also doesn't point to the .dll (points to .lib instead)
-  target_link_libraries(iree_target_amd-aie_Target_AIETargets PRIVATE iree-aie-xclbinutil)
-#  add_dependencies(iree_target_amd-aie_Target_AIETargets iree-aie-xclbinutil)
-endif()
-
 iree_cc_library(
   NAME
     Target
diff --git a/iree_compiler_plugin.cmake b/iree_compiler_plugin.cmake
index 1fe8977a5c..c69c3aad2f 100644
--- a/iree_compiler_plugin.cmake
+++ b/iree_compiler_plugin.cmake
@@ -5,8 +5,6 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
-find_package(Boost REQUIRED)
-include_directories(${Boost_INCLUDE_DIRS})
 
 set(IREE_AMD_AIE_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}")
 set(IREE_AMD_AIE_RUNTIME_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/runtime/src)