common.cmake 11 KB

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