Skip to content
Snippets Groups Projects
Commit 63186277 authored by Ammar Nejati's avatar Ammar Nejati
Browse files

[a.fixSWIGWrapperBuild_i891] Avoid unnecessary rebuilding of the SWIG wrappers (Closes #891)

Merging branch 'a.fixSWIGWrapperBuild_i891'  into 'main'.

* `SwigLib.cmake` is modified so that SWIG-produced wrappers be produced once (unless dependencies are updated).  
* All SWIG-related fragments are gathered into `SwigLib.cmake`.
* The dependencies of PyCore and Fit modules are corrected.
* Example Python-scripts are installed.

Closes issue #891

See merge request !2357
parents 48092cf7 a78bd154
No related branches found
No related tags found
1 merge request!2357Avoid unnecessary rebuilding of the SWIG wrappers
Pipeline #130122 passed
...@@ -240,7 +240,6 @@ foreach(lib ${CoreComponents}) ...@@ -240,7 +240,6 @@ foreach(lib ${CoreComponents})
add_subdirectory(${lib}) add_subdirectory(${lib})
endforeach() endforeach()
if(BORNAGAIN_PYTHON) if(BORNAGAIN_PYTHON)
add_dependencies(BornAgainFit swig_runtime)
if (CONFIGURE_EXAMPLES) if (CONFIGURE_EXAMPLES)
add_subdirectory(rawEx) add_subdirectory(rawEx)
endif() endif()
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# NOTE: The module is directly dependent on Python and Numpy C-API # NOTE: The module is directly dependent on Python and Numpy C-API
if(NOT BORNAGAIN_PYTHON) if(NOT BORNAGAIN_PYTHON)
return() message(FATAL_ERROR "PyCore module requested although BORNAGAIN_PYTHON=false")
endif() endif()
set(component PyCore) set(component PyCore)
...@@ -14,6 +14,7 @@ set(lib BornAgain${component}) ...@@ -14,6 +14,7 @@ set(lib BornAgain${component})
# Python-dependent source files # Python-dependent source files
file(GLOB_RECURSE source_files */*.cpp) file(GLOB_RECURSE source_files */*.cpp)
file(GLOB_RECURSE include_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} */*.h) file(GLOB_RECURSE include_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} */*.h)
list(APPEND include_files "${AUTO_WRAP_DIR}/swig_runtime.h")
# --- define target properties --- # --- define target properties ---
add_library(${lib} SHARED) add_library(${lib} SHARED)
...@@ -24,6 +25,10 @@ set_target_properties(${lib} PROPERTIES ...@@ -24,6 +25,10 @@ set_target_properties(${lib} PROPERTIES
INCLUDE_FILES "${include_files}" INCLUDE_FILES "${include_files}"
EMBED_PYTHON ${BORNAGAIN_PYTHON}) EMBED_PYTHON ${BORNAGAIN_PYTHON})
if(CONFIGURE_BINDINGS)
add_dependencies(${lib} swig_runtime)
endif(CONFIGURE_BINDINGS)
# --- make the library --- # --- make the library ---
MakeLib(${lib}) MakeLib(${lib})
......
...@@ -11,22 +11,6 @@ function(MakeLib lib) ...@@ -11,22 +11,6 @@ function(MakeLib lib)
string(STRIP "${swigtmpdir}" swigtmpdir) string(STRIP "${swigtmpdir}" swigtmpdir)
if(withPython AND swigtmpdir)
if((CLANG) OR (GCC))
# suppress warnings from auto-generated code (last updated for Swig 4.0.1)
set_source_files_properties(${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp
PROPERTIES COMPILE_OPTIONS
"\
-Wno-unused-parameter;\
-Wno-missing-field-initializers;\
-Wno-sometimes-uninitialized;\
-Wno-deprecated-declarations")
endif()
list(APPEND source_files ${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp)
endif()
set_target_properties(${lib} PROPERTIES set_target_properties(${lib} PROPERTIES
SOURCES "${source_files}" SOURCES "${source_files}"
PREFIX ${libprefix} PREFIX ${libprefix}
......
...@@ -63,3 +63,7 @@ if(BA_PY_PACKAGE) ...@@ -63,3 +63,7 @@ if(BA_PY_PACKAGE)
Py3_LIBRARY_DIRS "${Python3_LIBRARY_DIRS}" Py3_LIBRARY_DIRS "${Python3_LIBRARY_DIRS}"
) )
endif() endif()
# install Python example scripts
install(DIRECTORY ${EXAMPLES_PUBL_DIR}
DESTINATION ${destination_share} COMPONENT Examples FILES_MATCHING PATTERN *.py )
...@@ -8,31 +8,11 @@ function(SwigLib name lib swigtmpdir) ...@@ -8,31 +8,11 @@ function(SwigLib name lib swigtmpdir)
message(FATAL_ERROR "Function SwigLib called though BORNAGAIN_PYTHON=false") message(FATAL_ERROR "Function SwigLib called though BORNAGAIN_PYTHON=false")
endif() endif()
# configure SWIG runtime (only once) message(STATUS "SwigLib ${name}: ${lib} => ${swigtmpdir}")
if(NOT TARGET swig_runtime)
message(STATUS "SwigLib: configure SWIG runtime")
if(CONFIGURE_BINDINGS) get_target_property(include_files ${lib} INCLUDE_FILES)
add_custom_command(
OUTPUT ${AUTO_WRAP_DIR}/swig_runtime.h
COMMAND ${SWIG_EXECUTABLE} -c++;-python;-external-runtime;${AUTO_WRAP_DIR}/swig_runtime.h
DEPENDS ${swig_dependencies} ${include_files}
)
endif()
add_custom_target(swig_runtime DEPENDS ${AUTO_WRAP_DIR}/swig_runtime.h)
if(BA_CPP_API)
install(FILES ${AUTO_WRAP_DIR}/swig_runtime.h
DESTINATION ${destination_include}/Wrap COMPONENT Headers)
endif()
endif()
message(STATUS "SwigLib ${name}: ${lib} ${swigtmpdir}")
if(CONFIGURE_BINDINGS) if(CONFIGURE_BINDINGS)
file(MAKE_DIRECTORY ${swigtmpdir})
# static (manually written) input files # static (manually written) input files
set(swig_dependencies set(swig_dependencies
${SWIG_DIR}/lib${lib}.i ${SWIG_DIR}/lib${lib}.i
...@@ -41,12 +21,28 @@ function(SwigLib name lib swigtmpdir) ...@@ -41,12 +21,28 @@ function(SwigLib name lib swigtmpdir)
${SWIG_DIR}/ignoreSample.i ${SWIG_DIR}/ignoreSample.i
) )
foreach(FNAM ${swig_dependencies}) foreach(fname ${swig_dependencies})
if(NOT EXISTS ${FNAM}) if(NOT EXISTS ${fname})
message(FATAL_ERROR "Could NOT find SWIG input ${FNAM}") message(FATAL_ERROR "Could NOT find SWIG input ${fname}")
endif() endif()
endforeach() endforeach()
# configure SWIG runtime (only once)
# NOTE: PyCore module depends on SWIG runtime
if(NOT TARGET swig_runtime)
message(STATUS "SwigLib: configure SWIG runtime")
add_custom_command(
OUTPUT ${AUTO_WRAP_DIR}/swig_runtime.h
COMMAND ${SWIG_EXECUTABLE} -c++;-python;-external-runtime;${AUTO_WRAP_DIR}/swig_runtime.h
DEPENDS ${swig_dependencies} ${include_files}
)
add_custom_target(swig_runtime DEPENDS ${AUTO_WRAP_DIR}/swig_runtime.h)
endif()
file(MAKE_DIRECTORY ${swigtmpdir})
# Run Swig. # Run Swig.
# Please keep -Werror, in order not to overlook critical warnings. # Please keep -Werror, in order not to overlook critical warnings.
# Dispensable warnings are disabled in Wrap/Swig/warnings.i. # Dispensable warnings are disabled in Wrap/Swig/warnings.i.
...@@ -55,8 +51,8 @@ function(SwigLib name lib swigtmpdir) ...@@ -55,8 +51,8 @@ function(SwigLib name lib swigtmpdir)
";-I${LibHeinz_INCLUDE_DIR}" ";-I${LibHeinz_INCLUDE_DIR}"
";-I${CMAKE_SOURCE_DIR};-I${CMAKE_BINARY_DIR}/inc") ";-I${CMAKE_SOURCE_DIR};-I${CMAKE_BINARY_DIR}/inc")
add_custom_target(lib${lib}_py_wrappers ALL add_custom_command(
BYPRODUCTS ${swigtmpdir}/lib${lib}.py OUTPUT ${swigtmpdir}/lib${lib}.py
${AUTO_WRAP_DIR}/lib${lib}_wrap.h ${AUTO_WRAP_DIR}/lib${lib}_wrap.h
${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp ${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp
${AUTO_WRAP_DIR}/lib${lib}.py ${AUTO_WRAP_DIR}/lib${lib}.py
...@@ -68,30 +64,53 @@ function(SwigLib name lib swigtmpdir) ...@@ -68,30 +64,53 @@ function(SwigLib name lib swigtmpdir)
${AUTO_WRAP_DIR}/lib${lib}.py ${AUTO_WRAP_DIR}/lib${lib}.py
DEPENDS ${swig_dependencies} ${include_files} DEPENDS ${swig_dependencies} ${include_files}
COMMENT "SWIG: build Python wrapper ${AUTO_WRAP_DIR}/lib${lib}.py" COMMENT "SWIG: build Python wrapper ${AUTO_WRAP_DIR}/lib${lib}.py"
) )
string(REPLACE ";" " " SWIG_FLAGS_STR "${SWIG_FLAGS}") string(REPLACE ";" " " SWIG_FLAGS_STR "${SWIG_FLAGS}")
set(SWIG_FLAGS_STR "${SWIG_FLAGS_STR}" CACHE INTERNAL "") set(SWIG_FLAGS_STR "${SWIG_FLAGS_STR}" CACHE INTERNAL "")
set(SWIG_DIR "${SWIG_DIR}" CACHE INTERNAL "") set(SWIG_DIR "${SWIG_DIR}" CACHE INTERNAL "")
else()
# assumes that lib${lib}_wrap.h, lib${lib}_wrap.cpp and lib${lib}.py
# are already present in ${AUTO_WRAP_DIR}
add_custom_target(lib${lib}_py_wrappers ALL
DEPENDS ${AUTO_WRAP_DIR}/lib${lib}_wrap.h
${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp
${AUTO_WRAP_DIR}/lib${lib}.py
)
endif() endif()
# assumes that lib${lib}_wrap.h, lib${lib}_wrap.cpp and lib${lib}.py
# are already present in ${AUTO_WRAP_DIR}
add_custom_target(lib${lib}_py_wrappers
DEPENDS
${AUTO_WRAP_DIR}/lib${lib}_wrap.h
${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp
${AUTO_WRAP_DIR}/lib${lib}.py
COMMENT "SWIG: use Python wrapper ${AUTO_WRAP_DIR}/lib${lib}.py"
)
# include Python and Numpy headers and link to Python shared library # include Python and Numpy headers and link to Python shared library
target_include_directories(${lib} PRIVATE ${Python3_INCLUDE_DIRS} ${Python3_NumPy_INCLUDE_DIRS}) target_include_directories(${lib} PRIVATE ${Python3_INCLUDE_DIRS} ${Python3_NumPy_INCLUDE_DIRS})
target_link_libraries(${lib} PRIVATE ${Python3_LIBRARIES}) target_link_libraries(${lib} PRIVATE ${Python3_LIBRARIES})
set_target_properties(${lib} PROPERTIES set_target_properties(${lib} PROPERTIES
_LIBRARY_PY "${AUTO_WRAP_DIR}/lib${lib}.py" _LIBRARY_PY "${AUTO_WRAP_DIR}/lib${lib}.py"
) )
add_dependencies(${lib} lib${lib}_py_wrappers)
if((CLANG) OR (GCC))
# suppress warnings from auto-generated code (last updated for Swig 4.0.1)
set_source_files_properties(${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp
PROPERTIES COMPILE_OPTIONS
"\
-Wno-unused-parameter;\
-Wno-missing-field-initializers;\
-Wno-sometimes-uninitialized;\
-Wno-deprecated-declarations")
endif()
# add SWIG-produced C++ wrapper source code to library sources
get_target_property(source_files ${lib} SOURCES)
list(APPEND source_files ${AUTO_WRAP_DIR}/lib${lib}_wrap.cpp)
set_target_properties(${lib} PROPERTIES
SOURCES "${source_files}")
install(FILES "${AUTO_WRAP_DIR}/lib${lib}.py" if(BA_CPP_API)
DESTINATION ${destination_python} COMPONENT Libraries) install(FILES ${AUTO_WRAP_DIR}/swig_runtime.h
DESTINATION ${destination_include}/Wrap COMPONENT Headers)
endif()
endfunction() endfunction()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment