Procházet zdrojové kódy

UseSWIG: Detect module name from interface file (#10080)

If no SWIG_MODULE_NAME is specified explicitly and the .i file exists,
scan it for the module name declaration.
Julien Schueller před 11 roky
rodič
revize
e147c5c555
1 změnil soubory, kde provedl 24 přidání a 1 odebrání
  1. 24 1
      Modules/UseSWIG.cmake

+ 24 - 1
Modules/UseSWIG.cmake

@@ -86,7 +86,30 @@ macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
   get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
     ${infile} SWIG_MODULE_NAME)
   if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
-    get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
+
+    # try to get module name from "%module foo" syntax
+    if ( EXISTS ${infile} )
+      file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
+    endif ()
+    if ( _MODULE_NAME )
+      string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
+      set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
+
+    else ()
+      # try to get module name from "%module (options=...) foo" syntax
+      if ( EXISTS ${infile} )
+        file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
+      endif ()
+      if ( _MODULE_NAME )
+        string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
+        set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
+
+      else ()
+        # fallback to file basename
+        get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename ${infile} NAME_WE)
+      endif ()
+    endif ()
+
   endif()
   foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
     set(${outfiles} ${${outfiles}}