ReproducibleBuild.cmake 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #
  2. # Functions and utilities for build reproducibility
  3. #
  4. # Set a target to be reproducible
  5. function(set_reproducible_target target)
  6. # properties for static libraries
  7. set_target_properties(${target} PROPERTIES STATIC_LIBRARY_OPTIONS "-D")
  8. # properties on executables
  9. target_link_options(${target} PRIVATE -fdebug-prefix-map=${CMAKE_SOURCE_DIR}=)
  10. target_link_options(${target} PRIVATE -fdebug-prefix-map=${CMAKE_BINARY_DIR}=)
  11. if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "8")
  12. target_compile_options(${target} PRIVATE -ffile-prefix-map=${CMAKE_SOURCE_DIR}=)
  13. endif()
  14. # properties on sources
  15. get_target_property(sources ${target} SOURCES)
  16. get_target_property(source_dir ${target} SOURCE_DIR)
  17. foreach(file IN LISTS sources)
  18. cmake_path(ABSOLUTE_PATH file BASE_DIRECTORY ${source_dir})
  19. cmake_path(RELATIVE_PATH file BASE_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE rpath)
  20. set_property(
  21. SOURCE ${file}
  22. DIRECTORY ${source_dir}
  23. APPEND
  24. PROPERTY COMPILE_OPTIONS "-frandom-seed=${rpath}"
  25. )
  26. endforeach()
  27. endfunction()
  28. # Get the list of targets for all directories
  29. function(get_all_targets _result _dir)
  30. get_property(
  31. _subdirs
  32. DIRECTORY "${_dir}"
  33. PROPERTY SUBDIRECTORIES
  34. )
  35. foreach(_subdir IN LISTS _subdirs)
  36. get_all_targets(${_result} "${_subdir}")
  37. endforeach()
  38. get_directory_property(_sub_targets DIRECTORY "${_dir}" BUILDSYSTEM_TARGETS)
  39. set(${_result}
  40. ${${_result}} ${_sub_targets}
  41. PARENT_SCOPE
  42. )
  43. endfunction()
  44. # Make every target reproducible
  45. function(set_all_targets_reproducible)
  46. get_all_targets(targets ${CMAKE_SOURCE_DIR})
  47. foreach(target IN LISTS targets)
  48. set_reproducible_target(${target})
  49. endforeach()
  50. endfunction()
  51. # Set source epoch
  52. function(set_source_epoch epoch)
  53. set(ENV{SOURCE_DATE_EPOCH} ${epoch})
  54. if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "8")
  55. string(TIMESTAMP SOURCE_DATE_EPOCH "%Y-%m-%d")
  56. add_compile_definitions(SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH}")
  57. string(TIMESTAMP SOURCE_TIME_EPOCH "%H:%M:%S")
  58. add_compile_definitions(SOURCE_TIME_EPOCH="${SOURCE_TIME_EPOCH}")
  59. endif()
  60. endfunction()