|
1 |
| -# The OffloadGenerate target is used to regenerate the generated files in the |
2 |
| -# include directory. These files are checked in with the rest of the source, |
3 |
| -# therefore it is only needed when making changes to the API. |
| 1 | +# We want to clang-format the generated files if possible, since OffloadAPI.h is |
| 2 | +# the main public header for liboffload. Generate them in a temporary location, |
| 3 | +# then clang-format and copy them to the proper location. If clang-format is |
| 4 | +# missing just copy them. |
| 5 | +# Ideally we'd just clang-format them in place and avoid the copy but cmake |
| 6 | +# gets confused about the same path being a byproduct of two custom commands. |
4 | 7 |
|
5 |
| -find_program(CLANG_FORMAT clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) |
6 |
| -if (CLANG_FORMAT) |
7 |
| - set(LLVM_TARGET_DEFINITIONS ${CMAKE_CURRENT_SOURCE_DIR}/OffloadAPI.td) |
| 8 | +set(LLVM_TARGET_DEFINITIONS ${CMAKE_CURRENT_SOURCE_DIR}/OffloadAPI.td) |
| 9 | +set(files_to_copy "") |
8 | 10 |
|
9 |
| - tablegen(OFFLOAD OffloadAPI.h -gen-api) |
10 |
| - tablegen(OFFLOAD OffloadEntryPoints.inc -gen-entry-points) |
11 |
| - tablegen(OFFLOAD OffloadFuncs.inc -gen-func-names) |
12 |
| - tablegen(OFFLOAD OffloadImplFuncDecls.inc -gen-impl-func-decls) |
13 |
| - tablegen(OFFLOAD OffloadPrint.hpp -gen-print-header) |
14 |
| - tablegen(OFFLOAD OffloadErrcodes.inc -gen-errcodes) |
| 11 | +macro(offload_tablegen file) |
| 12 | + tablegen(OFFLOAD generated/${file}.gen ${ARGN}) |
| 13 | + list(APPEND files_to_copy ${file}) |
| 14 | +endmacro() |
15 | 15 |
|
16 |
| - set(FILES_TO_COPY "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp") |
17 |
| - set(GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated) |
18 |
| - add_public_tablegen_target(OffloadGenerate) |
19 |
| - add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT} |
20 |
| - -i ${TABLEGEN_OUTPUT}) |
21 |
| - add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND} |
22 |
| - -E copy_if_different ${FILES_TO_COPY} ${GEN_DIR}) |
23 |
| - add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND} |
24 |
| - -E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc") |
| 16 | +offload_tablegen(OffloadAPI.h -gen-api) |
| 17 | +offload_tablegen(OffloadEntryPoints.inc -gen-entry-points) |
| 18 | +offload_tablegen(OffloadFuncs.inc -gen-func-names) |
| 19 | +offload_tablegen(OffloadImplFuncDecls.inc -gen-impl-func-decls) |
| 20 | +offload_tablegen(OffloadPrint.hpp -gen-print-header) |
| 21 | + |
| 22 | +add_public_tablegen_target(OffloadGenerate) |
| 23 | + |
| 24 | +add_custom_target(OffloadAPI DEPENDS OffloadGenerate) |
| 25 | +find_program(clang_format clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH) |
| 26 | +if (clang_format) |
| 27 | + foreach(file IN LISTS files_to_copy) |
| 28 | + add_custom_command( |
| 29 | + OUTPUT ${file} |
| 30 | + COMMAND ${clang_format} -i generated/${file}.gen |
| 31 | + COMMAND ${CMAKE_COMMAND} -E copy_if_different generated/${file}.gen ${CMAKE_CURRENT_BINARY_DIR}/${file} |
| 32 | + DEPENDS generated/${file}.gen |
| 33 | + ) |
| 34 | + add_custom_target(OffloadAPI.${file} DEPENDS ${file}) |
| 35 | + add_dependencies(OffloadAPI OffloadAPI.${file}) |
| 36 | + endforeach() |
25 | 37 | else()
|
26 |
| - message(WARNING "clang-format was not found, so the OffloadGenerate target\ |
27 |
| - will not be available. Offload will still build, but you will not be\ |
28 |
| - able to make changes to the API.") |
| 38 | + message(WARNING "clang-format not found, the generated Offload API headers will not be formatted") |
| 39 | + foreach(file IN LISTS files_to_copy) |
| 40 | + add_custom_command( |
| 41 | + OUTPUT ${file} |
| 42 | + COMMAND ${CMAKE_COMMAND} -E copy_if_different generated/${file}.gen ${CMAKE_CURRENT_BINARY_DIR}/${file} |
| 43 | + DEPENDS generated/${file}.gen |
| 44 | + ) |
| 45 | + endforeach() |
29 | 46 | endif()
|
0 commit comments