Skip to content

Commit

Permalink
BUG: Condition building python stubs on python wrapping
Browse files Browse the repository at this point in the history
Only build the python stub files if python is being wrapped.

Provide conditionals so that the python artifacts are only generated
when python wrapping is requested.
  • Loading branch information
hjmjohnson committed Dec 9, 2021
1 parent f732f05 commit 4f33aab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
70 changes: 49 additions & 21 deletions Wrapping/Generators/SwigInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,14 @@ macro(itk_end_wrap_module_swig_interface)
set(mdx_file "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${WRAPPER_LIBRARY_NAME}.mdx")
set(module_interface_file "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${WRAPPER_LIBRARY_NAME}.i")
set(module_interface_ext_file "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${WRAPPER_LIBRARY_NAME}_ext.i")
set(ITK_STUB_DIR "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs")

set(ITK_STUB_PYI_FILES )
if(${module_prefix}_WRAP_PYTHON)
set(ITK_STUB_DIR "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs")
set(ITK_STUB_PYI_FILES)
else()
unset(ITK_STUB_DIR)
unset(ITK_STUB_PYI_FILES)
endif()

foreach(module ${SWIG_INTERFACE_MODULES})
# create the swig interface
Expand All @@ -326,7 +331,9 @@ macro(itk_end_wrap_module_swig_interface)
list(APPEND idx_files "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${module}.idx")
list(APPEND typedef_in_files "${WRAPPER_LIBRARY_OUTPUT_DIR}/${module}SwigInterface.h.in")
list(APPEND typedef_files "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${module}SwigInterface.h")
list(APPEND ITK_STUB_PYI_FILES "${ITK_STUB_DIR}/${module}.pyi")
if(${module_prefix}_WRAP_PYTHON)
list(APPEND ITK_STUB_PYI_FILES "${ITK_STUB_DIR}/${module}.pyi")
endif()


if(${module_prefix}_WRAP_EXPLICIT)
Expand Down Expand Up @@ -354,7 +361,10 @@ macro(itk_end_wrap_module_swig_interface)

list(LENGTH i_files number_interface_files)
if(number_interface_files GREATER 0)

if(${module_prefix}_WRAP_PYTHON)
set(ITK_STUB_DIR "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs")
set(ITK_STUB_PYI_FILE "${ITK_STUB_DIR}/${WRAPPER_LIBRARY_NAME}.pyi")
# NOTE: snake_case_config_file is both an input and an output to this command.
# the ${IGENERATOR} script appends to this file.
# NOTE: The Configuration files should be placed in the itk package directory.
Expand All @@ -363,28 +373,46 @@ macro(itk_end_wrap_module_swig_interface)
)
set(snake_case_config_file
"${ITK_WRAP_PYTHON_SNAKE_CONFIG_DIR}/${WRAPPER_LIBRARY_NAME}_snake_case.py")
add_custom_command(
OUTPUT ${i_files} ${typedef_files} ${idx_files} ${snake_case_config_file} ${ITK_STUB_PYI_FILES}
COMMAND ${Python3_EXECUTABLE} ${IGENERATOR}
${mdx_opts}
${swig_libs}
-w1 -w3 -w51 -w52 -w53 -w54
-A protected -A private
-p ${PYGCCXML_DIR}
-g ${CASTXML_EXECUTABLE}
--snake-case-file "${snake_case_config_file}"
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
--pyi_dir "${ITK_STUB_DIR}"
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
VERBATIM
)
else()
unset(ITK_STUB_DIR)
unset(ITK_STUB_PYI_FILE)
unset(snake_case_config_file)
add_custom_command(
OUTPUT ${i_files} ${typedef_files} ${idx_files}
COMMAND ${Python3_EXECUTABLE} ${IGENERATOR}
${mdx_opts}
${swig_libs}
-w1 -w3 -w51 -w52 -w53 -w54
-A protected -A private
-p ${PYGCCXML_DIR}
-g ${CASTXML_EXECUTABLE}
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
VERBATIM
)
endif()

add_custom_command(
OUTPUT ${i_files} ${typedef_files} ${idx_files} ${snake_case_config_file} ${ITK_STUB_PYI_FILES}
COMMAND ${Python3_EXECUTABLE} ${IGENERATOR}
${mdx_opts}
${swig_libs}
-w1 -w3 -w51 -w52 -w53 -w54
-A protected -A private
-p ${PYGCCXML_DIR}
-g ${CASTXML_EXECUTABLE}
--snake-case-file "${snake_case_config_file}"
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
--pyi_dir "${ITK_STUB_DIR}"
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
VERBATIM
)
endif()

# the ${WRAPPER_LIBRARY_NAME}Swig target
Expand Down
14 changes: 9 additions & 5 deletions Wrapping/Generators/SwigInterface/igenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,13 +1906,16 @@ def write_common_init_interface_file(interface_dir_path: Path) -> None:
"--pyi_dir",
action="store",
dest="pyi_dir",
default="",
type=str,
help="The directory for .pyi files to be generated",
)

options = argParser.parse_args()

# Ensure that the requested stub file directory exists
Path(f"{options.pyi_dir}").mkdir(exist_ok=True, parents=True)
if options.pyi_dir != "":
Path(options.pyi_dir).mkdir(exist_ok=True, parents=True)

sys.path.insert(1, options.pygccxml_path)
import pygccxml
Expand Down Expand Up @@ -2007,11 +2010,13 @@ def generate_swig_input(submoduleName, classes):

for submoduleName in ordered_submodule_list:
generate_swig_input(submoduleName, classes)
init_submodule_pyi_file(
if options.pyi_dir != "":
init_submodule_pyi_file(
Path(f"{options.pyi_dir}/{submoduleName}.pyi"), submoduleName
)
)

for itk_class in classes.keys():
if options.pyi_dir != "":
for itk_class in classes.keys():
outputPYIHeaderFile = StringIO()
outputPYIMethodFile = StringIO()
generate_class_pyi_def(
Expand All @@ -2025,7 +2030,6 @@ def generate_swig_input(submoduleName, classes):
outputPYIMethodFile.getvalue(),
)

write_common_init_interface_file(Path(options.pyi_dir))

snake_case_file = options.snake_case_file
if len(snake_case_file) > 1:
Expand Down

0 comments on commit 4f33aab

Please sign in to comment.