Merge pull request #865 from AppImage/fix_appimagetool_names_architecture_wrongly
[appimagekit.git] / src / build-runtime.cmake
blobeed2d6d0136c07bfea6c7530b9697b1bd71e46d0
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 set(runtime_cflags
19     -std=c99 -ffunction-sections -fdata-sections
20     -DGIT_COMMIT=\\"${GIT_COMMIT}\\"
21     -I${squashfuse_INCLUDE_DIRS}
22     -I${PROJECT_SOURCE_DIR}/include
23     -I${PROJECT_SOURCE_DIR}/lib/libappimage/include
24     -I${PROJECT_SOURCE_DIR}/lib/libappimage/src/libappimage_hashlib/include
25     ${DEPENDENCIES_CFLAGS}
27 set(runtime_ldflags -s -Wl,--gc-sections ${DEPENDENCIES_LDFLAGS})
29 if(BUILD_DEBUG)
30     message(WARNING "Debug build, adding debug information")
31     set(runtime_cflags -g ${runtime_cflags})
32 else()
33     message(STATUS "Release build, optimizing runtime")
34     set(runtime_cflags -Os ${runtime_cflags})
35 endif()
37 if(NOT xz_INCLUDE_DIRS STREQUAL "")
38     list(APPEND runtime_cflags -I${xz_INCLUDE_DIRS})
39 endif()
41 if(APPIMAGEKIT_RUNTIME_ENABLE_SETPROCTITLE)
42     set(runtime_cflags ${runtime_cflags} -DENABLE_SETPROCTITLE)
43 endif()
45 # objcopy requires actual files for creating new sections to populate the new section
46 # therefore, we generate 3 suitable files containing blank bytes in the right sizes
47 add_custom_command(
48     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/16_blank_bytes
49     COMMAND printf '\\0%.0s' {0..15} > ${CMAKE_CURRENT_BINARY_DIR}/16_blank_bytes
51 add_custom_command(
52     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
53     COMMAND printf '\\0%.0s' {0..1023} > ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
55 add_custom_command(
56     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/8192_blank_bytes
57     COMMAND printf '\\0%.0s' {0..8191} > ${CMAKE_CURRENT_BINARY_DIR}/8192_blank_bytes
60 # compile first raw object (not linked yet) into which the sections will be embedded
61 # TODO: find out how this .o object can be generated using a normal add_executable call
62 # that'd allow us to get rid of the -I parameters in runtime_cflags
63 add_custom_command(
64     MAIN_DEPENDENCY runtime.c
65     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.0.o
66     COMMAND ${CMAKE_C_COMPILER} ${runtime_cflags} -c ${CMAKE_CURRENT_SOURCE_DIR}/runtime.c -o runtime.0.o
67     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
70 # embed the sections, one by one
71 # TODO: find out whether all the sections can be embedded in a single objcopy call
72 add_custom_command(
73     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.1.o
74     COMMAND ${OBJCOPY} --add-section .digest_md5=16_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.0.o runtime.1.o
75     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.0.o
76     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/16_blank_bytes
77     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
79 add_custom_command(
80     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.2.o
81     COMMAND ${OBJCOPY} --add-section .upd_info=1024_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.1.o runtime.2.o
82     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.1.o
83     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
84     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
86 add_custom_command(
87     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.3.o
88     COMMAND ${OBJCOPY} --add-section .sha256_sig=1024_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.2.o runtime.3.o
89     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.2.o
90     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/1024_blank_bytes
91     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
93 add_custom_command(
94     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime.4.o
95     COMMAND ${OBJCOPY} --add-section .sig_key=8192_blank_bytes --set-section-flags .digest_md5=noload,readonly runtime.3.o runtime.4.o
96     MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/runtime.3.o
97     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/8192_blank_bytes
98     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
101 # add the runtime as a normal executable
102 # CLion will recognize it as a normal executable, one can simply step into the code
103 add_executable(runtime ${CMAKE_CURRENT_BINARY_DIR}/runtime.4.o notify.c)
104 # CMake gets confused by the .o object, therefore we need to tell it that it shall link everything using the C compiler
105 set_property(TARGET runtime PROPERTY LINKER_LANGUAGE C)
106 target_link_libraries(runtime PRIVATE squashfuse dl xz libzlib pthread libappimage_shared libappimage_hashlib)
107 target_include_directories(runtime PRIVATE ${PROJECT_SOURCE_DIR}/include)
109 if(BUILD_DEBUG)
110     message(WARNING "Debug build, not stripping runtime to allow debugging using gdb etc.")
111 else()
112     add_custom_command(
113         TARGET runtime
114         POST_BUILD
115         COMMAND ${STRIP} ${CMAKE_CURRENT_BINARY_DIR}/runtime
116     )
117 endif()
119 # embed the magic bytes after the runtime's build has finished
120 if(APPIMAGEKIT_EMBED_MAGIC_BYTES)
121     add_custom_command(
122         TARGET runtime
123         POST_BUILD
124         COMMAND printf '\\x41\\x49\\x02' | dd of=runtime bs=1 seek=8 count=3 conv=notrunc
125     )
126 endif()
128 # required for embedding in appimagetool
129 add_custom_command(
130     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/runtime_embed.o
131     COMMAND ${XXD} -i runtime | ${CMAKE_C_COMPILER} -c -x c - -o runtime_embed.o
132     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
133     MAIN_DEPENDENCY runtime