All of Yzena's common CMake tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

# ***** BEGIN LICENSE BLOCK *****
#
# Copyright 2017-2021 Yzena Tech
#
# Licensed under the Yzena Network License, Version 0.1 (the "Yzena Network
# License" or "YNL"). You may not use this file except in compliance with the
# Yzena Network License.
#
# You may obtain a copy of the Yzena Network License at
#
# https://yzena.com/yzena-network-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Yzena Network License is distributed under the
# following disclaimer:
#
# As far as the law allows, this software comes as is, without any
# warranty or condition, and no contributor will be liable to anyone for
# any damages related to this software or this license, under any kind of
# legal claim.
#
# ****** END LICENSE BLOCK ******
# Generate a C source array from a file
message(STATUS "Generating @outfile@ with array: \"const char @array_name@[]\"...")
file(READ "@CMAKE_CURRENT_SOURCE_DIR@/@infile@" contents HEX)
set(start_line @start_line@)
math(EXPR start_line "${start_line} - 1")
file(WRITE "@outfile@" "// @outfile@\n")
file(APPEND "@outfile@" "// AUTOMATICALLY GENERATED: DO NOT MODIFY\n")
string(REGEX MATCHALL ".." output "${contents}")
string(LENGTH "${contents}" contents_length)
set(lines 0)
list(LENGTH output len)
foreach(iter RANGE 1 ${len})
list(GET output "${iter}" line)
if("${line}" EQUAL "0a")
math(EXPR lines "${lines} + 1")
if("${lines}" EQUAL "${start_line}")
math(EXPR start "${iter} + 1")
break()
endif()
endif()
endforeach()
foreach(iter RANGE 1 ${start})
list(REMOVE_AT output 0)
endforeach()
string(REGEX REPLACE ";" ",\n0x" output "${output}")
file(APPEND "@outfile@" "const size_t @len_name@ = @file_len@;\n")
file(APPEND "@outfile@" "const char @array_name@[] = {0x${output}")
if (@cstr@)
file(APPEND "@outfile@" ",\n0x0")
endif()
file(APPEND "@outfile@" "};")