Skip to content

Commit 4f33aab

Browse files
committed
BUG: Condition building python stubs on python wrapping
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.
1 parent f732f05 commit 4f33aab

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

Wrapping/Generators/SwigInterface/CMakeLists.txt

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,14 @@ macro(itk_end_wrap_module_swig_interface)
315315
set(mdx_file "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${WRAPPER_LIBRARY_NAME}.mdx")
316316
set(module_interface_file "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${WRAPPER_LIBRARY_NAME}.i")
317317
set(module_interface_ext_file "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${WRAPPER_LIBRARY_NAME}_ext.i")
318-
set(ITK_STUB_DIR "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs")
319318

320-
set(ITK_STUB_PYI_FILES )
319+
if(${module_prefix}_WRAP_PYTHON)
320+
set(ITK_STUB_DIR "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs")
321+
set(ITK_STUB_PYI_FILES)
322+
else()
323+
unset(ITK_STUB_DIR)
324+
unset(ITK_STUB_PYI_FILES)
325+
endif()
321326

322327
foreach(module ${SWIG_INTERFACE_MODULES})
323328
# create the swig interface
@@ -326,7 +331,9 @@ macro(itk_end_wrap_module_swig_interface)
326331
list(APPEND idx_files "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${module}.idx")
327332
list(APPEND typedef_in_files "${WRAPPER_LIBRARY_OUTPUT_DIR}/${module}SwigInterface.h.in")
328333
list(APPEND typedef_files "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}/${module}SwigInterface.h")
329-
list(APPEND ITK_STUB_PYI_FILES "${ITK_STUB_DIR}/${module}.pyi")
334+
if(${module_prefix}_WRAP_PYTHON)
335+
list(APPEND ITK_STUB_PYI_FILES "${ITK_STUB_DIR}/${module}.pyi")
336+
endif()
330337

331338

332339
if(${module_prefix}_WRAP_EXPLICIT)
@@ -354,7 +361,10 @@ macro(itk_end_wrap_module_swig_interface)
354361

355362
list(LENGTH i_files number_interface_files)
356363
if(number_interface_files GREATER 0)
364+
357365
if(${module_prefix}_WRAP_PYTHON)
366+
set(ITK_STUB_DIR "${ITK_DIR}/Wrapping/Generators/Python/itk-stubs")
367+
set(ITK_STUB_PYI_FILE "${ITK_STUB_DIR}/${WRAPPER_LIBRARY_NAME}.pyi")
358368
# NOTE: snake_case_config_file is both an input and an output to this command.
359369
# the ${IGENERATOR} script appends to this file.
360370
# NOTE: The Configuration files should be placed in the itk package directory.
@@ -363,28 +373,46 @@ macro(itk_end_wrap_module_swig_interface)
363373
)
364374
set(snake_case_config_file
365375
"${ITK_WRAP_PYTHON_SNAKE_CONFIG_DIR}/${WRAPPER_LIBRARY_NAME}_snake_case.py")
376+
add_custom_command(
377+
OUTPUT ${i_files} ${typedef_files} ${idx_files} ${snake_case_config_file} ${ITK_STUB_PYI_FILES}
378+
COMMAND ${Python3_EXECUTABLE} ${IGENERATOR}
379+
${mdx_opts}
380+
${swig_libs}
381+
-w1 -w3 -w51 -w52 -w53 -w54
382+
-A protected -A private
383+
-p ${PYGCCXML_DIR}
384+
-g ${CASTXML_EXECUTABLE}
385+
--snake-case-file "${snake_case_config_file}"
386+
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
387+
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
388+
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
389+
--pyi_dir "${ITK_STUB_DIR}"
390+
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
391+
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
392+
VERBATIM
393+
)
366394
else()
395+
unset(ITK_STUB_DIR)
396+
unset(ITK_STUB_PYI_FILE)
367397
unset(snake_case_config_file)
398+
add_custom_command(
399+
OUTPUT ${i_files} ${typedef_files} ${idx_files}
400+
COMMAND ${Python3_EXECUTABLE} ${IGENERATOR}
401+
${mdx_opts}
402+
${swig_libs}
403+
-w1 -w3 -w51 -w52 -w53 -w54
404+
-A protected -A private
405+
-p ${PYGCCXML_DIR}
406+
-g ${CASTXML_EXECUTABLE}
407+
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
408+
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
409+
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
410+
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
411+
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
412+
VERBATIM
413+
)
368414
endif()
369415

370-
add_custom_command(
371-
OUTPUT ${i_files} ${typedef_files} ${idx_files} ${snake_case_config_file} ${ITK_STUB_PYI_FILES}
372-
COMMAND ${Python3_EXECUTABLE} ${IGENERATOR}
373-
${mdx_opts}
374-
${swig_libs}
375-
-w1 -w3 -w51 -w52 -w53 -w54
376-
-A protected -A private
377-
-p ${PYGCCXML_DIR}
378-
-g ${CASTXML_EXECUTABLE}
379-
--snake-case-file "${snake_case_config_file}"
380-
--interface-output-dir "${WRAPPER_MASTER_INDEX_OUTPUT_DIR}"
381-
--library-output-dir "${WRAPPER_LIBRARY_OUTPUT_DIR}"
382-
--submodule-order "${WRAPPER_SUBMODULE_ORDER}"
383-
--pyi_dir "${ITK_STUB_DIR}"
384-
DEPENDS ${ITK_WRAP_DOC_DOCSTRING_FILES} ${xml_files} ${IGENERATOR} ${typedef_in_files}
385-
WORKING_DIRECTORY / # Arguments to WORKING_DIRECTORY may use generator expressions
386-
VERBATIM
387-
)
388416
endif()
389417

390418
# the ${WRAPPER_LIBRARY_NAME}Swig target

Wrapping/Generators/SwigInterface/igenerator.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,13 +1906,16 @@ def write_common_init_interface_file(interface_dir_path: Path) -> None:
19061906
"--pyi_dir",
19071907
action="store",
19081908
dest="pyi_dir",
1909+
default="",
1910+
type=str,
19091911
help="The directory for .pyi files to be generated",
19101912
)
19111913

19121914
options = argParser.parse_args()
19131915

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

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

20082011
for submoduleName in ordered_submodule_list:
20092012
generate_swig_input(submoduleName, classes)
2010-
init_submodule_pyi_file(
2013+
if options.pyi_dir != "":
2014+
init_submodule_pyi_file(
20112015
Path(f"{options.pyi_dir}/{submoduleName}.pyi"), submoduleName
2012-
)
2016+
)
20132017

2014-
for itk_class in classes.keys():
2018+
if options.pyi_dir != "":
2019+
for itk_class in classes.keys():
20152020
outputPYIHeaderFile = StringIO()
20162021
outputPYIMethodFile = StringIO()
20172022
generate_class_pyi_def(
@@ -2025,7 +2030,6 @@ def generate_swig_input(submoduleName, classes):
20252030
outputPYIMethodFile.getvalue(),
20262031
)
20272032

2028-
write_common_init_interface_file(Path(options.pyi_dir))
20292033

20302034
snake_case_file = options.snake_case_file
20312035
if len(snake_case_file) > 1:

0 commit comments

Comments
 (0)