common.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # Set of common macros
  2. find_package(Python3 REQUIRED)
  3. add_compile_definitions(CATBOOST_OPENSOURCE=yes)
  4. # assumes ToolName is always both the binary and the target name
  5. function(get_built_tool_path OutBinPath OutDependency SrcPath ToolName)
  6. if (CMAKE_GENERATOR MATCHES "Visual.Studio.*")
  7. set(BinPath "${TOOLS_ROOT}/${SrcPath}/\$(Configuration)/${ToolName}${CMAKE_EXECUTABLE_SUFFIX}")
  8. else()
  9. set(BinPath "${TOOLS_ROOT}/${SrcPath}/${ToolName}${CMAKE_EXECUTABLE_SUFFIX}")
  10. endif()
  11. set(${OutBinPath} ${BinPath} PARENT_SCOPE)
  12. if (CMAKE_CROSSCOMPILING)
  13. set(${OutDependency} ${BinPath} PARENT_SCOPE)
  14. else()
  15. set(${OutDependency} ${ToolName} PARENT_SCOPE)
  16. endif()
  17. endfunction()
  18. function(target_ragel_lexers TgtName Key Src)
  19. SET(RAGEL_BIN ${CMAKE_BINARY_DIR}/bin/ragel${CMAKE_EXECUTABLE_SUFFIX})
  20. get_filename_component(OutPath ${Src} NAME_WLE)
  21. get_filename_component(SrcDirPath ${Src} DIRECTORY)
  22. get_filename_component(OutputExt ${OutPath} EXT)
  23. if (OutputExt STREQUAL "")
  24. string(APPEND OutPath .rl6.cpp)
  25. endif()
  26. add_custom_command(
  27. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OutPath}
  28. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/run_tool.py -- ${RAGEL_BIN} ${RAGEL_FLAGS} ${ARGN} -o ${CMAKE_CURRENT_BINARY_DIR}/${OutPath} ${Src}
  29. DEPENDS ${CMAKE_SOURCE_DIR}/build/scripts/run_tool.py ${Src}
  30. WORKING_DIRECTORY ${SrcDirPath}
  31. )
  32. target_sources(${TgtName} ${Key} ${CMAKE_CURRENT_BINARY_DIR}/${OutPath})
  33. endfunction()
  34. function(target_yasm_source TgtName Key Src)
  35. SET(YASM_BIN ${CMAKE_BINARY_DIR}/bin/yasm${CMAKE_EXECUTABLE_SUFFIX})
  36. get_filename_component(OutPath ${Src} NAME_WLE)
  37. string(APPEND OutPath .o)
  38. add_custom_command(
  39. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${OutPath}
  40. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/run_tool.py -- ${YASM_BIN} ${YASM_FLAGS} ${ARGN} -o ${CMAKE_CURRENT_BINARY_DIR}/${OutPath} ${Src}
  41. DEPENDS ${CMAKE_SOURCE_DIR}/build/scripts/run_tool.py ${Src}
  42. )
  43. target_sources(${TgtName} ${Key} ${CMAKE_CURRENT_BINARY_DIR}/${OutPath})
  44. endfunction()
  45. function(target_joined_source TgtName Out)
  46. foreach(InSrc ${ARGN})
  47. file(RELATIVE_PATH IncludePath ${CMAKE_SOURCE_DIR} ${InSrc})
  48. list(APPEND IncludesList ${IncludePath})
  49. endforeach()
  50. add_custom_command(
  51. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${Out}
  52. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/gen_join_srcs.py ${CMAKE_CURRENT_BINARY_DIR}/${Out} ${IncludesList}
  53. DEPENDS ${CMAKE_SOURCE_DIR}/build/scripts/gen_join_srcs.py ${ARGN}
  54. )
  55. target_sources(${TgtName} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${Out})
  56. endfunction()
  57. function(target_sources_custom TgtName CompileOutSuffix)
  58. set(opts "")
  59. set(oneval_args "")
  60. set(multival_args SRCS CUSTOM_FLAGS)
  61. cmake_parse_arguments(TARGET_SOURCES_CUSTOM
  62. "${opts}"
  63. "${oneval_args}"
  64. "${multival_args}"
  65. ${ARGN}
  66. )
  67. foreach(Src ${TARGET_SOURCES_CUSTOM_SRCS})
  68. file(RELATIVE_PATH SrcRealPath ${CMAKE_SOURCE_DIR} ${Src})
  69. get_filename_component(SrcDir ${SrcRealPath} DIRECTORY)
  70. get_filename_component(SrcName ${SrcRealPath} NAME_WLE)
  71. get_filename_component(SrcExt ${SrcRealPath} LAST_EXT)
  72. set(SrcCopy "${CMAKE_BINARY_DIR}/${SrcDir}/${SrcName}${CompileOutSuffix}${SrcExt}")
  73. add_custom_command(
  74. OUTPUT ${SrcCopy}
  75. COMMAND ${CMAKE_COMMAND} -E copy ${Src} ${SrcCopy}
  76. DEPENDS ${Src}
  77. )
  78. list(APPEND PreparedSrc ${SrcCopy})
  79. set_property(
  80. SOURCE
  81. ${SrcCopy}
  82. APPEND PROPERTY COMPILE_OPTIONS
  83. ${TARGET_SOURCES_CUSTOM_CUSTOM_FLAGS}
  84. -I${CMAKE_SOURCE_DIR}/${SrcDir}
  85. )
  86. endforeach()
  87. target_sources(
  88. ${TgtName}
  89. PRIVATE
  90. ${PreparedSrc}
  91. )
  92. endfunction()
  93. function(generate_enum_serilization Tgt Input)
  94. set(opts "")
  95. set(oneval_args INCLUDE_HEADERS GEN_HEADER)
  96. set(multival_args "")
  97. cmake_parse_arguments(ENUM_SERIALIZATION_ARGS
  98. "${opts}"
  99. "${oneval_args}"
  100. "${multival_args}"
  101. ${ARGN}
  102. )
  103. get_built_tool_path(enum_parser_bin enum_parser_dependency tools/enum_parser/enum_parser enum_parser)
  104. get_filename_component(BaseName ${Input} NAME)
  105. add_custom_command(
  106. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp ${ENUM_SERIALIZATION_ARGS_GEN_HEADER}
  107. COMMAND
  108. ${enum_parser_bin}
  109. ${Input}
  110. --include-path ${ENUM_SERIALIZATION_ARGS_INCLUDE_HEADERS}
  111. --output ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp
  112. DEPENDS ${Input} ${enum_parser_dependency}
  113. )
  114. if (ENUM_SERIALIZATION_ARGS_GEN_HEADER)
  115. set_property(SOURCE ${ENUM_SERIALIZATION_ARGS_GEN_HEADER} PROPERTY GENERATED On)
  116. endif()
  117. target_sources(${Tgt} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp)
  118. endfunction()
  119. if (MSVC AND (${CMAKE_VERSION} VERSION_LESS "3.21.0"))
  120. message(FATAL_ERROR "Build with MSVC-compatible toolchain requires at least cmake 3.21.0 because of used TARGET_OBJECTS feature")
  121. endif()
  122. function(add_global_library_for TgtName MainName)
  123. if (MSVC)
  124. add_library(${TgtName} OBJECT ${ARGN})
  125. add_dependencies(${TgtName} ${MainName}) # needed because object library can use some extra generated files in MainName
  126. target_link_libraries(${MainName} INTERFACE ${TgtName} "$<TARGET_OBJECTS:${TgtName}>")
  127. else()
  128. add_library(${TgtName} STATIC ${ARGN})
  129. add_library(${TgtName}.wholearchive INTERFACE)
  130. add_dependencies(${TgtName}.wholearchive ${TgtName})
  131. add_dependencies(${TgtName} ${MainName})
  132. if(APPLE)
  133. target_link_options(${TgtName}.wholearchive INTERFACE "SHELL:-Wl,-force_load,$<TARGET_FILE:${TgtName}>")
  134. else()
  135. target_link_options(${TgtName}.wholearchive INTERFACE "SHELL:-Wl,--whole-archive $<TARGET_FILE:${TgtName}> -Wl,--no-whole-archive")
  136. endif()
  137. target_link_libraries(${MainName} INTERFACE ${TgtName}.wholearchive)
  138. endif()
  139. endfunction()
  140. function(copy_file From To)
  141. add_custom_command(
  142. OUTPUT ${To}
  143. COMMAND ${CMAKE_COMMAND} -E copy ${From} ${To}
  144. DEPENDS ${From}
  145. )
  146. endfunction()
  147. function(vcs_info Tgt)
  148. add_custom_command(
  149. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vcs_info.json
  150. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/generate_vcs_info.py ${CMAKE_CURRENT_BINARY_DIR}/vcs_info.json ${CMAKE_SOURCE_DIR}
  151. DEPENDS ${CMAKE_SOURCE_DIR}/build/scripts/generate_vcs_info.py
  152. )
  153. add_custom_command(
  154. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/__vcs_version__.c
  155. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/vcs_info.py ${CMAKE_CURRENT_BINARY_DIR}/vcs_info.json ${CMAKE_CURRENT_BINARY_DIR}/__vcs_version__.c ${CMAKE_SOURCE_DIR}/build/scripts/c_templates/svn_interface.c
  156. DEPENDS ${CMAKE_SOURCE_DIR}/build/scripts/vcs_info.py ${CMAKE_SOURCE_DIR}/build/scripts/c_templates/svn_interface.c ${CMAKE_CURRENT_BINARY_DIR}/vcs_info.json
  157. )
  158. target_sources(${Tgt} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/__vcs_version__.c)
  159. endfunction()
  160. function(resources Tgt Output)
  161. set(opts "")
  162. set(oneval_args "")
  163. set(multival_args INPUTS KEYS)
  164. cmake_parse_arguments(RESOURCE_ARGS
  165. "${opts}"
  166. "${oneval_args}"
  167. "${multival_args}"
  168. ${ARGN}
  169. )
  170. list(LENGTH RESOURCE_ARGS_INPUTS InputsCount)
  171. list(LENGTH RESOURCE_ARGS_KEYS KeysCount)
  172. if (NOT ${InputsCount} EQUAL ${KeysCount})
  173. message(FATAL_ERROR "Resources inputs count isn't equal to keys count in " ${Tgt})
  174. endif()
  175. math(EXPR ListsMaxIdx "${InputsCount} - 1")
  176. foreach(Idx RANGE ${ListsMaxIdx})
  177. list(GET RESOURCE_ARGS_INPUTS ${Idx} Input)
  178. list(GET RESOURCE_ARGS_KEYS ${Idx} Key)
  179. list(APPEND ResourcesList ${Input})
  180. list(APPEND ResourcesList ${Key})
  181. endforeach()
  182. get_built_tool_path(rescompiler_bin rescompiler_dependency tools/rescompiler/bin rescompiler)
  183. add_custom_command(
  184. OUTPUT ${Output}
  185. COMMAND ${rescompiler_bin} ${Output} ${ResourcesList}
  186. DEPENDS ${RESOURCE_ARGS_INPUTS} ${rescompiler_dependency}
  187. )
  188. endfunction()
  189. function(use_export_script Target ExportFile)
  190. get_filename_component(OutName ${ExportFile} NAME)
  191. set(OutPath ${CMAKE_CURRENT_BINARY_DIR}/gen_${OutName})
  192. if (MSVC)
  193. target_link_options(${Target} PRIVATE /DEF:${OutPath})
  194. set(EXPORT_SCRIPT_FLAVOR msvc)
  195. elseif(APPLE)
  196. execute_process(
  197. COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/build/scripts/export_script_gen.py ${ExportFile} - --format darwin
  198. RESULT_VARIABLE _SCRIPT_RES
  199. OUTPUT_VARIABLE _SCRIPT_FLAGS
  200. ERROR_VARIABLE _SCRIPT_STDERR
  201. )
  202. if (NOT ${_SCRIPT_RES} EQUAL 0)
  203. message(FATAL_ERROR "Failed to parse export symbols from ${ExportFile}:\n${_SCRIPT_STDERR}")
  204. return()
  205. endif()
  206. separate_arguments(ParsedScriptFlags NATIVE_COMMAND ${_SCRIPT_FLAGS})
  207. target_link_options(${Target} PRIVATE ${ParsedScriptFlags})
  208. return()
  209. else()
  210. set(EXPORT_SCRIPT_FLAVOR gnu)
  211. target_link_options(${Target} PRIVATE -Wl,--gc-sections -rdynamic -Wl,--version-script=${OutPath})
  212. endif()
  213. add_custom_command(
  214. OUTPUT ${OutPath}
  215. COMMAND
  216. Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/export_script_gen.py ${ExportFile} ${OutPath} --format ${EXPORT_SCRIPT_FLAVOR}
  217. DEPENDS ${ExportFile} ${CMAKE_SOURCE_DIR}/build/scripts/export_script_gen.py
  218. )
  219. target_sources(${Target} PRIVATE ${OutPath})
  220. set_property(SOURCE ${OutPath} PROPERTY
  221. HEADER_FILE_ONLY On
  222. )
  223. set_property(TARGET ${Target} APPEND PROPERTY
  224. LINK_DEPENDS ${OutPath}
  225. )
  226. endfunction()
  227. function(add_yunittest)
  228. set(opts "")
  229. set(oneval_args NAME TEST_TARGET)
  230. set(multival_args TEST_ARG)
  231. cmake_parse_arguments(YUNITTEST_ARGS
  232. "${opts}"
  233. "${oneval_args}"
  234. "${multival_args}"
  235. ${ARGN}
  236. )
  237. get_property(SPLIT_FACTOR TARGET ${YUNITTEST_ARGS_TEST_TARGET} PROPERTY SPLIT_FACTOR)
  238. get_property(SPLIT_TYPE TARGET ${YUNITTEST_ARGS_TEST_TARGET} PROPERTY SPLIT_TYPE)
  239. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/run_testpack")
  240. add_test(NAME ${YUNITTEST_ARGS_NAME} COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/run_testpack" ${YUNITTEST_ARGS_TEST_ARG})
  241. set_property(TEST ${YUNITTEST_ARGS_NAME} PROPERTY ENVIRONMENT "source_root=${CMAKE_SOURCE_DIR};build_root=${CMAKE_BINARY_DIR};test_split_factor=${SPLIT_FACTOR};test_split_type=${SPLIT_TYPE}")
  242. return()
  243. endif()
  244. if (${SPLIT_FACTOR} EQUAL 1)
  245. add_test(NAME ${YUNITTEST_ARGS_NAME} COMMAND ${YUNITTEST_ARGS_TEST_TARGET} ${YUNITTEST_ARGS_TEST_ARG})
  246. return()
  247. endif()
  248. if ("${SPLIT_TYPE}")
  249. set(FORK_MODE_ARG --fork-mode ${SPLIT_TYPE})
  250. endif()
  251. math(EXPR LastIdx "${SPLIT_FACTOR} - 1")
  252. foreach(Idx RANGE ${LastIdx})
  253. add_test(NAME ${YUNITTEST_ARGS_NAME}_${Idx}
  254. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/split_unittest.py --split-factor ${SPLIT_FACTOR} ${FORK_MODE_ARG} --shard ${Idx}
  255. $<TARGET_FILE:${YUNITTEST_ARGS_TEST_TARGET}> ${YUNITTEST_ARGS_TEST_ARG})
  256. endforeach()
  257. endfunction()
  258. function(set_yunittest_property)
  259. set(opts "")
  260. set(oneval_args TEST PROPERTY)
  261. set(multival_args )
  262. cmake_parse_arguments(YUNITTEST_ARGS
  263. "${opts}"
  264. "${oneval_args}"
  265. "${multival_args}"
  266. ${ARGN}
  267. )
  268. get_property(SPLIT_FACTOR TARGET ${YUNITTEST_ARGS_TEST} PROPERTY SPLIT_FACTOR)
  269. if ((${SPLIT_FACTOR} EQUAL 1) OR (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/run_testpack"))
  270. set_property(TEST ${YUNITTEST_ARGS_TEST} PROPERTY ${YUNITTEST_ARGS_PROPERTY} "${YUNITTEST_ARGS_UNPARSED_ARGUMENTS}")
  271. return()
  272. endif()
  273. math(EXPR LastIdx "${SPLIT_FACTOR} - 1")
  274. foreach(Idx RANGE ${LastIdx})
  275. set_property(TEST ${YUNITTEST_ARGS_TEST}_${Idx} PROPERTY ${YUNITTEST_ARGS_PROPERTY} "${YUNITTEST_ARGS_UNPARSED_ARGUMENTS}")
  276. endforeach()
  277. endfunction()
  278. option(CUSTOM_ALLOCATORS "Enables use of per executable specified allocators. Can be turned off in order to use code instrumentation tooling relying on system allocator (sanitizers, heaptrack, ...)" On)
  279. function(target_allocator Tgt)
  280. if (CUSTOM_ALLOCATORS)
  281. target_link_libraries(${Tgt} PRIVATE ${ARGN})
  282. else()
  283. target_link_libraries(${Tgt} PRIVATE system_allocator)
  284. endif()
  285. endfunction()