فهرست منبع

Gather accurate depedencies for .ankiprog files

Panagiotis Christopoulos Charitos 3 سال پیش
والد
کامیت
4ad7cc6146
2فایلهای تغییر یافته به همراه81 افزوده شده و 5 حذف شده
  1. 11 5
      AnKi/Shaders/CMakeLists.txt
  2. 70 0
      Tools/Shader/ShaderProgramDependencies.py

+ 11 - 5
AnKi/Shaders/CMakeLists.txt

@@ -1,6 +1,4 @@
 file(GLOB_RECURSE prog_fnames *.ankiprog)
-file(GLOB_RECURSE glsl_fnames *.glsl)
-file(GLOB_RECURSE header_fnames *.h)
 
 ProcessorCount(proc_count)
 MATH(EXPR proc_count "${proc_count}-1")
@@ -34,17 +32,25 @@ else()
 	message("++ Leaving default shader precision")
 endif()
 
+include(FindPythonInterp)
+
 foreach(prog_fname ${prog_fnames})
 	get_filename_component(filename ${prog_fname} NAME)
 	set(bin_fname ${CMAKE_CURRENT_BINARY_DIR}/${filename}bin)
 
-	get_filename_component(filename ${prog_fname} NAME_WE)
-	set(target_name "${filename}_ankiprogbin")
+	get_filename_component(filename2 ${prog_fname} NAME_WE)
+	set(target_name "${filename2}_ankiprogbin")
+
+	# Get deps using a script
+	execute_process(
+		COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/../../Tools/Shader/ShaderProgramDependencies.py" "-i" "AnKi/Shaders/${filename}"
+		WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../.."
+		OUTPUT_VARIABLE deps)
 
 	add_custom_command(
 		OUTPUT ${bin_fname}
 		COMMAND ${shader_compiler_bin} -o ${bin_fname} -j ${proc_count} -I "${CMAKE_CURRENT_SOURCE_DIR}/../.." ${extra_compiler_args} ${prog_fname}
-		DEPENDS ${shader_compiler_dep} ${prog_fname} ${glsl_fnames} ${header_fnames}
+		DEPENDS ${shader_compiler_dep} ${prog_fname} ${deps}
 		COMMENT "Build ${prog_fname}")
 
 	add_custom_target(

+ 70 - 0
Tools/Shader/ShaderProgramDependencies.py

@@ -0,0 +1,70 @@
+#!/usr/bin/python3
+
+# Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
+# All rights reserved.
+# Code licensed under the BSD License.
+# http://www.anki3d.org/LICENSE
+
+# Generates the dependencies of a shader program file. Output consumed by CMake
+
+import optparse
+import re
+
+
+def parse_commandline():
+    """ Parse the command line arguments """
+
+    parser = optparse.OptionParser(usage="usage: %prog [options]",
+                                   description="This script gathers all the include files of a shader program")
+
+    parser.add_option("-i", "--input", dest="inp", type="string", help="Inpute .ankiprog file")
+
+    (options, args) = parser.parse_args()
+
+    if not options.inp:
+        parser.error("argument is missing")
+
+    return options.inp
+
+
+def parse_file(fname):
+    file = open(fname, mode="r")
+    txt = file.read()
+
+    my_includes = re.findall("\s*#\s*include\ <(.*)>", txt)
+    agregated_includes = []
+
+    for include in my_includes:
+        # Skip non-shaders
+        if "AnKi/Shaders" not in include:
+            continue
+
+        agregated_includes.append(include)
+
+        # Append recursively
+        other_includes = parse_file(include)
+        for other_include in other_includes:
+            if other_include not in agregated_includes:
+                agregated_includes.append(other_include)
+
+    return agregated_includes
+
+
+def main():
+    input_fname = parse_commandline()
+    includes = parse_file(input_fname)
+
+    str = ""
+    for i in range(len(includes)):
+        include = includes[i]
+        include = include.replace("AnKi/Shaders/", "")
+
+        if i < len(includes) - 1:
+            str += include + ";"  # CMake wants ; as a separator
+        else:
+            str += include
+    print(str, end="")  # No newline at the end
+
+
+if __name__ == "__main__":
+    main()