|
| 1 | +# Preprocess boost/mp11 headers to allow using specific version of mp11 (as |
| 2 | +# defined in these project's cmake files) within SYCL library w/o risk of |
| 3 | +# conflict with user program using another version of boost/mp11. Basically, |
| 4 | +# this transformation moves all APIs from boost namespace to sycl::boost and |
| 5 | +# adds SYCL_ prefix to boost macros names. See more specific comments in the |
| 6 | +# code below. |
| 7 | +# Variables which must be set by the caller to control behavior of this module: |
| 8 | +# - IN |
| 9 | +# The source directory with mp11 headers, must contain mp11.h. |
| 10 | +# - OUT |
| 11 | +# The destination directory where preprocessed source headers are put. |
| 12 | +# - SRC_PATH |
| 13 | +# An URL or directory name the source directory originates from. Used only in |
| 14 | +# generated README text. |
| 15 | +# - SRC_ID |
| 16 | +# Git hash/tag or other ID identifying the original source. Used only in |
| 17 | +# generated README text. |
| 18 | +# |
| 19 | +# Assumed to be invoked as a script: |
| 20 | +# ${CMAKE_COMMAND} -DIN=... -DOUT=... -DSRC_PATH=... -DSRC_ID=... -P <this file> |
| 21 | + |
| 22 | +function(preprocess_mp11_header) |
| 23 | + cmake_parse_arguments( |
| 24 | + MP11_HDR # prefix |
| 25 | + "" # options |
| 26 | + "SRC_NAME;DST_NAME;IN_DIR" # one value keywords |
| 27 | + "" # multi-value keywords |
| 28 | + ${ARGN}) # arguments |
| 29 | + file(READ ${MP11_HDR_SRC_NAME} FILE_CONTENTS) |
| 30 | + |
| 31 | + # 1) replace `BOOST_*` macros with `SYCL_BOOST_*`. |
| 32 | + string(REGEX REPLACE |
| 33 | + "([ \t\n\r!])BOOST_" |
| 34 | + "\\1SYCL_DETAIL_BOOST_" |
| 35 | + FILE_CONTENTS "${FILE_CONTENTS}") |
| 36 | + # 2) replace `namespace boost { ... }` with |
| 37 | + # `namespace sycl { namespace detail { namespace boost { ... } } }` |
| 38 | + string(REGEX REPLACE |
| 39 | + "(\n[ \t]*namespace[ \t\n\r]+boost)" |
| 40 | + "namespace sycl\n{\nnamespace detail\n{\\1" |
| 41 | + FILE_CONTENTS "${FILE_CONTENTS}") |
| 42 | + # ... use '} // namespace boost' as a marker for end-of-scope '}' replacement |
| 43 | + string(REGEX REPLACE |
| 44 | + "(\n[ \t]*}[ \t]*//[ \t]*namespace[ \t]+boost[ \t]*\n)" |
| 45 | + "\\1} // namespace detail\n} // namespace sycl\n" |
| 46 | + FILE_CONTENTS "${FILE_CONTENTS}") |
| 47 | + # 3) replace `boost` in `#include <boost/...>` or `#include "boost/..."` with |
| 48 | + # `sycl/detail/boost` |
| 49 | + string(REGEX REPLACE |
| 50 | + "(\n#include[ \t]*[<\"])boost" |
| 51 | + "\\1sycl/detail/boost" |
| 52 | + FILE_CONTENTS "${FILE_CONTENTS}") |
| 53 | + |
| 54 | + set(SYCL_DERIVED_COPYRIGHT_NOTICE "\ |
| 55 | + // -*- C++ -*-\n\ |
| 56 | + //===----------------------------------------------------------------------===//\n\ |
| 57 | + // Modifications Copyright Intel Corporation 2022\n\ |
| 58 | + // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n\ |
| 59 | + //===----------------------------------------------------------------------===//\n\ |
| 60 | + // Auto-generated from boost/mp11 sources https://github.com/boostorg/mp11\n\n") |
| 61 | + |
| 62 | + # 4) add proper copyright notice atop |
| 63 | + string(PREPEND FILE_CONTENTS ${SYCL_DERIVED_COPYRIGHT_NOTICE}) |
| 64 | + file(WRITE ${MP11_HDR_DST_NAME} "${FILE_CONTENTS}") |
| 65 | +endfunction(preprocess_mp11_header) |
| 66 | + |
| 67 | +function(preprocess_mp11_headers) |
| 68 | + cmake_parse_arguments( |
| 69 | + MP11_HDRS # prefix |
| 70 | + "" # options |
| 71 | + "IN;OUT;SRC_PATH;SRC_ID" # one value keywords |
| 72 | + "" # multi-value keywords |
| 73 | + ${ARGN}) # arguments |
| 74 | + |
| 75 | + # 1) Perform necessary preprocessing of headers. |
| 76 | + file(GLOB_RECURSE BOOST_MP11_SOURCES "${MP11_HDRS_IN}/*") |
| 77 | + |
| 78 | + foreach(SRC ${BOOST_MP11_SOURCES}) |
| 79 | + string(REPLACE "${MP11_HDRS_IN}" "${MP11_HDRS_OUT}" DST "${SRC}") |
| 80 | + preprocess_mp11_header( |
| 81 | + SRC_NAME ${SRC} |
| 82 | + DST_NAME ${DST} |
| 83 | + IN_DIR ${MP11_HDRS_IN} |
| 84 | + ) |
| 85 | + endforeach(SRC ${BOOST_MP11_SOURCES}) |
| 86 | + |
| 87 | + # 2) Add SYCL_README.txt to the output directory root |
| 88 | + set(SYCL_README_TEXT "\ |
| 89 | + This directory contains boost/mp11 headers imported from\n\ |
| 90 | + ${MP11_HDRS_SRC_PATH} (${MP11_HDRS_SRC_ID})\n\ |
| 91 | + and adapted for use in SYCL headers in a way that does not conflict with\n\ |
| 92 | + potential use of boost in user code. Particularly, `BOOST_*` macros are\n\ |
| 93 | + replaced with `SYCL_DETAIL_BOOST_*`, APIs are moved into the top-level |
| 94 | + `sycl::detail` namespace. For example, `sycl::detail::boost::mp11::mp_list`.\n") |
| 95 | + |
| 96 | + set(SYCL_README_FILE_NAME "${MP11_HDRS_OUT}/README.txt") |
| 97 | + |
| 98 | + file(WRITE ${SYCL_README_FILE_NAME} "${SYCL_README_TEXT}") |
| 99 | +endfunction(preprocess_mp11_headers) |
| 100 | + |
| 101 | +preprocess_mp11_headers( |
| 102 | + IN ${IN} |
| 103 | + OUT ${OUT} |
| 104 | + SRC_PATH ${SRC_PATH} |
| 105 | + SRC_ID ${SRC_ID} |
| 106 | +) |
0 commit comments