File: EmbedBinary.cmake

package info (click to toggle)
wlmaker 0.6.1%2Bgit20250919%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 12,056 kB
  • sloc: ansic: 144,965; sh: 10,507; xml: 4,163; python: 1,400; makefile: 499; yacc: 119; lex: 70; awk: 12
file content (66 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2024 Google LLC
#
# 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
#
# https://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.

CMAKE_MINIMUM_REQUIRED(VERSION 3.13)

# During the INCLUDE() call, CMAKE_CURRENT_LIST_DIR is this file's path.
# Store it for later, during the FUNCTION() call it'll be the caller's path.
SET(EMBED_BINARY_TEMPLATE_DIR ${CMAKE_CURRENT_LIST_DIR})

# Adds a static C library to embed the binary file with the specified prefix.
#
# Generates a C source and header file from the contents of `binary_file`, and
# adds both of these to a new library target `library_target`.
#
# The embedded data can then be accessed as external variables:
#
#    #include "<prefix>.h"
#
#    * uint8_t embedded_binary_<prefix>_data[];
#    * const size_t embedded_binary_<prefix>_size;
FUNCTION(EmbedBinary_ADD_LIBRARY library_target prefix binary_file)

  SET(output_basename "${CMAKE_CURRENT_BINARY_DIR}/${prefix}")
  SET(output_source "${output_basename}.c")
  SET(output_header "${output_basename}.h")
  SET(generated_cmake "${output_basename}.cmake")

  SET(template_header "${EMBED_BINARY_TEMPLATE_DIR}/embed_binary.h.in")
  SET(template_source "${EMBED_BINARY_TEMPLATE_DIR}/embed_binary.c.in")

  CONFIGURE_FILE(
    "${EMBED_BINARY_TEMPLATE_DIR}/embed.cmake.in"
    "${generated_cmake}"
    @ONLY)

  ADD_CUSTOM_COMMAND(
    OUTPUT "${output_source}" "${output_header}"
    COMMAND ${CMAKE_COMMAND} -P "${generated_cmake}"
    MAIN_DEPENDENCY "${binary_file}"
    DEPENDS "${generated_cmake}" "${template_header}" "${template_source}"
  )

  ADD_LIBRARY(${library_target} STATIC)
  TARGET_SOURCES(
    ${library_target} PRIVATE
    "${output_source}"
    "${output_header}")
  TARGET_INCLUDE_DIRECTORIES(
    ${library_target} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
  SET_TARGET_PROPERTIES(
    ${library_target}
    PROPERTIES VERSION 1.0
    PUBLIC_HEADER "${output_header}")

ENDFUNCTION()