Skip to content

[Offload] Correctly regenerate API files if modified #141679

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 26 additions & 10 deletions offload/liboffload/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# include directory. These files are checked in with the rest of the source,
# therefore it is only needed when making changes to the API.

find_program(CLANG_FORMAT clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
if (CLANG_FORMAT)
find_program(clang_format clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_program creates an entry into the CMakeCache.txt. By convention, those are upper case.

if (clang_format)
set(LLVM_TARGET_DEFINITIONS ${CMAKE_CURRENT_SOURCE_DIR}/OffloadAPI.td)

tablegen(OFFLOAD OffloadAPI.h -gen-api)
Expand All @@ -13,15 +13,31 @@ if (CLANG_FORMAT)
tablegen(OFFLOAD OffloadPrint.hpp -gen-print-header)
tablegen(OFFLOAD OffloadErrcodes.inc -gen-errcodes)

set(FILES_TO_COPY "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
set(GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
set(files_to_copy "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
set(generated_dir ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
set(shared_dir ${LIBOMPTARGET_INCLUDE_DIR}/Shared)
add_public_tablegen_target(OffloadGenerate)
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT}
-i ${TABLEGEN_OUTPUT})
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different ${FILES_TO_COPY} ${GEN_DIR})
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be missing after the change. @RossBrunton I'm not sure why it's separate from the others, is it because the output directory is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this confused me because there's no such file in this directory and there's already a .inc file there. So I'm pretty sure this does nothing so I removed it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RossBrunton it's safe to delete this, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here because Shared/OffloadErrcodes.inc is in a different directory, yes. I still think it's required though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so this is generated like the others but installed there. We really should just tablegen these on demand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very nearly have a PR ready to do that, should be up later today

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


add_custom_target(OffloadAPI DEPENDS OffloadGenerate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_custom_target(OffloadAPI DEPENDS OffloadGenerate)
add_custom_target(OffloadAPI)
add_dependencies(OffloadAPI OffloadGenerate)

DEPENDS is for file-level dependencies

add_custom_command(
OUTPUT ${shared_dir}/OffloadErrcodes.inc
COMMAND ${clang_format} -i OffloadErrcodes.inc
COMMAND ${CMAKE_COMMAND} -E copy_if_different OffloadErrcodes.inc ${shared_dir}
DEPENDS OffloadErrcodes.inc
)
add_custom_target(OffloadAPI.OffloadErrcodes.inc DEPENDS ${shared_dir}/OffloadErrcodes.inc)
add_dependencies(OffloadAPI OffloadAPI.OffloadErrcodes.inc)
foreach(file IN LISTS files_to_copy)
add_custom_command(
OUTPUT ${generated_dir}/${file}
COMMAND ${clang_format} -i ${file}
COMMAND ${CMAKE_COMMAND} -E make_directory ${generated_dir}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${file} ${generated_dir}
DEPENDS ${file}
)
add_custom_target(OffloadAPI.${file} DEPENDS ${generated_dir}/${file})
add_dependencies(OffloadAPI OffloadAPI.${file})
endforeach()
else()
message(WARNING "clang-format was not found, so the OffloadGenerate target\
will not be available. Offload will still build, but you will not be\
Expand Down
15 changes: 10 additions & 5 deletions offload/liboffload/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ if(LLVM_HAVE_LINK_VERSION_SCRIPT)
endif()

target_include_directories(LLVMOffload PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include/generated
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include)
${CMAKE_CURRENT_BINARY_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include/generated
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include)
Comment on lines +22 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] unrelated noise


target_compile_options(LLVMOffload PRIVATE ${offload_compile_flags})
target_link_options(LLVMOffload PRIVATE ${offload_link_flags})
Expand All @@ -33,6 +33,11 @@ target_compile_definitions(LLVMOffload PRIVATE
DEBUG_PREFIX="Liboffload"
)

# Make sure these are up-to-date if modified.
if(TARGET OffloadAPI)
add_dependencies(LLVMOffload OffloadAPI)
endif()

set_target_properties(LLVMOffload PROPERTIES
POSITION_INDEPENDENT_CODE ON
INSTALL_RPATH "$ORIGIN"
Expand Down
Loading