BuildFunctions.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. ################################################################################
  2. #
  3. # macros and functions
  4. #
  5. ################################################################################
  6. ########################################
  7. # FUNCTION set_output_directory
  8. ########################################
  9. function(set_output_directory target dir)
  10. set(out ${output_dir})
  11. if (MSVC OR XCODE) # multiconfiguration builds
  12. set(out ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
  13. endif()
  14. if ("${ARGV2}" STREQUAL "CURRENT_DIR")
  15. set(out .)
  16. if (UNIX AND "${dir}" STREQUAL ".")
  17. set(dir bin)
  18. endif()
  19. endif()
  20. if (MSVC OR XCODE)
  21. foreach(conf ${CMAKE_CONFIGURATION_TYPES})
  22. string(TOUPPER ${conf} conf2)
  23. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${conf2} ${out}/${conf}/${dir})
  24. set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${conf2} ${out}/${conf}/${dir})
  25. endforeach()
  26. else() # single configuration
  27. add_custom_command(TARGET ${target} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${out}/${dir})
  28. set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${out}/${dir})
  29. set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${out}/${dir})
  30. endif()
  31. endfunction(set_output_directory)
  32. ########################################
  33. # FUNCTION set_output_directory_unix
  34. ########################################
  35. function(set_output_directory_unix target dir)
  36. if (UNIX)
  37. set_output_directory(${target} ${dir} ${ARGN})
  38. endif()
  39. endfunction(set_output_directory_unix)
  40. ########################################
  41. # FUNCTION set_exported_symbols
  42. ########################################
  43. if (WIN32)
  44. function(set_exported_symbols target filename)
  45. set(def_file ${filename}.def)
  46. if ("${filename}" STREQUAL "empty")
  47. set(def_file)
  48. elseif("${filename}" STREQUAL "fbplugin")
  49. set(def_file "plugin.def")
  50. endif()
  51. if (NOT "${def_file}" STREQUAL "")
  52. if (MSVC)
  53. set_target_properties(${target} PROPERTIES LINK_FLAGS "/DEF:\"${CMAKE_SOURCE_DIR}/builds/win32/defs/${def_file}\"")
  54. endif()
  55. if (MINGW)
  56. #set_target_properties(${target} PROPERTIES LINK_FLAGS "-Wl,${CMAKE_SOURCE_DIR}/builds/win32/defs/${def_file}")
  57. endif()
  58. endif()
  59. endfunction(set_exported_symbols)
  60. endif()
  61. if (UNIX)
  62. function(set_exported_symbols target filename)
  63. set(def_file ${filename}.vers)
  64. if ("${filename}" STREQUAL "ib_udf")
  65. set(def_file)
  66. endif()
  67. if (NOT "${def_file}" STREQUAL "")
  68. set(wl_option "--version-script")
  69. if (APPLE)
  70. set(wl_option "-exported_symbols_list")
  71. endif()
  72. set_target_properties(${target} PROPERTIES LINK_FLAGS -Wl,${wl_option},${CMAKE_BINARY_DIR}/builds/posix/${def_file})
  73. endif()
  74. endfunction(set_exported_symbols)
  75. endif(UNIX)
  76. ########################################
  77. # FUNCTION epp_process
  78. ########################################
  79. function(epp_process type files)
  80. set(epp_suffix ".${type}.cpp")
  81. foreach(F ${${files}})
  82. set(in ${CMAKE_CURRENT_SOURCE_DIR}/${F})
  83. set(out ${CMAKE_CURRENT_BINARY_DIR}/${F}${epp_suffix})
  84. get_filename_component(dir ${out} PATH)
  85. if (MSVC OR XCODE)
  86. set(dir ${dir}/$<CONFIG>)
  87. endif()
  88. if ("${type}" STREQUAL "boot")
  89. add_custom_command(
  90. OUTPUT ${out}
  91. DEPENDS gpre_boot ${in}
  92. COMMENT "Calling GPRE boot for ${F}"
  93. #
  94. COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
  95. COMMAND ${ARGN} ${in} ${out}
  96. )
  97. elseif ("${type}" STREQUAL "master")
  98. get_filename_component(file ${out} NAME)
  99. set(dir ${dir}/${file}.d)
  100. add_custom_command(
  101. OUTPUT ${out}
  102. DEPENDS databases boot_gpre ${in}
  103. COMMENT "Calling GPRE master for ${F}"
  104. #
  105. COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
  106. COMMAND ${CMAKE_COMMAND} -E copy_if_different metadata.fdb ${dir}/yachts.lnk
  107. COMMAND ${CMAKE_COMMAND} -E copy_if_different security.fdb ${dir}/security.fdb
  108. COMMAND ${CMAKE_COMMAND} -E copy_if_different msg.fdb ${dir}/msg.fdb
  109. COMMAND ${ARGN} -b ${dir}/ ${in} ${out}
  110. )
  111. endif()
  112. endforeach()
  113. endfunction(epp_process)
  114. ########################################
  115. # FUNCTION add_epp_suffix
  116. ########################################
  117. function(add_epp_suffix files suffix)
  118. foreach(F ${${files}})
  119. list(APPEND ${files}_${suffix} ${CMAKE_CURRENT_SOURCE_DIR}/${F})
  120. list(APPEND ${files}_${suffix} ${GENERATED_DIR}/${F}.${suffix}.cpp)
  121. endforeach()
  122. set_source_files_properties(${${files}_${suffix}} PROPERTIES GENERATED TRUE)
  123. set(${files}_${suffix} ${${files}_${suffix}} PARENT_SCOPE)
  124. endfunction(add_epp_suffix)
  125. ########################################
  126. # FUNCTION set_win32
  127. ########################################
  128. function(set_win32 var)
  129. if (WIN32)
  130. set(${var} "${ARGN}" PARENT_SCOPE)
  131. endif()
  132. endfunction(set_win32)
  133. ########################################
  134. # FUNCTION set_unix
  135. ########################################
  136. function(set_unix var)
  137. if (UNIX)
  138. set(${var} "${ARGN}" PARENT_SCOPE)
  139. endif()
  140. endfunction(set_unix)
  141. ########################################
  142. # FUNCTION set_apple
  143. ########################################
  144. function(set_apple var)
  145. if (APPLE)
  146. set(${var} "${ARGN}" PARENT_SCOPE)
  147. endif()
  148. endfunction(set_apple)
  149. ########################################
  150. # FUNCTION add_src_win32
  151. ########################################
  152. function(add_src_win32 var)
  153. if (WIN32)
  154. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  155. endif()
  156. endfunction(add_src_win32)
  157. ########################################
  158. # FUNCTION add_src_unix
  159. ########################################
  160. function(add_src_unix var)
  161. if (UNIX)
  162. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  163. endif()
  164. endfunction(add_src_unix)
  165. ########################################
  166. # FUNCTION add_src_unix_not_apple
  167. ########################################
  168. function(add_src_unix_not_apple var)
  169. if (UNIX AND NOT APPLE)
  170. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  171. endif()
  172. endfunction(add_src_unix_not_apple)
  173. ########################################
  174. # FUNCTION add_src_apple
  175. ########################################
  176. function(add_src_apple var)
  177. if (APPLE)
  178. set(${var} ${${var}} ${ARGN} PARENT_SCOPE)
  179. endif()
  180. endfunction(add_src_apple)
  181. ########################################
  182. # FUNCTION copy_and_rename_lib
  183. ########################################
  184. function(copy_and_rename_lib target name)
  185. set(name2 $<TARGET_FILE_DIR:${target}>/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX})
  186. add_custom_command(
  187. TARGET ${target}
  188. POST_BUILD
  189. COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${target}> ${name2}
  190. )
  191. endfunction(copy_and_rename_lib)
  192. ########################################
  193. # FUNCTION project_group
  194. ########################################
  195. function(project_group target name)
  196. set_target_properties(${target} PROPERTIES FOLDER ${name})
  197. endfunction(project_group)
  198. ########################################
  199. # FUNCTION set_generated_directory
  200. ########################################
  201. function(set_generated_directory)
  202. if (NOT CMAKE_CROSSCOMPILING)
  203. set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
  204. else()
  205. string(REPLACE "${CMAKE_BINARY_DIR}" "${NATIVE_BUILD_DIR}" GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR})
  206. set(GENERATED_DIR ${GENERATED_DIR} PARENT_SCOPE)
  207. endif()
  208. endfunction(set_generated_directory)
  209. ########################################
  210. # FUNCTION add_dependencies_cc (cross compile)
  211. ########################################
  212. function(add_dependencies_cc target)
  213. if (NOT CMAKE_CROSSCOMPILING)
  214. add_dependencies(${target} ${ARGN})
  215. endif()
  216. endfunction(add_dependencies_cc)
  217. ########################################
  218. # FUNCTION add_dependencies_unix_cc (cross compile)
  219. ########################################
  220. function(add_dependencies_unix_cc target)
  221. if (UNIX)
  222. add_dependencies_cc(${target} ${ARGN})
  223. endif()
  224. endfunction(add_dependencies_unix_cc)
  225. ########################################
  226. # FUNCTION crosscompile_prebuild_steps
  227. ########################################
  228. function(crosscompile_prebuild_steps)
  229. if (CMAKE_CROSSCOMPILING)
  230. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${NATIVE_BUILD_DIR}/src/include/gen/parse.h ${CMAKE_BINARY_DIR}/src/include/gen/parse.h)
  231. endif()
  232. endfunction(crosscompile_prebuild_steps)
  233. ########################################
  234. # FUNCTION create_command
  235. ########################################
  236. function(create_command command type out)
  237. set(dir ${output_dir})
  238. if ("${type}" STREQUAL "boot")
  239. set(dir ${boot_dir})
  240. endif()
  241. set_win32(env "PATH=${dir}\;%PATH%")
  242. set_unix (env "PATH=${dir}/bin:$PATH")
  243. set(env "${env}"
  244. FIREBIRD=${dir}
  245. )
  246. set(cmd_name ${command})
  247. if (MSVC OR XCODE)
  248. set(conf _$<CONFIG>)
  249. endif()
  250. set(pre_cmd)
  251. set(ext .sh)
  252. set(export export)
  253. set(options $*)
  254. set(perm)
  255. if (WIN32)
  256. set(pre_cmd @)
  257. set(ext .bat)
  258. set(export set)
  259. set(options %*)
  260. endif()
  261. set(cmd_name ${cmd_name}${conf}${ext})
  262. set(cmd_name ${CMAKE_BINARY_DIR}/src/${cmd_name})
  263. set(content)
  264. foreach(e ${env})
  265. set(content "${content}${pre_cmd}${export} ${e}\n")
  266. endforeach()
  267. set(cmd $<TARGET_FILE:${cmd}>)
  268. set(content "${content}${pre_cmd}${cmd} ${options}")
  269. file(GENERATE OUTPUT ${cmd_name} CONTENT "${content}")
  270. if (UNIX)
  271. set(cmd_name chmod u+x ${cmd_name} COMMAND ${cmd_name})
  272. endif()
  273. string(TOUPPER ${command} CMD)
  274. set(${CMD}_CMD ${cmd_name} PARENT_SCOPE)
  275. set(${out} ${CMD}_CMD PARENT_SCOPE)
  276. endfunction(create_command)
  277. ########################################
  278. # FUNCTION create_boot_commands
  279. ########################################
  280. function(create_boot_commands)
  281. set(cmd_list
  282. boot_isql
  283. boot_gpre
  284. boot_gbak
  285. boot_gfix
  286. build_msg
  287. codes
  288. gpre_boot
  289. )
  290. foreach(cmd ${cmd_list})
  291. create_command(${cmd} boot out)
  292. set(${out} ${${out}} PARENT_SCOPE)
  293. endforeach()
  294. endfunction(create_boot_commands)
  295. ########################################
  296. # FUNCTION create_master_commands
  297. ########################################
  298. function(create_master_commands)
  299. set(cmd_list
  300. isql
  301. gpre
  302. empbuild
  303. )
  304. foreach(cmd ${cmd_list})
  305. create_command(${cmd} master out)
  306. set(${out} ${${out}} PARENT_SCOPE)
  307. endforeach()
  308. endfunction(create_master_commands)
  309. ################################################################################