common.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. if (ENUM_SERIALIZATION_ARGS_GEN_HEADER)
  106. set_property(SOURCE ${ENUM_SERIALIZATION_ARGS_GEN_HEADER} PROPERTY GENERATED On)
  107. add_custom_command(
  108. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp ${ENUM_SERIALIZATION_ARGS_GEN_HEADER}
  109. COMMAND
  110. ${enum_parser_bin}
  111. ${Input}
  112. --include-path ${ENUM_SERIALIZATION_ARGS_INCLUDE_HEADERS}
  113. --output ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp
  114. --header ${ENUM_SERIALIZATION_ARGS_GEN_HEADER}
  115. DEPENDS ${Input} ${enum_parser_dependency}
  116. )
  117. else()
  118. add_custom_command(
  119. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp
  120. COMMAND
  121. ${enum_parser_bin}
  122. ${Input}
  123. --include-path ${ENUM_SERIALIZATION_ARGS_INCLUDE_HEADERS}
  124. --output ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp
  125. DEPENDS ${Input} ${enum_parser_dependency}
  126. )
  127. endif()
  128. target_sources(${Tgt} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${BaseName}_serialized.cpp)
  129. endfunction()
  130. if (MSVC AND (${CMAKE_VERSION} VERSION_LESS "3.21.0"))
  131. message(FATAL_ERROR "Build with MSVC-compatible toolchain requires at least cmake 3.21.0 because of used TARGET_OBJECTS feature")
  132. endif()
  133. function(add_global_library_for TgtName MainName)
  134. if (MSVC)
  135. add_library(${TgtName} OBJECT ${ARGN})
  136. add_dependencies(${TgtName} ${MainName}) # needed because object library can use some extra generated files in MainName
  137. target_link_libraries(${MainName} INTERFACE ${TgtName} "$<TARGET_OBJECTS:${TgtName}>")
  138. else()
  139. add_library(${TgtName} STATIC ${ARGN})
  140. add_library(${TgtName}.wholearchive INTERFACE)
  141. add_dependencies(${TgtName}.wholearchive ${TgtName})
  142. add_dependencies(${TgtName} ${MainName})
  143. if(APPLE)
  144. target_link_options(${TgtName}.wholearchive INTERFACE "SHELL:-Wl,-force_load,$<TARGET_FILE:${TgtName}>")
  145. else()
  146. target_link_options(${TgtName}.wholearchive INTERFACE "SHELL:-Wl,--whole-archive $<TARGET_FILE:${TgtName}> -Wl,--no-whole-archive")
  147. endif()
  148. target_link_libraries(${MainName} INTERFACE ${TgtName}.wholearchive)
  149. endif()
  150. endfunction()
  151. function(copy_file From To)
  152. add_custom_command(
  153. OUTPUT ${To}
  154. COMMAND ${CMAKE_COMMAND} -E copy ${From} ${To}
  155. DEPENDS ${From}
  156. )
  157. endfunction()
  158. function(vcs_info Tgt)
  159. add_custom_command(
  160. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vcs_info.json
  161. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/generate_vcs_info.py ${CMAKE_CURRENT_BINARY_DIR}/vcs_info.json ${CMAKE_SOURCE_DIR}
  162. DEPENDS ${CMAKE_SOURCE_DIR}/build/scripts/generate_vcs_info.py
  163. )
  164. add_custom_command(
  165. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/__vcs_version__.c
  166. 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
  167. 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
  168. )
  169. target_sources(${Tgt} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/__vcs_version__.c)
  170. endfunction()
  171. function(resources Tgt Output)
  172. set(opts "")
  173. set(oneval_args "")
  174. set(multival_args INPUTS KEYS)
  175. cmake_parse_arguments(RESOURCE_ARGS
  176. "${opts}"
  177. "${oneval_args}"
  178. "${multival_args}"
  179. ${ARGN}
  180. )
  181. list(LENGTH RESOURCE_ARGS_INPUTS InputsCount)
  182. list(LENGTH RESOURCE_ARGS_KEYS KeysCount)
  183. if (NOT ${InputsCount} EQUAL ${KeysCount})
  184. message(FATAL_ERROR "Resources inputs count isn't equal to keys count in " ${Tgt})
  185. endif()
  186. math(EXPR ListsMaxIdx "${InputsCount} - 1")
  187. foreach(Idx RANGE ${ListsMaxIdx})
  188. list(GET RESOURCE_ARGS_INPUTS ${Idx} Input)
  189. list(GET RESOURCE_ARGS_KEYS ${Idx} Key)
  190. list(APPEND ResourcesList ${Input})
  191. list(APPEND ResourcesList ${Key})
  192. endforeach()
  193. get_built_tool_path(rescompiler_bin rescompiler_dependency tools/rescompiler/bin rescompiler)
  194. add_custom_command(
  195. OUTPUT ${Output}
  196. COMMAND ${rescompiler_bin} ${Output} ${ResourcesList}
  197. DEPENDS ${RESOURCE_ARGS_INPUTS} ${rescompiler_dependency}
  198. )
  199. endfunction()
  200. function(use_export_script Target ExportFile)
  201. get_filename_component(OutName ${ExportFile} NAME)
  202. set(OutPath ${CMAKE_CURRENT_BINARY_DIR}/gen_${OutName})
  203. if (MSVC)
  204. target_link_options(${Target} PRIVATE /DEF:${OutPath})
  205. set(EXPORT_SCRIPT_FLAVOR msvc)
  206. elseif(APPLE)
  207. execute_process(
  208. COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/build/scripts/export_script_gen.py ${ExportFile} - --format darwin
  209. RESULT_VARIABLE _SCRIPT_RES
  210. OUTPUT_VARIABLE _SCRIPT_FLAGS
  211. ERROR_VARIABLE _SCRIPT_STDERR
  212. )
  213. if (NOT ${_SCRIPT_RES} EQUAL 0)
  214. message(FATAL_ERROR "Failed to parse export symbols from ${ExportFile}:\n${_SCRIPT_STDERR}")
  215. return()
  216. endif()
  217. separate_arguments(ParsedScriptFlags NATIVE_COMMAND ${_SCRIPT_FLAGS})
  218. target_link_options(${Target} PRIVATE ${ParsedScriptFlags})
  219. return()
  220. else()
  221. set(EXPORT_SCRIPT_FLAVOR gnu)
  222. target_link_options(${Target} PRIVATE -Wl,--gc-sections -rdynamic -Wl,--version-script=${OutPath})
  223. endif()
  224. add_custom_command(
  225. OUTPUT ${OutPath}
  226. COMMAND
  227. Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/export_script_gen.py ${ExportFile} ${OutPath} --format ${EXPORT_SCRIPT_FLAVOR}
  228. DEPENDS ${ExportFile} ${CMAKE_SOURCE_DIR}/build/scripts/export_script_gen.py
  229. )
  230. target_sources(${Target} PRIVATE ${OutPath})
  231. set_property(SOURCE ${OutPath} PROPERTY
  232. HEADER_FILE_ONLY On
  233. )
  234. set_property(TARGET ${Target} APPEND PROPERTY
  235. LINK_DEPENDS ${OutPath}
  236. )
  237. endfunction()
  238. function(add_yunittest)
  239. set(opts "")
  240. set(oneval_args NAME TEST_TARGET)
  241. set(multival_args TEST_ARG)
  242. cmake_parse_arguments(YUNITTEST_ARGS
  243. "${opts}"
  244. "${oneval_args}"
  245. "${multival_args}"
  246. ${ARGN}
  247. )
  248. get_property(SPLIT_FACTOR TARGET ${YUNITTEST_ARGS_TEST_TARGET} PROPERTY SPLIT_FACTOR)
  249. get_property(SPLIT_TYPE TARGET ${YUNITTEST_ARGS_TEST_TARGET} PROPERTY SPLIT_TYPE)
  250. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/run_testpack")
  251. add_test(NAME ${YUNITTEST_ARGS_NAME} COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/run_testpack" ${YUNITTEST_ARGS_TEST_ARG})
  252. 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}")
  253. return()
  254. endif()
  255. if (${SPLIT_FACTOR} EQUAL 1)
  256. add_test(NAME ${YUNITTEST_ARGS_NAME} COMMAND ${YUNITTEST_ARGS_TEST_TARGET} ${YUNITTEST_ARGS_TEST_ARG})
  257. return()
  258. endif()
  259. if ("${SPLIT_TYPE}")
  260. set(FORK_MODE_ARG --fork-mode ${SPLIT_TYPE})
  261. endif()
  262. math(EXPR LastIdx "${SPLIT_FACTOR} - 1")
  263. foreach(Idx RANGE ${LastIdx})
  264. add_test(NAME ${YUNITTEST_ARGS_NAME}_${Idx}
  265. COMMAND Python3::Interpreter ${CMAKE_SOURCE_DIR}/build/scripts/split_unittest.py --split-factor ${SPLIT_FACTOR} ${FORK_MODE_ARG} --shard ${Idx}
  266. $<TARGET_FILE:${YUNITTEST_ARGS_TEST_TARGET}> ${YUNITTEST_ARGS_TEST_ARG})
  267. endforeach()
  268. endfunction()
  269. function(set_yunittest_property)
  270. set(opts "")
  271. set(oneval_args TEST PROPERTY)
  272. set(multival_args )
  273. cmake_parse_arguments(YUNITTEST_ARGS
  274. "${opts}"
  275. "${oneval_args}"
  276. "${multival_args}"
  277. ${ARGN}
  278. )
  279. get_property(SPLIT_FACTOR TARGET ${YUNITTEST_ARGS_TEST} PROPERTY SPLIT_FACTOR)
  280. if ((${SPLIT_FACTOR} EQUAL 1) OR (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/run_testpack"))
  281. set_property(TEST ${YUNITTEST_ARGS_TEST} PROPERTY ${YUNITTEST_ARGS_PROPERTY} "${YUNITTEST_ARGS_UNPARSED_ARGUMENTS}")
  282. return()
  283. endif()
  284. math(EXPR LastIdx "${SPLIT_FACTOR} - 1")
  285. foreach(Idx RANGE ${LastIdx})
  286. set_property(TEST ${YUNITTEST_ARGS_TEST}_${Idx} PROPERTY ${YUNITTEST_ARGS_PROPERTY} "${YUNITTEST_ARGS_UNPARSED_ARGUMENTS}")
  287. endforeach()
  288. endfunction()
  289. 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)
  290. function(target_allocator Tgt)
  291. if (CUSTOM_ALLOCATORS)
  292. target_link_libraries(${Tgt} PRIVATE ${ARGN})
  293. else()
  294. target_link_libraries(${Tgt} PRIVATE system_allocator)
  295. endif()
  296. endfunction()