if (g_pattern_match_simple(pattern, entry))
[appimagekit/gsi.git] / src / build-runtime.cmake
blob4a28d4a161c3e2f0f4f05c96e340b746e6eefa3a
1 set(APPIMAGEKIT_RUNTIME_ENABLE_SETPROCTITLE OFF CACHE BOOL "Useful for $TARGET_APPIMAGE; see issue #763")
3 # if set to anything but ON, the magic bytes won't be embedded
4 # CAUTION: the magic bytes are a hard requirement for type 2 AppImages! This option should NEVER be used unless you are
5 # 100% sure what you are doing here!
6 set(APPIMAGEKIT_EMBED_MAGIC_BYTES ON CACHE BOOL "")
7 # mark as advanced so it won't show up in CMake GUIs etc., to prevent users from accidentally using this option
8 mark_as_advanced(APPIMAGEKIT_EMBED_MAGIC_BYTES)
10 # check type of current build
11 string(TOUPPER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_UPPER)
12 if (BUILD_TYPE_UPPER STREQUAL DEBUG)
13     set(BUILD_DEBUG TRUE)
14 else()
15     set(BUILD_DEBUG FALSE)
16 endif()
18 if (NOT TARGET libsquashfuse)
19     message(FATAL_ERROR "TARGET NOT found libsquashfuse")
20 else()
21     get_target_property(squashfuse_INCLUDE_DIRS libsquashfuse INTERFACE_INCLUDE_DIRECTORIES)
22 endif()
24 set(runtime_cflags
25     -std=c99 -ffunction-sections -fdata-sections
26     -DGIT_COMMIT=\\"${GIT_COMMIT}\\"
27     -I${squashfuse_INCLUDE_DIRS}
28     -I${PROJECT_SOURCE_DIR}/include
29     -I${PROJECT_SOURCE_DIR}/lib/libappimage/include
30     -I${PROJECT_SOURCE_DIR}/lib/libappimage/src/libappimage_hashlib/include
31     ${DEPENDENCIES_CFLAGS}
33 set(runtime_ldflags -s -Wl,--gc-sections ${DEPENDENCIES_LDFLAGS})
35 if(BUILD_DEBUG)
36     message(WARNING "Debug build, adding debug information")
37     set(runtime_cflags -g ${runtime_cflags})
38 else()
39     message(STATUS "Release build, optimizing runtime")
40     set(runtime_cflags -Os ${runtime_cflags})
41 endif()
43 if(NOT xz_INCLUDE_DIRS STREQUAL "")
44     list(APPEND runtime_cflags -I${xz_INCLUDE_DIRS})
45 endif()
47 if(APPIMAGEKIT_RUNTIME_ENABLE_SETPROCTITLE)
48     set(runtime_cflags ${runtime_cflags} -DENABLE_SETPROCTITLE)
49 endif()
51 # objcopy requires actual files for creating new sections to populate the new section
52 # therefore, we generate 3 suitable files containing blank bytes in the right sizes
53 add_custom_command(
54     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/16_blank_bytes
55     COMMAND dd if=/dev/zero bs=1 count=16 of=${CMAKE_CURRENT_BINARY_DIR}/16_blank_bytes
57 add_custom_command(
58     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
59     COMMAND dd if=/dev/zero bs=1 count=1024 of=${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
61 add_custom_command(
62     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/8192_blank_bytes
63     COMMAND dd if=/dev/zero bs=1 count=8192 of=${CMAKE_CURRENT_BINARY_DIR}/8192_blank_bytes
66 # compile first raw object (not linked yet) into which the sections will be embedded
67 # TODO: find out how this .o object can be generated using a normal add_executable call
68 # that'd allow us to get rid of the -I parameters in runtime_cflags
69 add_custom_command(
70     MAIN_DEPENDENCY runtime.c
71     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.0.o
72     COMMAND ${CMAKE_C_COMPILER} ${runtime_cflags} -c ${CMAKE_CURRENT_SOURCE_DIR}/runtime.c -o runtime.0.o
73     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
76 # embed the sections, one by one
77 # TODO: find out whether all the sections can be embedded in a single objcopy call
78 add_custom_command(
79     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.1.o
80     COMMAND ${OBJCOPY} --add-section .digest_md5=16_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.0.o runtime.1.o
81     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.0.o
82     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/16_blank_bytes
83     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
85 add_custom_command(
86     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.2.o
87     COMMAND ${OBJCOPY} --add-section .upd_info=1024_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.1.o runtime.2.o
88     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.1.o
89     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
90     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
92 add_custom_command(
93     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.3.o
94     COMMAND ${OBJCOPY} --add-section .sha256_sig=1024_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.2.o runtime.3.o
95     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.2.o
96     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
97     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
99 add_custom_command(
100     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.4.o
101     COMMAND ${OBJCOPY} --add-section .sig_key=8192_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.3.o runtime.4.o
102     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.3.o
103     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/8192_blank_bytes
104     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
107 # add the runtime as a normal executable
108 # CLion will recognize it as a normal executable, one can simply step into the code
109 add_executable(runtime ${CMAKE_CURRENT_BINARY_DIR}/runtime.4.o notify.c)
110 # CMake gets confused by the .o object, therefore we need to tell it that it shall link everything using the C compiler
111 set_property(TARGET runtime PROPERTY LINKER_LANGUAGE C)
112 target_link_libraries(runtime PRIVATE libsquashfuse dl xz libzlib pthread libappimage_shared libappimage_hashlib)
113 target_include_directories(runtime PRIVATE ${PROJECT_SOURCE_DIR}/include)
115 if(BUILD_DEBUG)
116     message(WARNING "Debug build, not stripping runtime to allow debugging using gdb etc.")
117 else()
118     add_custom_command(
119         TARGET runtime
120         POST_BUILD
121         COMMAND ${STRIP} ${CMAKE_CURRENT_BINARY_DIR}/runtime
122     )
123 endif()
125 # embed the magic bytes after the runtime's build has finished
126 if(APPIMAGEKIT_EMBED_MAGIC_BYTES)
127     add_custom_command(
128         TARGET runtime
129         POST_BUILD
130         COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/embed-magic-bytes-in-file.sh ${CMAKE_CURRENT_BINARY_DIR}/runtime
131     )
132 endif()
134 # required for embedding in appimagetool
135 add_custom_command(
136     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime_embed.o
137     COMMAND ${XXD} -i runtime | ${CMAKE_C_COMPILER} -c -x c - -o runtime_embed.o
138     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
139     MAIN_DEPENDENCY runtime