Skip to content
This repository has been archived by the owner on Oct 3, 2018. It is now read-only.

Commit

Permalink
Added python implementation of xxd unix tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
elhigu committed Apr 22, 2014
1 parent 2463ad5 commit b5ba489
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
10 changes: 9 additions & 1 deletion include/clv/clv.h
Expand Up @@ -42,8 +42,16 @@ extern "C"
#define CLV_CALL
#endif

// TODO: dll export / import magic once we go dynamic
#ifdef CREATE_CLV_STATIC
#define CLV_API
#else
#ifdef CREATE_CLV_DLL_EXPORTS
#define CLV_API __declspec(dllexport)
#else
// This was not required after all #define CLV_API __declspec(dllimport)
#define CLV_API
#endif
#endif

typedef struct WebCLValidator *clv_program;

Expand Down
15 changes: 10 additions & 5 deletions lib/CMakeLists.txt
Expand Up @@ -11,9 +11,8 @@ macro(generate_hex_header name)
${CMAKE_CURRENT_SOURCE_DIR}/stripcr.py
${CMAKE_CURRENT_SOURCE_DIR}/${name}.cl
${name}-endlfix.cl
COMMAND xxd
-i ${name}-endlfix.cl
${name}.h
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/xxd.py ${name}-endlfix.cl ${name}.h
)
endmacro(generate_hex_header name)

Expand Down Expand Up @@ -76,7 +75,10 @@ install(TARGETS clv
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
RUNTIME DESTINATION bin)
set_target_properties(clv PROPERTIES FOLDER "Clang libraries")

set_target_properties(clv PROPERTIES
FOLDER "Clang libraries"
COMPILE_DEFINITIONS "CREATE_CLV_STATIC")

target_link_libraries(clv
clangTooling
Expand Down Expand Up @@ -104,7 +106,10 @@ install(TARGETS clv_standalone
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
RUNTIME DESTINATION bin)
set_target_properties(clv_standalone PROPERTIES FOLDER "Clang libraries")

set_target_properties(clv_standalone PROPERTIES
FOLDER "Clang libraries"
COMPILE_DEFINITIONS "CREATE_CLV_DLL_EXPORTS")

target_link_libraries(clv_standalone
clangTooling
Expand Down
54 changes: 54 additions & 0 deletions lib/xxd.py
@@ -0,0 +1,54 @@
# Copyright (C) 2013 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Represent a file as a C++ constant string.
Usage:
python xxd.py SOURCE DEST
"""

import sys
import os

def main():
input_filename, output_filename = sys.argv[1:]
variable_name = input_filename.replace('.','_')
with open(input_filename) as input_file:
input_text = input_file.read()
hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text]
const_declaration = 'unsigned char %s[] = {\n%s\n};\n' % (
variable_name, ', '.join(hex_values))

const_declaration += '\nunsigned int %s_len = %i;' % (variable_name, len(hex_values))

with open(output_filename, 'w') as output_file:
output_file.write(const_declaration)


if __name__ == '__main__':
sys.exit(main())

0 comments on commit b5ba489

Please sign in to comment.