代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- coding: utf-8 -*-
########################################################################
#
# Copyright (c) 2017 Baidu, Inc. 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.
#
########################################################################
"""
A helper script used by generic.cmake's resource_library function to generate
the ${target_name}.h header file and ${resource_src_name}.c impl files.
The args passed to resource_library are:
resource_library(target_name SRCS src_name0 src_name1 ...).
The ${target_name}.h is mapped from target_name, generated in CMAKE_CURRENT_BINARY_DIR
and includes all the resource definitions.
All the src_name files will be read from PROJECT_SOURCE_DIR/src_name, then used by this
script to generate ${resource_src_name}.c(in the CMAKE_CURRENT_BINARY_DIR).
The ${resource_src_name}.c file consists of two parts:
extern const char RESOURCE_${resource_src_name}[] = {$binary_data};
unsigned int ${resource_src_name}_len= xx;
${resource_src_name} is mapped from src_name by replacing '/' or '.' to '_'.
"""
import os
import sys
def generate_line(byte_list):
line = reduce(lambda x, y: x + ', ' + "%s" % str(y), byte_list)
return line + ','
def generate_resource_and_src_list(base_dir, src_list):
resource_list = []
for src in src_list:
prefix = os.path.commonprefix({base_dir, src})
component = src[len(prefix):].lstrip('/')
resource_name = component.replace('/', '_').replace('.', '_')
c_filename = resource_name + ".c"
resource_name = resource_name.replace('___', '')
resource_list.append((src, resource_name, c_filename, os.path.getsize(src)))
return resource_list
if __name__ == "__main__":
"""
Used by resource_library with following args:
python resurce_library.py ${base_dir} ${header_file} ${src_name0} ${src_name1} ...
"""
base_dir, header_file = sys.argv[1:3]
srcs = sys.argv[3:]
resource_list = generate_resource_and_src_list(base_dir, srcs)
hfile = open(header_file, 'w')
print >> hfile, '#ifdef __cplusplus'
print >> hfile, 'extern "C" {'
print >> hfile, '#endif'
for (source, resource_name, c_filename, size) in resource_list:
f = open(source, 'rb')
all = f.read()
all = bytearray(all)
line_list = []
byte_list = []
for b in all:
if len(byte_list) > 0 and len(byte_list) % 12 == 0:
line_list.append(generate_line(byte_list))
byte_list = []
byte_list.append(hex(b))
if len(byte_list) > 0:
line_list.append(generate_line(byte_list))
print >> hfile, "extern const char RESOURCE_%s[%d];" % (resource_name, size)
with open(c_filename, "w") as c_file:
print >> c_file, "const char RESOURCE_%s[] = {" % resource_name
line_list[len(line_list) - 1] = line_list[len(line_list) - 1][ : -1]
for line in line_list:
print >> c_file, ' ', line
print >> c_file, '};'
print >> c_file, "unsigned int %s_len = %d;" % (resource_name, size)
print >> hfile, '#ifdef __cplusplus'
print >> hfile, '}'
print >> hfile, '#endif'
hfile.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。