Implement --version for spirv-tools
authorqining <qining@google.com>
Mon, 22 Feb 2016 21:07:19 +0000 (16:07 -0500)
committerDavid Neto <dneto@google.com>
Tue, 23 Feb 2016 19:14:23 +0000 (14:14 -0500)
'spirv-as --version', 'spirv-dis --version' and 'spirv-val --version'
will display version information.

CMakeLists.txt
tools/as/as.cpp
tools/dis/dis.cpp
tools/val/val.cpp
utils/update_build_version.py [new file with mode: 0755]

index cd5f91d..ce8b42a 100644 (file)
@@ -103,6 +103,12 @@ function(spvtools_default_compile_options TARGET)
   endif()
 endfunction()
 
+add_custom_target(spirv-tools-build-version
+  ${PYTHON_EXE}
+  ${CMAKE_CURRENT_SOURCE_DIR}/utils/update_build_version.py
+  ${spirv-tools_SOURCE_DIR}
+  COMMENT "Update build-version.inc in the Spirv-tools build directory (if necessary).")
+
 set(SPIRV_SOURCES
   ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
   ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv/spirv.h
@@ -161,14 +167,21 @@ if (NOT ${SPIRV_SKIP_EXECUTABLES})
   add_executable(spirv-as ${CMAKE_CURRENT_SOURCE_DIR}/tools/as/as.cpp)
   spvtools_default_compile_options(spirv-as)
   target_link_libraries(spirv-as PRIVATE ${SPIRV_TOOLS})
+  target_include_directories(spirv-as PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
+  add_dependencies(${SPIRV_TOOLS} spirv-tools-build-version)
+
 
   add_executable(spirv-dis ${CMAKE_CURRENT_SOURCE_DIR}/tools/dis/dis.cpp)
   spvtools_default_compile_options(spirv-dis)
   target_link_libraries(spirv-dis PRIVATE ${SPIRV_TOOLS})
+  target_include_directories(spirv-dis PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
+  add_dependencies(${SPIRV_TOOLS} spirv-tools-build-version)
 
   add_executable(spirv-val ${CMAKE_CURRENT_SOURCE_DIR}/tools/val/val.cpp)
   spvtools_default_compile_options(spirv-val)
   target_link_libraries(spirv-val PRIVATE ${SPIRV_TOOLS})
+  target_include_directories(spirv-val PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
+  add_dependencies(${SPIRV_TOOLS} spirv-tools-build-version)
 
   # Find gmock if we can. If it's not already configured, then try finding
   # it in external/googletest
index 9d5bf74..10a7821 100644 (file)
@@ -47,10 +47,15 @@ Options:
   -h              Print this help.
 
   -o <filename>   Set the output filename. Use '-' to mean stdout.
+  --version       Display assembler version information.
 )",
       argv0, argv0);
 }
 
+const char kBuildVersion[] =
+#include "build-version.inc"
+;
+
 int main(int argc, char** argv) {
   const char* inFile = nullptr;
   const char* outFile = nullptr;
@@ -79,6 +84,15 @@ int main(int argc, char** argv) {
             return 1;
           }
         } break;
+        case '-': {
+          // Long options
+          if (0 == strcmp(argv[argi], "--version")) {
+            printf("%s\n", kBuildVersion);
+            printf("Target: SPIR-V %d.%d rev %d\n", SPV_SPIRV_VERSION_MAJOR,
+                   SPV_SPIRV_VERSION_MINOR, SPV_SPIRV_VERSION_REVISION);
+            return 0;
+          }
+        } break;
         default:
           print_usage(argv[0]);
           return 1;
index a4c300d..1f12f50 100644 (file)
@@ -43,6 +43,7 @@ or if the filename is "-", then the binary is read from standard input.
 Options:
 
   -h, --help      Print this help.
+  --version       Display disassembler version information.
 
   -o <filename>   Set the output filename.
                   Output goes to standard output if this option is
@@ -58,6 +59,10 @@ Options:
       argv0, argv0);
 }
 
+const char kBuildVersion[] =
+#include "build-version.inc"
+;
+
 int main(int argc, char** argv) {
   const char* inFile = nullptr;
   const char* outFile = nullptr;
@@ -94,6 +99,11 @@ int main(int argc, char** argv) {
           } else if (0 == strcmp(argv[argi], "--help")) {
             print_usage(argv[0]);
             return 0;
+          } else if (0 == strcmp(argv[argi], "--version")) {
+            printf("%s\n", kBuildVersion);
+            printf("Target: SPIR-V %d.%d rev %d\n", SPV_SPIRV_VERSION_MAJOR,
+                   SPV_SPIRV_VERSION_MINOR, SPV_SPIRV_VERSION_REVISION);
+            return 0;
           } else {
             print_usage(argv[0]);
             return 1;
index 629e389..c21e7f1 100644 (file)
@@ -49,10 +49,15 @@ Options:
   -id        Perform id validation (default OFF)
   -layout    Perform layout validation (default OFF)
   -rules     Perform rules validation (default OFF)
+  --version  Display validator version information
 )",
       argv0, argv0);
 }
 
+const char kBuildVersion[] =
+#include "build-version.inc"
+;
+
 int main(int argc, char** argv) {
   const char* inFile = nullptr;
   uint32_t options = 0;
@@ -60,7 +65,12 @@ int main(int argc, char** argv) {
   for (int argi = 1; argi < argc; ++argi) {
     const char* cur_arg = argv[argi];
     if ('-' == cur_arg[0]) {
-      if (!strcmp("all", cur_arg + 1)) {
+      if (0 == strcmp(cur_arg, "--version")) {
+        printf("%s\n", kBuildVersion);
+        printf("Target: SPIR-V %d.%d rev %d\n", SPV_SPIRV_VERSION_MAJOR,
+               SPV_SPIRV_VERSION_MINOR, SPV_SPIRV_VERSION_REVISION);
+        return 0;
+      } else if (!strcmp("all", cur_arg + 1)) {
         options |= SPV_VALIDATE_ALL;
       } else if (!strcmp("basic", cur_arg + 1)) {
         options |= SPV_VALIDATE_BASIC_BIT;
diff --git a/utils/update_build_version.py b/utils/update_build_version.py
new file mode 100755 (executable)
index 0000000..0a62488
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+
+# Copyright 2016 The Shaderc Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Updates build-version.inc in the current directory, unless the update is
+# identical to the existing content.
+#
+# Args: <spirv-tools_dir>
+#
+# For each directory, there will be a line in build-version.inc containing that
+# directory's "git describe" output enclosed in double quotes and appropriately
+# escaped.
+
+import datetime
+import os.path
+import subprocess
+import sys
+
+OUTFILE = 'build-version.inc'
+
+def describe(dir):
+    """Runs 'git describe' in dir.  If successful, returns the output; otherwise,
+returns 'unknown hash, <date>'."""
+    try:
+        return subprocess.check_output(["git", "describe"], cwd=dir).rstrip()
+    except subprocess.CalledProcessError:
+        return 'unknown hash, ' + datetime.date.today().isoformat()
+
+def main():
+    if len(sys.argv) != 2:
+        print 'usage: {0} <spirv-tools_dir>'.format(sys.argv[0])
+        sys.exit(1)
+
+    new_content = ('"spirv-tools ' + describe(sys.argv[1]).replace('"', '\\"') + '\\n"\n')
+    if os.path.isfile(OUTFILE) and new_content == open(OUTFILE, 'r').read():
+        sys.exit(0)
+    open(OUTFILE, 'w').write(new_content)
+
+if __name__ == '__main__':
+    main()