Skip to content

Commit

Permalink
cmake: reduce redundancy in MakeCPIO()
Browse files Browse the repository at this point in the history
Define helper variables to reduce redundancy

Signed-off-by: Axel Heider <[email protected]>
  • Loading branch information
Axel Heider committed Mar 14, 2024
1 parent 8c660fd commit d48f613
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions cmake-tool/helpers/cpio.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ function(MakeCPIO output_name input_files)
if(NOT "${MAKE_CPIO_CPIO_SYMBOL}" STREQUAL "")
set(archive_symbol ${MAKE_CPIO_CPIO_SYMBOL})
endif()

# intermediate files
set(cpio_archive "archive.${output_name}.cpio")
set(cpio_archive_s "${output_name}.S")

# Check that the reproducible flag is available. Don't use it if it isn't.
CheckCPIOArgument(cpio_reproducible_flag "--reproducible")
set(
commands
"bash;-c;cpio ${cpio_reproducible_flag} --quiet --create -H newc --file=${CMAKE_CURRENT_BINARY_DIR}/archive.${output_name}.cpio;&&"
cpio_cmd
"cpio ${cpio_reproducible_flag} --quiet --create -H newc --file=${CMAKE_CURRENT_BINARY_DIR}/${cpio_archive}"
)

set(tmp_dir "temp_${output_name}")
set(commands "bash;-c;${cpio_cmd};&&")
foreach(file IN LISTS input_files)
# Try and generate reproducible cpio meta-data as we do this:
# - touch -d @0 file sets the modified time to 0
Expand All @@ -57,30 +65,31 @@ function(MakeCPIO output_name input_files)
list(
APPEND
commands
"bash;-c; mkdir -p temp_${output_name} && cd temp_${output_name} && cp -a ${file} . && touch -d 1970-01-01T00:00:00Z `basename ${file}` && echo `basename ${file}` | cpio --append ${cpio_reproducible_flag} --owner=+0:+0 --quiet -o -H newc --file=${CMAKE_CURRENT_BINARY_DIR}/archive.${output_name}.cpio && rm `basename ${file}` && cd ../ && rmdir temp_${output_name};&&"
"bash;-c; mkdir -p ${tmp_dir} && cd ${tmp_dir} && cp -a ${file} . && touch -d 1970-01-01T00:00:00Z `basename ${file}` && echo `basename ${file}` | ${cpio_cmd} --append --owner=+0:+0 && rm `basename ${file}` && cd ../ && rmdir ${tmp_dir};&&"
)
endforeach()
list(APPEND commands "true")

separate_arguments(cmake_c_flags_sep NATIVE_COMMAND "${CMAKE_C_FLAGS}")
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
list(APPEND cmake_c_flags_sep "${CMAKE_C_COMPILE_OPTIONS_TARGET}${CMAKE_C_COMPILER_TARGET}")
endif()

add_custom_command(
OUTPUT ${output_name}
COMMAND rm -f archive.${output_name}.cpio
COMMAND rm -f ${cpio_archive}
COMMAND ${commands}
COMMAND
sh -c
"echo 'X.section ._archive_cpio,\"aw\"X.globl ${archive_symbol}, ${archive_symbol}_endX${archive_symbol}:X.incbin \"archive.${output_name}.cpio\"X${archive_symbol}_end:X' | tr X '\\n'"
> ${output_name}.S
"echo 'X.section ._archive_cpio,\"aw\"X.globl ${archive_symbol}, ${archive_symbol}_endX${archive_symbol}:X.incbin \"${cpio_archive}\"X${archive_symbol}_end:X' | tr X '\\n'"
> "${cpio_archive_s}"
COMMAND
${CMAKE_C_COMPILER} ${cmake_c_flags_sep} -c -o ${output_name} ${output_name}.S
${CMAKE_C_COMPILER} ${cmake_c_flags_sep} -c -o ${output_name} "${cpio_archive_s}"
DEPENDS ${input_files} ${MAKE_CPIO_DEPENDS}
VERBATIM
BYPRODUCTS
archive.${output_name}.cpio
${output_name}.S
"${cpio_archive}"
"${cpio_archive_s}"
COMMENT "Generate CPIO archive ${output_name}"
)
endfunction(MakeCPIO)

0 comments on commit d48f613

Please sign in to comment.