Merge pull request #207 from wavexx/cmake_tweaks

cmake build tweaks
pull/212/head
Yuri D'Elia 2022-10-10 19:35:57 +02:00 committed by GitHub
commit b4e95f0c49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 18 deletions

View File

@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.19)
include(cmake/Utilities.cmake) include(cmake/Utilities.cmake)
include(cmake/GetGitRevisionDescription.cmake) include(cmake/GetGitRevisionDescription.cmake)
include(cmake/ProjectVersion.cmake) include(cmake/ProjectVersion.cmake)
include(cmake/ReproducibleBuild.cmake)
project( project(
MMU MMU
@ -95,25 +96,31 @@ add_compile_options(-g)
# optimizations # optimizations
if(CMAKE_CROSSCOMPILING) if(CMAKE_CROSSCOMPILING)
if(CMAKE_BUILD_TYPE STREQUAL "Debug") # TODO: get date from the last git commit to set as epoch
add_compile_options(-Og) set_source_epoch(0)
else()
add_compile_options(-Os) # default optimization flags
endif() set(CMAKE_CXX_FLAGS_DEBUG "-Og -g")
set(CMAKE_CXX_FLAGS_RELEASE "-Os -g -DNDEBUG")
set(CMAKE_C_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
set(CMAKE_C_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
# mcu related settings # mcu related settings
set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L) set(MCU_FLAGS -mmcu=atmega32u4 -DF_CPU=16000000L)
add_compile_options(${MCU_FLAGS}) add_compile_options(${MCU_FLAGS})
add_link_options(${MCU_FLAGS}) add_link_options(${MCU_FLAGS})
# disable some C++ language features
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-threadsafe-statics>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
# disable exceptions
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-unwind-tables>)
# split and gc sections # split and gc sections
add_compile_options(-ffunction-sections -fdata-sections) add_compile_options(-ffunction-sections -fdata-sections)
add_link_options(-Wl,--gc-sections) add_link_options(-ffunction-sections -fdata-sections -Wl,--gc-sections)
# disable exceptions and related metadata
add_compile_options(-fno-exceptions -fno-unwind-tables)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
add_link_options(-Wl,--defsym,__exidx_start=0,--defsym,__exidx_end=0)
else() else()
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-O0) add_compile_options(-O0)
@ -161,12 +168,16 @@ if(CMAKE_CROSSCOMPILING)
# produce ASM listing # produce ASM listing
add_custom_command( add_custom_command(
TARGET firmware POST_BUILD COMMAND ${CMAKE_OBJDUMP} -CSd firmware > firmware.asm TARGET firmware
POST_BUILD
COMMAND ${CMAKE_OBJDUMP} --prefix ${CMAKE_SOURCE_DIR} -CSd firmware > firmware.asm
) )
# inform about the firmware's size in terminal # inform about the firmware's size in terminal
add_custom_command( add_custom_command(
TARGET firmware POST_BUILD COMMAND ${CMAKE_SIZE_UTIL} -C --mcu=atmega32u4 firmware TARGET firmware
POST_BUILD
COMMAND ${CMAKE_SIZE_UTIL} -C --mcu=atmega32u4 firmware
) )
report_size(firmware) report_size(firmware)
@ -175,12 +186,12 @@ if(CMAKE_CROSSCOMPILING)
# Put Prusa Magic at the beginning of the hex # Put Prusa Magic at the beginning of the hex
add_custom_command( add_custom_command(
TARGET firmware POST_BUILD TARGET firmware
POST_BUILD
COMMAND COMMAND
${CMAKE_COMMAND} -D WORK_DIR=${CMAKE_BINARY_DIR} -D ${CMAKE_COMMAND} -D WORK_DIR=${CMAKE_BINARY_DIR} -D
HEX_NAME="MMU2SR_${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex" -P HEX_NAME="MMU2SR_${PROJECT_VERSION}${PROJECT_VERSION_SUFFIX_SHORT}.hex" -P
${CMAKE_SOURCE_DIR}/cmake/HexConcat.cmake ${CMAKE_SOURCE_DIR}/cmake/HexConcat.cmake DEPENDS firmware.hex
DEPENDS firmware.hex
) )
endif() endif()
@ -191,7 +202,9 @@ target_compile_options(firmware PRIVATE -Wdouble-promotion)
add_subdirectory(src) add_subdirectory(src)
if(NOT CMAKE_CROSSCOMPILING) if(CMAKE_CROSSCOMPILING)
set_all_targets_reproducible()
else()
# do not build the firmware by default (tests are the focus if not crosscompiling) # do not build the firmware by default (tests are the focus if not crosscompiling)
set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL YES) set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL YES)

View File

@ -0,0 +1,66 @@
#
# Functions and utilities for build reproducibility
#
# Set a target to be reproducible
function(set_reproducible_target target)
# properties for static libraries
set_target_properties(${target} PROPERTIES STATIC_LIBRARY_OPTIONS "-D")
# properties on executables
target_link_options(${target} PRIVATE -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=)
target_link_options(${target} PRIVATE -fdebug-prefix-map=${CMAKE_BINARY_DIR}=)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8")
target_compile_options(${target} PRIVATE -ffile-prefix-map=${CMAKE_SOURCE_DIR}=)
endif()
# properties on sources
get_target_property(sources ${target} SOURCES)
get_target_property(source_dir ${target} SOURCE_DIR)
foreach(file IN LISTS sources)
cmake_path(ABSOLUTE_PATH file BASE_DIRECTORY ${source_dir})
cmake_path(RELATIVE_PATH file BASE_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE rpath)
set_property(
SOURCE ${file}
DIRECTORY ${source_dir}
APPEND
PROPERTY COMPILE_OPTIONS "-frandom-seed=${rpath}"
)
endforeach()
endfunction()
# Get the list of targets for all directories
function(get_all_targets _result _dir)
get_property(
_subdirs
DIRECTORY "${_dir}"
PROPERTY SUBDIRECTORIES
)
foreach(_subdir IN LISTS _subdirs)
get_all_targets(${_result} "${_subdir}")
endforeach()
get_directory_property(_sub_targets DIRECTORY "${_dir}" BUILDSYSTEM_TARGETS)
set(${_result}
${${_result}} ${_sub_targets}
PARENT_SCOPE
)
endfunction()
# Make every target reproducible
function(set_all_targets_reproducible)
get_all_targets(targets ${CMAKE_SOURCE_DIR})
foreach(target IN LISTS targets)
set_reproducible_target(${target})
endforeach()
endfunction()
# Set source epoch
function(set_source_epoch epoch)
set(ENV{SOURCE_DATE_EPOCH} ${epoch})
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8")
string(TIMESTAMP SOURCE_DATE_EPOCH "%Y-%m-%d")
add_compile_definitions(SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH}")
string(TIMESTAMP SOURCE_TIME_EPOCH "%H:%M:%S")
add_compile_definitions(SOURCE_TIME_EPOCH="${SOURCE_TIME_EPOCH}")
endif()
endfunction()