Support building under CYGWIN
- Parse CHANGES file with Universal Python line endings in case
the source tree was checked out with Windows line endings.
- Use our own clone of strnlen_s which might not be available
everywhere.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/508
diff --git a/CHANGES b/CHANGES
index 352db8f..47b3c2f 100644
--- a/CHANGES
+++ b/CHANGES
@@ -3,6 +3,8 @@
v2016.7-dev 2016-12-13
- Add build target spirv-tools-vimsyntax to generate spvasm.vim, a SPIR-V
assembly syntax file for Vim.
+ - Fixes:
+ #508: Support compilation under CYGWIN
v2016.6 2016-12-13
- Published the C++ interface for assembling, disassembling, validation, and
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0f0d5ea..7765caf 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -30,6 +30,8 @@
add_definitions(-DSPIRV_LINUX)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
add_definitions(-DSPIRV_WINDOWS)
+elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
+ add_definitions(-DSPIRV_WINDOWS)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
add_definitions(-DSPIRV_MAC)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
diff --git a/source/binary.cpp b/source/binary.cpp
index 689937f..cdd2eab 100644
--- a/source/binary.cpp
+++ b/source/binary.cpp
@@ -550,7 +550,7 @@
const size_t remaining_input_bytes =
sizeof(uint32_t) * (_.num_words - _.word_index);
const size_t string_num_content_bytes =
- strnlen(string, remaining_input_bytes);
+ spv_strnlen_s(string, remaining_input_bytes);
// If there was no terminating null byte, then that's an end-of-input
// error.
if (string_num_content_bytes == remaining_input_bytes)
@@ -770,3 +770,11 @@
delete[] binary->code;
delete binary;
}
+
+size_t spv_strnlen_s(const char* str, size_t strsz) {
+ if (!str) return 0;
+ for (size_t i = 0; i < strsz; i++) {
+ if (!str[i]) return i;
+ }
+ return strsz;
+}
diff --git a/source/binary.h b/source/binary.h
index 375e010..f6237e3 100644
--- a/source/binary.h
+++ b/source/binary.h
@@ -27,4 +27,10 @@
const spv_endianness_t endian,
spv_header_t* header);
+// Returns the number of non-null characters in str before the first null
+// character, or strsz if there is no null character. Examines at most the
+// first strsz characters in str. Returns 0 if str is nullptr. This is a
+// replacement for C11's strnlen_s which might not exist in all environments.
+size_t spv_strnlen_s(const char* str, size_t strsz);
+
#endif // LIBSPIRV_BINARY_H_
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 891828b..6017793 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -71,6 +71,7 @@
binary_endianness_test.cpp
binary_header_get_test.cpp
binary_parse_test.cpp
+ binary_strnlen_s_test.cpp
binary_to_text_test.cpp
binary_to_text.literal_test.cpp
capability_set_test.cpp
diff --git a/test/binary_strnlen_s_test.cpp b/test/binary_strnlen_s_test.cpp
new file mode 100644
index 0000000..2d2170b
--- /dev/null
+++ b/test/binary_strnlen_s_test.cpp
@@ -0,0 +1,30 @@
+// Copyright (c) 2016 Google Inc.
+//
+// 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.
+
+#include "unit_spirv.h"
+
+namespace {
+
+TEST(Strnlen, Samples) {
+ EXPECT_EQ(0u, spv_strnlen_s(nullptr, 0));
+ EXPECT_EQ(0u, spv_strnlen_s(nullptr, 5));
+ EXPECT_EQ(0u, spv_strnlen_s("abc", 0));
+ EXPECT_EQ(1u, spv_strnlen_s("abc", 1));
+ EXPECT_EQ(3u, spv_strnlen_s("abc", 3));
+ EXPECT_EQ(3u, spv_strnlen_s("abc\0", 5));
+ EXPECT_EQ(0u, spv_strnlen_s("\0", 5));
+ EXPECT_EQ(1u, spv_strnlen_s("a\0c", 5));
+}
+
+} // anonymous namespace
diff --git a/utils/update_build_version.py b/utils/update_build_version.py
index ae55694..95e4237 100755
--- a/utils/update_build_version.py
+++ b/utils/update_build_version.py
@@ -78,7 +78,7 @@
pattern = re.compile(r'(v\d+\.\d+(-dev)?) \d\d\d\d-\d\d-\d\d$')
changes_file = os.path.join(directory, 'CHANGES')
- with open(changes_file) as f:
+ with open(changes_file, mode='rU') as f:
for line in f.readlines():
match = pattern.match(line)
if match: