Skip to content

Commit

Permalink
Support building under CYGWIN
Browse files Browse the repository at this point in the history
- 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 KhronosGroup#508
  • Loading branch information
dneto0 committed Dec 21, 2016
1 parent e0e4044 commit 37422e9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Revision history for SPIRV-Tools
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
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
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")
Expand Down
10 changes: 9 additions & 1 deletion source/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
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)
Expand Down Expand Up @@ -770,3 +770,11 @@ void spvBinaryDestroy(spv_binary binary) {
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;
}
6 changes: 6 additions & 0 deletions source/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ spv_result_t spvBinaryHeaderGet(const spv_const_binary binary,
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_
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ set(TEST_SOURCES
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
Expand Down
30 changes: 30 additions & 0 deletions test/binary_strnlen_s_test.cpp
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion utils/update_build_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def deduce_software_version(directory):

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:
Expand Down

0 comments on commit 37422e9

Please sign in to comment.