-
Notifications
You must be signed in to change notification settings - Fork 603
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
Add a dedicated iree_c_module CMake module #5214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
include(CMakeParseArguments) | ||
|
||
# iree_c_module() | ||
# | ||
# Parameters: | ||
# NAME: Name of target (see Note). | ||
# SRC: MLIR source file to compile into a c module. | ||
# H_FILE_OUTPUT: The H header file to output. | ||
# TRANSLATE_TOOL: Translation tool to invoke (CMake target). The default | ||
# tool is "iree-translate". | ||
# FLAGS: Flags to pass to the translation tool (list of strings). The | ||
# default flag set is "-iree-vm-ir-to-c-module". | ||
# TESTONLY: When added, this target will only be built if user passes | ||
# -DIREE_BUILD_TESTS=ON to CMake. | ||
# | ||
# Note: | ||
# By default, iree_c_module will create a library named ${NAME}, | ||
# and alias target iree::${NAME}. The iree:: form should always be used. | ||
# This is to reduce namespace pollution. | ||
function(iree_c_module) | ||
cmake_parse_arguments( | ||
_RULE | ||
"TESTONLY" | ||
"NAME;SRC;H_FILE_OUTPUT;TRANSLATE_TOOL" | ||
"FLAGS" | ||
${ARGN} | ||
) | ||
|
||
if(_RULE_TESTONLY AND NOT IREE_BUILD_TESTS) | ||
return() | ||
endif() | ||
|
||
# Replace dependencies passed by ::name with iree::package::name | ||
iree_package_ns(_PACKAGE_NS) | ||
list(TRANSFORM _RULE_DEPS REPLACE "^::" "${_PACKAGE_NS}::") | ||
|
||
# Prefix the library with the package name, so we get: iree_package_name. | ||
iree_package_name(_PACKAGE_NAME) | ||
set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}") | ||
|
||
# Set defaults for FLAGS and TRANSLATE_TOOL | ||
if(DEFINED _RULE_FLAGS) | ||
set(_FLAGS ${_RULE_FLAGS}) | ||
else() | ||
set(_FLAGS "-iree-vm-ir-to-c-module") | ||
endif() | ||
if(DEFINED _RULE_TRANSLATE_TOOL) | ||
set(_TRANSLATE_TOOL ${_RULE_TRANSLATE_TOOL}) | ||
else() | ||
set(_TRANSLATE_TOOL "iree-translate") | ||
endif() | ||
|
||
iree_get_executable_path(_TRANSLATE_TOOL_EXECUTABLE ${_TRANSLATE_TOOL}) | ||
|
||
set(_ARGS "${_FLAGS}") | ||
list(APPEND _ARGS "${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_SRC}") | ||
list(APPEND _ARGS "-o") | ||
list(APPEND _ARGS "${_RULE_H_FILE_OUTPUT}") | ||
|
||
add_custom_command( | ||
OUTPUT "${_RULE_H_FILE_OUTPUT}" | ||
COMMAND ${_TRANSLATE_TOOL_EXECUTABLE} ${_ARGS} | ||
# Changes to either the translation tool or the input source should | ||
# trigger rebuilding. | ||
DEPENDS ${_TRANSLATE_TOOL_EXECUTABLE} ${_RULE_SRC} | ||
) | ||
|
||
set(_GEN_TARGET "${_NAME}_gen") | ||
add_custom_target( | ||
${_GEN_TARGET} | ||
DEPENDS | ||
${_RULE_H_FILE_OUTPUT} | ||
) | ||
Comment on lines
+83
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that if the only thing that depends on a generated file from This blog post is from 2015, but I think may still be relevant https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/ I think that interface libraries do support sources (although the documentation is somewhat confused). https://cmake.org/cmake/help/v3.13/command/add_library.html#interface-libraries. We don't appear to use that functionality in iree_cc_library, but I think we maybe should. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed in discord. What we have here works and other things appear not to. The extra wrinkle of this being a generated header file (and therefore not compiled), means that my suggestion here doesn't work. |
||
|
||
add_library(${_NAME} INTERFACE) | ||
add_dependencies(${_NAME} ${_GEN_TARGET}) | ||
|
||
# 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_package_dir(_PACKAGE_DIR) | ||
if(${_RULE_NAME} STREQUAL ${_PACKAGE_DIR}) | ||
# If the library name matches the package then treat it as a default. | ||
# For example, foo/bar/ library 'bar' would end up as 'foo::bar'. | ||
add_library(${_PACKAGE_NS} ALIAS ${_NAME}) | ||
endif() | ||
endfunction() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it would be just as clear to include the command args in the call to
add_custom_command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nearly a takeover from https://github.com/google/iree/blob/881de81238971869d6d62cabf8dc0b54a17be704/build_tools/cmake/iree_bytecode_module.cmake#L64%C3%9FL77.
I have no preference, but I think it might be nice to keep it consistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair, though I don't think consistency in this case really buys us much. I don't care too much either way.