common.cmake 12 KB

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